forked from dimagi/commcare-hq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
112 lines (92 loc) · 3.94 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
from fabric.api import *
from fabric.contrib import console
from fabric import utils
env.code_repo = 'git://github.com/dimagi/commcare-hq.git'
def _join(*args):
"""
We're deploying on Linux, so hard-code that path separator here.
"""
return '/'.join(args)
def production():
""" use production environment on remote host"""
env.root = root = '/opt/www.commcarehq.org_project'
env.virtualenv_root = _join(root, 'env/cchq_www')
env.code_root = _join(root, 'src/commcare-hq')
env.pre_code_root = _join(root, 'src/_commcare-hq')
env.code_branch = 'master'
env.sudo_user = 'cchqwww'
env.hosts = ['10.84.168.241']
env.environment = 'production'
env.user = prompt("Username: ", default=env.user)
env.restart_server = True
def migration():
"""pull from staging branch into production to do a data migration"""
production()
env.code_branch = 'staging'
env.restart_server = False
def staging():
""" use staging environment on remote host"""
env.root = root = '/home/dimagivm/'
env.virtualenv_root = _join(root, 'cchq')
env.code_root = _join(root, 'commcare-hq')
env.code_branch = 'staging'
env.sudo_user = 'root'
env.hosts = ['192.168.7.223']
env.environment = 'staging'
env.user = prompt("Username: ", default='dimagivm')
def enter_virtualenv():
"""
modify path to use virtualenv's python
usage:
with enter_virtualenv():
run('python script.py')
"""
return prefix('PATH=%(virtualenv_root)s/bin/:$PATH' % env)
def preindex_views():
with cd(env.pre_code_root):
_update_code()
with enter_virtualenv():
sudo('nohup python manage.py sync_prepare_couchdb > preindex_views.out 2> preindex_views.err', user=env.sudo_user)
def _update_code():
sudo('git pull', user=env.sudo_user)
sudo('git checkout %(code_branch)s' % env, user=env.sudo_user)
sudo('git pull', user=env.sudo_user)
sudo('git submodule sync', user=env.sudo_user)
sudo('git submodule update --init --recursive', user=env.sudo_user)
def deploy():
""" deploy code to remote host by checking out the latest via git """
require('root', provided_by=('staging', 'production'))
if env.environment == 'production':
if not console.confirm('Are you sure you want to deploy production?', default=False):
utils.abort('Production deployment aborted.')
with cd(env.code_root):
_update_code()
with enter_virtualenv():
sudo('pip install -r requirements.txt', user=env.sudo_user)
sudo('python manage.py make_bootstrap direct-lessc', user=env.sudo_user)
sudo('python manage.py sync_finish_couchdb', user=env.sudo_user)
sudo('python manage.py syncdb --noinput', user=env.sudo_user)
sudo('python manage.py migrate --noinput', user=env.sudo_user)
sudo('python manage.py collectstatic --noinput', user=env.sudo_user)
sudo('rm -f tmp.sh resource_versions.py; python manage.py printstatic > tmp.sh; bash tmp.sh > resource_versions.py', user=env.sudo_user)
# remove all .pyc files in the project
sudo("find . -name '*.pyc' -delete")
if env.environment == "production" and env.restart_server:
service_restart()
def service_restart():
"""
restart cchq_www service on remote host. This will call a stop, reload the initctl to
have any config file updates be reloaded into intictl, then start cchqwww again.
"""
require('root', provided_by=('staging', 'production'))
with settings(sudo_user="root"):
sudo('stop cchq_www', user=env.sudo_user)
sudo('initctl reload-configuration', user=env.sudo_user)
sudo('start cchq_www', user=env.sudo_user)
def service_stop():
"""
stop cchq_www service on remote host.
"""
require('root', provided_by=('staging', 'production'))
with settings(sudo_user="root"):
sudo('stop cchq_www', user=env.sudo_user)