-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
gen_docker_compose_by_variant.py
96 lines (73 loc) · 3.27 KB
/
gen_docker_compose_by_variant.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
"""
Generate docker-compose.yml based on docker-compose.template file.
Some how balena doesn't like output of:
`docker-compose -f docker-compose.template config > docker-compose.yml`
That's probably because output for `docker-compose config` does not retain the
order of the sections present in the file. So as a workaround this small file
uses jinja2 and tries to do the same thing but without re-arranging sections
and just replacing placeholder values.
"""
import os
import argparse
from typing import Union
from configparser import ConfigParser
from jinja2 import Environment, select_autoescape, FileSystemLoader
from hm_pyhelper.hardware_definitions import variant_definitions
prog_name = os.path.basename(os.path.abspath(__file__))
here = os.path.dirname(os.path.abspath(__file__))
class DockerComposer:
"""
A class to facilitate generation of docker-compose.yml files
from templates.
"""
def __init__(self, templates_folder: Union[str, None] = None):
if templates_folder is None:
templates_folder = os.path.join(here, 'templates')
self.jinja_env = Environment(
loader=FileSystemLoader(templates_folder),
autoescape=select_autoescape()
)
config = ConfigParser(strict=True)
# Suppress default behavior of converting key names to lower-case.
config.optionxform = lambda option: option
config.read('settings.ini')
self.config = config
def generate_compose_file(
self,
variant_type: str,
template_file: str,
output_file: str
) -> None:
"""generate_compose_file Render template_file to generate compose file.
Args:
variant_type (str): The target device variant for the docker compose file.
template_file (str): Template file name without folder name.
Should be present in the templates folder.
output_file (str): Output filename. Can be a path. Otherwise
file is created in current folder.
"""
template = self.jinja_env.get_template(template_file)
template_args = {}
if 'versions' not in self.config:
raise RuntimeError("Bad config, no [versions] section found")
for k, v in self.config['versions'].items():
template_args[k] = v
template_args["ENV"] = os.environ
template_args["ARCH"] = variant_definitions[variant_type]["CPU_ARCH"]
output = template.render(**template_args)
with open(output_file, 'w') as template_output:
template_output.write(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser("Generate docker-compose.yml.")
parser.add_argument('variant_type',
help="Target variant.")
parser.add_argument('--template', '-t', default='docker-compose.template',
help="Input template file. Should be present in "
"templates folder.")
parser.add_argument('--output', '-o', default='docker-compose.yml',
help="Output file. Created in current folder.")
args = parser.parse_args()
# print(args)
composer = DockerComposer()
composer.generate_compose_file(
args.variant_type, args.template, args.output)