1. Mikhail Korobov
  2. django-fab-deploy


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

The default branch has multiple heads

# coding: utf-8
from __future__ import with_statement

from fabric.api import  env, settings, sudo
from taskset import TaskSet, task_method

from fab_deploy import utils
from fab_deploy import system

__all__ = ['Nginx']

class Nginx(TaskSet):

    def __init__(self, config='nginx.config'):
        self.config = config

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

    def install(self):
        """ Installs nginx. """
        os = utils.detect_os()
        options = {'lenny': '-t lenny-backports'}
        system.aptitude_install('nginx', options.get(os, ''))
        sudo('rm -f /etc/nginx/sites-enabled/default')

    def update_config(self):
        """ Updates nginx config and restarts nginx. """
        name = self._get_server_config_name()
                                     '/etc/nginx/sites-available/%s' % name,
        with settings(warn_only=True):
            sudo('ln -s /etc/nginx/sites-available/%s /etc/nginx/sites-enabled/%s' % (name, name))
        sudo('invoke-rc.d nginx restart')

    def remove_config(self):
        """ Removes nginx config and reloads nginx. """
        name = self._get_server_config_name()
        sudo('rm -f /etc/nginx/sites-enabled/'+name)
        sudo('rm -f /etc/nginx/sites-available/'+name)
        sudo('invoke-rc.d nginx reload')