Compare commits

...

10 Commits

Author SHA1 Message Date
SLW\ARNAUA a77f2e2821 OPMAAS-4466 - Change recipient for reports 2023-06-21 16:25:19 +02:00
SLW\ARNAUA 4c419c4512 OPMAAS-4466 - Change recipient for reports 2023-06-21 16:23:19 +02:00
SLW\ARNAUA 2982c77f2c modified environment file 2023-06-06 15:11:06 +02:00
SLW\ARNAUA 7ad1e4ac64 updating documentation 2023-06-06 15:02:31 +02:00
SLW\ARNAUA 663f8184e9 updating documentation 2023-06-06 14:56:47 +02:00
SLW\ARNAUA 3813fd4035 updating documentation 2023-06-06 14:54:50 +02:00
SLW\ARNAUA bcd119cf5a updating documentation 2023-06-06 14:52:30 +02:00
SLW\ARNAUA 83e8bec4fd updating documentation 2023-06-06 14:51:37 +02:00
SLW\ARNAUA 3c087ae694 finalized and added documentation 2023-06-06 14:48:16 +02:00
SLW\ARNAUA cfd119a437 added readme file 2023-06-05 17:26:17 +02:00
5 changed files with 156 additions and 21 deletions

21
Jenkinsfile vendored
View File

