Source

sprintsite / components / nginx / component.py

from batou.component import Component, platform
from batou.lib.file import File
from batou.utils import Address
import logging

logger = logging.getLogger(__name__)


class Nginx(Component):

    # Set via environment
    server_name = 'localhost'

    def configure(self):
        self.address = Address(self.server_name, 80)
        self.haproxy = self.require('haproxy')[0]
        self += File('sprintsite.conf', is_template=True)


@platform('gocept.net', Nginx)
class GoceptNetNginx(Component):
    """Copies all config files to /etc/nginx/local and reloads nginx if
    necessary.
    """

    def configure(self):
        for config_file in self.config_files:
            self += File(
                '/etc/nginx/local/%s' % os.path.basename(config_file.path),
                content=config_file.content)

    @property
    def config_files(self):
        for f in self.parent.sub_components:
            if not isinstance(f, File):
                continue
            if not f.path.endswith('.conf'):
                continue
            yield f

    def verify(self):
        self.assert_no_subcomponent_changes()

    def update(self):
        stdout, stderr = self.cmd('sudo /etc/init.d/nginx reload')
        if 'failed, please correct errors above' in stderr:
            logger.error(stdout)
            logger.error(stderr)
            raise RuntimeError('nginx could not be reloaded')