forked from dimagi/logistics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
252 lines (216 loc) · 8.21 KB
/
Copy pathfabfile.py
File metadata and controls
252 lines (216 loc) · 8.21 KB
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
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8
from __future__ import with_statement
import os
import sys
import uuid
from datetime import datetime
from fabric import utils
from fabric.api import *
from fabric.contrib import files, console
from fabric.decorators import hosts
from subprocess import Popen
"""
CONFIGURATION
"""
PATH_SEP = "/" # necessary to deploy from windows to *nix
env.code_cleanup = True
env.db_cleanup = True
env.db_name = "logistics_production"
env.db_user = "root"
env.local_db_user = "root"
env.db_type = "mysql"
env.remote = "origin"
env.branch = "master"
env.pathhack = False
env.stop_start = False
env.virtualenv_root = None
def do_nothing(): pass
env.extras = do_nothing
def enter_virtualenv():
"""
modify path to use virtualenv's python
usage:
with enter_virtualenv():
run('python script.py')
"""
if env.virtualenv_root:
return prefix('PATH=%(virtualenv_root)s/bin/:PATH' % env)
else:
# this is just a noop
return prefix("pwd")
def _join(*args):
if env.pathhack:
return PATH_SEP.join(args)
else:
return os.path.join(*args)
def test():
env.config = 'test'
env.deploy_dir = os.path.dirname(__file__)
env.code_dir = _join(list(env.deploy_dir, 'logistics'))
env.hosts = ['localhost']
def _malawi_shared():
env.pathhack = True # sketchily, this must come before any join calls
env.config = 'malawi'
env.code_dir = _join(env.deploy_dir, 'logistics')
env.code_cleanup = False
env.db_cleanup = False
env.stop_start = True
env.branch = "malawi-dev"
def malawi_extras():
run("python manage.py malawi_init")
run("python manage.py loaddata ../deploy/malawi/initial_data.json")
sudo("/etc/init.d/memcached restart")
env.extras = malawi_extras
def malawi():
"""
Malawi configuration (vmracks)
"""
env.deploy_dir = '/home/dimagi/src'
env.db_name = "cstock"
env.hosts = ['dimagi@216.240.181.53']
_malawi_shared()
def malawi_old():
"""
Malawi configuration (rackspace)
"""
env.deploy_dir = '/home/sc4ccm/src'
env.db_name = "sc4ccm"
env.hosts = ['sc4ccm@50.56.116.170']
_malawi_shared()
def _tz_shared():
env.pathhack = True # sketchily, this must come before any join calls
env.config = 'tz'
env.db_type = "postgres"
env.db_name = "ilsgateway"
env.db_user = "postgres"
env.code_dir = _join(env.deploy_dir, 'logistics')
env.code_cleanup = False
env.db_cleanup = False
env.stop_start = True
env.branch = "tz-master"
def tz_extras():
run("python manage.py tz_update")
sudo("/etc/init.d/memcached restart")
env.extras = tz_extras
def tz_staging():
"""
TZ configuration (staging)
"""
env.deploy_dir = '/home/dimagivm/src'
env.db_name = "logistics_project"
env.hosts = ['dimagivm@ilsstaging.dimagi.com']
_tz_shared()
def tz_production():
"""
TZ configuration (staging)
"""
env.deploy_dir = '/home/dimagi/src'
env.hosts = ['ilsgateway@ilsgateway.com']
env.virtualenv_root = "/home/dimagi/src/logistics-env"
_tz_shared()
env.db_name = "logistics_production"
def staging():
env.config = 'staging'
env.deploy_dir = '/home/ewsghana'
env.code_dir = _join(env.deploy_dir, 'logistics')
env.hosts = ['ewsghana@ewsghana.dyndns.org']
def production():
env.config = 'production'
env.deploy_dir = '/home/ewsghana'
env.code_dir = _join(env.deploy_dir, 'logistics')
env.hosts = ['ewsghana@ewsghana.dyndns.org']
def _local_sudo(command, user=None):
if user:
return Popen("sudo %s" % command, shell=True)
else:
return Popen("sudo -u %s %s" % (user, command), shell=True)
def _get_db(dumpname):
sudo("gzip /tmp/%s" % dumpname)
get("/tmp/%s.gz" % dumpname, local_path="/tmp/%s.gz" % dumpname)
local("gunzip /tmp/%s.gz" % dumpname)
def sync_postgres_db():
""" Untested as of yet. """
dumpname = "db_%s_%s.sql" % (env.db_name, datetime.now().isoformat())
sudo("pg_dump %(dbname)s > /tmp/%(dumpname)s" % {"dbname": env.db_name, "dumpname": dumpname}, user="postgres")
_get_db(dumpname)
_local_sudo('dropdb %(dbname)s' % {"dbname": env.db_name}, user="postgres")
_local_sudo('createdb %(dbname)s' % {"dbname": env.db_name}, user="postgres")
_local_sudo('psql --dbname %(dbname)s < /tmp/%(dumpname)s' % {"dbname":env.db_name, "dumpname":dumpname})
def sync_mysql_db():
dumpname = "db_%s_%s.sql" % (env.db_name, datetime.now().isoformat())
sudo("mysqldump -u %(user)s -p%(password)s %(dbname)s > /tmp/%(dumpname)s" % {"user": env.db_user, "password": env.db_password, "dbname": env.db_name, "dumpname": dumpname}, user="mysql")
_get_db(dumpname)
local('mysqladmin -u %(user)s -p%(password)s drop %(dbname)s -f' % {"user": env.local_db_user, "password": env.local_db_password, "dbname": env.db_name})
local('mysqladmin -u %(user)s -p%(password)s create %(dbname)s' % {"user": env.local_db_user, "password": env.local_db_password, "dbname": env.db_name})
local('mysql -u %(user)s -p%(password)s %(dbname)s < /tmp/%(dumpname)s' % {"dbname":env.db_name, "dumpname":dumpname, "user": env.local_db_user, "password": env.local_db_password})
def sync_db():
""" Copies a database to your local machine. """
require('config', provided_by=('malawi', 'malawi_old'))
if not console.confirm('Are you sure you want to wipe out the local %s database?' % env.db_name,
default=False):
utils.abort('Deployment aborted.')
if env.db_type == "mysql":
env.db_password = prompt("Password for remote %s database (user: %s): " % (env.db_name, env.db_user))
env.local_db_password = prompt("Password for local %s database (user: %s): " % (env.db_name, env.db_user))
sync_mysql_db()
elif env.db_type == "postgres":
sync_postgres_db()
"""
CAN'T TOUCH THIS
"""
def django_tests():
"""run django tests"""
with cd('logistics'):
local('./manage.py test --noinput', capture=False)
def update_requirements():
""" update external dependencies """
with cd(env.code_dir):
with enter_virtualenv():
sudo('pip install -r %s' % _join(env.code_dir, "pip-requires.txt"))
def bootstrap(subdir='logistics_project'):
""" run this after you've checked out the code """
with cd(env.code_dir):
run('git submodule init')
run('git submodule update')
update_requirements()
with cd(subdir):
with enter_virtualenv():
run('./manage.py syncdb --noinput')
run('./manage.py migrate --noinput')
# this doesn't seem to exist
#run('./bootstrap_db.py')
env.extras()
def deploy():
""" deploy code to some remote environment """
require('config', provided_by=('test', 'staging', 'production', 'malawi'))
if env.stop_start:
sudo("supervisorctl stop all")
if env.config == 'production':
if not console.confirm('Are you sure you want to deploy production?',
default=False):
utils.abort('Production deployment aborted.')
if env.code_cleanup:
if not console.confirm('Are you sure you want to wipe out the "logistics" folder?',
default=False):
utils.abort('Deployment aborted.')
run('rm -rf logistics')
run('git clone git://github.com/dimagi/logistics.git')
else:
with cd(env.code_dir):
run('git fetch')
run('git checkout %(branch)s' % {"branch": env.branch})
run('git pull %(repo)s %(branch)s' % {"repo": env.remote, "branch": env.branch})
if env.db_cleanup:
if not console.confirm('Are you sure you want to wipe out the database?',
default=False):
utils.abort('Deployment aborted.')
sudo('dropdb %(dbname)s' % {"dbname": env.db_name}, user="postgres")
sudo('createdb %(dbname)s' % {"dbname": env.db_name}, user="postgres")
bootstrap(subdir='logistics_project')
if env.stop_start:
sudo("/etc/init.d/apache2 reload")
sudo("supervisorctl start all")
def test_and_deploy():
django_tests()
deploy()