Source

django-fab-deploy / fab_deploy / webserver / uwsgi.py

# coding: utf-8
from __future__ import with_statement, absolute_import

from fabric.api import cd, sudo, prefix #env, run, settings, hide, abort
#from fabric.contrib import files, console

from taskset import task_method
from fab_deploy import utils
from fab_deploy import system
from fab_deploy.webserver.wsgi_backend import WsgiBackend

__all__ = ['Uwsgi']

class Uwsgi(WsgiBackend):
    """ Only lucid and squeeze are supported. """

    def __init__(self, config='uwsgi.config', wsgi='wsgi.py'):
        super(Uwsgi, self).__init__(wsgi)
        self.config = config

    def _get_server_config_name(self):
        return '%s_%s' % (env.conf['INSTANCE_NAME'], self.config)

    @task_method
    @utils.run_as_sudo
    def upload_config(self):
        """ Updates uwsgi config. """
        name = self._get_server_config_name()
        utils.upload_config_template(
            self.config, '/etc/uwsgi/apps-available/%s' % name,
            extra_context={'CURRENT_BACKEND': self}, use_sudo=True
        )
        with cd('/etc/uwsgi/apps-enabled'):
            sudo('ln -s ../apps-available/%s .' % name)

    @task_method
    @utils.run_as_sudo
    def remove_config(self):
        """ Removes uwsgi config and reloads uwsgi. """
        name = self._get_server_config_name()
        sudo('invoke-rc.d uwsgi stop '+name)
        sudo('rm -f /etc/uwsgi/apps-enabled/'+name)
        sudo('rm -f /etc/uwsgi/apps-available/'+name)

    @task_method
    @utils.run_as_sudo
    def restart(self):
        """ Restarts uwsgi using init.d script. """
        # here should be restart of the Emperor, but 
        # for a moment here is restart of the concrete vassal
        name = self._get_server_config_name()
        sudo('invoke-rc.d uwsgi restart '+name)

    @task_method
    def update_config(self):
        """ Updates uwsgi config, wsgi script and restarts uwsgi. """
        self.upload_config()
        self.upload_wsgi()
        self.restart()

    @task_method
    @utils.run_as_sudo
    def is_running(self):
        """
        Returns whether uwsgi is running
        """
        return True # temporary plug
        #with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
        #    output = sudo('invoke-rc.d apache2 status')
        #return output.succeeded

    # ==== installation ===

    @task_method
    @utils.run_as_sudo
    def install(self, confirm=True):
        """ Installs uwsgi. """
        os = utils.detect_os()
        options = {'squeeze': '-t testing'}
        if os=='squeeze':
            # We need to preseed libc interactive prompt. It popups since uwsgi
            # pulls its upgrade to version from Wheezy.
            # E.g. see http://www.50ply.com/blog/2012/07/16/automating-debian-installs-with-preseed-and-puppet/
            system.aptitude_install('debconf-utils')
            sudo("echo '%s' | debconf-set-selections" % 'libc6 libraries/restart-without-asking boolean true')
        # Also we need to turn-off apt-listchanges as it reduces automation.
        with prefix('export APT_LISTCHANGES_FRONTEND=text'):
            system.aptitude_install('uwsgi uwsgi-plugin-python', options.get(os, ''))