-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_tagged_compose_files.py
45 lines (36 loc) · 1.52 KB
/
generate_tagged_compose_files.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
import yaml
import os
import sys
work_folder = os.path.dirname(os.path.realpath(__file__))
compose_folder = os.path.join(work_folder, "docker-config")
prod_yml_path = os.path.join(compose_folder, "prod.yml")
prod_output_yml_path = os.path.join(compose_folder, "prod_tagged.yml")
dev_yml_path = os.path.join(compose_folder, "dev.yml")
dev_output_yml_path = os.path.join(compose_folder, "dev_tagged.yml")
def read_yml(yml_path):
with open(yml_path) as f:
yml_dict = yaml.load(f)
return yml_dict
def write_yml(target_path, yml_dict):
with open(target_path, "w") as f:
f.write("# This file is automatically generated. Changes are not committed to the repo and will be overwritten." + os.linesep)
f.write("version: '2'" + os.linesep)
yaml.safe_dump(yml_dict, f)
def insert_image_tag(yml_dict, tag):
for service in yml_dict["services"].keys():
image_name = yml_dict["services"][service]["image"]
tagged_image_name = image_name + ":" + tag
del yml_dict["services"][service]["image"]
yml_dict["services"][service]["image"] = tagged_image_name
if len(sys.argv) == 2:
tag = sys.argv[1]
# Prod:
prod_yml = read_yml(prod_yml_path)
insert_image_tag(prod_yml, tag)
write_yml(prod_output_yml_path, prod_yml)
# Dev:
dev_yml = read_yml(dev_yml_path)
insert_image_tag(dev_yml, tag)
write_yml(dev_output_yml_path, dev_yml)
else:
print("Aborting. 1 argument is necessary for injecting the tag of the images into docker-compose-file: prod.yml")