django-fab-deploy / fab_deploy / webserver /

# 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=''):
        super(Uwsgi, self).__init__(wsgi)
        self.config = config

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

    def upload_config(self):
        """ Updates uwsgi config. """
        name = self._get_server_config_name()
            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)

    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)

    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)

    def update_config(self):
        """ Updates uwsgi config, wsgi script and restarts uwsgi. """

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

    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
            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, ''))