-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
280 lines (202 loc) · 7.22 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from fabric.api import local, task, prefix, run, sudo, env, require, cd, quiet
from fabric.colors import green, yellow
from fabric.contrib import django
from functools import wraps
import sys
import os.path
from getpass import getuser
from socket import gethostname
# put project directory in path
project_root = os.path.abspath(os.path.dirname(__file__))
sys.path.append(project_root)
django.project('chopin')
from django.conf import settings
REPOSITORY = 'https://scm.cch.kcl.ac.uk/hg/chopin-django'
env.user = settings.FABRIC_USER
env.hosts = ['ocve3.dighum.kcl.ac.uk']
env.root_path = '/vol/ocve3/webroot/'
env.envs_path = os.path.join(env.root_path, 'envs')
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()
@task
@server
def localhost():
""" local server """
env.srvr = 'local'
env.path = os.path.dirname(os.path.realpath(__file__))
env.within_virtualenv = 'workon chopin'
env.hosts = [gethostname()]
env.user = getuser()
@task
@server
def vagrant():
env.srvr = 'vagrant'
env.path = os.path.join('/', env.srvr)
# this is necessary because ssh will fail when known hosts keys vary
# every time vagrant is destroyed, a new key will be generated
env.disable_known_hosts =True
env.within_virtualenv = 'source {}'.format(
os.path.join('~', 'venv', 'bin', 'activate'))
result = dict(line.split()
for line in local('vagrant ssh-config',
capture=True).splitlines())
env.hosts = ['%s:%s' % (result['HostName'], result['Port'])]
env.key_filename = result['IdentityFile']
env.user = result['User']
print(env.key_filename, env.hosts, env.user)
@task
def unlock():
"""os x servers need to be unlocked"""
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path):
run('security unlock-keychain')
def set_srvr_vars():
env.path = os.path.join(env.root_path, env.srvr, 'django')
env.within_virtualenv = 'source {}'.format(
os.path.join(env.envs_path, env.srvr, 'bin', 'activate'))
@task
def create_virtualenv():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
env_vpath = os.path.join(env.envs_path, env.srvr)
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))
@task
def clone_repo():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
if run('ls {}'.format(os.path.join(env.path, '.hg'))).succeeded:
print(green(('repository at'
' [{}] exists').format(env.path)))
return
print(yellow('cloneing repository to [{}]'.format(env.path)))
run('hg clone {} {}'.format(REPOSITORY, env.path))
@task
def setup_environment():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
create_virtualenv()
clone_repo()
install_requirements()
@task
def deploy(branch=None):
update(branch)
install_requirements()
migrate()
own_django_log()
collect_static()
update_index()
touch_wsgi()
@task
def update(branch=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if branch:
# try specified branch first
to_branch = branch
elif not branch and env.srvr in ['local', 'vagrant', 'dev']:
# if loval, vagrant or dev deploy to master
to_branch = 'default'
else:
# else deploy to server tag
to_branch = env.srvr
with cd(env.path), prefix(env.within_virtualenv):
run('hg pull')
run('hg up {}'.format(to_branch))
@task
def makemigrations(app=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['dev', 'stg', 'liv']:
print(yellow('Do not run makemigrations on the servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py makemigrations {}'.format(app if app else ''))
@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 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 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 install_requirements():
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 -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 own_django_log():
""" make sure logs/django.log is owned by www-data"""
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['local', 'vagrant']:
print(yellow('Do not change ownership of django log on local servers'))
return
sudo(
'chown www-data:www-data {}'.format(
os.path.join(env.path, 'logs', 'django.log')))
@task
def touch_wsgi():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(os.path.join(env.path, 'chopin')), prefix(env.within_virtualenv):
run('touch wsgi.py')
@task
def runserver(port='8000'):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr not in ['local', 'vagrant']:
print(yellow('this server only runs for development purposes'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py runserver 0.0.0.0:{}'.format(port))