forked from kobotoolbox/kobocat
/
fabfile.py
168 lines (131 loc) · 5.72 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
import glob
import os
from subprocess import check_call
import sys
from fabric.api import cd, env, prefix, run, sudo
from fabric.contrib import files
from fabric.operations import put
DEPLOYMENTS = {
'kobocat': {
'home': '/home/ubuntu/src/',
'host_string': 'ubuntu@kobocat.dev.kobotoolbox.org',
'project': 'kobocat',
'key_filename': os.path.expanduser('~/.ssh/kobo01.pem'),
'celeryd': '/etc/init.d/celeryd',
'django_config_module': 'onadata.settings.local_settings',
'pid': '/run/kobocat.pid',
'template': 'https://github.com/kobotoolbox/kobocat-template.git',
},
'staging': {
'home': '/home/ubuntu/src/',
'host_string': 'ubuntu@kc.staging.kobotoolbox.org',
'project': 'kobocat',
'key_filename': os.path.expanduser('~/.ssh/kobo01.pem'),
'celeryd': '/etc/init.d/celeryd',
'django_config_module': 'onadata.settings.local_settings',
'pid': '/run/kobocat.pid',
'template': 'https://github.com/kobotoolbox/kobocat-template.git',
'template_branch': 'master',
},
}
CONFIG_PATH_DEPRECATED = 'formhub/local_settings.py'
def local_settings_check(config_module):
config_path = config_module.replace('.', '/') + '.py'
if not files.exists(config_path):
if files.exists(CONFIG_PATH_DEPRECATED):
run('mv %s %s' % (CONFIG_PATH_DEPRECATED, config_path))
files.sed(config_path, 'formhub\.settings',
'onadata\.settings\.common')
else:
raise RuntimeError('Django config module not found in %s or %s' % (
config_path, CONFIG_PATH_DEPRECATED))
def source(path):
return prefix('source %s' % path)
def exit_with_error(message):
print message
sys.exit(1)
def check_key_filename(deployment_name):
if 'key_filename' in DEPLOYMENTS[deployment_name] and \
not os.path.exists(DEPLOYMENTS[deployment_name]['key_filename']):
exit_with_error("Cannot find required permissions file: %s" %
DEPLOYMENTS[deployment_name]['key_filename'])
def setup_env(deployment_name):
deployment = DEPLOYMENTS.get(deployment_name)
if deployment is None:
exit_with_error('Deployment "%s" not found.' % deployment_name)
env.update(deployment)
check_key_filename(deployment_name)
env.virtualenv = os.path.join('/home', 'ubuntu', '.virtualenvs',
env.project, 'bin', 'activate')
env.code_src = os.path.join(env.home, env.project)
env.pip_requirements_file = os.path.join(env.code_src,
'requirements/base.pip')
env.template_dir = 'onadata/libs/custom_template'
env.template_repo = '../kobocat-template'
def deploy_template(env):
if env.get('template'):
run("mkdir -p %s" % env.template_repo)
run("ls -al %s" % env.template_repo)
run("rm -rf %s" % env.template_repo)
if env.get('template_branch'):
run("git clone -b %s %s %s" % (env.get('template_branch'), env.get('template'), env.template_repo))
else:
run("git clone %s %s" % (env.get('template'), env.template_repo))
def reload(deployment_name, branch='master'):
setup_env(deployment_name)
run("sudo %s restart" % env.celeryd)
#run("sudo /etc/init.d/celerybeat-ona restart")
run("sudo /usr/local/bin/uwsgi --reload %s" % env.pid)
def deploy(deployment_name, branch='master'):
setup_env(deployment_name)
with cd(env.code_src):
run("git fetch origin")
run("git checkout origin/%s" % branch)
deploy_template(env)
run('find . -name "*.pyc" -exec rm -rf {} \;')
run('find . -type d -empty -delete')
# numpy pip install from requirements file fails
with source(env.virtualenv):
# remove django-registration
run("pip uninstall -qy django-registration || echo $?")
run("pip install numpy")
run("pip install -r %s" % env.pip_requirements_file)
with cd(env.code_src):
config_module = env.django_config_module
local_settings_check(config_module)
with source(env.virtualenv):
run("python manage.py syncdb --all --settings=%s" % config_module)
run("python manage.py migrate --settings=%s" % config_module)
run("python manage.py collectstatic --settings=%s --noinput"
% config_module)
run("sudo %s restart" % env.celeryd)
run("sudo /usr/local/bin/uwsgi --reload %s" % env.pid)
def update_xforms(deployment_name, username, path):
setup_env(deployment_name)
# compress and upload
path = path.rstrip("/")
dir_name = os.path.basename(path)
path_compressed = '%s.tgz' % dir_name
check_call(['tar', 'czvf', path_compressed, '-C', os.path.dirname(path),
dir_name])
with cd('/tmp'):
put(path_compressed, '%s.tgz' % dir_name)
# decompress on server
run('tar xzvf %s.tgz' % dir_name)
try:
with cd(env.code_src):
with source(env.virtualenv):
# run replace command
for f in glob.glob(os.path.join(path, '*')):
file_path = '/tmp/%s/%s' % (dir_name, os.path.basename(f))
run('python manage.py publish_xls -r %s %s --settings=%s' %
(file_path, username, env.django_config_module))
finally:
run('rm -r /tmp/%s /tmp/%s.tgz' % (dir_name, dir_name))
check_call(['rm', path_compressed])
def deploy_logrotate_celery(deployment_name, branch='master'):
setup_env(deployment_name)
with cd(env.code_src):
run("git fetch origin")
run("git checkout origin/%s" % branch)
sudo("cp extras/celeryd/etc/logrotate.d/celeryd /etc/logrotate.d/")