-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
316 lines (228 loc) · 8.24 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os.path
import sys
from functools import wraps
from getpass import getuser
from socket import gethostname
from django.conf import settings # noqa
from fabric.api import cd, env, local, prefix, quiet, require, run, 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)
django.project("dprr")
REPOSITORY = "[email protected]:kingsdigitallab/dprr-django.git"
env.user = settings.FABRIC_USER
env.gateway = "ssh.cch.kcl.ac.uk"
env.hosts = ["dprr.dighum.kcl.ac.uk"]
env.root_path = "/vol/dprr/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 dprr"
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", "dprr-django")
env.within_virtualenv = "source {}".format(
os.path.join(env.envs_path, "dprr-" + 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, "dprr-" + 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, ".git"))).succeeded:
print((green(("repository at" " [{}] exists").format(env.path))))
return
print((yellow("cloning repository to [{}]".format(env.path))))
run("git clone --recursive {} {}".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, index="yes"):
update(branch)
install_requirements()
# migrate also creates the cache table
migrate()
own_django_log()
collect_static()
# clear_cache()
if index.lower() == "yes":
update_index()
touch_wsgi()
@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 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 createcachetable")
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 build_solr_schema > schema.xml")
run("mv schema.xml ../../solr/collection1/conf/")
sudo("service tomcat7-{} restart".format(env.srvr))
run("./manage.py update_index")
@task
def rebuild_index():
require("srvr", "path", "within_virtualenv", provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run("./manage.py build_solr_schema > schema.xml")
run("mv schema.xml ../../solr/collection1/conf/")
sudo("service tomcat7-{} restart".format(env.srvr))
run("./manage.py rebuild_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, "dprr")), prefix(env.within_virtualenv):
run("touch wsgi.py")
@task
def command(name=None):
require("srvr", "path", "within_virtualenv", provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run("./manage.py {}".format(name if name else ""))
@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))