diff --git a/gvafile/osusers/tasks.py b/gvafile/osusers/tasks.py new file mode 100644 index 0000000..5035169 --- /dev/null +++ b/gvafile/osusers/tasks.py @@ -0,0 +1,82 @@ +""" +This module defines `Celery`_ tasks to manage file system entities. + +.. _Celery: http://www.celeryproject.org/ + +""" +from __future__ import absolute_import, unicode_literals + +import os +import subprocess + +from django.conf import settings + +from celery import shared_task +from celery.utils.log import get_task_logger +from celery.exceptions import Reject + + +_logger = get_task_logger(__name__) + + +def _build_sftp_directory_name(username): + """ + Constructs the SFTP directory name for a given username. + + """ + return os.path.join(settings.GVAFILE_SFTP_DIRECTORY, username) + + +def _build_mail_directory_name(username): + """ + Constructs the mailbox directory name for a given username. + + """ + return os.path.join(settings.GVAFILE_MAIL_DIRECTORY, username) + + +@shared_task +def setup_file_sftp_userdir(username): + """ + This task creates the home directory for an SFTP user if it does not exist + yet. + + The task is rejected if the directory creation fails. + + :param str username: the user name + :raises celery.exceptions.Reject: if the SFTP directory of the user cannot + be created + :return: the created directory name + :rtype: str + + """ + sftp_directory = _build_sftp_directory_name(username) + try: + subprocess.check_output([ + 'sudo', 'install', '-o', username, '-g', username, '-m', '0750', + '-d', sftp_directory], stderr=subprocess.STDOUT) + subprocess.check_output([ + 'sudo' 'setfacl', '-r', '-m', 'www-data:--x', sftp_directory], + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: + _logger.exception( + 'could not create SFTP directory for user {0}: {1}', + username, exc.output + ) + raise Reject(exc, requeue=False) + return sftp_directory + + +@shared_task +def delete_file_sftp_userdir(username): + pass + + +@shared_task +def setup_file_mail_userdir(username): + pass + + +@shared_task +def delete_file_mail_userdir(username): + pass