-
Notifications
You must be signed in to change notification settings - Fork 1
/
modifyComposes.py
executable file
·107 lines (88 loc) · 4.02 KB
/
modifyComposes.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
#!/bin/python3
from genericpath import isfile
import yaml, os, sys
def redprint(text):
print("\033[1;31m{}\033[0;m".format(text))
def orangeprint(text):
print("\033[1;33m{}\033[0;m".format(text))
modulesPath="modules"
if not os.path.exists(modulesPath):
redprint("Error: init script not executed properly")
sys.exit(1)
orangeprint("patching modules ...")
for module in os.listdir(modulesPath):
if module.find("_") > -1:
os.rename(os.path.join(modulesPath, module), os.path.join(modulesPath, module.replace("_", "-")))
module = module.replace("_", "-")
moduleDir = os.path.join(modulesPath, module)
if os.path.exists(os.path.join(moduleDir, "data")):
print(" renaming directory 'data' to 'data_template' ...")
os.rename(os.path.join(moduleDir, "data"), os.path.join(moduleDir, "data_template"))
composefile = os.path.join(moduleDir, "docker-compose.yml")
if os.path.exists(composefile):
orangeprint(" modifying module '{}' ('{}') ...".format(module, moduleDir))
sfile = open(composefile, "r")
dockercompose = yaml.safe_load(sfile.read())
sfile.close()
servicesToRename = []
for service in dockercompose["services"]:
addToRenameList = False
if not service.endswith("-$city_normalize"):
addToRenameList = True
if 2 > service.count(module):
addToRenameList = True
if addToRenameList:
servicesToRename.append(service)
print(" service '{}'".format(service))
if "volumes" in dockercompose["services"][service]:
for index, content in enumerate(dockercompose["services"][service]["volumes"]):
volumeConf = content.split(":", 1)
hostVolumePart = volumeConf[0]
del volumeConf[0]
normalize = hostVolumePart.lower().replace("./", "").replace("/", "")
if normalize.find("data") > -1:
print(" volume configuration ...")
dockercompose["services"][service]["volumes"][index] = hostVolumePart.replace("data", "data_$city") + ":" + ":".join(volumeConf)
if "ports" in dockercompose["services"][service]:
print(" remove port binding ...")
del dockercompose["services"][service]["ports"]
for service in servicesToRename:
newName = module + "-" + service.replace("-$city_normalize", "") + "-$city_normalize"
dockercompose["services"][newName] = dockercompose["services"][service]
del dockercompose["services"][service]
dockercompose["networks"]["default"]["name"] = os.path.basename(os.getcwd())
sfile = open(os.path.join(moduleDir, "docker-compose.yml"), "w")
sfile.write(yaml.dump(dockercompose, sort_keys=False, default_flow_style=False))
sfile.close()
orangeprint("patching chief")
sfile = open("docker-compose.yml", "r")
dockercompose = yaml.safe_load(sfile.read())
sfile.close()
dockercompose["networks"]["default"]["name"] = os.path.basename(os.getcwd())
sfile = open("docker-compose.yml", "w")
sfile.write(yaml.dump(dockercompose, sort_keys=False, default_flow_style=False))
sfile.close()
orangeprint("patching chief plugins")
pluginsChiefDir = os.path.join("plugins", "chief")
for chief in os.listdir(pluginsChiefDir):
chiefPath = os.path.join(pluginsChiefDir, chief)
if os.path.isfile(chiefPath) and chiefPath.endswith(".yml"):
orangeprint(" modifying chief plugin '{}' ('{}') ...".format(chief.split(".", -1)[0], pluginsChiefDir))
sfile = open(chiefPath, "r")
dockercompose = yaml.safe_load(sfile.read())
sfile.close()
servicesToRename = []
for service in dockercompose["services"]:
print(" service '{}'".format(service))
if not service.startswith("chief-"):
servicesToRename.append(service)
if "ports" in dockercompose["services"][service]:
print(" remove port binding ...")
del dockercompose["services"][service]["ports"]
dockercompose["networks"]["default"]["name"] = os.path.basename(os.getcwd())
for service in servicesToRename:
dockercompose["services"]["chief-" + service] = dockercompose["services"][service]
del dockercompose["services"][service]
sfile = open(chiefPath, "w")
sfile.write(yaml.dump(dockercompose, sort_keys=False, default_flow_style=False))
sfile.close()