define webtasks interface and initial settings

This commit is contained in:
Jan Dittberner 2015-01-26 18:12:14 +01:00
parent ddd6287f03
commit faedcb77f0
5 changed files with 178 additions and 0 deletions

View file

15
gvaweb/gvaweb/celery.py Normal file
View file

@ -0,0 +1,15 @@
"""
This module defines the Celery_ app for gvaweb.
.. _Celery: http://www.celeryproject.org/
"""
from __future__ import absolute_import
from celery import Celery
#: The Celery application
app = Celery('gvaweb')
app.config_from_object('gvaweb.settings')
app.autodiscover_tasks(['webtasks'], force=True)

47
gvaweb/gvaweb/settings.py Normal file
View file

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# pymode:lint_ignore=E501
"""
Common settings and globals.
"""
from os import environ
def get_env_setting(setting):
"""
Get the environment setting or return exception.
:param str setting: name of an environment setting
:raises ImproperlyConfigured: if the environment setting is not defined
:return: environment setting value
:rtype: str
"""
try:
return environ[setting]
except KeyError:
error_msg = "Set the %s env variable" % setting
raise AssertionError(error_msg)
########## CELERY CONFIGURATION
CELERY_TIMEZONE = 'Europe/Berlin'
CELERY_ENABLE_UTC = True
CELERY_RESULT_BACKEND = 'amqp'
CELERY_RESULT_PERSISTENT = True
CELERY_TASK_RESULT_EXPIRES = None
CELERY_ROUTES = (
'gvacommon.celeryrouters.GvaRouter',
)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
BROKER_URL = get_env_setting('GVAWEB_BROKER_URL')
########## END CELERY CONFIGURATION
########## GVAWEB CONFIGURATION
GVAWEB_NGINX_SITES_AVAILABLE = get_env_setting('GVAWEB_NGINX_SITES_AVAILABLE')
GVAWEB_NGINX_SITES_ENABLED = get_env_setting('GVAWEB_NGINX_SITES_ENABLED')
GVAWEB_PHPFPM_POOL = get_env_setting('GVAWEB_PHPFPM_POOL')
GVAWEB_WWWUSER_MOUNT = get_env_setting('GVAWEB_WWWUSER_MOUNT')
########## END GVAWEB CONFIGURATION

View file

@ -0,0 +1,4 @@
"""
This module contains :py:mod:`webtasks.tasks`.
"""

112
gvaweb/webtasks/tasks.py Normal file
View file

@ -0,0 +1,112 @@
"""
This module defines Celery_ tasks to manage website configurations.
"""
from __future__ import absolute_import
import os
from celery import shared_task
from celery.utils.log import get_task_logger
from gvaweb import settings
_LOGGER = get_task_logger(__name__)
SUDO_CMD = '/usr/bin/sudo'
RM_CMD = '/bin/rm'
LN_CMD = '/bin/ln'
SERVICE_CMD = '/usr/sbin/service'
INSTALL_CMD = '/usr/bin/install'
def _build_vhost_config_path(sitename):
return os.path.join(settings.GVAWEB_NGINX_SITES_AVAILABLE, sitename)
def _build_enabled_vhost_path(sitename):
return os.path.join(settings.GVAWEB_NGINX_SITES_ENABLED, sitename)
def _build_php_fpm_pool_file(username):
return os.path.join(settings.GVAWEB_PHPFPM_POOL, "{username}.conf".format(
username=username))
def _build_document_root_path(sitename, username):
return os.path.join(
settings.GVAWEB_WWWUSER_MOUNT, username, sitename, 'html')
@shared_task
def create_web_vhost_config(username, sitename):
"""
This task creates a virtual host configuration on an nginx web
server.
:param str username: user who owns the site
:param str sitename: site name
:return: :py:const:`True` if the creation finished successfully
:rtype: boolean
"""
@shared_task
def disable_web_vhost(sitename):
"""
This task disables a virtual host configuration on an nginx web server.
:param str sitename: site name
:return: :py:const:`True` if the virtual host has been disabled
:rtype: boolean
"""
@shared_task
def enable_web_vhost(sitename):
"""
This task enables an existing virtual host configuration on an nginx web
server.
:param str sitename: site name
:return: :py:const:`True` if the virtual host has been enabled
:rtype: boolean
"""
@shared_task
def delete_web_vhost_config(sitename):
"""
This task removes a virtual host configuration on an nginx web server.
:param str sitename: site name
:return: :py:const:`True` if the configuration has been deleted
:rtype: boolean
"""
@shared_task
def create_web_php_fpm_pool_config(username):
"""
This task creates a PHP FPM pool configuration.
:param str username: user name
:return: :py:const:`True` if the creation finished successfully
:rtype: boolean
"""
@shared_task
def delete_web_php_fpm_pool_config(username):
"""
This task deletes a PHP FPM pool configuration.
:param str username: user name
:return: :py:const:`True` if the pool has been deleted
:rtype: boolean
"""