-
Notifications
You must be signed in to change notification settings - Fork 14
/
dodo.py
259 lines (211 loc) · 5.25 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
import json
import pathlib
import re
import sys
from functools import lru_cache
from doit.tools import LongRunning
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from yaml import safe_load
from refractr.cfg import CFG, CalledProcessError, call, git
IMAGE = "itsre/refractr"
DOIT_CONFIG = {
"default_tasks": ["test"],
"verbosity": 2,
}
@lru_cache()
def envs(sep=" ", **kwargs):
envs = dict(
REFRACTR_VERSION=CFG.VERSION,
)
return sep.join(
[f"{key}={value}" for key, value in sorted(dict(envs, **kwargs).items())]
)
def write_json(filename, **items):
with open(filename, "w") as f:
f.write(json.dumps(items, indent=4, sort_keys=True))
def task_deployed():
"""
write refractr/deployed json file
"""
items = dict(
DEPLOYED_BY=CFG.COMMITTED_BY,
DEPLOYED_ENV=CFG.DEPLOYED_ENV,
DEPLOYED_WHEN=CFG.DEPLOYED_WHEN,
)
if CFG.COMMITTED_BY != CFG.AUTHORED_BY:
items.update(AUTHORED_BY=CFG.AUTHORED_BY)
return {
"actions": [lambda: write_json(f"{CFG.IMAGE}/deployed", **items)],
}
def task_version():
"""
write refractr/version json file
"""
items = dict(
BRANCH=CFG.BRANCH,
REVISION=CFG.REVISION,
VERSION=CFG.VERSION,
)
return {
"actions": [lambda: write_json(f"{CFG.IMAGE}/version", **items)],
}
def task_schema():
"""
test refractr.yml against schema.yml using jsonschema
"""
def schema():
schema_json = pathlib.Path(CFG.SCHEMA_JSON)
schema = None
refractr_yaml = pathlib.Path(CFG.REFRACTR_YML)
refracts = None
print(f"validating {CFG.REFRACTR_YML} against {schema_json} =>", end=" ")
try:
with refractr_yaml.open("r") as fh:
refracts = safe_load(fh)
with schema_json.open("r") as fh:
schema = json.load(fh)
except FileNotFoundError as err:
print(f'FAILURE: {err.strerror} ("{err.filename}")')
sys.exit(1)
try:
validate(refracts, schema)
print("SUCCESS")
return True
except ValidationError as ve:
print("FAILURE")
print(ve)
return False
return {
"task_dep": [],
"actions": [
schema,
],
}
def task_nginx():
"""
generate nginx.conf files from refractr.yml
"""
cmd = f"bin/refractr nginx > {CFG.NGINX}/conf.d/refractr.conf"
return {
"task_dep": [
"schema",
],
"actions": [
cmd,
f'echo "{cmd}"',
],
}
def task_refracts():
"""
create refracts.json from loading refractr.yml
"""
cmd = f"bin/refractr --output json show > {CFG.IMAGE}/refracts"
return {
"task_dep": [
"schema",
],
"actions": [
cmd,
f'echo "{cmd}"',
],
}
def task_build():
"""
run docker compose build for refractr
"""
return {
"task_dep": [
"deployed",
"version",
"nginx",
"refracts",
],
"actions": [
f"env {envs()} docker compose build refractr",
],
}
def task_check():
"""
run nginx -t test on refractr nginx config
"""
return {
"task_dep": [
"build",
],
"actions": [
f"env {envs()} docker compose run refractr check",
],
}
def task_drun():
"""
run refractr container via docker compose up -d
"""
return {
"task_dep": [
"check",
],
"actions": [
# https://github.com/docker/compose/issues/1113#issuecomment-185466449
f"env {envs()} docker compose rm --force refractr",
LongRunning(
f"nohup env {envs()} docker compose up -d --remove-orphans refractr >/dev/null &"
),
],
}
def task_test():
"""
run pytest tests against the locally running container
"""
return {
"task_dep": [
"build",
"drun",
],
"actions": [
"sleep 1",
"PYTHONPATH=. python3 -m pytest -vv -q -s",
],
}
def task_show():
"""
show CI variables
"""
def show():
print(
f"CI={CFG.CI} "
f"TAG={CFG.TAG} "
f"VERSION={CFG.VERSION} "
f"BRANCH={CFG.BRANCH} "
f"DEPLOYED_ENV={CFG.DEPLOYED_ENV}"
)
return {
"actions": [
show,
]
}
def task_refractslisting():
"""
create refracts list from loading refractr.json
"""
cmd = f"bin/refractr --output json show > {CFG.IMAGE}/refracts.json"
cmd1 = "bin/refracts-listing"
return {
"task_dep": [
"schema",
],
"actions": [
cmd,
cmd1,
f'echo "{cmd1}"',
],
}
def task_certificate_manager_input():
"""
create input file for certificate manager tf module
"""
cmd = f"bin/refractr --output yaml cmi > {CFG.IMAGE}/certificate_manager_input.yaml"
return {
"task_dep": ["schema"],
"actions": [cmd, f'echo "{cmd}"'],
}