Commits

theoden committed f701aca

+ WsgiBackend was introduced

Comments (0)

Files changed (6)

fab_deploy/webserver/apache.py

 from fabric.api import env, run, settings, sudo, hide, abort
 from fabric.contrib import files, console
 
-from taskset import TaskSet, task_method
+from taskset import task_method
 from fab_deploy import utils
 from fab_deploy import system
+from fab_deploy.webserver.wsgi_backend import WsgiBackend
 
 __all__ = ['Apache']
 
 TAKEOVER_STRING = '# This file is managed by django-fab-deploy. "Listen" directives are in /etc/apache2/sites-available/*'
 OLD_TAKEOVER_STRING = '# This file is managed by django-fab-deploy. Please do not edit it manually.'
 
-class Apache(TaskSet):
+class Apache(WsgiBackend):
     
     def __init__(self, config='apache.conf', wsgi='wsgi.py'):
+        super(Apache, self).__init__(wsgi)
         self.config = config
-        self.wsgi = wsgi
-    
-    @task_method
-    def touch(self, wsgi_file=None):
-        """ Reloads source code by touching the wsgi file. """
-        if wsgi_file is None:
-            wsgi_file = env.conf['ENV_DIR'] + '/var/wsgi/' + env.conf['INSTANCE_NAME'] + '.py'
-        run('touch ' + wsgi_file)
-    
-    @task_method
-    def make_wsgi(self):
-        """ Uploads wsgi deployment script. """
-        wsgi_dir = env.conf['ENV_DIR'] + '/var/wsgi/'
-        run('mkdir -p ' + wsgi_dir)
-        file_name = env.conf['INSTANCE_NAME'] + '.py'
-        utils.upload_config_template(self.wsgi, wsgi_dir + file_name)
-    
+
     @task_method
     @utils.run_as_sudo
-    def is_running(self):
-        """
-        Returns if apache is running
-        """
-        with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
-            output = sudo('invoke-rc.d apache2 status')
-        return output.succeeded
-    
-    
+    def upload_config(self):
+        """ Updates apache config. """
+        name = env.conf['INSTANCE_NAME']
+        utils.upload_config_template(self.config,
+                                     '/etc/apache2/sites-available/%s' % name,
+                                     use_sudo=True)
+        sudo('a2ensite %s' % name)
+
     @task_method
     @utils.run_as_sudo
     def restart(self):
         # without pty=False restart silently fails on Ubuntu 10.04.
         sudo('invoke-rc.d apache2 stop', pty=False)
         sudo('invoke-rc.d apache2 start', pty=False)
-    
+
+    @task_method
+    def update_config(self):
+        """ Updates apache config, wsgi script and restarts apache. """
+        self.upload_config()
+        super(Apache, self).upload_wsgi()
+        self.restart()
+
+    @task_method
+    @utils.run_as_sudo
+    def is_running(self):
+        """
+        Returns whether apache is running
+        """
+        with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
+            output = sudo('invoke-rc.d apache2 status')
+        return output.succeeded
+
     # ==== installation ===
     
     @task_method
                 _disable_ports_conf()
             else:
                 abort("Aborting.")
-    
-    
-    @task_method
-    @utils.run_as_sudo
-    def make_config(self):
-        """ Updates apache config. """
-        name = env.conf['INSTANCE_NAME']
-        utils.upload_config_template(self.config,
-                                     '/etc/apache2/sites-available/%s' % name,
-                                     use_sudo=True)
-        sudo('a2ensite %s' % name)
-    
-    @task_method
-    def update_config(self):
-        """ Updates apache config, wsgi script and restarts apache. """
-        self.make_config()
-        self.make_wsgi()
-        self.restart()
-    
+
     @task_method
     @utils.run_as_sudo
     def setup_locale(self):

fab_deploy/webserver/wsgi_backend.py

