-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
151 lines (133 loc) · 4.27 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
import os
from fabric.api import env, cd, run
from datetime import datetime
# default settings for all deployments
env.update(
{
'deployments_path': '/home/wsgi/srv',
'project_name': 'nmis',
'virtualenv_directory': 'project_env',
'migrate': True,
}
)
DEPLOYMENTS = {
'dev': {
'folder_name': 'nmis_dev',
'host_string': '[email protected]',
'branch': 'develop',
'backup': False,
},
'staging': {
'folder_name': 'nmis_dev',
'host_string': '[email protected]',
'branch': 'develop',
'backup': False,
},
'production': {
'folder_name': 'nmis_production',
'host_string': '[email protected]',
'branch': 'develop',
'backup': True,
'database_name': 'nmis_production',
}
}
def _setup_env(deployment_name):
env.update(DEPLOYMENTS[deployment_name])
env.project_path = os.path.join(
env.deployments_path,
env.folder_name
)
env.code_path = os.path.join(
env.project_path,
'nmis'
)
env.apache_dir = os.path.join(
env.project_path,
'apache'
)
def backup(deployment_name):
_setup_env(deployment_name)
cur_timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
env.backup_directory_path = os.path.join(
env.project_path,
'backups',
cur_timestamp
)
run("mkdir -p %(backup_directory_path)s" % env)
with cd(env.backup_directory_path):
run("mysqldump -u root -p %(database_name)s > %(database_name)s.sql" % env)
run("gzip %(database_name)s.sql" % env)
def deploy(deployment_name, reload_lgas="none"):
"""
Example command line usage:
fab deploy:staging,reload_lgas=none
"""
_setup_env(deployment_name)
# import json
# print json.dumps(env)
# import sys
# sys.exit(0)
if env.backup:
backup(deployment_name)
def pull_code():
"""
Pull updated code from nmis repo.
"""
with cd(env.code_path):
run("git pull origin %(branch)s" % env)
pull_code()
def remove_pyc():
with cd(env.code_path):
run('find . -name "*.pyc" -exec rm -rf {} \;')
remove_pyc()
def pull_data():
with cd(os.path.join(env.code_path, 'data')):
run("git pull origin master")
pull_data()
def _run_in_virtualenv(command):
activate_path = os.path.join(
env.project_path,
env.virtualenv_directory,
'bin', 'activate'
)
activate_virtualenv = "source %s" % activate_path
with cd(env.code_path):
run(activate_virtualenv + ' && ' + command)
def install_pip_requirements():
with cd(env.code_path):
_run_in_virtualenv("pip install -r requirements.pip")
install_pip_requirements()
# NOTE: I recently installed the registration app, to get this
# working you need to use a syncdb, migrate isn't enough.
def migrate_database():
if env.migrate:
with cd(env.code_path):
_run_in_virtualenv("python manage.py migrate")
migrate_database()
def collect_static():
with cd(env.code_path):
_run_in_virtualenv("python manage.py collectstatic --noinput")
collect_static()
def restart_web_server():
"""
touch wsgi file to trigger reload
"""
with cd(env.apache_dir):
run("touch environment.wsgi")
restart_web_server()
# def reload_fixtures(flag=""):
# with cd(env.code_path):
# _run_in_virtualenv("python manage.py load_fixtures %s" % flag)
# if reload == "all":
# print "Loading all data in from csvs."
# reload_fixtures("-S")
# elif reload == "limit":
# reload_fixtures("-lS")
# elif reload == "table_defs":
# reload_fixtures("load_table_defs")
if reload_lgas != "none":
_run_in_virtualenv("python manage.py mark_available_lgas")
_run_in_virtualenv("python manage.py reload_key_renames")
_run_in_virtualenv("python manage.py reload_variables")
_run_in_virtualenv("python manage.py reload_table_defs")
_run_in_virtualenv("python manage.py load_lgas %s" % reload_lgas)