forked from ryankanno/django-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
327 lines (238 loc) · 7.63 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from fabric.api import env, require, task, settings, run, sudo, roles, cd, prefix, put
from fabric.operations import prompt
from fabric.contrib.files import exists, sed, upload_template
from fabric.colors import *
from contextlib import contextmanager
import os
import calendar
import time
from time import gmtime, strftime
"""
Configuration
#CHANGEME
"""
# root
env.root = os.path.abspath(os.path.dirname(__file__))
# project
env.project_name = 'skeleton'
env.django_project_dir = 'sample_project'
# paths
# > remote
env.path = '/var/www/apps/%(project_name)s' % env
env.env_path = '%(path)s/env' % env
env.repo_path = '%(path)s/repo' % env
env.rel_path = '%(path)s/rel' % env
env.curr_path = '%(path)s/current' % env
env.pip_req_file = '%(repo_path)s/etc/requirements.txt' % env
# > local
env.maintenance_file = '%(root)s/etc/maintenance.html.tmpl' % env
# config
env.activate = 'source %(env_path)s/bin/activate' % env
env.python = 'python2.7'
env.utc_ts = gmtime()
env.utc_ts_str = strftime('%Y%m%d_%H%M%S', env.utc_ts)
env.user = 'django'
env.password = 'django'
env.git_repo = 'https://github.com/ryankanno/django-skeleton.git'
env.num_releases = 5
env.cache_buster = ''
"""
Environments
"""
@task
def production():
env.settings = 'production'
env.hosts = ['88.88.88.88']
env.roledefs.update({'www': ['88.88.88.88']})
@task
def staging():
env.settings = 'staging'
@task
def local():
env.settings = 'local'
"""
Branches
"""
@task
def master():
env.branch = 'master'
@task
def branch(branch_name):
env.branch = branch_name
"""
Task helpers
"""
@contextmanager
def virtualenv():
with cd(env.env_path):
with prefix(env.activate):
yield
def setup_directories():
with settings(warn_only=True):
run('mkdir -p %(path)s' % env)
run('mkdir -p %(env_path)s' % env)
run('mkdir -p %(repo_path)s' % env)
run('mkdir -p %(rel_path)s' % env)
def setup_virtualenv():
if not exists('%(env_path)s/bin' % env):
run('virtualenv -p %(python)s --no-site-packages %(env_path)s;' % env)
with virtualenv():
run('easy_install -U setuptools')
run('easy_install pip')
def install_requirements():
""" Install pip requirements.txt """
with virtualenv():
run('pip install -r %(pip_req_file)s' % env)
def clone_repo():
""" Clone repo """
if not exists('%(repo_path)s/.git' % env):
run('git clone %(git_repo)s %(repo_path)s' % env)
def checkout():
""" Checkout """
with cd(env.repo_path):
run('git checkout %(branch)s; git pull origin %(branch)s; git submodule update --init' % env)
def service(name, *actions):
""" Generic service function """
for action in actions:
sudo('/etc/init.d/%s %s' % (name, action))
def copy_to_releases():
run('cp -R %(repo_path)s %(rel_path)s/%(utc_ts_str)s' % env)
run('rm -rf %(rel_path)s/%(utc_ts_str)s/.git*' % env)
def configure_app():
run('cp %(rel_path)s/%(utc_ts_str)s/%(django_project_dir)s/settings_%(settings)s.py \
%(rel_path)s/%(utc_ts_str)s/%(django_project_dir)s/settings.py' % env)
manage('collectstatic', '%(rel_path)s/%(utc_ts_str)s' % env)
#sed('%(rel_path)s/%(utc_ts_str)s/templates/base.html' % env,
# '%(cache_buster)s' % env, str(calendar.timegm(env.utc_ts)))
def symlink_release():
""" Symlink the current release """
""" See: http://blog.moertel.com/articles/2005/08/22/how-to-change-symlinks-atomically """
milliseconds_since_epoch = int(round(time.time() * 1000))
curr_tmp = '%s_%s' % (env.curr_path, milliseconds_since_epoch)
run('ln -s %s %s && mv -Tf %s %s' %
(get_latest_release(),
curr_tmp,
curr_tmp,
'%(curr_path)s' % env))
def get_sorted_releases():
""" Returns the list of releases sorted """
with cd('%(rel_path)s' % env):
return sorted(run('ls -xt').split())
def get_latest_release():
releases = get_sorted_releases()
return '%s/%s' % (env.rel_path, releases[-1])
def remove_latest_release():
latest_release = get_latest_release()
with cd('%(rel_path)s' % env):
run('rm -rf %s' % latest_release)
def keep_num_releases(num_releases):
releases = get_sorted_releases()
num_to_delete = len(releases) - num_releases
# Must keep a minimum of one release
if num_to_delete > 1:
del_releases = releases[:num_to_delete]
with cd('%(rel_path)s' % env):
for release in del_releases:
run('rm -rf %s' % release)
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
yield os.path.join(root, basename)
"""
Setup
"""
@task
def configure_www(file):
""" Configure the Nginx www server"""
require('settings', provided_by=[production, staging, local])
context = {
'server_name': env.project_name,
'curr_path': env.curr_path,
}
upload_template(file,
'/etc/nginx/nginx.conf',
context=context, use_sudo=True)
www('restart')
@task
def configure_uwsgi(file):
""" Configure UWSGI app server"""
require('settings', provided_by=[production, staging, local])
upload_template(file,
'/etc/uwsgi/apps-available/%(project_name)s' % env,
use_sudo=True)
sudo('ln -fs /etc/uwsgi/apps-available/%(project_name)s /etc/uwsgi/apps-enabled/%(project_name)s.xml' % env)
app('restart')
"""
Release
"""
@task
def setup():
require('settings', provided_by=[production, staging, local])
require('branch', provided_by=[master, branch])
setup_directories()
setup_virtualenv()
clone_repo()
checkout()
install_requirements()
@task
def deploy(with_maintenance=False, update_requirements=False):
""" Checkout, configure, symlink release """
require('settings', provided_by=[production, staging, local])
require('branch', provided_by=[master, branch])
if with_maintenance:
with settings(warn_only=True):
maintenance_up()
checkout()
copy_to_releases()
if update_requirements:
install_requirements()
configure_app()
symlink_release()
@task
def rollback():
""" Removes latest release and repoints current symlink to the previous release """
require('settings', provided_by=[production, staging, local])
remove_latest_release()
symlink_release()
@task
def cleanup(num_releases=0):
""" Cleans up deploy directory, keeping N number of releases """
require('settings', provided_by=[production, staging, local])
keep_num_releases(num_releases or env.num_releases)
@task
def maintenance_up():
""" Prompts for a reason why we are down, then deploys maintenance page """
ctx = {}
ctx['reason'] = prompt("Why are we downs?")
upload_template('%(maintenance_file)s' % env, '%(curr_path)s/maintenance.html' % env, context=ctx, backup=False)
@task
def maintenance_down():
""" Removes maintenance page """
if exists('%(curr_path)s/maintenance.html' % env):
run('rm %(curr_path)s/maintenance.html' % env)
@task
def shiva():
prompt("Do you want to remove all references to the project?")
"""
Maintenance
"""
@task
@roles('www')
def www(action):
service('nginx', action)
@task
@roles('www')
def app(action):
service('uwsgi', action)
@task
@roles('www')
def cache(action):
if action == 'purge':
run('redis-cli FLUSHALL')
@task
@roles('www')
def manage(command, path=env.curr_path):
with virtualenv():
with cd(path):
run('python manage.py %s' % (command,))