This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
330 lines (249 loc) · 8.51 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
from fabric.api import *
from fabric.colors import red
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
import os
import sys
import time
# Namespaced fabric tasks
import envs
import api
import renderer
import markup_renderer
####################
# Global variables #
####################
env.project_name = 'editorsnotes'
env.release = time.strftime('%Y%m%d%H%M%S')
env.TMP_DIR = os.path.join(os.path.dirname(env.real_fabfile), 'tmp')
env.git = {}
env.git['api'] = os.getenv('EDITORSNOTES_API_GIT')
env.git['renderer'] = os.getenv('EDITORSNOTES_RENDERER_GIT')
if not env.git['api']:
abort(red(
'Set EDITORSNOTES_API_GIT environment variable with a path to an '
'Editors\'s Notes API git repository.'
))
if not env.git['renderer']:
abort(red(
'Set EDITORSNOTES_API_RENDERER environment variable with a path to an '
'Editors\'s Notes renderer git repository.'
))
#########
# Tasks #
#########
@task
def create_confs():
create_uwsgi_conf()
create_nginx_conf()
create_api_service()
create_renderer_service()
create_markup_renderer_service()
create_systemd_target()
def create_template(template_vars, template_script):
require(*template_vars, provided_by=envs.ENVS)
template_string = ' '.join(['{' + var + '}' for var in template_vars])
template_command = (template_script + ' ' + template_string).format(**env)
return local(template_command, capture=True)
def write_config(conf_type, filename, content):
if os.path.exists(filename):
msg = '{} configuration already exists at {}\nOverwrite?'.format(
conf_type, filename)
print ''
if not confirm(msg):
return
with open(filename, 'w') as outfile:
outfile.write(content)
@task
def show_logs():
sudo('journalctl -xe --unit={}.*'.format(env.host))
@task
def create_uwsgi_conf():
template_vars = [
'host',
'project_path',
'uwsgi_gid',
'uwsgi_uid',
'uwsgi_socket_gid',
'uwsgi_socket_uid',
'uwsgi_socket_chmod'
]
output_filename = 'uwsgi/uwsgi-{host}.ini'.format(**env)
uwsgi_conf = create_template(
template_vars, './uwsgi/uwsgi-TEMPLATE.ini.py')
write_config('uWSGI', output_filename, uwsgi_conf)
@task
def create_nginx_conf():
template_vars = [
'host',
'project_path',
'uwsgi_socket_location',
'renderer_port',
'ssl_conf_file'
]
output_filename = 'nginx/nginx-{host}.conf'.format(**env)
nginx_conf = create_template(
template_vars, './nginx/nginx-TEMPLATE.conf.py')
write_config('Nginx', output_filename, nginx_conf)
@task
def create_systemd_target():
template_vars = [
'host'
]
output_filename = 'systemd/{host}.target'.format(**env)
systemd_target = create_template(
template_vars, './systemd/TEMPLATE.target.py')
write_config('Systemd target', output_filename, systemd_target)
@task
def create_api_service():
template_vars = [
'host',
'uwsgi_bin',
'uwsgi_service_gid',
'uwsgi_service_uid',
'uwsgi_socket_uid',
'uwsgi_socket_uid',
'uwsgi_conf_file',
]
output_filename = 'systemd/{host}.api.service'.format(**env)
api_service = create_template(
template_vars, './systemd/TEMPLATE.api.service.py')
write_config('API service', output_filename, api_service)
@task
def create_renderer_service():
template_vars = [
'host',
'node_bin',
'project_path',
'renderer_port',
]
output_filename = 'systemd/{host}.renderer.service'.format(**env)
renderer_service = create_template(
template_vars, './systemd/TEMPLATE.renderer.service.py')
write_config('Renderer service', output_filename, renderer_service)
@task
def create_markup_renderer_service():
template_vars = [
'host',
'node_bin',
'project_path',
'markup_renderer_port',
]
output_filename = 'systemd/{host}.markup-renderer.service'.format(**env)
renderer_service = create_template(
template_vars, './systemd/TEMPLATE.markup-renderer.service.py')
write_config('Markup renderer service', output_filename, renderer_service)
@task
def setup():
"""
Setup all project directories.
"""
require('hosts', 'project_path', provided_by=envs.ENVS)
if not exists(env.project_path):
abort(red('Project path ({project_path}) does not exist. '
'Create it on the server before continuing.'.format(**env)))
with cd(env.project_path):
run('mkdir -p api renderer conf markup_renderer')
run('mkdir -p api/static api/uploads')
make_release_folders('api')
make_release_folders('renderer')
@task
def upload_uwsgi_conf():
require('uwsgi_conf_file', 'host', provided_by=envs.ENVS)
uwsgi_file = 'uwsgi/uwsgi-{host}.ini'.format(**env)
check_file(uwsgi_file)
put(uwsgi_file, '{project_path}/conf/uwsgi.ini.tmp'.format(**env))
with cd(env.project_path):
sudo('chmod 644 conf/uwsgi.ini.tmp')
sudo('chown uwsgi:uwsgi conf/uwsgi.ini.tmp')
sudo('mv -f conf/uwsgi.ini.tmp {uwsgi_conf_file}'
.format(**env), pty=True)
@task
def upload_nginx_conf():
require('nginx_conf_file', 'host', provided_by=envs.ENVS)
nginx_file = 'nginx/nginx-{host}.conf'.format(**env)
check_file(nginx_file)
put(nginx_file, '{project_path}/conf/nginx.conf.tmp'.format(**env))
with cd(env.project_path):
sudo('chown root:root conf/nginx.conf.tmp')
sudo('chmod 644 conf/nginx.conf.tmp')
sudo('mv -f conf/nginx.conf.tmp {nginx_conf_file}'.format(**env),
pty=True)
@task
def install_systemd_services():
require('host', 'project_path', provided_by=envs.ENVS)
units = [
'api.service',
'renderer.service',
'markup-renderer.service',
'target'
]
for unit in units:
unit_file = '{}.{}'.format(env.host, unit)
local_conf = 'systemd/{}'.format(unit_file)
check_file(local_conf)
put(local_conf, '{}/conf/{}.tmp'.format(env.project_path, unit_file))
with cd(os.path.join(env.project_path, 'conf')):
sudo('chown root:root {}.tmp'.format(unit_file))
sudo('chmod 644 {}.tmp'.format(unit_file))
sudo('mv -f {0}.tmp /etc/systemd/system/{0}'.format(unit_file))
make_uwsgi_run_dir()
sudo('systemctl daemon-reload')
@task
def remove_systemd_services():
units = [
'api.service',
'renderer.service',
'markup-renderer.service',
'target'
]
for unit in units:
sudo('rm -f /etc/systemd/system/{}.{}'.format(env.host, unit))
sudo('systemctl daemon-reload')
@task
def restart_all_services():
require('host', provided_by=envs.ENVS)
make_uwsgi_run_dir()
sudo('systemctl restart {}.target nginx.service'.format(env.host))
def check_file(filename):
if not os.path.exists(filename):
abort(red('Missing config file for {} at {}'.format(
env.host, filename)))
def make_release_folders(dirname):
"""
Setup folders for each subproject.
* logs: Logs for long-running process
* releases: Code for release currently being run
* packages: tarballs for the folders in releases
"""
require('hosts', 'project_path', provided_by=envs.ENVS)
with cd(env.project_path):
with cd(dirname):
run('mkdir -p logs releases packages')
with cd('releases'):
run('touch none')
run('test ! -e current && ln -s none current', quiet=True)
run('test ! -e previous && ln -s none previous', quiet=True)
@task
def full_deploy(api_version='HEAD', renderer_version='HEAD',
markup_renderer_version=None):
"""
Deploy the site, migrate the database, and open in a web browser.
"""
setup()
api.full_deploy(api_version)
renderer.full_deploy(renderer_version)
markup_renderer.full_deploy(markup_renderer_version)
upload_nginx_conf()
upload_uwsgi_conf()
install_systemd_services()
@task
def full_deploy_with_restart(api_version='HEAD', renderer_version='HEAD',
markup_renderer_version=None):
full_deploy(api_version, renderer_version)
restart_all_services()
time.sleep(2)
local('rmdir --ignore-fail-on-non-empty {TMP_DIR}'.format(**env))
local('{} http://{}/'.format(
'xdg-open' if 'linux' in sys.platform else 'open', env.host))