forked from praekeltfoundation/txtalert
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fabfile.py
246 lines (195 loc) · 7.83 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from datetime import datetime
from os.path import join as _join
from springfield.deploy.utils import git, system, base, twistd
RELEASE_NAME_FORMAT = '%Y%m%d_%H%M%S' # timestamped
# default for now
env.hosts = ['ovm1.praekelt.com']
def _setup_env(fn):
def wrapper(branch, *args, **kwargs):
layout(branch)
return fn(branch, *args, **kwargs)
wrapper.func_name = fn.func_name
wrapper.func_doc = (fn.func_doc or '') + \
"Requires the branch from which you " \
"want to deploy as an argument."
return wrapper
def _setup_env_for(branch):
env.branch = branch
# the github_user and github_repo variables are set by the ~/.fabricrc
# or whatever is provided to `fab` with the `-c` command line variable.
# See the sample fabric.config file.
env.github_repo = 'git://github.com/%(github_user)s/%(github_repo_name)s.git' % env
env.deploy_to = '/var/praekelt/%(github_repo_name)s/%(branch)s' % env
env.releases_path = "%(deploy_to)s/releases" % env
env.current = "%(deploy_to)s/current" % env
env.shared_path = "%(deploy_to)s/shared" % env
env.tmp_path = "%(shared_path)s/tmp" % env
env.pip_cache_path = "%(tmp_path)s/cache/pip" % env
env.pids_path = "%(tmp_path)s/pids" % env
env.logs_path = "%(shared_path)s/logs" % env
env.repo_path = "%(shared_path)s/repositories" % env
env.django_settings_file = "environments.%(branch)s" % env
env.layout = [
env.releases_path,
env.tmp_path,
env.pip_cache_path,
env.pids_path,
env.logs_path,
env.repo_path,
]
def _repo_path(repo_name):
return '%(repo_path)s/%(github_repo_name)s' % env
def _repo(repo_name):
"""helper to quickly switch to a repository"""
return cd(_repo_path(repo_name))
def layout(branch):
"""
Create a file system directory layout for deploying to.
"""
require('hosts')
_setup_env_for(branch)
require('layout', provided_by=['_setup_env_for'])
system.create_dirs(env.layout)
@_setup_env
def deploy(branch):
"""
Deploy the application in a timestamped release folder.
$ fab deploy:staging
Internally this does the following:
* `git pull` if a cached repository already exists
* `git clone` if it's the first deploy ever
* Checkout the current selected branch
* Create a new timestamped release directory
* Copy the cached repository to the new release directory
* Setup the virtualenv
* Install PIP's requirements, downloading new ones if not already cached
* Symlink `<branch>/current` to `<branch>/releases/<timestamped release directory>`
"""
if not git.is_repository(_repo_path(env.github_repo_name)):
# repository doesn't exist, do a fresh clone
with cd(env.repo_path):
git.clone(env.github_repo, env.github_repo_name)
with _repo(env.github_repo_name):
git.checkout(branch)
else:
# repository exists
with _repo(env.github_repo_name):
if not (branch == git.current_branch()):
# switch to our branch if not already
git.checkout(branch)
# pull in the latest code
git.pull(branch)
# 20100603_125848
new_release_name = datetime.utcnow().strftime(RELEASE_NAME_FORMAT)
# /var/praekelt/richmond/staging/releases/20100603_125848
new_release_path = _join(env.releases_path, new_release_name)
# /var/praekelt/richmond/staging/releases/20100603_125848/richmond
# Django needs the project name as it's parent dir since that is
# automagically appended to the loadpath
new_release_repo = _join(new_release_path, env.github_repo_name)
system.create_dir(new_release_path)
system.copy_dirs(_repo_path(env.github_repo_name), new_release_path)
# copy_settings_file(branch, release=new_release_name)
symlink_shared_dirs = ['logs', 'tmp']
for dirname in symlink_shared_dirs:
with cd(new_release_repo):
system.remove(dirname, recursive_force=True)
system.symlink(_join(env.shared_path, dirname), dirname)
# create the virtualenv
create_virtualenv(branch)
# ensure we're deploying the exact revision as we locally have
base.set_current(new_release_name)
@_setup_env
def copy_settings_file(branch, release=None):
"""
Copy the settings file for this branch to the server
$ fab copy_settings_file:staging
If no release is specified it defaults to the latest release.
"""
release = release or base.current_release()
directory = _join(env.releases_path, release, env.github_repo_name)
put(
"environments/%(branch)s.py" % env,
_join(directory, "environments/%(branch)s.py" % env)
)
@_setup_env
def managepy(branch, command, release=None):
"""
Execute a ./manage.py command in the virtualenv with the current
settings file
$ fab managepy:staging,"syncdb"
This will do a `./manage.py syncdb --settings=environments.staging`
within the virtualenv.
If no release is specified it defaults to the latest release.
"""
return execute(branch, "./manage.py %s --settings=%s" % (
command,
env.django_settings_file
), release)
@_setup_env
def execute(branch, command, release=None):
"""
Execute a shell command in the virtualenv
$ fab execute:staging,"tail logs/*.log"
If no release is specified it defaults to the latest release.
"""
release = release or base.current_release()
directory = _join(env.releases_path, release, env.github_repo_name)
return _virtualenv(directory, command)
@_setup_env
def create_virtualenv(branch, release=None):
"""
Create the virtualenv and install the PIP requirements
$ fab create_virtualenv:staging
If no release is specified it defaults to the latest release
"""
release = release or base.current_release()
directory = _join(env.releases_path, release, env.github_repo_name)
with cd(directory):
return run(" && ".join([
"virtualenv --no-site-packages ve",
"source ve/bin/activate",
"pip -E ve install --download-cache=%(pip_cache_path)s -r requirements.pip" % env,
"python setup.py develop",
]))
def _virtualenv(directory, command, env_name='ve'):
activate = 'source %s/bin/activate' % env_name
deactivate = 'deactivate'
with cd(directory):
run(' && '.join([activate, command, deactivate]))
@_setup_env
def update(branch):
"""
Pull in the latest code for the latest release.
$ fab update:staging
Only to be used for small fixed, typos etc..
"""
current_release = base.releases(env.releases_path)[-1]
with cd(_join(env.current, env.github_repo_name)):
git.pull(branch)
@_setup_env
def start(branch):
return execute(branch, "supervisorctl -c supervisord.%(branch)s.conf start gunicorn:*" % env)
@_setup_env
def stop(branch):
return execute(branch, "supervisorctl -c supervisord.%(branch)s.conf stop gunicorn:*" % env)
@_setup_env
def restart(branch):
return execute(branch, "supervisorctl -c supervisord.%(branch)s.conf restart gunicorn:*" % env)
@_setup_env
def cleanup(branch,limit=5):
"""
Cleanup old releases
$ fab cleanup:staging,limit=10
Remove old releases, the limit argument is optional (defaults to 5).
"""
run("cd %(releases_path)s && ls -1 . | head --line=-%(limit)s | " \
"xargs rm -rf " % {
'releases_path': env.releases_path,
'limit': limit
}
)