-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
229 lines (170 loc) · 6.08 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os.path
import sys
from functools import wraps
from django.conf import settings
from fabric.api import cd, env, 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('kdl')
REPOSITORY = 'https://github.com/kingsdigitallab/kdl-django.git'
env.user = settings.FABRIC_USER
env.hosts = ['kdl.kcl.ac.uk']
env.root_path = '/vol/kdl/webroot/'
env.gateway = 'ssh.cch.kcl.ac.uk'
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()
def set_srvr_vars():
env.path = os.path.join(env.root_path, env.srvr, 'django',
'kdl-django')
env.within_virtualenv = 'source {}'.format(
os.path.join(env.envs_path, env.srvr, 'bin', 'activate'))
@task
def install_system_packages():
sudo('apt-get -y --force-yes install apt-transport-https')
sudo(('wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | '
'apt-key add -'))
sudo(('echo '
'"deb https://packages.elastic.co/elasticsearch/2.x/debian '
'stable main" | '
'tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list'))
sudo('apt-get update')
sudo(('apt-get -y --force-yes install '
'python-dev python-pip python-setuptools python-virtualenv '
'openjdk-7-jre elasticsearch '
'libjpeg-dev libxml2-dev libxslt-dev '
'libldap2-dev libsasl2-dev '
'libpq-dev postgresql-client '
'git git-core'))
sudo('sudo update-rc.d elasticsearch defaults 95 10')
@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 = 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, '.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():
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 --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()
own_django_log()
migrate()
collect_static()
update_index()
clear_cache()
restar_uwsgi()
@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 own_django_log():
""" make sure logs/django.log is owned by www-data"""
require('srvr', 'path', 'within_virtualenv', 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))
@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 restar_uwsgi():
sudo('service uwsgi restart django-{}'.format(env.srvr))