-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
335 lines (256 loc) · 9.85 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
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os.path
import sys
from functools import wraps
from django.conf import settings as django_settings
from django.core.management.utils import get_random_secret_key
from fabric.api import (cd, env, prefix, prompt, put, quiet, require, run,
settings, sudo, task)
from fabric.colors import green, yellow
from fabric.contrib import django
# put project directory in path
project_root = os.path.abspath(os.path.dirname(__file__))
sys.path.append(project_root)
# -------------------------------
# SETTINGS VARIABLES
# Please verify each variable below and edit as necessary to match
# your project configuration.
# TODO: externalise to settings to base.py
# so this becomes a generic script without project-specific code.
# The name of the Django app for this project
# Folder that contains wsgi.py
PROJECT_NAME = 'tap'
# Git repository pointer
REPOSITORY = 'https://github.com/kingsdigitallab/{}-django.git'.format(
PROJECT_NAME)
env.gateway = 'ssh.kdl.kcl.ac.uk'
# Host names used as deployment targets
env.hosts = ['{}.kdl.kcl.ac.uk'.format(PROJECT_NAME)]
# Absolute filesystem path to project 'webroot'
env.root_path = '/vol/{}/webroot/'.format(PROJECT_NAME)
# Absolute filesystem path to project Django root
env.django_root_path = '/vol/{}/webroot/'.format(PROJECT_NAME)
# Absolute filesystem path to Python virtualenv for this project
env.envs_path = os.path.join(env.root_path, 'envs')
# -------------------------------
django.project(PROJECT_NAME)
# Set FABRIC_GATEWAY = '[email protected]' in local.py
# if you are behind a proxy.
FABRIC_GATEWAY = getattr(django_settings, 'FABRIC_GATEWAY', None)
if FABRIC_GATEWAY:
env.forward_agent = True
env.gateway = FABRIC_GATEWAY
# Name of linux user who deploys on the remote server
env.user = django_settings.FABRIC_USER
def server(func):
"""Wraps functions that set environment variables for servers"""
@wraps(func)
def decorated(*args, **kwargs):
try:
env.servers.append(func)
except AttributeError:
env.servers = [func]
return func(*args, **kwargs)
return decorated
@task
@server
def dev():
env.srvr = 'dev'
set_srvr_vars()
@task
@server
def stg():
env.srvr = 'stg'
set_srvr_vars()
@task
@server
def liv():
env.srvr = 'liv'
set_srvr_vars()
def set_srvr_vars():
# Absolute filesystem path to the django project root
# Contains manage.py
env.path = os.path.join(env.root_path, env.srvr, 'django',
'{}-django'.format(PROJECT_NAME))
env.within_virtualenv = 'source {}'.format(
os.path.join(get_virtual_env_path(), 'bin', 'activate'))
@task
def setup_environment(version=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
create_virtualenv()
clone_repo()
update(version)
install_requirements()
@task
def create_virtualenv():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
env_vpath = get_virtual_env_path()
if run('ls {}'.format(env_vpath)).succeeded:
print(
green('virtual environment at [{}] exists'.format(env_vpath)))
return
print(yellow('setting up virtual environment in [{}]'.format(env_vpath)))
run('virtualenv {}'.format(env_vpath))
def get_virtual_env_path():
'''Returns the absolute path to the python virtualenv for the server
(dev, stg, live) we are working on.
E.g. /vol/tvof/webroot/envs/dev
'''
return os.path.join(env.envs_path, env.srvr)
@task
def clone_repo():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
if run('ls {}'.format(os.path.join(env.path, '.git'))).succeeded:
print(green(('repository at'
' [{}] exists').format(env.path)))
return
print(yellow('cloneing repository to [{}]'.format(env.path)))
run('git clone {} {}'.format(REPOSITORY, env.path))
@task
def install_requirements():
fix_permissions('virtualenv')
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
reqs = 'requirements-{}.txt'.format(env.srvr)
try:
assert os.path.exists(reqs)
except AssertionError:
reqs = 'requirements.txt'
with cd(env.path), prefix(env.within_virtualenv):
run('pip install -q --no-cache -U -r {}'.format(reqs))
@task
def reinstall_requirement(which):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('pip uninstall {0} && pip install --no-deps {0}'.format(which))
@task
def deploy(version=None):
update(version)
install_requirements()
upload_local_settings()
own_django_log()
fix_permissions()
migrate()
collect_static()
# update_index()
# clear_cache()
touch_wsgi()
check_deploy()
@task
def update(version=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if version:
# try specified version first
to_version = version
elif not version and env.srvr in ['local', 'vagrant', 'dev']:
# if local, vagrant or dev deploy to develop branch
to_version = 'develop'
else:
# else deploy to master branch
to_version = 'master'
with cd(env.path), prefix(env.within_virtualenv):
run('git pull')
run('git checkout {}'.format(to_version))
@task
def upload_local_settings():
require('srvr', 'path', provided_by=env.servers)
with cd(env.path):
with settings(warn_only=True):
if run('ls {}/settings/local.py'.format(PROJECT_NAME)).failed:
db_host = prompt('Database host: ')
db_pwd = prompt('Database password: ')
put('{}/settings/local_{}.py'.format(PROJECT_NAME, env.srvr),
'{}/settings/local.py'.format(PROJECT_NAME), mode='0664')
run('echo >> {}/settings/local.py'.format(PROJECT_NAME))
run('echo '
'"DATABASES[\'default\'][\'PASSWORD\'] = \'{}\'" >>'
'{}/settings/local.py'.format(db_pwd, PROJECT_NAME))
run('echo '
'"DATABASES[\'default\'][\'HOST\'] = \'{}\'" >>'
'{}/settings/local.py'.format(db_host, PROJECT_NAME))
run('echo '
'"SECRET_KEY = \'{}\'" >>'
'{}/settings/local.py'.format(
get_random_secret_key(), PROJECT_NAME))
@task
def own_django_log():
""" make sure logs/django.log is owned by www-data"""
require('srvr', 'path', provided_by=env.servers)
with quiet():
log_path = os.path.join(env.path, 'logs', 'django.log')
if run('ls {}'.format(log_path)).succeeded:
sudo('chown www-data:www-data {}'.format(log_path))
sudo('chmod g+rw {}'.format(log_path))
@task
def fix_permissions(category='static'):
'''
Reset the permissions on various paths.
category: determines which set of paths to work on:
'static' (default): django static path + general project path
'virtualenv': fix the virtualenv permissions
'''
require('srvr', 'path', provided_by=env.servers)
processed = False
with quiet():
if category == 'static':
processed = True
log_path = os.path.join(env.path, 'logs', 'django.log')
if run('ls {}'.format(log_path)).succeeded:
sudo('setfacl -R -m g:www-data:rwx {0}/logs {0}/static'.
format(env.path))
sudo('setfacl -R -d -m g:www-data:rwx {0}/logs {0}/static'.
format(env.path))
sudo('setfacl -R -m g:kdl-staff:rwx {0}/logs {0}/static'.
format(env.path))
sudo('setfacl -R -d -m g:kdl-staff:rwx {0}/logs {0}/static'.
format(env.path))
sudo('chgrp -Rf kdl-staff {}'.format(env.path))
sudo('chmod -Rf g+w {}'.format(env.path))
if category == 'virtualenv':
path = get_virtual_env_path()
sudo('chgrp -Rf kdl-staff {}'.format(path))
sudo('chmod -Rf g+rw {}'.format(path))
processed = True
if not processed:
raise Exception(
'fix_permission(category="{}"): unrecognised category name.'.
format(category)
)
@task
def migrate(app=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py migrate {}'.format(app if app else ''))
@task
def collect_static(process=False):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['local', 'vagrant']:
print(yellow('Do not run collect_static on local servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py collectstatic {process} --noinput'.format(
process=('--no-post-process' if not process else '')))
@task
def update_index():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py update_index')
@task
def clear_cache():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py clear_cache')
@task
def touch_wsgi():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(os.path.join(env.path, PROJECT_NAME)), \
prefix(env.within_virtualenv):
run('touch wsgi.py')
@task
def check_deploy():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['stg', 'liv']:
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py check --deploy')