1 from __future__ import with_statement
8 logging.basicConfig(stream=sys.stderr, format="%(levelname)s:: %(message)s", level=logging.INFO)
10 from string import Template
12 class DeploySite(object):
14 def __init__(self, **env):
17 for arg in ('ROOT', 'PROJECT_NAME', 'PYTHON_VERSION'):
18 if arg not in self.env:
19 raise ValueError("Argument '%s' is required." % arg)
21 if 'PYTHON_BASE' not in self.env:
22 self.env['PYTHON_BASE'] = os.path.join(self.env['ROOT'], 'pythonenv')
24 if 'PYTHON_BIN' not in self.env:
25 self.env['PYTHON_BIN'] = os.path.join(
26 self.env['PYTHON_BASE'], 'bin', 'python') + self.env['PYTHON_VERSION']
28 if 'PIP_BIN' not in self.env:
29 self.env['PIP_BIN'] = os.path.join(self.env['PYTHON_BASE'], 'bin', 'pip')
31 if 'PYTHON_SITE' not in self.env:
32 self.env['PYTHON_SITE'] = os.path.join(
33 self.env['PYTHON_BASE'], 'lib',
34 'python' + self.env['PYTHON_VERSION'], 'site-packages')
36 if 'APP_DIR' not in self.env:
37 self.env['APP_DIR'] = os.path.join(self.env['ROOT'], 'application')
39 if 'CONFIG_DIR' not in self.env:
40 self.env['CONFIG_DIR'] = os.path.join(self.env['ROOT'], 'etc')
42 if 'MEDIA_DIR' not in self.env:
43 self.env['MEDIA_DIR'] = os.path.join(self.env['ROOT'], 'www', 'media')
45 self._logger = logging.getLogger("deployment")
47 def info(self, *args, **kwargs):
48 self._logger.info(*args, **kwargs)
50 def render_template(self, source, dest, extra_context={}):
51 self.info("Rendering template: %s", source)
53 with open(source, 'rb') as source_file:
54 t = Template(source_file.read())
56 context = dict(self.env)
57 context.update(extra_context)
59 with open(dest, 'wb') as dest_file:
60 dest_file.write(t.safe_substitute(context))
64 def restart_app(self):
70 def install_dependencies(self):
75 self.install_dependencies()
79 def find_resource(self, path):
80 for dir in (self.env['CONFIG_DIR'], self.env['APP_DIR']):
81 full_path = os.path.join(dir, path)
82 if os.path.isfile(full_path):
85 raise ValueError("Resource '%s' not found" % path)
88 def run_deploy(cls, *args, **kwargs):
89 site = cls(*args, **kwargs)
92 class WSGISite(DeploySite):
94 def __init__(self, **env):
95 super(WSGISite, self).__init__(**env)
97 if 'WSGI_FILE' not in self.env:
98 self.env['WSGI_FILE'] = os.path.join(self.env['ROOT'], 'www',
99 'wsgi', self.env['PROJECT_NAME']) + '.wsgi'
101 if 'WSGI_SOURCE_FILE' not in self.env:
102 self.env['WSGI_SOURCE_FILE'] = 'wsgi_app.template'
104 if 'WSGI_USER' not in self.env:
105 self.env['WSGI_USER'] = 'www-data'
107 def restart_app(self):
108 self.info("Restarting wsgi application: %s", self.env['WSGI_FILE'])
109 os.system("touch %s" % self.env['WSGI_FILE'])
111 def update_config(self):
112 source = self.find_resource(self.env['WSGI_SOURCE_FILE'])
113 self.render_template(source, self.env['WSGI_FILE'])
115 class PIPSite(DeploySite):
117 def install_dependencies(self):
118 self.info("Installing requirements")
119 os.system("%s install -r %s" % (self.env['PIP_BIN'], self.find_resource('requirements.txt')))
122 self.info("Installing local requirements")
123 os.system("%s install -r %s" % (self.env['PIP_BIN'], self.find_resource('requirements_local.txt')))
127 class GitSite(DeploySite):
129 def update_app(self):
130 self.info("Updating repository.")
131 os.system("cd %s; git pull" % self.env['APP_DIR'])
133 class ApacheSite(DeploySite):
135 def __init__(self, **env):
136 super(ApacheSite, self).__init__(**env)
138 if 'VHOST_SOURCE_FILE' not in self.env:
139 self.env['VHOST_SOURCE_FILE'] = 'apache_vhost.template'
141 if 'VHOST_FILE' not in self.env:
142 self.env['VHOST_FILE'] = os.path.join(self.env['CONFIG_DIR'], self.env['PROJECT_NAME'] + '.vhost')
144 def update_config(self):
145 source = self.find_resource(self.env['VHOST_SOURCE_FILE'])
146 self.render_template(source, self.env['VHOST_CONFIG_FILE'])