implement delete SFTP and setup/delete for mail base directories

This commit is contained in:
Jan Dittberner 2014-12-26 00:39:53 +01:00
parent 1fe16eda45
commit 3218d58ee4
2 changed files with 67 additions and 5 deletions

View file

@ -1,5 +1,7 @@
Changelog Changelog
========= =========
* :feature:`-` implement tasks for creating and deleting SFTP and mail base directories
* :release:`0.0.1 <2014-12-25>` * :release:`0.0.1 <2014-12-25>`
* :feature:`-` initial setup of django application and celery worker * :feature:`-` initial setup of django application and celery worker

View file

@ -22,6 +22,7 @@ _logger = get_task_logger(__name__)
SUDO_CMD = '/usr/bin/sudo' SUDO_CMD = '/usr/bin/sudo'
INSTALL_CMD = '/usr/bin/install' INSTALL_CMD = '/usr/bin/install'
SETFACL_CMD = '/usr/bin/setfacl' SETFACL_CMD = '/usr/bin/setfacl'
RM_CMD = '/bin/rm'
def _build_sftp_directory_name(username): def _build_sftp_directory_name(username):
@ -46,8 +47,6 @@ def setup_file_sftp_userdir(username):
This task creates the home directory for an SFTP user if it does not exist This task creates the home directory for an SFTP user if it does not exist
yet. yet.
The task is rejected if the directory creation fails.
:param str username: the user name :param str username: the user name
:raises Exception: if the SFTP directory of the user cannot be created :raises Exception: if the SFTP directory of the user cannot be created
:return: the created directory name :return: the created directory name
@ -72,14 +71,75 @@ def setup_file_sftp_userdir(username):
@shared_task @shared_task
def delete_file_sftp_userdir(username): def delete_file_sftp_userdir(username):
pass """
This task recursively deletes the home directory of an SFTP user if it
does not exist yet.
:param str username: the user name
:raises Exception: if the SFTP directory of the user cannot be removed
:return: the removed directory name
:rtype: str
"""
sftp_directory = _build_sftp_directory_name(username)
try:
subprocess.check_output([
SUDO_CMD, RM_CMD, '-r', '-f', sftp_directory],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
_logger.exception(
'could not remove SFTP directory for user %s', username)
raise GVAFileException(
"could not remove SFTP directory for user %s" % username)
return sftp_directory
@shared_task @shared_task
def setup_file_mail_userdir(username): def setup_file_mail_userdir(username):
pass """
This task creates the mail base directory for a user if it does not exist
yet.
:param str username: the user name
:raises Exception: if the mail base directory for the user cannot be
created
:return: the created directory name
:rtype: str
"""
mail_directory = _build_mail_directory_name(username)
try:
subprocess.check_output([
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
'-m', '0750', '-d', mail_directory], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
_logger.exception(
'could not create mail base directory for user %s', username)
raise GVAFileException(
"could not create mail base directory for user %s" % username)
return mail_directory
@shared_task @shared_task
def delete_file_mail_userdir(username): def delete_file_mail_userdir(username):
pass """
This task recursively deletes the mail base directory for a user if it
does not exist yet.
:param str username: the user name
:raises Exception: if the mail base directory of the user cannot be removed
:return: the removed directory name
:rtype: str
"""
mail_directory = _build_mail_directory_name(username)
try:
subprocess.check_output([
SUDO_CMD, RM_CMD, '-r', '-f', mail_directory],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
_logger.exception(
'could not remove mail base directory of user %s', username)
raise GVAFileException(
"could not remove mail base directory of user %s" % username)
return mail_directory