-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_docker_compose_for_testing.py
More file actions
110 lines (90 loc) · 4.04 KB
/
create_docker_compose_for_testing.py
File metadata and controls
110 lines (90 loc) · 4.04 KB
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
#!/usr/bin/env python3
import json
import os.path
import subprocess
import argparse
from ruamel.yaml import YAML
def argument_parser_init() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='docker-compose.yml creation tool')
parser.add_argument('-c', '--config', type=str, default='./docker_compose_config.json', help='configuration file')
parser.add_argument('-o', '--output', type=str, default='./', help='output directory for docker-compose.yml')
return parser.parse_args()
def validate_config(config, docker_compose):
if 'components' not in config:
print("'components' is not defined in the configuration file")
return False
for component in config['components']:
if 'name' in component:
if component['name'] not in docker_compose['services']:
print(f"{component['name']} is not defined in the docker-compose.yml")
return False
else:
print("'name' is not defined in one of the components")
return False
if 'path' not in component:
print("'path' is not defined in one of the components")
return False
if 'replace' in component:
if not isinstance(component['replace'], bool):
print("'replace' must be a boolean")
return False
else:
print("'replace' is not defined in one of the components")
return False
if 'force_rebuild' in component:
if not isinstance(component['force_rebuild'], bool):
print("'force_rebuild' must be a boolean")
return False
else:
print("'force_rebuild' is not defined in one of the components")
return False
return True
def build_docker_image(component):
if os.path.isfile(os.path.join(component['path'], 'Dockerfile')):
print(f"Building docker image for {component['name']}")
command = (f"docker build {'--no-cache ' if component['force_rebuild'] else ''}"
f"-t {component['name']}:testing -f {component['path']}/Dockerfile {component['path']}")
rc = subprocess.call(command.split(" "), text=True)
if rc != 0:
print(f"Failed to build docker image for {component['name']}")
return False
return True
else:
print(f"Path provided for {component['name']} does not lead to a Dockerfile")
return False
def replace_volumes(docker_compose, component, etna_path):
volumes = docker_compose['services'][component['name']]['volumes']
docker_compose['services'][component['name']]['volumes'] = []
for volume in volumes:
paths = volume.split(':')
docker_compose['services'][component['name']]['volumes'].append(
os.path.join(etna_path, paths[0]) + ":" + paths[1]
)
def main():
args = argument_parser_init()
yaml = YAML()
yaml.preserve_quotes = True
with open(args.config) as file:
config = json.load(file)
if 'etna_path' in config:
etna_path = os.path.abspath(config['etna_path'])
with open(os.path.join(etna_path, 'docker-compose.yml')) as file:
docker_compose = yaml.load(file)
else:
print("'etna_path' is not defined in the configuration file")
return
if not validate_config(config, docker_compose):
return
for component in config['components']:
if component['replace']:
if not build_docker_image(component):
return
docker_compose['services'][component['name']]['image'] = f"{component['name']}:testing"
replace_volumes(docker_compose, component, etna_path)
if 'profiles' in docker_compose['services'][component['name']]:
docker_compose['services'][component['name']]['profiles'].append(f"{component['name']}-testing")
with open(os.path.join(args.output, 'docker-compose.yml'), 'w') as file:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(docker_compose, file)
if __name__ == '__main__':
main()