-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
475 lines (393 loc) · 17.6 KB
/
main.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
import os
import sys
import subprocess
import json
import logging
import yaml
from typing import Any
from pathlib import Path
from tempfile import mkstemp
from fastapi import FastAPI, UploadFile, status
from fastapi.responses import JSONResponse, FileResponse
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
from jinja2 import Environment, PackageLoader, select_autoescape, meta
from jinja2.exceptions import TemplateNotFound
from pydantic import BaseModel, Field, ConfigDict, create_model, ValidationError
env = Environment(
loader=PackageLoader("main"),
autoescape=select_autoescape()
)
logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(levelname)s:\t%(message)s'))
logger.addHandler(handler)
TEMPLATES_DIRPATH = '/app/templates'
BLUEPRINT_FILEPATH = '/app/templates/__blueprints.yaml'
class Param(BaseModel):
name: str = Field(title='The name of the template to be used to generate the ignition manifest', max_length=100)
model_config: str|int|dict = ConfigDict(extra='allow')
class Blueprint(BaseModel):
name: str = Field(title='The name of the blueprint to be used to generate the ignition manifest', max_length=100)
template: str = Field(title='The path of the template to be used with the blueprint', max_length=100)
model_config: str|int|dict = ConfigDict(extra='allow')
def _ignition_generation(butane_config_filename):
cmd = f'butane --files-dir {TEMPLATES_DIRPATH} --strict --check {butane_config_filename}'.split()
run = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
msg = {
'message': f'Submitted butane config not valid',
'error': f'{run.stdout.decode("utf-8")}'.split('\n'),
}
if run.stdout:
raise SyntaxError(msg)
from random import choice as r_choice
from string import ascii_letters
ignition_config_filename = f'/tmp/{''.join(r_choice(ascii_letters) for _ in range(10))}'
cmd = f'butane --files-dir {TEMPLATES_DIRPATH} --strict --pretty --raw {butane_config_filename} --output {ignition_config_filename}'.split()
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
msg = run.stdout.decode("utf-8")
if run.stdout:
raise Warning(msg)
with open(ignition_config_filename) as i:
res = i.read()
return res
description = """
## The need
To install CoreOS/Flatcar you need to expose the ignition file on an HTTP server and the use it remotely from your installation live session or you have to have a copy of your ignition file locally in your installation live session.
The pain increase with th number of servers to deploy.
## The solution
Instead of serve the ignition configs for each deployment you need, you can generate the ignition configs on-the-fly from butane templates you can customize as you want.
"""
app = FastAPI(
title = 'Ignition Server',
summary = 'Get, Add and Update Ignition configurations via a simple and flexible API',
description = description,
version = os.getenv('API_VERSION'),
contact={
"name": "Ignition Server",
"url": "https://github.com/soubinan/ignition-server",
},
)
@app.get('/', status_code=200, tags=['Home'])
def home() -> JSONResponse:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={'details': {
'info': 'Go to /redocs for more info',
'test': 'Go to /docs to test',
},}
)
@app.post('/configs', status_code=202, tags=['Configurations'], description='Generate an Ignition config from parameters query')
def generate_config(param: Param,) -> JSONResponse:
try:
template_name = param.name
template_path = f'./{template_name}.yaml'
template_source = env.loader.get_source(env, template_path)
parsed_content = env.parse(template_source)
fields = meta.find_undeclared_variables(parsed_content)
DynamicParamsModel = create_model('DynamicParamsModel', **{field: (Any, ...) for field in fields if field != 'name'}, __base__=Param)
values = DynamicParamsModel(**param.model_dump())
template = env.get_template(template_path)
result = template.render(values)
td, n = mkstemp(text=True)
with open(td, 'w') as t:
t.write(result)
out = _ignition_generation(n)
Path(n).unlink(missing_ok=True)
return JSONResponse(
status_code=status.HTTP_202_ACCEPTED,
content=json.loads(out)
)
except SyntaxError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
content={'details': json.loads(e),}
)
except ValidationError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_510_NOT_EXTENDED,
content={'details': json.loads(e.json()),}
)
except TemplateNotFound as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_510_NOT_EXTENDED,
content={'details': f'Template {template_name} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.get('/configs/{blueprint_id}', status_code=200, tags=['Configurations'], description='Generate an Ignition config from a predefined blueprint')
def get_config(blueprint_id: str,) -> JSONResponse:
blueprint = ''
try:
with open(BLUEPRINT_FILEPATH) as b:
blueprints = yaml.safe_load(b.read())
blueprint = blueprints[blueprint_id]
template = env.get_template(blueprint['template'])
result = template.render(blueprint)
td, n = mkstemp(text=True)
with open(td, 'w') as t:
t.write(result)
out = _ignition_generation(n)
Path(n).unlink(missing_ok=True)
return JSONResponse(
status_code=status.HTTP_200_OK,
content=json.loads(out)
)
except SyntaxError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
content={'details': e.msg,}
)
except KeyError as e:
status_code = status.HTTP_404_NOT_FOUND
if blueprint:
logger.info(out)
return JSONResponse(
status_code = status_code,
content={'details': 'Invalid Blueprint: Missing template path',}
)
else:
logger.info(e)
return JSONResponse(
status_code = status_code,
content={'details': f'Blueprint {blueprint_id} not found',}
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Mandatory Blueprints file {BLUEPRINT_FILEPATH} not found',}
)
except TemplateNotFound as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_510_NOT_EXTENDED,
content={'details': f'Template {blueprint['template']} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.get('/blueprints/{blueprint_id}', status_code=200, tags=['Blueprints'], description='Get one specific blueprint')
def get_blueprint(blueprint_id: str,) -> JSONResponse:
try:
with open(BLUEPRINT_FILEPATH) as b:
blueprints = yaml.safe_load(b.read())
blueprint = blueprints[blueprint_id]
return JSONResponse(
status_code=status.HTTP_200_OK,
content=blueprint
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Mandatory Blueprints file {BLUEPRINT_FILEPATH} not found',}
)
except KeyError as e:
status_code = status.HTTP_404_NOT_FOUND
logger.info(e)
return JSONResponse(
status_code = status_code,
content={'details': f'Blueprint {blueprint_id} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.get('/blueprints', status_code=200, tags=['Blueprints'], description='Get available blueprints list')
def get_blueprints() -> JSONResponse:
try:
with open(BLUEPRINT_FILEPATH) as b:
blueprints = yaml.safe_load(b.read())
return JSONResponse(
status_code=status.HTTP_200_OK,
content=blueprints
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Mandatory Blueprints file {BLUEPRINT_FILEPATH} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.post('/blueprints', status_code=201, tags=['Blueprints'], description='Add a new blueprint')
def add_blueprint(blueprint: Blueprint,) -> JSONResponse:
try:
with open(BLUEPRINT_FILEPATH) as b:
blueprints = yaml.safe_load(b.read())
if blueprint.name in blueprints:
raise ValueError(f'Blueprint ID {blueprint.name} already exists')
blueprints[blueprint.name] = {k:v for k,v in blueprint.model_dump().items() if k != 'name'}
with open(BLUEPRINT_FILEPATH, 'w') as b:
yaml.dump(blueprints, b)
return JSONResponse(
status_code=status.HTTP_201_CREATED,
content=blueprints
)
except ValueError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_409_CONFLICT,
content={'details': f'Blueprint ID {blueprint.name} already exists',}
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Mandatory Blueprints file {BLUEPRINT_FILEPATH} not found',}
)
except KeyError as e:
status_code = status.HTTP_404_NOT_FOUND
logger.info(e)
return JSONResponse(
status_code = status_code,
content={'details': f'Blueprint {blueprint.name} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.put('/blueprints/{blueprint_id}', status_code=202, tags=['Blueprints'], description='Update an existing blueprint')
def update_blueprint(blueprint_id: str, blueprint: Blueprint,) -> JSONResponse:
if os.getenv('READ_APPEND_ONLY') == 'true':
return JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
content={'details': 'You are in read_append_only mode, you can only read and add templates/blueprints. Update operations are not allowed'}
)
try:
with open(BLUEPRINT_FILEPATH) as b:
blueprints = yaml.safe_load(b.read())
_, blueprints[blueprint_id] = blueprints[blueprint_id], {k:v for k,v in blueprint.model_dump().items() if k != 'name'}
with open(BLUEPRINT_FILEPATH, 'w') as b:
yaml.dump(blueprints, b)
return JSONResponse(
status_code=status.HTTP_202_ACCEPTED,
content=blueprints
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Mandatory Blueprints file {BLUEPRINT_FILEPATH} not found',}
)
except KeyError as e:
status_code = status.HTTP_404_NOT_FOUND
logger.info(e)
return JSONResponse(
status_code = status_code,
content={'details': f'Blueprint {blueprint_id} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.get('/templates/{template_id}', status_code=200, tags=['Templates'], description='Get a specific template')
def get_template(template_id: str,) -> JSONResponse:
try:
if template_id == 'blueprints':
raise ValueError('Template filename should be different from Blueprint filename')
return FileResponse(
path=f'{TEMPLATES_DIRPATH}/{template_id}.yaml',
filename='{template_id}.yaml'
)
except ValueError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_409_CONFLICT,
content={'details': f'Template filename should be different from Blueprint filename',}
)
except FileNotFoundError as e:
logger.info(e)
return JSONResponse(
status_code = status.HTTP_501_NOT_IMPLEMENTED,
content={'details': f'Template file {TEMPLATES_DIRPATH}/{template_id}.yaml not found',}
)
except KeyError as e:
status_code = status.HTTP_404_NOT_FOUND
logger.info(e)
return JSONResponse(
status_code = status_code,
content={'details': f'Template {template_id} not found',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.get('/templates', status_code=200, tags=['Templates'], description='Get available templates list')
def get_templates() -> JSONResponse:
try:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
'path': TEMPLATES_DIRPATH,
'templates': [t for t in env.list_templates() if t != '__blueprints.yaml']
}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)
@app.post('/templates', status_code=201, tags=['Templates'], description='Upload a new template file')
async def add_template(template: UploadFile) -> JSONResponse:
already_exists = False
try:
templates = [t for t in env.list_templates() if t != '__blueprints.yaml']
if template.filename == '__blueprints.yaml':
raise ValueError('Template filename should be different from Blueprint filename')
if template.filename in templates:
already_exists = True
raise ValueError(f'Template name {template.filename} already exists. try with a different one')
with open(f'{TEMPLATES_DIRPATH}/{template.filename}', 'wb') as t:
t.write(template.file.read())
return JSONResponse(
status_code = status.HTTP_201_CREATED,
content={
'details': f'Template {TEMPLATES_DIRPATH}/{template.filename} successfully added',
'templates': [t for t in env.list_templates() if t != '__blueprints.yaml'],
}
)
except ValueError as e:
logger.info(e)
status_code = status.HTTP_409_CONFLICT
if already_exists:
return JSONResponse(
status_code = status_code,
content={'details': f'Template name {template.filename} already exists. Try with a different one',}
)
else:
return JSONResponse(
status_code = status_code,
content={'details': f'Template filename should be different from Blueprint filename',}
)
except Exception as e:
logger.exception(e)
return JSONResponse(
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR,
content={'details': 'Unexpected Internal Server Error Occurred, Please open an issue about this bug: https://github.com/soubinan/ignition-server/issues/new/choose',}
)