From f313af38467fcacb73cd7c8abc4324109b4e00fb Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Sun, 1 Jan 2023 16:03:19 +0100 Subject: [PATCH 1/5] Adding script to template --- onboarding.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 37 +++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 3 files changed, 85 insertions(+) create mode 100644 onboarding.py create mode 100644 readme.md create mode 100644 requirements.txt diff --git a/onboarding.py b/onboarding.py new file mode 100644 index 00000000..9ea9e2d9 --- /dev/null +++ b/onboarding.py @@ -0,0 +1,45 @@ +import yaml +from decouple import config +import argparse +import os +parser = argparse.ArgumentParser(description="Generate predefined Dynatrace configuration via Terraform", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + +parser.add_argument("-A", "--appname", type=str,required=True, help="Your app_name") +parser.add_argument('-C', "--compassid", type=str,required=True, help="Your compass_id") +args = parser.parse_args() + +#Config options for onboarding. Will be extended with other configuration options in future +configoptions = ["management_zone"] +envconfigoptions = ["alerting","dashboard"] +def main(slo_path): + print("Generating tf files...") + if args.appname and args.compassid: + + with open('./environment.yaml') as file: + doc = yaml.safe_load(file) + + for item, doc in doc.items(): + envs = dict(doc[1]) + for configoption in configoptions: + with open('./_templates/_template_'+configoption+'.tf') as file: + data = file.read() + data = data.replace("", args.appname) + data = data.replace("", args.compassid.lower()) + with open('./'+item+'/'+configoption+'/'+args.appname+'.tf', 'w') as file: + file.write(data) + for envconfigoption in envconfigoptions: + for env in envs: + with open('./_templates/_template_'+envconfigoption+'.tf') as file: + data = file.read() + data = data.replace("", args.appname) + data = data.replace("", args.compassid.lower()) + data = data.replace("", env) + with open('./'+item+'/'+envconfigoption+'/'+args.appname+'_'+env+'.tf', 'w') as file: + file.write(data) + else: + print("ERROR: No appname/compassid specified") + + +if __name__ == "__main__": + main('') \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 00000000..f15c0304 --- /dev/null +++ b/readme.md @@ -0,0 +1,37 @@ +# Dynatrace onboarding repo / Terreform config +This repository holds the Dynatrace configuration for onboarding purposes + +# Prerequisites + +## Python packages +Before executing scripts, python requirements have to be satisfied. To do so, execute following command: + pip install -r requirements.txt + +# Usage + + 1. Clone template: + git clone --branch template ssh://git@git.bmwgroup.net:7999/opapm/coco_apm_terraform_onboarding.git + 2. Create branch: + git checkout -b CD_ + 3.Install python dependencies: + pip install -r requirements.txt + 4.Run onboarding script: + python ./onboarding.py -A CD_ -C + 5. Review created files: + git status + 6. Commit your changes: + git add + + git commit -m " - Add initial configuration of application to Dynatrace" + + git push -u origin CD_ + 7. Create pull request from CD_ to master +# Files + +## onboarding.py + +This scripts generates predefined configuration basing on your app_name and compass ids + +## requirements.txt + +File containing required python packages diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..06c938c3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-decouple +pyyaml +argparse \ No newline at end of file From 052997403511261193987ac198f85316ecf5c486 Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Sun, 1 Jan 2023 16:27:39 +0100 Subject: [PATCH 2/5] Adjusting script and file names --- ...ate_alerting_profile.tf => _template_alerting.tf} | 0 environment.yaml | 12 ++++++++++++ environments.yaml | 0 onboarding.py | 6 ++++-- 4 files changed, 16 insertions(+), 2 deletions(-) rename _templates/{_template_alerting_profile.tf => _template_alerting.tf} (100%) create mode 100644 environment.yaml delete mode 100644 environments.yaml diff --git a/_templates/_template_alerting_profile.tf b/_templates/_template_alerting.tf similarity index 100% rename from _templates/_template_alerting_profile.tf rename to _templates/_template_alerting.tf diff --git a/environment.yaml b/environment.yaml new file mode 100644 index 00000000..7ffc9390 --- /dev/null +++ b/environment.yaml @@ -0,0 +1,12 @@ +EMEA_PROD: + - environments: [E2E,PROD] +EMEA_PREPROD: + - environments: [TEST,INT] +NA_PROD: + - environments: [E2E,PROD] +NA_PREPROD: + - environments: [TEST,INT] +CN_PROD: + - environments: [E2E,PROD] +CN_PREPROD: + - environments: [TEST,INT] \ No newline at end of file diff --git a/environments.yaml b/environments.yaml deleted file mode 100644 index e69de29b..00000000 diff --git a/onboarding.py b/onboarding.py index 9ea9e2d9..9aeed045 100644 --- a/onboarding.py +++ b/onboarding.py @@ -20,21 +20,23 @@ def main(slo_path): doc = yaml.safe_load(file) for item, doc in doc.items(): - envs = dict(doc[1]) + envs = dict(doc[0]) for configoption in configoptions: with open('./_templates/_template_'+configoption+'.tf') as file: data = file.read() data = data.replace("", args.appname) data = data.replace("", args.compassid.lower()) + #print('./'+item+'/'+configoption+'/'+args.appname+'.tf') with open('./'+item+'/'+configoption+'/'+args.appname+'.tf', 'w') as file: file.write(data) for envconfigoption in envconfigoptions: - for env in envs: + for env in envs.get("environments"): with open('./_templates/_template_'+envconfigoption+'.tf') as file: data = file.read() data = data.replace("", args.appname) data = data.replace("", args.compassid.lower()) data = data.replace("", env) + #print('./'+item+'/'+envconfigoption+'/'+args.appname+'_'+env+'.tf') with open('./'+item+'/'+envconfigoption+'/'+args.appname+'_'+env+'.tf', 'w') as file: file.write(data) else: From 0cfe6ea59d4bd4ffb25ab9eb8f4cd7eb9f96466a Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Sun, 1 Jan 2023 16:43:30 +0100 Subject: [PATCH 3/5] Adjusting readme file --- readme.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index f15c0304..b53d03a7 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ This repository holds the Dynatrace configuration for onboarding purposes Before executing scripts, python requirements have to be satisfied. To do so, execute following command: pip install -r requirements.txt -# Usage +# Instructions 1. Clone template: git clone --branch template ssh://git@git.bmwgroup.net:7999/opapm/coco_apm_terraform_onboarding.git @@ -26,6 +26,26 @@ Before executing scripts, python requirements have to be satisfied. To do so, ex git push -u origin CD_ 7. Create pull request from CD_ to master + +# Adjusting tf files + +For more information about adjusting tf files to your need please follow: +1. Management zones: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/management_zone +2. Alerting profiles: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/alerting +3. Dashboards: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/dashboard + +# Usage + usage: onboarding.py [-h] -A APPNAME -C COMPASSID + + Generate predefined Dynatrace configuration via Terraform + + options: + -h, --help show this help message and exit + -A APPNAME, --appname APPNAME + Your app_name (default: None) + -C COMPASSID, --compassid COMPASSID + Your compass_id (default: None) + # Files ## onboarding.py From 12c44f185c2d62f6d215ec794893e21296e2e32b Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Mon, 9 Jan 2023 11:50:07 +0100 Subject: [PATCH 4/5] Extending onboarding features --- CN_PREPROD/custom_service/.gitkeep | 0 CN_PREPROD/processgroup_naming/.gitkeep | 0 CN_PREPROD/processgroup_naming/.gitkeep copy | 0 CN_PREPROD/service_naming/.gitkeep | 0 CN_PROD/custom_service/.gitkeep | 0 CN_PROD/processgroup_naming/.gitkeep | 0 CN_PROD/processgroup_naming/.gitkeep copy | 0 CN_PROD/service_naming/.gitkeep | 0 EMEA_PREPROD/custom_service/.gitkeep | 0 EMEA_PREPROD/processgroup_naming/.gitkeep | 0 .../processgroup_naming/.gitkeep copy | 0 EMEA_PREPROD/service_naming/.gitkeep | 0 EMEA_PROD/custom_service/.gitkeep | 0 EMEA_PROD/processgroup_naming/.gitkeep | 0 EMEA_PROD/processgroup_naming/.gitkeep copy | 0 EMEA_PROD/service_naming/.gitkeep | 0 NA_PREPROD/custom_service/.gitkeep | 0 NA_PREPROD/processgroup_naming/.gitkeep | 0 NA_PREPROD/processgroup_naming/.gitkeep copy | 0 NA_PREPROD/service_naming/.gitkeep | 0 NA_PROD/custom_service/.gitkeep | 0 NA_PROD/processgroup_naming/.gitkeep | 0 NA_PROD/processgroup_naming/.gitkeep copy | 0 NA_PROD/service_naming/.gitkeep | 0 _templates/_template_custom_service.tf | 25 ++++++++++++++++ _templates/_template_processgroup_naming.tf | 23 +++++++++++++++ _templates/_template_service_naming.tf | 23 +++++++++++++++ onboarding.py | 6 +++- readme.md | 29 ++++++++++++++----- 29 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 CN_PREPROD/custom_service/.gitkeep create mode 100644 CN_PREPROD/processgroup_naming/.gitkeep create mode 100644 CN_PREPROD/processgroup_naming/.gitkeep copy create mode 100644 CN_PREPROD/service_naming/.gitkeep create mode 100644 CN_PROD/custom_service/.gitkeep create mode 100644 CN_PROD/processgroup_naming/.gitkeep create mode 100644 CN_PROD/processgroup_naming/.gitkeep copy create mode 100644 CN_PROD/service_naming/.gitkeep create mode 100644 EMEA_PREPROD/custom_service/.gitkeep create mode 100644 EMEA_PREPROD/processgroup_naming/.gitkeep create mode 100644 EMEA_PREPROD/processgroup_naming/.gitkeep copy create mode 100644 EMEA_PREPROD/service_naming/.gitkeep create mode 100644 EMEA_PROD/custom_service/.gitkeep create mode 100644 EMEA_PROD/processgroup_naming/.gitkeep create mode 100644 EMEA_PROD/processgroup_naming/.gitkeep copy create mode 100644 EMEA_PROD/service_naming/.gitkeep create mode 100644 NA_PREPROD/custom_service/.gitkeep create mode 100644 NA_PREPROD/processgroup_naming/.gitkeep create mode 100644 NA_PREPROD/processgroup_naming/.gitkeep copy create mode 100644 NA_PREPROD/service_naming/.gitkeep create mode 100644 NA_PROD/custom_service/.gitkeep create mode 100644 NA_PROD/processgroup_naming/.gitkeep create mode 100644 NA_PROD/processgroup_naming/.gitkeep copy create mode 100644 NA_PROD/service_naming/.gitkeep create mode 100644 _templates/_template_custom_service.tf create mode 100644 _templates/_template_processgroup_naming.tf create mode 100644 _templates/_template_service_naming.tf diff --git a/CN_PREPROD/custom_service/.gitkeep b/CN_PREPROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/CN_PREPROD/processgroup_naming/.gitkeep b/CN_PREPROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/CN_PREPROD/processgroup_naming/.gitkeep copy b/CN_PREPROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/CN_PREPROD/service_naming/.gitkeep b/CN_PREPROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/CN_PROD/custom_service/.gitkeep b/CN_PROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/CN_PROD/processgroup_naming/.gitkeep b/CN_PROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/CN_PROD/processgroup_naming/.gitkeep copy b/CN_PROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/CN_PROD/service_naming/.gitkeep b/CN_PROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PREPROD/custom_service/.gitkeep b/EMEA_PREPROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PREPROD/processgroup_naming/.gitkeep b/EMEA_PREPROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PREPROD/processgroup_naming/.gitkeep copy b/EMEA_PREPROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PREPROD/service_naming/.gitkeep b/EMEA_PREPROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PROD/custom_service/.gitkeep b/EMEA_PROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PROD/processgroup_naming/.gitkeep b/EMEA_PROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PROD/processgroup_naming/.gitkeep copy b/EMEA_PROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/EMEA_PROD/service_naming/.gitkeep b/EMEA_PROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PREPROD/custom_service/.gitkeep b/NA_PREPROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PREPROD/processgroup_naming/.gitkeep b/NA_PREPROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PREPROD/processgroup_naming/.gitkeep copy b/NA_PREPROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/NA_PREPROD/service_naming/.gitkeep b/NA_PREPROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PROD/custom_service/.gitkeep b/NA_PROD/custom_service/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PROD/processgroup_naming/.gitkeep b/NA_PROD/processgroup_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/NA_PROD/processgroup_naming/.gitkeep copy b/NA_PROD/processgroup_naming/.gitkeep copy new file mode 100644 index 00000000..e69de29b diff --git a/NA_PROD/service_naming/.gitkeep b/NA_PROD/service_naming/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/_templates/_template_custom_service.tf b/_templates/_template_custom_service.tf new file mode 100644 index 00000000..173cb5e6 --- /dev/null +++ b/_templates/_template_custom_service.tf @@ -0,0 +1,25 @@ +resource "dynatrace_custom_service" "" { + name = "" + enabled = true + # queue_entry_point = false + #TO BE FILLED - technology - possible options: java,dotNet + technology = "" + rule { + enabled = true + class { + #TO BE FILLED - classname (FQN) + name = "" + match = "EQUALS" + } + method { + #TO BE FILLED - methodname (FQN) + name = "[methodname]" + #TO BE FILLED - argument type list (FQN) + arguments = [] + #TO BE FILLED - return type (FQN or void) + returns = "" + #TO BE FILLED - visibility (private,public) + visibility = "" + } + } +} diff --git a/_templates/_template_processgroup_naming.tf b/_templates/_template_processgroup_naming.tf new file mode 100644 index 00000000..48a2eea3 --- /dev/null +++ b/_templates/_template_processgroup_naming.tf @@ -0,0 +1,23 @@ +resource "dynatrace_processgroup_naming" "" { + name = "" + enabled = true + format = "{ProcessGroup:DetectedName} - {HostGroup:Name/[^\\_]*$}" + conditions { + condition { + key { + type = "STATIC" + attribute = "PROCESS_GROUP_TAGS" + } + tag { + # negate = false + operator = "EQUALS" + value { + context = "CONTEXTLESS" + key = "Component" + #TO BE FILLED - Component tag value + value = "" + } + } + } + } +} \ No newline at end of file diff --git a/_templates/_template_service_naming.tf b/_templates/_template_service_naming.tf new file mode 100644 index 00000000..c6a99501 --- /dev/null +++ b/_templates/_template_service_naming.tf @@ -0,0 +1,23 @@ +resource "dynatrace_service_naming" "" { + name = "" + enabled = true + format = "{Service:DetectedName} - {HostGroup:Name/[^\\_]*$}" + conditions { + condition { + key { + type = "STATIC" + attribute = "SERVICE_TAGS" + } + tag { + # negate = false + operator = "EQUALS" + value { + context = "CONTEXTLESS" + key = "Component" + #TO BE FILLED - Component tag value + value = "" + } + } + } + } +} diff --git a/onboarding.py b/onboarding.py index 9aeed045..e6d019c1 100644 --- a/onboarding.py +++ b/onboarding.py @@ -2,16 +2,20 @@ import yaml from decouple import config import argparse import os -parser = argparse.ArgumentParser(description="Generate predefined Dynatrace configuration via Terraform", +parser = argparse.ArgumentParser(description="Generate predefined Dynatrace configuration via Terraform. Default features are Management zone, Alerting profile and predefined dashboard. For more see options below", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-A", "--appname", type=str,required=True, help="Your app_name") parser.add_argument('-C', "--compassid", type=str,required=True, help="Your compass_id") +parser.add_argument('-F', "--features", type=str,required=True, help="Comma separated additional features. Possible options are: custom_service, processgroup_naming, service_naming") args = parser.parse_args() #Config options for onboarding. Will be extended with other configuration options in future configoptions = ["management_zone"] envconfigoptions = ["alerting","dashboard"] +if(args.features): + features = args.features.split(",") + configoptions = configoptions + features def main(slo_path): print("Generating tf files...") if args.appname and args.compassid: diff --git a/readme.md b/readme.md index b53d03a7..a23ed5fd 100644 --- a/readme.md +++ b/readme.md @@ -16,9 +16,14 @@ Before executing scripts, python requirements have to be satisfied. To do so, ex 3.Install python dependencies: pip install -r requirements.txt 4.Run onboarding script: - python ./onboarding.py -A CD_ -C + 4.1 Default implementation (Management zone, Alerting profile and predefined dashboard) + python ./onboarding.py -A CD_ -C + 4.2 Additional features (see Usage section): + python ./onboarding.py -A CD_ -C -F 5. Review created files: git status + + 5.1 Adjust additional feature files (if generated with "-F" option). See "Adjusting tf files" section 6. Commit your changes: git add @@ -33,18 +38,26 @@ For more information about adjusting tf files to your need please follow: 1. Management zones: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/management_zone 2. Alerting profiles: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/alerting 3. Dashboards: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/dashboard +4. Custom service: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/custom_service +5. Notification: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/notification +6. Process group naming: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/processgroup_naming +7. Service naming: https://registry.terraform.io/providers/dynatrace-oss/dynatrace/latest/docs/resources/service_naming # Usage - usage: onboarding.py [-h] -A APPNAME -C COMPASSID + usage: onboarding.py [-h] -A APPNAME -C COMPASSID -F FEATURES - Generate predefined Dynatrace configuration via Terraform + Generate predefined Dynatrace configuration via Terraform. Default features are Management zone, Alerting profile and predefined dashboard. For more see + options below options: - -h, --help show this help message and exit - -A APPNAME, --appname APPNAME - Your app_name (default: None) - -C COMPASSID, --compassid COMPASSID - Your compass_id (default: None) + -h, --help show this help message and exit + -A APPNAME, --appname APPNAME + Your app_name (default: None) + -C COMPASSID, --compassid COMPASSID + Your compass_id (default: None) + -F FEATURES, --features FEATURES + Comma separated additional features. Possible options are: custom_service, notification, processgroup_naming, service_naming (default: + None) # Files From c1d84387808b7d20a89225b721c880eeaea1bfe9 Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Mon, 9 Jan 2023 12:29:59 +0100 Subject: [PATCH 5/5] Removing duplicates --- CN_PREPROD/processgroup_naming/.gitkeep copy | 0 CN_PROD/processgroup_naming/.gitkeep copy | 0 EMEA_PREPROD/processgroup_naming/.gitkeep copy | 0 EMEA_PROD/processgroup_naming/.gitkeep copy | 0 NA_PREPROD/processgroup_naming/.gitkeep copy | 0 NA_PROD/processgroup_naming/.gitkeep copy | 0 6 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 CN_PREPROD/processgroup_naming/.gitkeep copy delete mode 100644 CN_PROD/processgroup_naming/.gitkeep copy delete mode 100644 EMEA_PREPROD/processgroup_naming/.gitkeep copy delete mode 100644 EMEA_PROD/processgroup_naming/.gitkeep copy delete mode 100644 NA_PREPROD/processgroup_naming/.gitkeep copy delete mode 100644 NA_PROD/processgroup_naming/.gitkeep copy diff --git a/CN_PREPROD/processgroup_naming/.gitkeep copy b/CN_PREPROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000 diff --git a/CN_PROD/processgroup_naming/.gitkeep copy b/CN_PROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000 diff --git a/EMEA_PREPROD/processgroup_naming/.gitkeep copy b/EMEA_PREPROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000 diff --git a/EMEA_PROD/processgroup_naming/.gitkeep copy b/EMEA_PROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000 diff --git a/NA_PREPROD/processgroup_naming/.gitkeep copy b/NA_PREPROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000 diff --git a/NA_PROD/processgroup_naming/.gitkeep copy b/NA_PROD/processgroup_naming/.gitkeep copy deleted file mode 100644 index e69de29b..00000000