This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dodo.py
executable file
·498 lines (453 loc) · 12.6 KB
/
dodo.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import pwd
import sys
from doit import get_var
from ruamel import yaml
REPOROOT = os.path.dirname(os.path.abspath(__file__))
PROJNAME = 'autocert'
PROJDIR = REPOROOT + '/' + PROJNAME
APPDIR = PROJDIR + '/api'
CLIDIR = PROJDIR + '/cli'
TESTDIR = REPOROOT + '/tests' #FIXME: PROJDIR?
UTILSDIR = REPOROOT + '/repos/utils'
LOGDIR = REPOROOT + '/oldlogs'
AC_UID = os.getuid()
AC_GID = pwd.getpwuid(AC_UID).pw_gid
AC_USER = pwd.getpwuid(AC_UID).pw_name
AC_APP_PORT=os.environ.get('AC_APP_PORT', 8000)
AC_APP_TIMEOUT=os.environ.get('AC_APP_TIMEOUT', 120)
AC_APP_WORKERS=os.environ.get('AC_APP_WORKERS', 2)
AC_APP_MODULE=os.environ.get('AC_APP_MODULE', 'main:app')
sys.path.insert(0, APPDIR)
from config import _update_config, CONFIG_YML, DOT_CONFIG_YML
from utils.shell import call
from utils.timestamp import utcnow, datetime2int
MINIMUM_DOCKER_COMPOSE_VERSION = '1.6'
LOG_LEVELS = [
'DEBUG',
'INFO',
'WARNING',
'ERROR',
'CRITICAL',
]
DOIT_CONFIG = {
'default_tasks': ['pull', 'deploy', 'rmimages', 'rmvolumes', 'count'],
'verbosity': 2,
}
class UnknownPkgmgrError(Exception):
def __init__(self):
super(UnknownPkgmgrError, self).__init__('unknown pkgmgr!')
def get_ac_envs():
return [
f'AC_UID={AC_UID}',
f'AC_GID={AC_GID}',
f'AC_USER={AC_USER}',
f'AC_APP_PORT={AC_APP_PORT}',
f'AC_APP_TIMEOUT={AC_APP_TIMEOUT}',
f'AC_APP_WORKERS={AC_APP_WORKERS}',
f'AC_APP_MODULE={AC_APP_MODULE}',
]
def get_env_vars(regex=None):
return [key+'='+value for key, value in os.environ.items() if regex == None or regex.search(key)]
def check_hash(program):
from subprocess import check_call, CalledProcessError, PIPE
try:
check_call(f'hash {program}', shell=True, stdout=PIPE, stderr=PIPE)
return True
except CalledProcessError:
return False
def get_pkgmgr():
if check_hash('dpkg'):
return 'deb'
elif check_hash('rpm'):
return 'rpm'
elif check_hash('brew'):
return 'brew'
raise UnknownPkgmgrError
def task_count():
'''
use the cloc utility to count lines of code
'''
excludes = [
'dist',
'venv',
'__pycache__',
'auto_cert_cli.egg-info',
]
excludes = '--exclude-dir=' + ','.join(excludes)
scandir = os.path.dirname(__file__)
return {
'actions': [
f'cloc {excludes} {scandir}',
],
'uptodate': [
lambda: not check_hash('cloc'),
],
}
def task_checkpath():
'''
check for required path /data/autocert/bundles
'''
return {
'actions': [
'test -d /data/autocert/bundles',
],
}
def task_checkreqs():
'''
check for required software
'''
DEBS = [
'docker-ce',
]
RPMS = [
'docker-ce',
]
return {
'deb': {
'actions': ['dpkg -s ' + deb for deb in DEBS],
},
'rpm': {
'actions': ['rpm -q ' + rpm for rpm in RPMS],
},
'brew': {
'actions': ['true'],
}
}[get_pkgmgr()]
def task_dockercompose():
'''
assert docker-compose version ({0}) or higher
'''
from utils.function import docstr
docstr(MINIMUM_DOCKER_COMPOSE_VERSION)
def check_docker_compose():
import re
from subprocess import check_output
from packaging.version import parse as version_parse
pattern = '(docker-compose version) ([0-9.]+(-rc[0-9])?)(, build [a-z0-9]+)'
output = call('docker-compose --version')[1].strip()
regex = re.compile(pattern)
match = regex.search(output)
version = match.groups()[1]
assert version_parse(version) >= version_parse(MINIMUM_DOCKER_COMPOSE_VERSION)
return {
'actions': [
check_docker_compose,
],
}
def task_noroot():
'''
make sure script isn't run as root
'''
then = 'echo " DO NOT RUN AS ROOT!"; echo; exit 1'
bash = 'if [[ $(id -u) -eq 0 ]]; then {0}; fi'.format(then)
return {
'actions': [
'bash -c \'{0}\''.format(bash),
],
}
def task_pull():
'''
do a safe git pull
'''
submods = call("git submodule status | awk '{print $2}'")[1].split()
test = '`git diff-index --quiet HEAD --`'
pull = 'git pull --rebase'
update = 'git submodule update --remote'
dirty = 'echo "refusing to \'{cmd}\' because the tree is dirty"'
dirty_pull = dirty.format(cmd=pull)
dirty_update = dirty.format(cmd=update)
yield {
'name': 'mozilla-it/autocert',
'actions': [
f'if {test}; then {pull}; else {dirty_pull}; exit 1; fi',
],
}
for submod in submods:
yield {
'name': submod,
'actions': [
f'cd {submod} && if {test}; then {update}; else {dirty_update}; exit 1; fi',
],
}
def task_test():
'''
setup venv and run pytest
'''
PYTHONPATH = 'PYTHONPATH=.:autocert:autocert/api:$PYTHONPATH'
return {
'task_dep': [
'noroot',
'config',
],
'actions': [
'virtualenv --python=$(which python3) venv',
'venv/bin/pip3 install --upgrade pip',
f'venv/bin/pip3 install -r {REPOROOT}/requirements.txt',
f'venv/bin/pip3 install -r {TESTDIR}/requirements.txt',
f'{PYTHONPATH} venv/bin/python3 -m pytest -s -vv tests/api',
],
}
def task_version():
'''
write git describe to VERSION file
'''
return {
'actions': [
f'git describe --abbrev=7 | xargs echo -n > {APPDIR}/VERSION',
],
}
def task_savelogs():
'''
save the logs to a timestamped file
'''
timestamp = datetime2int(utcnow())
return {
'task_dep': [
'checkreqs',
'dockercompose'
],
'actions': [
f'mkdir -p {LOGDIR}',
f'cd {PROJDIR} && docker-compose logs > {LOGDIR}/{timestamp}.log',
]
}
def task_environment():
'''
set the env vars to be used inside of the container
'''
def add_env_vars():
print(f'{PROJDIR}/docker-compose.yml.wo-envs -> {PROJDIR}/docker-compose.yml')
print('adding env vars to docker-compose.yml file')
dcy = yaml.safe_load(open(f'{PROJDIR}/docker-compose.yml.wo-envs'))
for svc in dcy['services'].keys():
envs = dcy['services'][svc].get('environment', [])
envs += get_ac_envs()
envs += get_env_vars(re.compile('(no|http|https)_proxy', re.IGNORECASE))
print(f'{svc}:')
for env in envs:
print(f' - {env}')
dcy['services'][svc]['environment'] = envs
with open(f'{PROJDIR}/docker-compose.yml', 'w') as f:
yaml.dump(dcy, f, default_flow_style=False)
return {
'task_dep': [
],
'actions': [
add_env_vars,
]
}
def task_tls():
'''
create server key, csr and crt files
'''
name = 'server'
tls = '/data/autocert/tls'
env = 'PASS=TEST'
envp = 'env:PASS'
targets = [
f'{tls}/{name}.key',
f'{tls}/{name}.crt',
]
subject = '/C=US/ST=Oregon/L=Portland/O=Autocert Server/OU=Server/CN=0.0.0.0'
def uptodate():
return all([os.path.isfile(t) for t in targets])
return {
'actions': [
f'mkdir -p {tls}',
f'{env} openssl genrsa -aes256 -passout {envp} -out {tls}/{name}.key 2048',
f'{env} openssl req -new -passin {envp} -subj "{subject}" -key {tls}/{name}.key -out {tls}/{name}.csr',
f'{env} openssl x509 -req -days 365 -in {tls}/{name}.csr -signkey {tls}/{name}.key -passin {envp} -out {tls}/{name}.crt',
f'{env} openssl rsa -passin {envp} -in {tls}/{name}.key -out {tls}/{name}.key',
],
'targets': targets,
'uptodate': [uptodate],
}
def task_deploy():
'''
deloy flask app via docker-compose
'''
return {
'task_dep': [
'noroot',
'checkpath',
'checkreqs',
'version',
'test',
'config',
'dockercompose',
'environment',
'savelogs',
],
'actions': [
f'cd {PROJDIR} && docker-compose build',
f'cd {PROJDIR} && docker-compose up --remove-orphans -d',
],
}
def task_rmimages():
'''
remove dangling docker images
'''
query = '`docker images -q -f dangling=true`'
return {
'actions': [
f'docker rmi {query}',
],
'uptodate': [
f'[ -z "{query}" ] && exit 0 || exit 1',
],
}
def task_rmvolumes():
'''
remove dangling docker volumes
'''
query = '`docker volume ls -q -f dangling=true`'
return {
'actions': [
f'docker volume rm {query}',
],
'uptodate': [
f'[ -z "{query}" ] && exit 0 || exit 1',
],
}
def task_logs():
'''
simple wrapper that calls 'docker-compose logs'
'''
return {
'actions': [
f'cd {PROJDIR} && docker-compose logs',
],
}
def task_config():
'''
write config.yml -> .config.yml
'''
log_level = 'WARNING'
filename = PROJDIR + '/LOG_LEVEL'
if os.path.isfile(filename):
log_level = open(filename).read().strip()
log_level = get_var('LOG_LEVEL', log_level)
if log_level not in LOG_LEVELS:
raise UnknownLogLevelError(log_level)
punch = f'''
logging:
loggers:
api:
level: {log_level}
handlers:
console:
level: {log_level}
'''
return {
'actions': [
f'echo "cp {CONFIG_YML}\n-> {DOT_CONFIG_YML}"',
f'echo "setting LOG_LEVEL={log_level}"',
f'cp {CONFIG_YML} {DOT_CONFIG_YML}',
lambda: _update_config(DOT_CONFIG_YML, yaml.safe_load(punch)),
]
}
def task_example():
'''
cp|strip config.yml -> config.yml.example
'''
apikey = '82_CHAR_APIKEY'
punch = f'''
authorities:
digicert:
apikey: {apikey}
destinations:
zeus:
apikey: {apikey}
'''
return {
'actions': [
f'cp {CONFIG_YML}.example {CONFIG_YML}.bak',
f'cp {CONFIG_YML} {CONFIG_YML}.example',
lambda: _update_config(CONFIG_YML+'.example', yaml.safe_load(punch)),
],
}
def task_rmcache():
'''
recursively delete python cache files
'''
rmrf = 'rm -rf "{}" \;'
return dict(
actions=[
f'sudo find {REPOROOT} -depth -name __pycache__ -type d -exec {rmrf}',
f'sudo find {REPOROOT} -depth -name *.pyc -type f -exec {rmrf}',
]
)
def task_tidy():
'''
delete cached files
'''
TIDY_FILES = [
'.doit.db/',
'venv/',
'{APPDIR}/VERSION',
]
return {
'actions': [
'rm -rf ' + ' '.join(TIDY_FILES),
'find . | grep -E "(__pycache__|\.pyc$)" | xargs rm -rf',
],
}
def task_nuke():
'''
git clean and reset
'''
return {
'task_dep': ['tidy'],
'actions': [
'docker-compose kill',
'docker-compose rm -f',
'git clean -fd',
'git reset --hard HEAD',
],
}
def task_prune():
'''
prune stopped containers
'''
return {
'actions': ['docker rm `docker ps -q -f "status=exited"`'],
'uptodate': ['[ -n "`docker ps -q -f status=exited`" ] && exit 1 || exit 0']
}
def task_zeus():
'''
launch zeus containers
'''
image = 'zeus17.3'
for num in (1, 2):
container = f'{image}_test{num}'
yield {
'task_dep': ['prune'],
'name': container,
'actions': [f'docker run -d -p 909{num}:9090 --name {container} {image}'],
'uptodate': [f'[ -n "`docker ps -q -f name={container}`" ] && exit 0 || exit 1']
}
def task_stop():
'''
stop running autocert containers
'''
def check_docker_ps():
cmd = 'docker ps --format "{{.Names}}" | grep ' + PROJNAME + ' | { grep -v grep || true; }'
out = call(cmd, throw=True)[1]
return out.split('\n') if out else []
containers = ' '.join(check_docker_ps())
return {
'actions': [
f'docker rm -f {containers}',
],
'uptodate': [
lambda: len(check_docker_ps()) == 0,
],
}
if __name__ == '__main__':
print('should be run with doit installed')
import doit
doit.run(globals())