working version
parent
5186de078c
commit
5877926f5f
125
convert.py
125
convert.py
|
|
@ -6,10 +6,13 @@ import yaml
|
|||
import os
|
||||
from nested_lookup import nested_lookup
|
||||
|
||||
|
||||
# pre-initialization get current working directory
|
||||
cwd = os.getcwd()
|
||||
|
||||
|
||||
# defines
|
||||
METRIC_KEYS = 4
|
||||
SLOS_FOLDER_BASENAME = os.path.basename("slos")
|
||||
OUTPUT_FOLDER_BASENAME = os.path.basename("output")
|
||||
TEMPLATE_FOLDER_BASENAME = os.path.basename("templates")
|
||||
|
|
@ -25,9 +28,9 @@ TEMPLATE_FILES = {
|
|||
"calc:service.vehicleservice_oes_fivexx_count",
|
||||
"calc:service.vehicleservice_oes_request_count_total"]
|
||||
}
|
||||
|
||||
SLO_FILE_BASENAME = os.path.basename("TP_FTS-example.yaml")
|
||||
|
||||
|
||||
# define entries
|
||||
slo_id = None
|
||||
slo_name = None
|
||||
|
|
@ -43,62 +46,76 @@ metrics = {}
|
|||
services = {}
|
||||
keyRequests = {}
|
||||
|
||||
|
||||
# methods
|
||||
def logic(metrics,environment,TEMPLATE_FILES):
|
||||
def homogenize_metric_list(metrics):
|
||||
for metric in metrics:
|
||||
if len(metrics) < METRIC_KEYS:
|
||||
metrics.append('')
|
||||
return metrics
|
||||
|
||||
|
||||
def template_logic(metrics,TEMPLATE_FILES):
|
||||
for template,metric in TEMPLATE_FILES.items():
|
||||
print(set(metric))
|
||||
print(set(metrics[environment]))
|
||||
if set(metrics[environment]) == set(metric):
|
||||
return template,metric
|
||||
else:
|
||||
print("Lists do not match.")
|
||||
if set(metrics) == set(metric):
|
||||
return template
|
||||
print("Problem occured: Exiting program...")
|
||||
sys.exit(0)
|
||||
|
||||
# read yaml file
|
||||
with open(os.path.join(SLOS_FOLDER_BASENAME,SLO_FILE_BASENAME)) as f:
|
||||
slos = list(yaml.safe_load_all(f))
|
||||
|
||||
# fill values
|
||||
for i,slo in enumerate(slos):
|
||||
slo_id = slo["slo_id"]
|
||||
slo_name = slo["slo_name"]
|
||||
module_name = re.sub(r'[^a-zA-Z ]+', '', slo_name).replace(' ', '_').replace('__', '_')
|
||||
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 j,environment in enumerate(slo["slo_definition"]):
|
||||
environments.append(environment)
|
||||
metrics[environment] = nested_lookup('metric',slo["slo_definition"][environment])
|
||||
services[environment] = '~",\n\t\t\t\t~"'.join(nested_lookup('service',slo["slo_definition"][environment]))
|
||||
keyRequests[environment] = nested_lookup('keyRequests',slo["slo_definition"][environment])
|
||||
keyRequests[environment] = '~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(keyRequests[environment]))
|
||||
environment = None
|
||||
# main
|
||||
def main():
|
||||
|
||||
# read template file
|
||||
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_FOLDER_BASENAME),
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True)
|
||||
# read yaml file
|
||||
with open(os.path.join(SLOS_FOLDER_BASENAME,SLO_FILE_BASENAME)) as f:
|
||||
slos = list(yaml.safe_load_all(f))
|
||||
|
||||
# create template file
|
||||
for e,environment in enumerate(environments):
|
||||
folder_path = os.path.join(cwd,OUTPUT_FOLDER_BASENAME,environment.replace('-','_'),"slo")
|
||||
if not os.path.exists(folder_path):
|
||||
os.makedirs(folder_path)
|
||||
with open(os.path.join(folder_path,module_name+".yaml"),"w+") as file:
|
||||
t,m = logic(metrics,environment,TEMPLATE_FILES)
|
||||
jinja_template = jinja_environment.get_template(t)
|
||||
file.write(jinja_template.render(module=module_name,
|
||||
slo_name=slo_name,
|
||||
metric=m[0],
|
||||
metricA=m[0],
|
||||
metricB=m[1],
|
||||
metricC=m[2],
|
||||
metricD=m[3],
|
||||
description=description,
|
||||
service=services[environment],
|
||||
keyRequest=keyRequests[environment],
|
||||
target=slo_definition_tresholds_failure,
|
||||
warning=slo_definition_tresholds_warning))
|
||||
# fill values
|
||||
for i,slo in enumerate(slos):
|
||||
slo_id = slo["slo_id"]
|
||||
slo_name = slo["slo_name"]
|
||||
module_name = re.sub(r'[^a-zA-Z ]+', '', slo_name).replace(' ', '_').replace('__', '_')
|
||||
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 j,environment in enumerate(slo["slo_definition"]):
|
||||
environments.append(environment)
|
||||
metrics[environment] = nested_lookup('metric',slo["slo_definition"][environment])
|
||||
services[environment] = '~",\n\t\t\t\t~"'.join(nested_lookup('service',slo["slo_definition"][environment]))
|
||||
keyRequests[environment] = nested_lookup('keyRequests',slo["slo_definition"][environment])
|
||||
keyRequests[environment] = '~",\n\t\t\t\t~"'.join(itertools.chain.from_iterable(keyRequests[environment]))
|
||||
environment = None
|
||||
|
||||
# read template file
|
||||
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_FOLDER_BASENAME),
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True)
|
||||
|
||||
# create template file
|
||||
for e,environment in enumerate(environments):
|
||||
folder_path = os.path.join(cwd,OUTPUT_FOLDER_BASENAME,environment.replace('-','_'),"slo")
|
||||
if not os.path.exists(folder_path):
|
||||
os.makedirs(folder_path)
|
||||
with open(os.path.join(folder_path,module_name+".yaml"),"w+") as file:
|
||||
t = template_logic(metrics[environment],TEMPLATE_FILES)
|
||||
m = homogenize_metric_list(metrics[environment])
|
||||
jinja_template = jinja_environment.get_template(t)
|
||||
file.write(jinja_template.render(module=module_name,
|
||||
slo_name=slo_name,
|
||||
metric=m[0],
|
||||
metricA=m[0],
|
||||
metricB=m[1],
|
||||
metricC=m[2],
|
||||
metricD=m[3],
|
||||
description=description,
|
||||
service=services[environment],
|
||||
keyRequest=keyRequests[environment],
|
||||
target=slo_definition_tresholds_failure,
|
||||
warning=slo_definition_tresholds_warning))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in New Issue