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
This commit is contained in:
Jan Dittberner 2014-12-25 18:26:37 +01:00
parent b80bccb7c0
commit caf9d2125e

82
gvafile/osusers/tasks.py Normal file
View file

@ -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