Make deploy search in config dir first.
[redakcja.git] / deployment.py
1 from __future__ import with_statement
2
3 import shutil
4 import os
5 import sys
6 import logging
7
8 logging.basicConfig(stream=sys.stderr, format="%(levelname)s:: %(message)s", level=logging.INFO)
9
10 from string import Template
11
12 class DeploySite(object):
13
14     def __init__(self, **env):
15         self.env = env
16
17         for arg in ('ROOT', 'PROJECT_NAME', 'PYTHON_VERSION'):
18             if arg not in self.env:
19                 raise ValueError("Argument '%s' is required." % arg)
20
21         if 'PYTHON_BASE' not in self.env:
22             self.env['PYTHON_BASE'] = os.path.join(self.env['ROOT'], 'pythonenv')
23
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']
27
28         if 'PYTHON_SITE' not in self.env:
29             self.env['PYTHON_SITE'] = os.path.join(
30                         self.env['PYTHON_BASE'], 'lib',
31                         'python' + self.env['PYTHON_VERSION'], 'site-packages')
32
33         if 'APP_DIR' not in self.env:
34             self.env['APP_DIR'] = os.path.join(self.env['ROOT'], 'application')
35
36         if 'CONFIG_DIR' not in self.env:
37             self.env['CONFIG_DIR'] = os.path.join(self.env['ROOT'], 'etc')
38
39         if 'MEDIA_DIR' not in self.env:
40             self.env['MEDIA_DIR'] = os.path.join(self.env['ROOT'], 'www', 'media')
41
42         self._logger = logging.getLogger("deployment")
43
44     def info(self, *args, **kwargs):
45         self._logger.info(*args, **kwargs)
46
47     def render_template(self, source, dest, extra_context={}):
48         self.info("Rendering template: %s", source)
49
50         with open(source, 'rb') as source_file:
51             t = Template(source_file.read())
52
53         context = dict(self.env)
54         context.update(extra_context)
55
56         with open(dest, 'wb') as dest_file:
57             dest_file.write(t.safe_substitute(context))
58
59         self.info("Done.")
60
61     def restart_app(self):
62         pass
63
64     def update_app(self):
65         pass
66
67     def install_dependencies(self):
68         pass
69
70     def deploy(self):
71         self.update_app()
72         self.install_dependencies()
73         self.update_config()
74         self.restart_app()
75
76     def find_resource(self, path):
77         for dir in (self.env['CONFIG_DIR'], self.env['APP_DIR']):
78             full_path = os.path.join(dir, path)
79             if os.path.isfile(full_path):
80                 return full_path
81
82         raise ValueError("Resource '%s' not found" % path)
83
84     @classmethod
85     def run_deploy(cls, *args, **kwargs):
86         site = cls(*args, **kwargs)
87         return site.deploy()
88
89 class WSGISite(DeploySite):
90
91     def __init__(self, **env):
92         super(WSGISite, self).__init__(**env)
93
94         if 'WSGI_FILE' not in self.env:
95             self.env['WSGI_FILE'] = os.path.join(self.env['ROOT'], 'www',
96                                         'wsgi', self.env['PROJECT_NAME']) + '.wsgi'
97
98         if 'WSGI_SOURCE_FILE' not in self.env:
99             self.env['WSGI_SOURCE_FILE'] = 'wsgi_app.template'
100
101         if 'WSGI_USER' not in self.env:
102             self.env['WSGI_USER'] = 'www-data'
103
104     def restart_app(self):
105         self.info("Restarting wsgi application: %s", self.env['WSGI_FILE'])
106         os.system("touch %s" % self.env['WSGI_FILE'])
107
108     def update_config(self):
109         source = self.find_resource(self.env['WSGI_SOURCE_FILE'])
110         self.render_template(source, self.env['WSGI_FILE'])
111
112 class PIPSite(DeploySite):
113
114     def install_dependencies(self):
115         self.info("Installing requirements")
116         os.system("pip install -r %s" % self.find_resource('requirements.txt'))
117
118         try:
119             self.info("Installing local requirements")
120             os.system("pip install -r %s" % self.find_resource('requirements_local.txt'))
121         except ValueError:
122             pass
123
124 class GitSite(DeploySite):
125
126     def update_app(self):
127         self.info("Updating repository.")
128         os.system("cd %s; git pull" % self.env['APP_DIR'])
129
130 class ApacheSite(DeploySite):
131
132     def __init__(self, **env):
133         super(ApacheSite, self).__init__(**env)
134
135         if 'VHOST_SOURCE_FILE' not in self.env:
136             self.env['VHOST_SOURCE_FILE'] = 'apache_vhost.template'
137
138         if 'VHOST_FILE' not in self.env:
139             self.env['VHOST_FILE'] = os.path.join(self.env['CONFIG_DIR'], self.env['PROJECT_NAME'] + '.vhost')
140
141     def update_config(self):
142         source = self.find_resource(self.env['VHOST_SOURCE_FILE'])
143         self.render_template(source, self.env['VHOST_CONFIG_FILE'])