-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
237 lines (178 loc) · 7.45 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
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
from datetime import datetime as _datetime
from fabric.api import cd, env, local, run
from fabric.colors import blue
from fabvenv import virtualenv as _venv
def development():
"""fab development [command]"""
env.name = 'development'
env.environment = env.name
# env.hosts = ['localhost']
env.path = './'
# env.virtualenv_path = 'workon colegend'
env.backup_path = './backups/development'
env.branch = 'development'
env.push_branch = env.branch
env.push_remote = 'origin'
# env.reload_cmd = ''
env.db_name = 'colegend'
env.db_username = ''
env.after_deploy_url = 'http://127.0.0.1:8004/'
env.settings = '--settings=config.settings.local'
env.requirements = 'requirements/local.txt'
def staging():
"""fab staging [command]"""
env.name = 'staging'
env.environment = env.name
env.hosts = ['colegend.org']
env.path = 'staging.colegend.org/project'
env.virtualenv_path = 'staging.colegend.org/env'
env.backup_path = './backups/staging'
env.branch = 'staging'
env.push_branch = env.branch
env.push_remote = 'origin'
env.start_cmd = 'svc -u colegendstaging'
env.stop_cmd = 'svc -d colegendstaging'
env.restart_cmd = 'svc -du colegendstaging'
env.db_name = 'colegendstaging'
env.db_username = 'colegend'
env.after_deploy_url = 'https://staging.colegend.org'
env.settings = '--settings=config.settings.production'
env.requirements = 'requirements/production.txt'
def production():
"""fab production [command]"""
env.name = 'production'
env.environment = env.name
env.hosts = ['colegend.org']
env.use_ssh_config = True
env.path = '~/colegend/colegend'
env.virtualenv_path = '~/colegend/env'
env.backup_path = '~/colegend/backups'
env.branch = 'master'
env.push_branch = env.branch
env.push_remote = 'origin'
env.start_cmd = 'svc -u ~/service/gunicorn'
env.stop_cmd = 'svc -d ~/service/gunicorn'
env.restart_cmd = 'svc -du ~/service/gunicorn'
env.db_name = 'colegend'
env.db_username = 'colegend'
env.after_deploy_url = 'https://www.colegend.org'
env.settings = '--settings=config.settings.production'
env.requirements = 'requirements/production.txt'
def backup():
"""Create a database backup: fab [environment] backup"""
if env.name == 'development':
local('pg_dump -Fc {db_name} '
'> {backup_path}/{db_name}-{environment}_`date +%Y-%m-%d_%H%M%S`.dump'.format(**env))
elif env.name in ['staging', 'production']:
with cd(env.backup_path):
env.timestamp = _datetime.now().strftime('%Y-%m-%d_%H%M%S')
env.backup_file = '{backup_path}/{db_name}-{environment}_{timestamp}.dump'.format(**env)
print(blue('=> dumping database'))
run("pg_dump -Fc -U {db_username} {db_name} > {backup_file}".format(**env))
print(blue('=> downloading database'))
local('scp {hosts[0]}:{backup_file} backups/{environment}/'.format(**env))
def restore(file_path):
"""Restore database backup: fab [environment] restore"""
if env.name == 'development':
local('pg_restore -c -d {db_name} {file_path}`.dump'
.format(file_path=file_path, **env))
elif env.name in ['staging', 'production']:
print(blue('=> remote restore script not yet implemented'))
def migrate():
"""Migrate the database: fab [environment] migrate"""
with _venv(env.virtualenv_path):
run("{path}/manage.py migrate {settings}".format(**env))
def enable_debug():
"""Enable django debug mode: fab [environment] enable_debug"""
with _venv(env.virtualenv_path):
run("sed -i -e 's/DJANGO_DEBUG=False/DJANGO_DEBUG=True/' {path}/.env".format(**env))
restart()
def disable_debug():
"""Disable django debug mode: fab [environment] disable_debug"""
with _venv(env.virtualenv_path):
run("sed -i -e 's/DJANGO_DEBUG=True/DJANGO_DEBUG=False/' {path}/.env".format(**env))
restart()
def deploy():
"""Deploy the project: fab [environment] deploy"""
push()
with cd(env.path):
run('git pull {push_remote} {push_branch}'.format(**env))
with _venv(env.virtualenv_path):
run('pip install -Ur {requirements}'.format(**env))
migrate()
with _venv(env.virtualenv_path):
run('./manage.py collectstatic --noinput {settings}'.format(**env))
# run('./manage.py compilemessages %(settings)s'.format(**env))
restart()
# ping()
def ping():
"""Ping the url for a status code: fab [environment] ping"""
run('echo {after_deploy_url} returned: '
'\>\>\> $(curl --write-out %{{http_code}} --silent --output /dev/null {after_deploy_url})'.format(**env))
def stop():
"""Stop the webserver: fab [environment] stop"""
run(env.stop_cmd)
def start():
"""Start the webserver: fab [environment] start"""
run(env.start_cmd)
def restart():
"""Restart the webserver: fab [environment] restart"""
run(env.restart_cmd)
def push():
"""Push local code to the repository: fab [environment] push"""
local('git push origin {push_branch}'.format(**env))
def update():
"""Update the server repository: fab [environment] update"""
with cd(env.path):
run('git pull {push_remote} {push_branch}'.format(**env))
restart()
def ps():
"""Show the running server processes: fab [environment] ps"""
run('htop')
def logs():
"""Show server logs: fab [environment] ps"""
run('zcat -f ~/service/gunicorn/log/main/* | tai64nlocal | less | tail')
def open():
"""Open the project url: fab [environment] open"""
local('open {after_deploy_url}'.format(**env))
def shell():
"""Open a shell: fab [environment] shell"""
with _venv(env.virtualenv_path):
run('{path}/manage.py shell_plus {settings}'.format(**env))
def run_command(command):
"""
Run a management command: fab [environment] run_command my_command
Example: fab production run_command:'./manage.py check'
"""
with cd(env.path):
with _venv(env.virtualenv_path):
run('{command} {settings}'.format(command=command, settings=env.settings))
def create_superuser():
"""Create a django superuser: fab [environment] create_superuser"""
with _venv(env.virtualenv_path):
run('{path}/manage.py createsuperuser {settings}'.format(**env))
def show_crontab():
"""Show the current crontab entries: fab [environment] show_crontab"""
with _venv(env.virtualenv_path):
run('{path}/manage.py crontab show {settings}'.format(**env))
def add_crontab():
"""Update the crontab entries: fab [environment] add_crontab"""
with _venv(env.virtualenv_path):
run('{path}/manage.py crontab add {settings}'.format(**env))
def remove_crontab():
"""Remove the crontab entries: fab [environment] remove_crontab"""
with _venv(env.virtualenv_path):
run('{path}/manage.py crontab remove {settings}'.format(**env))
def graph():
"""Create a model visualization graph: fab [environment] graph"""
if env.name == 'development':
local("./manage.py graph_models -a -g -o model_graph.png")
def restart_cobot():
"""Restart the chat bot: fab [environment] restart_cobot"""
run('svc -du ~/service/cobot')
def restart_coworker():
"""Restart the celery worker: fab [environment] restart_coworker"""
run('svc -du ~/service/coworker')
def restart_cobeat():
"""Restart the celery beat scheduler: fab [environment] restart_cobeat"""
run('svc -du ~/service/cobeat')