From 1482f949cf81833eba15e29c0113bdeef878d10e Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Wed, 27 Jul 2022 22:13:52 +0200 Subject: [PATCH] Implemented interaction with git (archive and shared config) --- .gitignore | 4 +- createDash.py | 39 ++++++- requirements.txt | 3 +- slo_parameter.yaml | 262 --------------------------------------------- 4 files changed, 40 insertions(+), 268 deletions(-) delete mode 100644 slo_parameter.yaml diff --git a/.gitignore b/.gitignore index 72b94cf..0b40d78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .env dashboard_tiles_* -\[STAGING\]* \ No newline at end of file +\[STAGING\]* +shared_configuration/ +archive/ \ No newline at end of file diff --git a/createDash.py b/createDash.py index 3a36e46..9138136 100644 --- a/createDash.py +++ b/createDash.py @@ -4,14 +4,35 @@ import json import argparse import requests from datetime import datetime +from git import Repo +import os #set STAGING global dashboard name DASHBOARD_NAME = "[STAGING]Global Offboard Reliability - Touchpoint Mobile #" +CONFIG_REPO_URL = "https://atc.bmwgroup.net/bitbucket/scm/opapm/shared_configuration.git" +CONFIG_REPO_NAME = "shared_configuration" +ARCHIVE_REPO_URL = "https://atc.bmwgroup.net/bitbucket/scm/opapm/archive.git" +ARCHIVE_REPO_NAME = "archive" parser = argparse.ArgumentParser(description="Generate and deploy the Dynatrace Global Dashboard as Code. Auto deployment works only for STAGING dashboard", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-R", "--rows", type=int, help="Number of rows per dashboard. If not specified, all rows will be added to single dashboard") parser.add_argument('--auto-upload', default=False, action='store_true', help="Auto upload to STAGING dashboard") args = parser.parse_args() +def clone_repo_if_notexist(repourl, reponame): + if(not os.path.isdir(reponame)): + repo = Repo.clone_from(repourl, reponame) + return repo + return Repo(reponame) +def pull_repo(repo): + origin = repo.remotes.origin + origin.pull() + +def push_repo(repo, message): + repo.git.add(all=True) + repo.index.commit(message) + origin = repo.remotes.origin + origin.push() + def load_slo_parameter(path): # the first part is to read a yaml and only select latest, valid config with open(path) as file: @@ -62,7 +83,10 @@ def backup_dashboards(DTAPIToken, DTENV, dashboards): print("Downloaded dashboard from Dynatrace: "+entityResponse["dashboardMetadata"]["name"]+", creating backup...") now=datetime.now() strnow = now.strftime("%Y%m%d_%H%M%S") - with open(entityResponse["dashboardMetadata"]["name"]+"_"+strnow+".json", "w") as file: + strnowdate = now.strftime("%Y%m%d") + if not os.path.isdir("./archive/"+strnowdate): + os.makedirs("./archive/"+strnowdate) + with open("./archive/"+strnowdate+"/"+entityResponse["dashboardMetadata"]["name"]+"_"+strnow+".json", "w") as file: json.dump(entityResponse, file, indent=2) def remove_dashboards(DTAPIToken, DTENV, dashboards): for dashboard in dashboards: @@ -70,7 +94,6 @@ def remove_dashboards(DTAPIToken, DTENV, dashboards): DTAPIURL = DTENV + "api/config/v1/dashboards/" + dashboard["id"] print(make_request(DTAPIURL,DTAPIToken,True,"delete",None)) def create_or_update_dashboard(DTAPIToken, DTENV, dashboards, files): - backup_dashboards(DTAPIToken, DTENV, dashboards) if(files): for index, filename in enumerate(files,start=1): with open('./'+filename) as file: @@ -248,6 +271,10 @@ def create_default_tiles(): return newDashboardTiles def main(slo_path): + configrepo = clone_repo_if_notexist(CONFIG_REPO_URL, CONFIG_REPO_NAME) + pull_repo(configrepo) + archiverepo = clone_repo_if_notexist(ARCHIVE_REPO_URL, ARCHIVE_REPO_NAME) + pull_repo(archiverepo) print("Generating dashboard tiles...") slo_doc = load_slo_parameter(slo_path) dashboard_json = create_default_tiles() @@ -328,11 +355,15 @@ def main(slo_path): print("Gather data, hold on a minute") DTTOKEN = config(token.get('env-token-name')) DTURL = url.get('env-url') - + existingdashboards = get_all_dashboards_withname(DTTOKEN, DTURL,DASHBOARD_NAME) print("Uploading STAGING dashboards to Dynatrace...") + backup_dashboards(DTTOKEN, DTURL, existingdashboards) + now=datetime.now() + strnowdate = now.strftime("%Y%m%d") + push_repo(archiverepo, strnowdate+"_Global dashboard as code auto-upload backup") create_or_update_dashboard(DTTOKEN, DTURL, existingdashboards, generatedfiles) if __name__ == "__main__": - main('./slo_parameter.yaml') \ No newline at end of file + main('./shared_configuration/slo_parameter.yaml') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 51a920c..c8afe58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ python-decouple pyyaml requests datetime -argparse \ No newline at end of file +argparse +GitPython \ No newline at end of file diff --git a/slo_parameter.yaml b/slo_parameter.yaml deleted file mode 100644 index 6eb3ab0..0000000 --- a/slo_parameter.yaml +++ /dev/null @@ -1,262 +0,0 @@ ---- -TP_Mobile_Login: - index: 1 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Login" - department: "DE-442" - metric: "func:slo.tp_mobile_login" - doc_url: "https://atc.bmwgroup.net/confluence/x/R1OqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_Mapping: - index: 2 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Mapping" - department: "DE-443" - metric: "func:slo.tp_mobile_mapping" - doc_url: "https://atc.bmwgroup.net/confluence/x/WFOqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_VehicleList: - index: 3 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Vehicle List" - department: "DE-43" - metric: "func:slo.tp_mobile_vehiclelist" - doc_url: "https://atc.bmwgroup.net/confluence/x/c1OqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_VehicleData: - index: 4 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Vehicle Data" - department: "DE-733" - metric: "func:slo.tp_mobile_vehicledata" - doc_url: "https://atc.bmwgroup.net/confluence/x/LFOqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_RemoteServices: - index: 5 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Remote Services" - department: "DE-723" - metric: "func:slo.tp_mobile_remoteservices" - doc_url: "https://atc.bmwgroup.net/confluence/x/dFSqdQ" - ops_dashboard: - emea: "https://xxu26128.live.dynatrace.com/#dashboard;gtf=-35m%20to%20-5m;gf=all;id=a3d9155e-24fd-4ec9-b9bc-31eb5772422f" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_Remote360: - index: 6 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Remote Camera" - department: "DE-723" - metric: "func:slo.tp_mobile_remote360" - doc_url: "https://atc.bmwgroup.net/confluence/x/aVSqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_Send2VehicleMGU: - index: 7 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Send to Vehicle (MGU)" - department: "DE-320" - metric: "func:slo.tp_mobile_send2vehiclemgu" - doc_url: "https://atc.bmwgroup.net/confluence/x/qFSqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_Send2VehicleLegacy: - index: 8 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Send to Vehicle (Legacy)" - department: "DE-723" - metric: "func:slo.tp_mobile_send2vehiclelegacy" - doc_url: "https://atc.bmwgroup.net/confluence/x/z1SqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_PersonalFavorites: - index: 9 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Personal Favorites" - department: "DE-443" - metric: "func:slo.tp_mobile_personalfavorites" - doc_url: "https://atc.bmwgroup.net/confluence/x/w1SqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_DigitalKey: - index: 10 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "Digital Key" - department: "DE-730" - metric: "func:slo.tp_mobile_digitalkey" - doc_url: "https://atc.bmwgroup.net/confluence/x/u1SqdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" - -TP_Mobile_RSU: - index: 11 - selector_type: "text" - selector_var: 'CoCo-QM-Report_Mobile' - yearstart: "2022-01-01" - displayname: "RSU Mobile" - department: "DE-430" - metric: "func:slo.wirkkette__rsu__mobile_____reliability_of_key_requests" - doc_url: "https://atc.bmwgroup.net/confluence/x/NpnwdQ" - ops_dashboard: - emea: "https" - na: "" - cn: "" - hubs: - euprod: - tiles: ["actual","graph","ytd"] - naprod: - tiles: ["actual","graph","ytd"] - cnprod: - tiles: ["actual","graph","ytd"] - thresholds: - single_value: "99_#577025|98_#f5d30f|0_#ff0000" - graph_value: "99_#353535|98_#f5d30f|0_#ff0000" \ No newline at end of file