Robert Myers avatar Robert Myers committed 66ba0a8

work on proxy and worker classes

Comments (0)

Files changed (2)

             main.conf
             vhost.conf
             ...
-            uwsgi/
-                worker.conf
-                ...
-            gunicorn/
-                worker.conf
-                ...
+            uwsgi.conf
+            ...
+            gunicorn.conf
+            ...
         apache/
+            main.coonf
             ...
 """
 
 import os
 import posixpath
 
+from django.template import TemplateDoesNotExist
 from django.template.loader import render_to_string
 
 from cannula.conf import CANNULA_BASE
 
-class ProxyBase(object):
+class Proxy(object):
     
-    name = 'base'
-    template_base = 'proxy'
-    
-    def __init__(self, base=CANNULA_BASE):
+    def __init__(self, base=CANNULA_BASE, name='base', 
+                 template_base='proxy', default_vhost='localhost'):
         self.cannula_base = base
+        self.name = name
+        self.template_base = posixpath.join(self.template_base, self.name)
+        self.default_vhost = default_vhost
         self.base = os.path.join(self.cannula_base, self.name)
-        self.main_conf = posixpath.join(self.template_base, self.name, 'main.conf')
-        self.vhost_conf = posixpath.join(self.template_base, self.name, 'vhost.conf')
+        self.main_conf = os.path.join(self.base, '%s.conf' % self.name)
         self.vhost_base = os.path.join(self.base, 'vhosts')
     
-    def default_context(self):
-        return {}
-       
+    def write_file(self, filename, context, template=None):
+        """
+        Write file to the filesystem, if the template does not 
+        exist fail silently. Otherwise write the file out and return
+        boolean if the content had changed from last write. If the 
+        file is new then return True.
+        """
+        if template is None:
+            template = os.path.basename(filename)
+        if '/' not in template:
+            # template is not a full path, generate it now.
+            template = posixpath.join(self.template_base, template)
+        try:
+            content = render_to_string(template, context)
+        except TemplateDoesNotExist:
+            return False
+        
+        if not os.path.isdir(os.path.dirname(filename)):
+            os.makedirs(os.path.dirname(filename), mode=0750)
+        
+        original_content = ''
+        if os.path.isfile(filename):
+            with open(filename, 'rb') as file:
+                original_content = file.read()
+        
+        with open(filename, 'wb') as file:
+            file.write(content)
+        
+        return content != original_content
+
+    
+    def write_worker_conf(self, worker, deployment, context):
+        name = "%s_%s.conf" % (deployment.project.group.abbr,
+                               deployment.project.abbr)
+        vhost = self.default_vhost
+        if deployment.vhost:
+            vhost = self.default_vhost
+        filename = os.path.join(self.vhost_base, vhost, name)
+        
+        return self.write_file(filename, context, '%s.conf' % worker)
+        
+     
     def write_main_conf(self, context={}):
-        if not os.path.isdir(self.base):
-            os.makedirs(self.base, mode=0750)
-        conf_file = os.path.join(self.base, '%s.conf' % self.name)
-        content = render_to_string(self.main_conf, context)
-        with open(conf_file, 'wb') as file:
-            file.write(content)
-            file.close()
+        return self.write_file(self.main_conf, context, 'main.conf')
     
-    def write_vhost_conf(self, vhost='localhost', context={}):
-        vhost_name = vhost.replace('.', '_')
-        apps_base = os.path.join(self.vhost_base, vhost)
-        if not os.path.isdir(apps_base):
-            os.makedirs(apps_base, mode=0750)
+    def write_vhost_conf(self, vhost, context={}):
+        vhost_name = vhost.replace('.', '_') + '.conf'
         vhost_conf_file = os.path.join(self.vhost_base, vhost_name)
-        content = render_to_string(self.vhost_conf, context)
-        with open(vhost_conf_file, 'wb') as file:
-            file.write(content)
-            file.close()
         
+        return self.write_file(vhost_conf_file, context, 'vhost.conf')
 
-class Apache(ProxyBase):
-    
-    name = 'apache'
 
-
-class Nginx(ProxyBase):
-    
-    name = 'nginx'
-
-
-apache = Apache()
-nginx = Nginx()
+apache = Proxy(name='apache')
+nginx = Proxy(name='nginx')

cannula/worker.py

 import os
+import posixpath
+
+from django.template import TemplateDoesNotExist
+from django.template.loader import render_to_string
 
 from cannula.conf import api, proxy, CANNULA_CLUSTER_DEFAULTS, CANNULA_BASE
 
 class InvalidStatus(DeploymentError):
     """The deployment is not in the correct state for this action."""
 
-class WorkerBase(object):
-    """Base Worker class. Subclasses need to define 'deploy', 'delete' 
-    and 'modify'
+class Worker(object):
+    """
+    Worker Class
+    
+    Created when a task is being run based off of the projects
+    settings. These can be overwritten by the deployment.
     """
     
-    # The process runner, name used for templates ('uwsgi', 'gunicorn', 'fastcgi')
-    worker = ''
-    # The type of application ('django', 'pylons', 'paste', 'php')
-    flavor = ''
-    
+    def __init__(self, worker, flavor, template_base='worker'):
+        # The process runner, name used for templates ('uwsgi', 'gunicorn', 'fastcgi')
+        self.worker = worker
+        # The type of application ('django', 'pylons', 'paste', 'php')
+        self.flavor = flavor
+        self.template_base = template_base
     
     def defaults(self, deployment):
         """Return a dictionary of the default values for this deployment."""
     def deploy(self, deployment, **kwargs):
         """Deploy the deployment object. """
         defaults = self.defaults(deployment)
-        worker_base = os.path.join(CANNULA_BASE, self.worker)
+        # Common name for all files.
+        name = '%s_%s' % (deployment.project.group.abbr,
+                          deployment.project.abbr)
+        # Write the wsgi file for this deployment.
+        wsgi_file = os.path.join(CANNULA_BASE, self.worker, '%s.py' % name)
+        wsgi_file_changed = self.write_file(wsgi_file, defaults, 
+                                            template='wsgi.py')
         
-    
+        ini_file = os.path.join(CANNULA_BASE, self.worker, '%s.ini' % name)
+        ini_file_changed = self.write_file(ini_file, defaults, 
+                                           template='project.ini')
+        # Write the proxy conf snippet.
+        proxy_conf_changed = proxy.write_worker_conf(self.worker, deployment, defaults)
+        
+        changed = {'wsgi': wsgi_file_changed,
+                   'ini': ini_file_changed,
+                   'proxy': proxy_conf_changed,
+        }
+        return changed
+        
+        
+    def write_file(self, filename, context, template=None):
+        """
+        Write file to the filesystem, if the template does not 
+        exist fail silently. Otherwise write the file out and return
+        boolean if the content had changed from last write. If the 
+        file is new then return True.
+        """
+        if template is None:
+            template = os.path.basename(filename)
+        if '/' not in template:
+            # template is not a full path, generate it now.
+            template = posixpath.join(self.template_base, self.worker, template)
+        try:
+            content = render_to_string(template, context)
+        except TemplateDoesNotExist:
+            return False
+        
+        if not os.path.isdir(os.path.dirname(filename)):
+            os.makedirs(os.path.dirname(filename), mode=0750)
+        
+        original_content = ''
+        if os.path.isfile(filename):
+            with open(filename, 'rb') as file:
+                original_content = file.read()
+        
+        with open(filename, 'wb') as file:
+            file.write(content)
+        
+        return content != original_content
+        
     def delete(self, *args, **kwargs):
         raise NotImplementedError
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.