-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompose.py
executable file
·194 lines (158 loc) · 5.86 KB
/
decompose.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
#!/usr/bin/env python
# Author: Jonathan Adami ([email protected])
# Source: https://github.com/PiTiLeZarD/decompose
import argparse
import glob
import os
import subprocess
import json
parser = argparse.ArgumentParser()
parser.add_argument(
"-e",
"--env",
help="Specify the environment to use (default: local)",
default="local",
choices=["local", "uat", "production", "front"],
)
parser.add_argument(
"-g", "--group", action="append", help="Specify a group of services", default=[]
)
parser.add_argument(
"-c", "--configfile", help="Specify a config file", default="decompose.conf.json"
)
parser.add_argument(
"-p", "--path", default=os.getcwd(), help="Path to find the root of modules"
)
parser.add_argument(
"-pr",
"--path_relative",
default=None,
help="Use relative path (can use a variable here)",
)
parser.add_argument(
"--substitute", action="append", help="Other substitutions", default=[]
)
parser.add_argument(
"-mp", "--modules_path", default="modules", help="Path to find modules from root"
)
parser.add_argument(
"-o",
"--output",
default="docker-compose.yml",
help="File in which the config will be saved",
)
parser.add_argument(
"--dockercompose_version",
default="3.8",
help="Target a specific dockercompose version",
)
parser.add_argument(
"-s", "--service", action="append", help="Specify a service", default=[]
)
arguments = parser.parse_args()
def getPath(*args):
return "/".join([arguments.path] + list(args))
def getConfig():
if not os.path.isdir(arguments.path):
raise Exception("{0} is not a valid path".format(arguments.path))
configfile = arguments.configfile
if not os.path.isfile(configfile):
configfile = getPath(arguments.configfile)
if not os.path.isfile(configfile):
raise Exception("{0} could not be found".format(configfile))
config = None
with open(configfile, "rb") as f:
config = json.loads(f.read())
return config
def getAllServices():
groups = getConfig()["groups"]
for group in arguments.group:
if group not in groups:
raise Exception("Group {0} not found".format(group))
available_services = set(
[
f.split("/")[-2]
for f in glob.glob(getPath(arguments.modules_path, "**/docker-compose.yml"))
]
)
services = set([s for g in arguments.group for s in groups[g]] + arguments.service)
services = [s for s in services if s in available_services]
if len(services) == 0:
raise Exception("No service selected")
for service in services:
if not os.path.isdir(getPath(arguments.modules_path, service)):
raise Exception("Service {0} could not be found".format(service))
return services
# gather all common files
common = {}
for common_file in glob.glob(getPath(arguments.modules_path, "*.yml")):
with open(common_file, "r") as f:
common[os.path.basename(common_file)] = f.read()
def use_file(service, env=None):
filename = "docker-compose.{0}yml".format("" if env is None else "{0}.".format(env))
filename = getPath(arguments.modules_path, service, filename)
if not os.path.isfile(filename):
raise Exception(
"Service {0} misconfigured (cannot find {1})".format(service, filename)
)
file_content = None
with open(filename, "r") as f:
file_content = f.read()
file_content = file_content.replace(
"##VERSION##", 'version: "{0}"'.format(arguments.dockercompose_version)
)
for item in common:
file_content = file_content.replace("# include {0}".format(item), common[item])
file_content = file_content.replace("#<<: *", "<<: *")
grouped_maps_file = []
cursor = 0
group = []
prefix = " <<: "
for num, line in enumerate(file_content.split("\n"), 1):
if "%s*" % prefix in line:
if num - cursor > 1:
group = []
cursor = num
group.append(line.replace(prefix, ""))
else:
if len(group) > 0:
grouped_maps_file.append("%s[%s]" % (prefix, ", ".join(group)))
group = []
grouped_maps_file.append(line)
filename = getPath("{0}_{1}".format(service, os.path.basename(filename)))
with open(filename, "w") as f:
f.write("\n".join(grouped_maps_file))
return filename
if __name__ == "__main__":
if arguments.env not in getConfig()["environments"]:
raise Exception("{0} is not a valid environment".format(arguments.env))
# create a folder with all files ready for docker-compose
compose_files = []
for service in getAllServices():
compose_files.append(use_file(service))
compose_files.append(use_file(service, env=arguments.env))
docker_compose_command = ["docker-compose"]
env_file = getPath(".env.{0}".format(arguments.env))
if os.path.isfile(env_file):
docker_compose_command.append("--env-file")
docker_compose_command.append(env_file)
for compose_file in compose_files:
docker_compose_command.append("-f")
docker_compose_command.append(compose_file)
docker_compose_command.append("config")
try:
config = subprocess.check_output(docker_compose_command).decode("utf-8")
dockercompose_file = getPath(arguments.output)
if arguments.path_relative is not None:
config = config.replace(os.path.abspath(getPath()), arguments.path_relative)
for substitution in arguments.substitute:
config = config.replace(*substitution.split(" "))
with open(dockercompose_file, "w") as f:
f.write(config)
except subprocess.CalledProcessError as e:
print("---- ERROR -----")
print(e.stderr)
print("----------------")
finally:
for compose_file in compose_files:
os.unlink(compose_file)