Commits

theoden committed 8b64c5c

* extended socket timeout to download python packages more stable
+ added UwsgiPrepareTest to 'prepare' test set
* preparevm.py now creates an additional snapshot per backend

  • Participants
  • Parent commits 9fcd994
  • Branches uwsgi

Comments (0)

Files changed (4)

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

File fab_deploy_tests/preparevm.py

 #!/usr/bin/env python
 import sys
 import os
+import socket
 
 # always use fab_deploy from the checkout, not the installed version
 # plus make fab_deploy_tests available for imports
 from fab_deploy.system import prepare
 from fab_deploy.db import mysql, postgres
 from fab_deploy.webserver.nginx import Nginx
+
 from fab_deploy.webserver.apache import Apache
+from fab_deploy.webserver.uwsgi import Uwsgi
 
 from utils import setup_ssh, setup_sudo, private_key_path
 
 def _download_pip_requirements():
+    socket.setdefaulttimeout(120)
     DIR = '/tmp/sdist'
     run('mkdir -p %s' % DIR)
     reqs = [
 
     box = VirtualBox(name)
 
-    if not box.snapshot_exists('fabtest-prepared-server'):
-        box.snapshot_restore('fabtest-initial')
-        setup_sudo()
-        setup_ssh()
-        fab(prepare)
-        fab(Apache().install, confirm=False)
-        fab(Nginx().install)
-        fab(mysql.install)
-        fab(postgres.install)
-        fab(_download_pip_requirements)
-        box.snapshot_take('fabtest-prepared-server')
+    # key 'server' is used for Apache just for backward compatibility
+    backends = {'server': Apache(), 'uwsgi': Uwsgi()}
+
+    for name, backend in backends.iteritems():
+        if not box.snapshot_exists('fabtest-prepared-'+name):
+            box.snapshot_restore('fabtest-initial')
+            setup_sudo()
+            setup_ssh()
+            fab(prepare)
+            fab(backend.install, confirm=False)
+            fab(Nginx().install)
+            fab(mysql.install)
+            fab(postgres.install)
+            fab(_download_pip_requirements)
+            box.snapshot_take('fabtest-prepared-'+name)
 
     box.stop()
 

File fab_deploy_tests/runtests.py

         'deploy': TestSuite(load([
                     DeployTest, CustomLayoutDeployTest, Django14LayoutDeployTest
                 ])),
-        'prepare': TestSuite(load(initial + [PrepareServerTest])),
+        'prepare': TestSuite(load(initial + [PrepareServerTest, UwsgiPrepareTest])),
     }
     suites['all'] = TestSuite([suites['misc'], suites['deploy']])
 

File fab_deploy_tests/tests/uwsgi_prepare.py

+# coding: utf-8
+from __future__ import absolute_import
+from fabric.api import run, env, abort
+from fabtest import fab, FabricAbortException
+
+from fab_deploy.utils import run_as
+from fab_deploy.system import prepare, ssh_add_key
+from fab_deploy.project import WebProject
+from fab_deploy.django import Django
+from fab_deploy.webserver.nginx import Nginx
+from fab_deploy.webserver.uwsgi import Uwsgi
+
+from fab_deploy_tests.utils import setup_ssh, public_key_path, setup_sudo
+from .base import FabDeployTest
+
+class UwsgiPrepareTest(FabDeployTest):
+    def test_prepare_server_ok(self):
+        setup_sudo()
+        setup_ssh()
+
+        fab(prepare)
+        self.assertPackageInstalled('python')
+        project = WebProject(dict(django=Django(Nginx(), Uwsgi()))).expose_as_module('project')
+        apps = project.apps
+        fab(apps.django.backend.install, confirm=False)
+        fab(apps.django.frontend.install)
+        self.assertPackageInstalled('nginx')
+        self.assertPackageInstalled('uwsgi')