@ -37,7 +37,9 @@ pipeline {
script {
checkRetryCount = checkRetryCount + 1
}
git branch: "master", credentialsId: "jaws_dynatrace_bitbuket_user", url: "https://atc.bmwgroup.net/bitbucket/scm/opapm/coco_apm_dynatrace_terraform_backup.git"
git branch: "master",
credentialsId: "jaws_dynatrace_bitbuket_user",
url: "https://atc.bmwgroup.net/bitbucket/scm/opapm/coco_apm_dynatrace_terraform_backup.git"
}
}
dir("/opt/workspace/coco_apm_dynatrace_terraform_backup_exporter/") {
@ -46,7 +48,9 @@ pipeline {
script {
checkRetryCountSec = checkRetryCountSec + 1
}
git branch: "master", credentialsId: "jaws_dynatrace_bitbuket_user", url: "https://atc.bmwgroup.net/bitbucket/scm/opapm/coco_apm_dynatrace_terraform_backup_exporter.git"
git branch: "master",
credentialsId: "jaws_dynatrace_bitbuket_user",
url: "https://atc.bmwgroup.net/bitbucket/scm/opapm/coco_apm_dynatrace_terraform_backup_exporter.git"
}
}
}
@ -133,18 +137,17 @@ pipeline {
// script {
// dir("/opt/workspace/qm_report/") {
// try {
// emailext subject: "NEW ${env.JOB_NAME}",
// emailext subject: "BackUp ${env.JOB_NAME}",
// mimeType: 'text/html',
// body: "Please find the output of the daily QM-Report attached </br></br>${summary}",
// body: "BackUp successful!",
// // to: 'michaela.jaeger@bmw.de, OOC-Support@bmwgroup.com, Andreas.DA.Danzer@partner.bmw.de',
// // to: 'rene.forstner@nttdata.com, Andreas.DA.Danzer@partner.bmw.de, linnea.bickeboeller@partner.bmwgroup.com',
// // to: 'rene.forstner@nttdata.com, stephan.oertelt@bmw.de, Mohammed.Abadel@bmw.de, michaela.jaeger@bmw.de',
// // to: 'rene.forstner@nttdata.com, ermis.wieger@nttdata.com, patryk.gudalewicz.bp@nttdata.com',
// // to: 'patryk.gudalewicz.bp@nttdata.com',
// // to: 'rene.forstner@nttdata.com, ermis.wieger@nttdata.com, arnel.arnautovic@nttdata.com, patryk.gudalewicz.bp@nttdata.com, stephan.oertelt@bmw.de, Mohammed.Abadel@bmw.de, michaela.jaeger@bmw.de, OOC-Support@bmwgroup.com, Andreas.DB.Danzer@bmwgroup.com',
// to: 'Andreas.DB.Danzer@bmwgroup.com, Arnel.Arnautovic@nttdata.com, Ermis.Wieger@nttdata.com',
// replyTo: 'coco-apm@bmw.de',
// attachmentsPattern: '*.xlsx'
// to: 'Andreas.DB.Danzer@bmwgroup.com, Arnel.Arnautovic@nttdata.com, Ermis.Wieger@nttdata.com, ops-xibix@list.bmw.com, omo-xibix@list.bmw.com, omo@bmwgroup.com',
// replyTo: 'coco-apm@bmw.de'
// } catch ( mailExc ) {
// echo "Sending Email Failed: ${mailExc}"
// }
@ -157,8 +160,8 @@ pipeline {
post {
failure {
emailext subject: "${env.JOB_NAME} build ${env.BUILD_ID} failed",
body: "QM report failed, see logs for details: ${env.BUILD_URL}",
to: 'Arnel.Arnautovic@nttdata.com'
body: "BackUp failed, see logs for details: ${env.BUILD_URL}",
to: 'Arnel.Arnautovic@nttdata.com, ops-xibix@list.bmw.com, omo-xibix@list.bmw.com, omo@bmwgroup.com'
// to post to the teams channel "0 - APM Service Desk" just uncomment the following command:
// office365ConnectorSend webhookUrl: "https://bmwgroup.webhook.office.com/webhookb2/483edc00-c925-4672-8088-8299a0139fca@ce849bab-cc1c-465b-b62e-18f07c9ac198/JenkinsCI/9aca6923685b40f794134853fcbe88f1/ff31bcee-96b3-4481-9bd8-4f74180b263b",
// message: "QM report failed, see logs for details: ${env.BUILD_URL}",

126
README.md Normal file
View File

@ -0,0 +1,126 @@
# CoCo APM Dynatrace Terraform BackUp Exporter
The main purpose of the coco_apm_dynatrace_terraform_backup_exporter is to pull all the available configuration files from Dynatrace to back them up.
## Environment Variables
To run this project, you will need to add an .env file with the following environment variables:
`EUPROD_TOKEN_VAR`
`EUPREPROD_TOKEN_VAR`
`NAPROD_TOKEN_VAR`
`NAPREPROD_TOKEN_VAR`
`CNROD_TOKEN_VAR`
`CNPREPROD_TOKEN_VAR`
## Installation
In order to run the program on the local machine, it is necessary to install the packages that are listed within the requirements.txt file by:
```bash
pip3 install --user -r requirements.txt
```
### Terraform Provider for Dynatrace
It is also required to install the Terraform Provider for [Dynatrace](https://github.com/dynatrace-oss/terraform-provider-dynatrace/releases). Once you download and extract a selected version of the provider, please make sure to add the `directory` of the extracted `executable` as well as the filename of it in `tf-provider-config.yaml`.
### Additional configurations
Note: Additionally you must configure the environment url `env-url` and output directory `output-dir` within the `env-config.yaml` file in the future if your environment url changes or if you prefer a different output directory.
## Run Locally
In order to make a backup of all the available enviornments that are configured within the `configuration.yaml` file you can execute the script with the following command:
```bash
python3 .\export
```
## Documentation
The Documentation briefly describes the currently set up backup process and a scenario for restoration.
### BackUp Process
The export script is executed every day at 5 AM by [Jenkins](https://jaws.bmwgroup.net/opapm/job/coco_apm_dynatrace_terraform_backup_exporter/). Jenkins pushes the backup to the [coco_apm_dynatrace_terraform_backup](https://atc.bmwgroup.net/bitbucket/projects/OPAPM/repos/coco_apm_dynatrace_terraform_backup/browse) by creating a branch with a timestamp. Each branch contains the dashboards and all other configurations stored both separately. The reason for this is due to the Terraform Dynatrace Provider - in the [GitHub documentation](https://atc.bmwgroup.net/bitbucket/projects/OPAPM/repos/coco_apm_dynatrace_terraform_backup/browse) it is stated that dashboards need to be exported separately.
Additonally, Jenkins downloads the Terraform Dynatrace Provider version [1.30.1](https://github.com/dynatrace-oss/terraform-provider-dynatrace/releases/tag/v1.30.1), however you can configure this within the Jenkinsfile by adjusting the global parameters `bin`, `file`and `link`.
Note: It might be possible that you could encounter issues when upgrading to a higher version due to compability issues. Here is an example:
```
Warning: Provider source not supported in Terraform v0.12
on ___providers___.tf line 3, in terraform:
3: dynatrace = {
4: source = "dynatrace-oss/dynatrace"
5: version = "1.30.2"
6: }
A source was declared for provider dynatrace. Terraform v0.12 does not support
the provider source attribute. It will be ignored.
Error: no provider exists with the given name
```
Explanation:
- (A) The error indicates that Terraform v0.12 does not support the downloaded Terraform Dynatrace Provider version 1.30.2. Therefore it was necessary to downgrade to 1.30.1!
- (B) It could also mean a misconfiguration of the environment within `env-conig.yaml`. Example: Wrong token or environment url, therefore the environment cannot be found and the warning alongside the error are thrown. As a result no configuration files as well as dashboards could be fetched, resulting in an empty backup directory.
### Restore Process
If you are planning to restore the backed up configuration files or dashboards, then you will need to install [Terraform](https://developer.hashicorp.com/terraform/downloads) on your local machine. Do not forget to add the binary file to your system environment path.
#### Usage/Examples for Restoration
Scenario: A dashboard tile has been changed or accidently deleted. How can this be solved?
First you will need to know when the incident happened. After downloading the backup repository you will need to checkout the branch with the day before the accident happened. Then, you will need to configure the `___providers___.tf` **file which resides is in the same direcory as the** `main.tf` file:
```
terraform {
required_providers {
dynatrace = {
source = "dynatrace-oss/dynatrace"
version = "1.30.1"
}
}
}
provider "dynatrace" {
dt_env_url = "<tenant-url>"
dt_api_token = "<your-token>"
}
```
You need to add the url of the tenant and also the associated token! After that has been done you need to initialize it by the following command:
```bash
terraform init
```
Once the backend has been initalized, all you need to do now is to first import the state of the affected dashboard as in the following example:
```bash
terraform import module.dashboard.dynatrace_dashboard.CD_VDLM_XXXXX 1123jk2-1234ud
```
Basically the above expression indicates that the current state of the affected dashboard will be genearted (state file) within the following syntax:
```
terraform import module.<module_name>.<resource_type>.<resource_name> ADDRESS_ID
```
More about the Syntax can be read here [Resource Addressing](https://developer.hashicorp.com/terraform/cli/state/resource-addressing) and here [Command: import](https://developer.hashicorp.com/terraform/cli/commands/import).
Once the state is imported you will have to apply the changes of the backed up file by using the following command:
```bash
terraform apply -target module.dashboard.dynatrace_dashboard.CD_VDLM_XXXXX
```
The above command indicates that the backed up configuration will be applied. With that the changes of the affected dashboard will be reverted back to a state before the accident.
Note in the above syntax that `apply` is used with the option `-target` in order to specifiy the resource name. Hence the ADDRESS_ID is omitted since the backup process fetches the ids and adds them as a comment in the first line of each configuration file! Also after the command is executed it will prompt you to type "yes" in order to execute the command. In [Resource Targeting](https://developer.hashicorp.com/terraform/cli/commands/apply) you can read more about it.

View File

@ -13,8 +13,12 @@ import yaml
def setEnv(doc, timestamp, configuration):
os.environ['DYNATRACE_ENV_URL'] = dict(doc[1]).get("env-url")
os.environ['DYNATRACE_API_TOKEN'] = config(dict(doc[2]).get("env-token-name"))
os.environ['DYNATRACE_TARGET_FOLDER'] = str(dict(doc[4]).get("output-dir") + timestamp + "_" + dict(doc[0]).get("name") + configuration)
os.environ['DYNATRACE_API_TOKEN'] = config(dict(doc[2])
.get("env-token-name"))
os.environ['DYNATRACE_TARGET_FOLDER'] = str(dict(doc[4]).get("output-dir")
+ timestamp + "_"
+ dict(doc[0]).get("name")
+ configuration)
return os.environ
@ -45,18 +49,17 @@ def runProcess(process_name, input_params):
if __name__ == "__main__":
if(len(sys.argv) == 1):
# dir = "C:/arnaua/repository/bmw/coco_apm_dynatrace_terraform_backup_exporter/bin"
dir = "/opt/workspace/coco_apm_dynatrace_terraform_backup_exporter"
# file = "terraform-provider-dynatrace_v1.30.1.exe"
file = "terraform-provider-dynatrace_v1.30.1"
bin = os.path.join(dir, file)
with open(os.path.basename("./tf-provider-config.yaml")) as tf_cfg:
tf_config = yaml.safe_load(tf_cfg)
bin = os.path.join(str(tf_config.get('directory')),
str(tf_config.get('executable')))
timestamp = time.strftime("%Y%m%d-%H%M%S")
environments = ["euprod", "eupreprod", "naprod", "napreprod", "cnprod", "cnpreprod"]
with open(os.path.basename("./environment.yaml")) as file:
env_doc = yaml.safe_load(file)
with open(os.path.basename("./env-config.yaml")) as env_cfg:
env_config = yaml.safe_load(env_cfg)
for item, doc in env_doc.items():
for item, doc in env_config.items():
setEnv(doc, timestamp, "_configurations")
runProcess("Export", [bin, "-export", "-id"])
setEnv(doc, timestamp, "_dashboards")
@ -65,4 +68,4 @@ if __name__ == "__main__":
print("Finished!")
else:
print("Usage example: ")
print("python .\export.py")
print("python3 .\export.py")

3
tf-provider-config.yaml Normal file
View File

@ -0,0 +1,3 @@
---
directory: "/opt/workspace/coco_apm_dynatrace_terraform_backup_exporter"
executable: "terraform-provider-dynatrace_v1.30.1"