+# coding: utf-8
+from __future__ import with_statement, absolute_import
+import posixpath
+
+from fabric.api import env, run
+
+from taskset import TaskSet, task_method
+from fab_deploy import utils
+
+class WsgiBackend(TaskSet):
+    """ Base class for backends using wsgi. """
+
+    def __init__(self, wsgi):
+        self.wsgi = wsgi
+
+    def get_wsgi_file_name(self):
+        return self.wsgi
+    
+    def get_wsgi_dir(self):
+        return posixpath.join(
+            env.conf['ENV_DIR'],
+            'var/wsgi/',
+            env.conf['INSTANCE_NAME']
+        )
+
+    @task_method
+    def touch(self, wsgi_file=None):
+        """
+        Reloads source code by touching the wsgi file.
+        
+        If backend doesn't have this feature then this method must be
+        overriden to provide same effect in other way (restart, reload, etc).
+        """
+        if wsgi_file is None:
+            wsgi_file = posixpath.join(
+                self.get_wsgi_dir(),
+                self.get_wsgi_file_name()
+            )
+        run('touch ' + wsgi_file)
+
+    @task_method
+    def upload_wsgi(self):
+        """ Uploads wsgi deployment script. """
+        wsgi_dir = self.get_wsgi_dir()
+        run('mkdir -p ' + wsgi_dir)
+        utils.upload_config_template(
+            self.wsgi, 
+            posixpath.join(wsgi_dir, self.get_wsgi_file_name())
+        )

fab_deploy_tests/test_project/config_templates/apache.config

     WSGIDaemonProcess {{ INSTANCE_NAME }} user={{ USER }} group={{ USER }} processes={{ PROCESSES }} threads={{ THREADS }}
     WSGIProcessGroup {{ INSTANCE_NAME }}
 
-    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}.py
-    <Directory {{ ENV_DIR }}/var/wsgi/>
+    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/django_wsgi.py
+    <Directory {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/>
         Order deny,allow
         allow from all
     </Directory>

fab_deploy_tests/test_project2/hosting/apache.config

     WSGIDaemonProcess {{ INSTANCE_NAME }} user={{ USER }} group={{ USER }} processes={{ PROCESSES }} threads={{ THREADS }}
     WSGIProcessGroup {{ INSTANCE_NAME }}
 
-    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}.py
-    <Directory {{ ENV_DIR }}/var/wsgi/>
+    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/django_wsgi.py
+    <Directory {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/>
         Order deny,allow
         allow from all
     </Directory>

fab_deploy_tests/test_project3/test_project3/config_templates/apache.config

     WSGIDaemonProcess {{ INSTANCE_NAME }} user={{ USER }} group={{ USER }} processes={{ PROCESSES }} threads={{ THREADS }}
     WSGIProcessGroup {{ INSTANCE_NAME }}
 
-    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}.py
-    <Directory {{ ENV_DIR }}/var/wsgi/>
+    WSGIScriptAlias / {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/django_wsgi.py
+    <Directory {{ ENV_DIR }}/var/wsgi/{{ INSTANCE_NAME }}/>
         Order deny,allow
         allow from all
     </Directory>

fab_deploy_tests/tests/deploy.py

 
         # first site
         fab(foo_site)
-        fab(env.conf.APPS.django.backend.make_config)
+        fab(env.conf.APPS.django.backend.upload_config)
 
         foo_port = env.conf.PORTS['apache']
         self.assertPortNotBound(foo_port)
 
         # second site
         fab(bar_site)
-        fab(env.conf.APPS.django.backend.make_config)
+        fab(env.conf.APPS.django.backend.upload_config)
 
         bar_port = env.conf.PORTS['apache']
         self.assertNotEqual(foo_port, bar_port)
         self.assertPortBound(bar_port)
 
         # re-configuring doesn't lead to errors
-        fab(env.conf.APPS.django.backend.make_config)
+        fab(env.conf.APPS.django.backend.upload_config)
         fab(apacheNoConfig.restart)
         self.assertPortBound(bar_port)
 
     def test_apache_make_wsgi(self):
         self.assertNoFile(env.conf.ENV_DIR+'/var/wsgi/foo.py')
-        fab(Apache(wsgi='django_wsgi.py').make_wsgi)
+        fab(Apache(wsgi='django_wsgi.py').upload_wsgi)
         self.assertFileExists(env.conf.ENV_DIR+'/var/wsgi/foo.py')