forked from onaio/onadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
138 lines (110 loc) · 4.74 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
import os
import sys
from fabric.api import cd, env, prefix, run
from fabric.contrib import files
DEPLOYMENTS = {
'stage': {
'home': '/home/ubuntu/src/',
'host_string': '[email protected]',
'project': 'ona',
'key_filename': os.path.expanduser('~/.ssh/ona.pem'),
'celeryd': '/etc/init.d/celeryd-ona',
'django_config_module': 'onadata.settings.local_settings',
'pid': '/var/run/ona.pid',
'template': '[email protected]:onaio/onadata-template.git',
},
'prod': {
'home': '/home/ubuntu/src/',
'host_string': '[email protected]',
'project': 'ona',
'key_filename': os.path.expanduser('~/.ssh/ona.pem'),
'celeryd': '/etc/init.d/celeryd-ona',
'django_config_module': 'onadata.settings.local_settings',
'pid': '/var/run/ona.pid',
'template': '[email protected]:onaio/onadata-template.git',
},
'formhub': {
'home': '/home/ubuntu/src/',
'host_string': '[email protected]',
'project': 'formhub',
'key_filename': os.path.expanduser('~/.ssh/modilabs.pem'),
'celeryd': '/etc/init.d/celeryd',
'django_config_module': 'onadata.settings.local_settings',
'pid': '/run/formhub.pid',
'template': '[email protected]:SEL-Columbia/formhub-template.git',
},
'kobocat': {
'home': '/home/ubuntu/src/',
'host_string': '[email protected]',
'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': '[email protected]:kobotoolbox/kobocat-template.git',
},
}
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/common.pip')
env.template_dir = 'onadata/libs/custom_template'
def deploy_template(env):
if env.get('template'):
run("git remote add template %s || true" % env.template)
run("git fetch template")
run("git reset HEAD %s && rm -rf %s" % (env.template_dir,
env.template_dir))
run("git read-tree --prefix=%s -u template/master"
% env.template_dir)
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):
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 --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 /etc/init.d/celerybeat-ona restart")
run("sudo /usr/local/bin/uwsgi --reload %s" % env.pid)