160 lines
3.4 KiB
Python
160 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import sys as _sys
|
|
import os as _os
|
|
import yaml as _yaml
|
|
import json as _json
|
|
import argparse as _argparse
|
|
|
|
|
|
def file_read(path):
|
|
handle = open(path, "r")
|
|
content = handle.read()
|
|
handle.close()
|
|
return content
|
|
|
|
|
|
def file_write(path, content):
|
|
directory = _os.path.dirname(path)
|
|
if (not _os.path.exists(directory)):
|
|
_os.makedirs(directory, exist_ok = True)
|
|
else:
|
|
pass
|
|
handle = open(path, "w")
|
|
handle.write(content)
|
|
handle.close()
|
|
return content
|
|
|
|
|
|
def dict_merge(core, mantle, path = None):
|
|
if (path is None):
|
|
path = []
|
|
result = {}
|
|
for source in [core, mantle]:
|
|
for (key, value_new, ) in source.items():
|
|
path_ = (path + [key])
|
|
type_new = type(value_new)
|
|
if (not (key in result)):
|
|
result[key] = value_new
|
|
else:
|
|
value_old = result[key]
|
|
type_old = type(value_old)
|
|
if (value_old is None):
|
|
result[key] = value_new
|
|
else:
|
|
if (not (type_old == type_new)):
|
|
raise ValueError(
|
|
"type mismatch at path %s: %s vs. %s"
|
|
% (
|
|
".".join(path),
|
|
str(type_old),
|
|
str(type_new),
|
|
)
|
|
)
|
|
else:
|
|
if (type_old == dict):
|
|
result[key] = dict_merge(value_old, value_new, path_)
|
|
elif (type_old == list):
|
|
result[key] = (value_old + value_new)
|
|
else:
|
|
result[key] = value_new
|
|
return result
|
|
|
|
|
|
def main():
|
|
## args
|
|
argument_parser = _argparse.ArgumentParser()
|
|
argument_parser.add_argument(
|
|
"-s",
|
|
"--source-directory",
|
|
type = str,
|
|
dest = "source_directory",
|
|
default = "/opt/mas/conf.d",
|
|
metavar = "<source-directory>",
|
|
)
|
|
argument_parser.add_argument(
|
|
"-f",
|
|
"--output-format",
|
|
type = str,
|
|
choices = ["json", "yaml"],
|
|
dest = "output_format",
|
|
default = "yaml",
|
|
metavar = "<output-format>",
|
|
)
|
|
argument_parser.add_argument(
|
|
"-o",
|
|
"--output-path",
|
|
type = str,
|
|
dest = "output_path",
|
|
default = "/opt/mas/config.yaml",
|
|
metavar = "<output-path>",
|
|
)
|
|
args = argument_parser.parse_args()
|
|
|
|
## exec
|
|
data = {}
|
|
### base
|
|
if True:
|
|
data_raw = _yaml.safe_load(file_read(_os.path.join(args.source_directory, "base.yaml")))
|
|
data = dict_merge(
|
|
data,
|
|
{
|
|
"secrets": data_raw["secrets"],
|
|
"passwords": data_raw["passwords"],
|
|
}
|
|
)
|
|
### database
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
_json.loads(file_read(_os.path.join(args.source_directory, "database.json")))
|
|
)
|
|
### http
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
_json.loads(file_read(_os.path.join(args.source_directory, "http.json")))
|
|
)
|
|
### matrix
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
_json.loads(file_read(_os.path.join(args.source_directory, "matrix.json")))
|
|
)
|
|
### upstream
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
_json.loads(file_read(_os.path.join(args.source_directory, "upstream.json")))
|
|
)
|
|
### email
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
_json.loads(file_read(_os.path.join(args.source_directory, "email.json")))
|
|
)
|
|
### clients
|
|
if True:
|
|
data = dict_merge(
|
|
data,
|
|
{
|
|
"clients": list(
|
|
map(
|
|
lambda name: _json.loads(file_read(_os.path.join(args.source_directory, "clients", name))),
|
|
_os.listdir(_os.path.join(args.source_directory, "clients"))
|
|
)
|
|
),
|
|
}
|
|
)
|
|
## output
|
|
if True:
|
|
if (args.output_format == "json"):
|
|
output_content = _json.dumps(data, indent = "\t")
|
|
elif (args.output_format == "yaml"):
|
|
output_content = _yaml.dump(data)
|
|
else:
|
|
raise ValueError("invalid output format")
|
|
file_write(args.output_path, output_content)
|
|
|
|
|
|
main()
|