improved logging in fileservertasks.tasks
This commit is contained in:
parent
735a468806
commit
4e144fb49d
4 changed files with 71 additions and 67 deletions
|
@ -1,6 +1,9 @@
|
||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
* :support:`-` improved logging in fileservertasks.tasks, got rid of
|
||||||
|
GVAFileException
|
||||||
|
|
||||||
* :release:`0.4.0 <2015-01-26>`
|
* :release:`0.4.0 <2015-01-26>`
|
||||||
* :feature:`-` implement new tasks create_file_website_hierarchy and
|
* :feature:`-` implement new tasks create_file_website_hierarchy and
|
||||||
delete_file_website_hierarchy
|
delete_file_website_hierarchy
|
||||||
|
|
|
@ -20,12 +20,6 @@ The project module :py:mod:`gvafile`
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
:py:mod:`exceptions <gvafile.exceptions>`
|
|
||||||
-----------------------------------------
|
|
||||||
|
|
||||||
.. automodule:: gvafile.exceptions
|
|
||||||
|
|
||||||
|
|
||||||
:py:mod:`settings <gvafile.settings>`
|
:py:mod:`settings <gvafile.settings>`
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
|
|
||||||
|
@ -33,8 +27,8 @@ The project module :py:mod:`gvafile`
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
||||||
:py:mod:`fileservertasks` app
|
:py:mod:`fileservertasks` module
|
||||||
=============================
|
================================
|
||||||
|
|
||||||
.. automodule:: fileservertasks
|
.. automodule:: fileservertasks
|
||||||
|
|
||||||
|
|
|
@ -14,10 +14,7 @@ from gvafile import settings
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
from celery.utils.log import get_task_logger
|
from celery.utils.log import get_task_logger
|
||||||
|
|
||||||
from gvafile.exceptions import GVAFileException
|
_LOGGER = get_task_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
_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'
|
||||||
|
@ -25,6 +22,21 @@ SETFACL_CMD = '/usr/bin/setfacl'
|
||||||
RM_CMD = '/bin/rm'
|
RM_CMD = '/bin/rm'
|
||||||
|
|
||||||
|
|
||||||
|
def log_and_raise(exception, message, *args):
|
||||||
|
"""
|
||||||
|
Log and raise a :py:class:`subprocess.CalledProcessError`.
|
||||||
|
|
||||||
|
:param exception: exception
|
||||||
|
:param str message: log message
|
||||||
|
:param args: arguments to fill placeholders in message
|
||||||
|
:raises Exception: raises an exception with the formatted message
|
||||||
|
|
||||||
|
"""
|
||||||
|
logargs = list(args) + [exception.returncode, exception.output]
|
||||||
|
_LOGGER.error(message + "\nreturncode: %d\noutput:\n%s", *logargs)
|
||||||
|
raise Exception(message % args)
|
||||||
|
|
||||||
|
|
||||||
def _build_sftp_directory_name(username):
|
def _build_sftp_directory_name(username):
|
||||||
"""
|
"""
|
||||||
Constructs the SFTP directory name for a given username.
|
Constructs the SFTP directory name for a given username.
|
||||||
|
@ -61,11 +73,11 @@ def setup_file_sftp_userdir(username):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, SETFACL_CMD, '-m', 'www-data:--x',
|
SUDO_CMD, SETFACL_CMD, '-m', 'www-data:--x',
|
||||||
sftp_directory], stderr=subprocess.STDOUT)
|
sftp_directory], stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not create SFTP directory for user %s', username)
|
cpe, 'cold not create SFTP directory for user %s', username)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
"could not create SFTP directory for user %s" % username)
|
'created sftp directory %s for user %s', sftp_directory, username)
|
||||||
return sftp_directory
|
return sftp_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,11 +98,11 @@ def delete_file_sftp_userdir(username):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, RM_CMD, '-r', '-f', sftp_directory],
|
SUDO_CMD, RM_CMD, '-r', '-f', sftp_directory],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not remove SFTP directory for user %s', username)
|
cpe, 'could not remove SFTP directory for user %s', username)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
"could not remove SFTP directory for user %s" % username)
|
"deleted sftp directory %s of user %s", sftp_directory, username)
|
||||||
return sftp_directory
|
return sftp_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,11 +124,11 @@ def setup_file_mail_userdir(username):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
||||||
'-m', '0500', '-d', mail_directory], stderr=subprocess.STDOUT)
|
'-m', '0500', '-d', mail_directory], stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not create mail base directory for user %s', username)
|
cpe, 'could not create mail base directory for user %s', username)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
"could not create mail base directory for user %s" % username)
|
'created mail directory %s for user %s', mail_directory, username)
|
||||||
return mail_directory
|
return mail_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,11 +149,11 @@ def delete_file_mail_userdir(username):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, RM_CMD, '-r', '-f', mail_directory],
|
SUDO_CMD, RM_CMD, '-r', '-f', mail_directory],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not remove mail base directory of user %s', username)
|
cpe, 'could not remove mail base directory of user %s', username)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
"could not remove mail base directory of user %s" % username)
|
'deleted mail directory %s of user %s', mail_directory, username)
|
||||||
return mail_directory
|
return mail_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,7 +165,7 @@ def create_file_mailbox(username, mailboxname):
|
||||||
|
|
||||||
:param str username: the user name
|
:param str username: the user name
|
||||||
:param str mailboxname: the mailbox name
|
:param str mailboxname: the mailbox name
|
||||||
:raises GVAFileException: if the mailbox directory cannot be created
|
:raises Exception: if the mailbox directory cannot be created
|
||||||
:return: the created mailbox directory name
|
:return: the created mailbox directory name
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
|
@ -164,12 +176,13 @@ def create_file_mailbox(username, mailboxname):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
||||||
'-m', '0700', '-d', mailbox_directory], stderr=subprocess.STDOUT)
|
'-m', '0700', '-d', mailbox_directory], stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not create mailbox %s for user %s', mailboxname, username)
|
cpe, 'could not create mailbox %s for user %s', mailboxname,
|
||||||
raise GVAFileException(
|
username)
|
||||||
"could not create mailbox %s for user %s" % (mailboxname, username)
|
_LOGGER.info(
|
||||||
)
|
'created mailbox directory %s for user %s', mailbox_directory,
|
||||||
|
username)
|
||||||
return mailbox_directory
|
return mailbox_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -180,7 +193,7 @@ def delete_file_mailbox(username, mailboxname):
|
||||||
|
|
||||||
:param str username: the user name
|
:param str username: the user name
|
||||||
:param str mailboxname: the mailbox name
|
:param str mailboxname: the mailbox name
|
||||||
:raises GVAFileException: if the mailbox directory cannot be deleted
|
:raises Exception: if the mailbox directory cannot be deleted
|
||||||
:return: the deleted mailbox directory name
|
:return: the deleted mailbox directory name
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
|
@ -191,11 +204,12 @@ def delete_file_mailbox(username, mailboxname):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, RM_CMD, '-r', '-f', mailbox_directory],
|
SUDO_CMD, RM_CMD, '-r', '-f', mailbox_directory],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
'could not remove mailbox %s of user %s', mailboxname, username)
|
cpe, 'could not remove mailbox %s of user %s', mailboxname,
|
||||||
raise GVAFileException(
|
username)
|
||||||
"could not remove mailbox %s of user %s" % (mailboxname, username))
|
_LOGGER.info(
|
||||||
|
'deleted mailbox directory %s of user %s', mailbox_directory, username)
|
||||||
return mailbox_directory
|
return mailbox_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -206,6 +220,8 @@ def create_file_website_hierarchy(username, sitename):
|
||||||
|
|
||||||
:param str username: the user name
|
:param str username: the user name
|
||||||
:param str sitename: name of the website
|
:param str sitename: name of the website
|
||||||
|
:raises Exception: if the website directory hierarchy directory cannot be
|
||||||
|
created
|
||||||
:return: the directory name
|
:return: the directory name
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
|
@ -233,13 +249,14 @@ def create_file_website_hierarchy(username, sitename):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
SUDO_CMD, INSTALL_CMD, '-o', username, '-g', username,
|
||||||
'-m', '0750', '-d', tmpdir], stderr=subprocess.STDOUT)
|
'-m', '0750', '-d', tmpdir], stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
|
cpe,
|
||||||
("could not setup website file hierarchy for user %s's "
|
("could not setup website file hierarchy for user %s's "
|
||||||
"website %s"), username, sitename)
|
"website %s"), username, sitename)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
("could not setup website file hierarchy for user %s's "
|
'created website directory %s for user %s', website_directory,
|
||||||
"website %s") % (username, sitename))
|
username)
|
||||||
return website_directory
|
return website_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -250,6 +267,8 @@ def delete_file_website_hierarchy(username, sitename):
|
||||||
|
|
||||||
:param str username: the user name
|
:param str username: the user name
|
||||||
:param str sitename: name of the website
|
:param str sitename: name of the website
|
||||||
|
:raises Exception: if the website directory hierarchy directory cannot be
|
||||||
|
deleted
|
||||||
:return: the directory name
|
:return: the directory name
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
|
@ -260,11 +279,11 @@ def delete_file_website_hierarchy(username, sitename):
|
||||||
subprocess.check_output([
|
subprocess.check_output([
|
||||||
SUDO_CMD, RM_CMD, '-r', '-f', website_directory],
|
SUDO_CMD, RM_CMD, '-r', '-f', website_directory],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError as cpe:
|
||||||
_logger.exception(
|
log_and_raise(
|
||||||
|
cpe,
|
||||||
("could not delete the website file hierarchy of user %s's "
|
("could not delete the website file hierarchy of user %s's "
|
||||||
"website %s"), username, sitename)
|
"website %s"), username, sitename)
|
||||||
raise GVAFileException(
|
_LOGGER.info(
|
||||||
("could not delete the website file hierarchy of user %s's "
|
'deleted website directory %s of user %s', website_directory, username)
|
||||||
"website %s") % (username, sitename))
|
|
||||||
return website_directory
|
return website_directory
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
"""
|
|
||||||
This module defines exceptions for gvafile.
|
|
||||||
|
|
||||||
"""
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
|
|
||||||
class GVAFileException(Exception):
|
|
||||||
"""
|
|
||||||
Generic Exception class for gvafile.
|
|
||||||
|
|
||||||
"""
|
|
Loading…
Reference in a new issue