From caf9d2125e74690d3ff9fff8e8a82057d91fbebd Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 25 Dec 2014 18:26:37 +0100 Subject: [PATCH] define stub tasks and implement setup_file_sftp_userdir task - create new module osusers.tasks - define helper functions _build_sftp_directory_name and _build_mail_directory_name - implement task setup_file_sftp_userdir - define stubs for delete_file_sftp_userdir, setup_file_mail_userdir and delete_file_mail_userdir --- gvafile/osusers/tasks.py | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 gvafile/osusers/tasks.py 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