137 lines
5.2 KiB
Python
137 lines
5.2 KiB
Python
import glob
|
|
import sys
|
|
import itertools
|
|
import jinja2
|
|
import re
|
|
import yaml
|
|
import os
|
|
from nested_lookup import nested_lookup
|
|
from pathlib import Path
|
|
|
|
# defines
|
|
OUTPUT_FOLDER_BASENAME = os.path.basename("output")
|
|
TEMPLATE_FOLDER_BASENAME = os.path.basename("jinja-templates")
|
|
TEMPLATE_FILES = {
|
|
"terraform-template-1.j2":["builtin:service.keyRequest.errors.fivexx.rate"],
|
|
"terraform-template-2.j2":["builtin:service.keyRequest.errors.server.successCount"],
|
|
"terraform-template-3.j2":["builtin:service.keyRequest.errors.fivexx.count"],
|
|
"terraform-template-4.j2":["builtin:service.errors.server.successCount"],
|
|
"terraform-template-5.j2":["builtin:service.errors.fivexx.successCount"]
|
|
}
|
|
FOLDERS = {"My Journey":"DE-3",
|
|
"Connected Vehicle Platforms":"DE-7",
|
|
"My Life":"DE-4",
|
|
"China Services":"EC-DE"}
|
|
|
|
# methods
|
|
def template_logic(metric):
|
|
for template,metricElement in TEMPLATE_FILES.items():
|
|
if all([item in metricElement for item in metric]):
|
|
# if set(metricList) == set(metricTemplate):
|
|
return template
|
|
print("Problem occured while matching metric: Exiting program...")
|
|
sys.exit(0)
|
|
|
|
# main
|
|
def main(configFile):
|
|
slo_id = None
|
|
slo_name = None
|
|
module_name = None
|
|
displayname = None
|
|
department = None
|
|
description = None
|
|
doc_url = None
|
|
slo_definition_tresholds_warning = None
|
|
slo_definition_tresholds_failure = None
|
|
environments = {}
|
|
template = None
|
|
metric = None
|
|
services = []
|
|
keyRequests = []
|
|
|
|
try:
|
|
# read yaml file
|
|
with open(configFile) as cF:
|
|
slos = list(yaml.safe_load_all(cF))
|
|
except EnvironmentError:
|
|
print('Corrupt yaml file:' + str(configFile))
|
|
|
|
# fill values
|
|
for slo in slos:
|
|
slo_id = slo["slo_id"]
|
|
slo_name = slo["slo_name"]
|
|
module_name = slo_name
|
|
displayname = slo["displayname"]
|
|
description = slo["description"]
|
|
department = slo["department"]
|
|
doc_url = slo["doc_url"]
|
|
slo_definition_tresholds_warning = slo["slo_definition"]["tresholds"]["warning"]
|
|
slo_definition_tresholds_failure = slo["slo_definition"]["tresholds"]["failure"]
|
|
slo["slo_definition"].pop("tresholds")
|
|
|
|
for env in slo["slo_definition"]:
|
|
if "EMEA-Prod" in slo["slo_definition"]:
|
|
environments["EMEA-Prod"] = slo["slo_definition"][env]
|
|
if "NA-Prod" in slo["slo_definition"]:
|
|
environments["NA-Prod"] = slo["slo_definition"][env]
|
|
if "CN-Prod" in slo["slo_definition"]:
|
|
environments["CN-Prod"] = slo["slo_definition"][env]
|
|
|
|
# read template file
|
|
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_FOLDER_BASENAME),
|
|
trim_blocks=True,
|
|
lstrip_blocks=True)
|
|
|
|
# generate terraform file
|
|
for environment,ev in environments.items():
|
|
folder_path = os.path.join(cwd,OUTPUT_FOLDER_BASENAME,environment.replace('-','_'),"slo")
|
|
if not os.path.exists(folder_path):
|
|
os.makedirs(folder_path)
|
|
metric = nested_lookup('metric',ev)
|
|
|
|
for service in ev[0]["filter"]:
|
|
services.append(nested_lookup('service',service))
|
|
keyRequests.append(nested_lookup('keyRequests',service))
|
|
|
|
with open(os.path.join(folder_path,module_name+".tf"),"w+") as file:
|
|
jinja_template = jinja_environment.get_template(template_logic(metric))
|
|
file.write(jinja_template.render(module=module_name,
|
|
slo_name=slo_name,
|
|
metric=metric[0],
|
|
description=description,
|
|
services='~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(services)),
|
|
keyRequests='~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(itertools.chain.from_iterable(keyRequests))),
|
|
target=slo_definition_tresholds_failure,
|
|
warning=slo_definition_tresholds_warning))
|
|
services = []
|
|
keyRequests = []
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# pre-initialization get current working directory
|
|
cwd = os.getcwd()
|
|
|
|
print(sys.argv)
|
|
if len(sys.argv) != 2:
|
|
print(".\generate.py <FOLDER>")
|
|
print("Example: .\generate.py DE-3")
|
|
print("Example: .\generate.py DE-3,DE-4,...")
|
|
sys.exit()
|
|
|
|
# init folder
|
|
for folderName,businessLine in FOLDERS.items():
|
|
businessLinePath = os.path.join(cwd,os.path.basename(businessLine))
|
|
if not os.path.exists(businessLinePath):
|
|
os.makedirs(businessLinePath)
|
|
|
|
# for each folder
|
|
for arg in [sys.argv[1]]:
|
|
# return path of slo config files
|
|
configFiles = glob.glob(pathname=os.path.join(cwd,os.path.basename(arg))+'\\*.yaml',
|
|
# root_dir=os.path.join(cwd,os.path.basename(arg)),
|
|
recursive=True)
|
|
|
|
# for each slo config file in folder
|
|
for configFile in configFiles:
|
|
main(configFile)
|