diff --git a/eu/prelive/.terraform-version b/eu/prelive/.terraform-version deleted file mode 100644 index e516bb9..0000000 --- a/eu/prelive/.terraform-version +++ /dev/null @@ -1 +0,0 @@ -1.4.5 diff --git a/eu/prelive/Jenkinsfile b/eu/prelive/Jenkinsfile deleted file mode 100644 index 43beaab..0000000 --- a/eu/prelive/Jenkinsfile +++ /dev/null @@ -1,101 +0,0 @@ -String release = "${env.RELEASE_VERSION ?: "SNAPSHOT"}" -String tag = "modules-aws-$release" - -def modules_paths = [ - "eu/prelive", -] - -def terraform_check(paths) { - paths.each { path -> - sh("terraform -chdir=${path} init -backend=false") - sh("terraform -chdir=${path} fmt -recursive --check") - } -} - - -pipeline { - agent { - node { - label 'vsds-terraform' - } - } - - environment { - GIT_SSH_COMMAND = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" - } - - options { - timestamps() - ansiColor('xterm') - disableConcurrentBuilds(abortPrevious: true) - timeout(time: 1, unit: 'HOURS') - } - - stages { - stage("Check Tag") { - when { expression { !release.equals("SNAPSHOT") } } - steps { - sshagent(credentials: ['sofa-user-automation']) { - script { - env.PLAN_STATUS = sh(script: """ - | if [[ "\$(git ls-remote origin 2>/dev/null | grep 'refs/tags/$tag\$')" ]]; then - | echo "Release $tag exists" - | exit 1 - | fi - """.stripMargin('| '), returnStatus: true) - - if (env.PLAN_STATUS == "1") { - currentBuild.result = "FAILURE" - error('Aborting the build.') - return - } - } - } - } - } - - stage('Terraform validate') { - steps { - container('terraform') { - sshagent(credentials: ['sofa-user-automation']) { - script { - terraform_check(modules_paths) - } - } - } - } - } - - stage('Terraform plan') { - steps { - container('terraform') { - sshagent(credentials: ['sofa-user-automation']) { - sh 'terraform plan' - } - } - } - } - - stage('Create tag') { - when { expression { !release.equals("SNAPSHOT") } } - steps { - sshagent(credentials: ['sofa-user-automation']) { - sh """ - | git tag $tag - | git push origin $tag - """.stripMargin('| ') - } - } - } - } - - post { - always { - cleanWs() - script { - currentBuild.result = currentBuild.result ?: 'SUCCESS' - notifyBitbucket(projectKey: 'modules') - } - } - } -} \ No newline at end of file diff --git a/eu/prelive/backend.tf b/eu/prelive/backend.tf deleted file mode 100644 index 8a13a2a..0000000 --- a/eu/prelive/backend.tf +++ /dev/null @@ -1,10 +0,0 @@ -terraform { - backend "s3" { - bucket = "acdc-eu-west-1-prelive-tfstate" - key = "acdc/dynatrace/bootstrap.tfstate" - region = "eu-west-1" - encrypt = true - kms_key_id = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" #AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, kms-key -# dynamodb_table = "acdc-eu-west-1-prelive-tfstate-lock" - } -} \ No newline at end of file diff --git a/eu/prelive/module.tf b/eu/prelive/module.tf deleted file mode 100644 index ffb778a..0000000 --- a/eu/prelive/module.tf +++ /dev/null @@ -1,12 +0,0 @@ -module "s3-dynatrace" { - source = "git::ssh://git@collaboration.msi.audi.com:4444/vsdsinf/vsds-terraform-modules.git//modules/aws/marketplace/s3?ref=1.4.3" - - name = format("%s-%s-%s-dynatrace", var.stack, var.aws_region, var.stage) - kms_key_arn = var.kms["s3"] - tags = merge( - var.tags, - { - Namespace = "platform" - }, - ) -} diff --git a/eu/prelive/provider.tf b/eu/prelive/provider.tf deleted file mode 100644 index 6844c36..0000000 --- a/eu/prelive/provider.tf +++ /dev/null @@ -1,8 +0,0 @@ -terraform { - required_providers { - dynatrace = { - version = "1.31.0" - source = "dynatrace-oss/dynatrace" - } - } -} \ No newline at end of file diff --git a/modules/aws/Jenkinsfile b/modules/aws/Jenkinsfile deleted file mode 100644 index ac0579c..0000000 --- a/modules/aws/Jenkinsfile +++ /dev/null @@ -1,91 +0,0 @@ -String release = "${env.RELEASE_VERSION ?: "SNAPSHOT"}" -String tag = "modules-aws-$release" - -def modules_paths = [ - "modules/aws/dynatrace", -] - -def terraform_check(paths) { - paths.each { path -> - sh("terraform -chdir=${path} init -backend=false") - sh("terraform -chdir=${path} fmt -recursive --check") - } -} - - -pipeline { - agent { - node { - label 'vsds-terraform' - } - } - - environment { - GIT_SSH_COMMAND = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" - } - - options { - timestamps() - ansiColor('xterm') - disableConcurrentBuilds(abortPrevious: true) - timeout(time: 1, unit: 'HOURS') - } - - stages { - stage("Check Tag") { - when { expression { !release.equals("SNAPSHOT") } } - steps { - sshagent(credentials: ['sofa-user-automation']) { - script { - env.PLAN_STATUS = sh(script: """ - | if [[ "\$(git ls-remote origin 2>/dev/null | grep 'refs/tags/$tag\$')" ]]; then - | echo "Release $tag exists" - | exit 1 - | fi - """.stripMargin('| '), returnStatus: true) - - if (env.PLAN_STATUS == "1") { - currentBuild.result = "FAILURE" - error('Aborting the build.') - return - } - } - } - } - } - - stage('Terraform validate') { - steps { - container('terraform') { - sshagent(credentials: ['sofa-user-automation']) { - script { - terraform_check(modules_paths) - } - } - } - } - } - - stage('Create tag') { - when { expression { !release.equals("SNAPSHOT") } } - steps { - sshagent(credentials: ['sofa-user-automation']) { - sh """ - | git tag $tag - | git push origin $tag - """.stripMargin('| ') - } - } - } - } - - post { - always { - cleanWs() - script { - currentBuild.result = currentBuild.result ?: 'SUCCESS' - notifyBitbucket(projectKey: 'modules') - } - } - } -} \ No newline at end of file diff --git a/modules/aws/dynatrace/variables.tf b/modules/aws/dynatrace/variables.tf index ce02433..76bf10a 100644 --- a/modules/aws/dynatrace/variables.tf +++ b/modules/aws/dynatrace/variables.tf @@ -1,11 +1,11 @@ variable "name" { description = "Name to be used on all the resources as identifier" - type = string + type = string } variable "stage" { description = "Environment stage" - type = string + type = string } variable "region" { diff --git a/packages/Jenkinsfile b/packages/Jenkinsfile new file mode 100644 index 0000000..7e8b243 --- /dev/null +++ b/packages/Jenkinsfile @@ -0,0 +1,159 @@ +// Get the build name using the directory structure in Jenkins +String[] buildElements = env.JOB_NAME.split('/') +String buildName = buildElements[buildElements.length - 2] +String release = "${env.RELEASE_VERSION ?: "SNAPSHOT"}" +String tag = "packages-$release" +String repository = "ssh://git@collaboration.msi.audi.com:4444/vsdsinf/${buildName}.git" + +def modules_paths = [ + "modules/aws/dynatrace", + "packages/eu-west-1/int", + "packages/eu-west-1/prelive", + "packages/us-east-1/prelive", +] + +def terraform_check(paths) { + paths.each { path -> + sh("terraform -chdir=${path} init -backend=false") + sh("terraform -chdir=${path} fmt -recursive --check") + } +} + + +pipeline { + agent { + node { + label 'vsds-terraform' + } + } + + parameters { + choice choices: ['none', 'eu-west-1/int', 'eu-west-1/prelive', 'us-east-1/prelive'], description: 'Please choose the environment for this deployment', name: 'TARGET' + } + + environment { + GIT_SSH_COMMAND = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" + } + + options { + timestamps() + ansiColor('xterm') + disableConcurrentBuilds(abortPrevious: true) + timeout(time: 1, unit: 'HOURS') + } + + stages { + stage("Check Tag") { + when { expression { !release.equals("SNAPSHOT") } } + steps { + sshagent(credentials: ['sofa-user-automation']) { + script { + env.PLAN_STATUS = sh(script: """ + | if [[ "\$(git ls-remote origin 2>/dev/null | grep 'refs/tags/$tag\$')" ]]; then + | echo "Release $tag exists" + | exit 1 + | fi + """.stripMargin('| '), returnStatus: true) + + if (env.PLAN_STATUS == "1") { + currentBuild.result = "FAILURE" + error('Aborting the build.') + return + } + } + } + } + } + stage('Terraform validate') { + steps { + container('terraform') { + sshagent(credentials: ['sofa-user-automation']) { + script { + terraform_check(modules_paths) + } + } + } + } + } + + stage('Terraform plan') { + when { expression { params.TARGET != "none"} } + steps { + container('terraform') { + sshagent(credentials: ['sofa-user-automation']) { + script { + if ( params.TARGET == 'eu-west-1/int' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_INT', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_INT', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} init" + statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String + } + } else if ( params.TARGET == 'eu-west-1/prelive' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} init" + statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String + } + } else if ( params.TARGET == 'us-east-1/prelive' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} init" + statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String + } + } + } + } + } + } + } + + stage('Terraform apply') { + when { beforeInput true; expression { params.TARGET != "none" }; expression { statusCode == "2" } } + input { + message "Applying terraform code ?" + submitter "cicd_ACDC_administrators" + } + steps { + container('terraform') { + sshagent(credentials: ['sofa-user-automation']) { + script { + if ( params.TARGET == 'eu-west-1/int' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_INT', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_INT', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} apply" + } + if ( params.TARGET == 'eu-west-1/prelive' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} apply" + } + } else if ( params.TARGET == 'us-east-1/prelive' ) { + withCredentials([string(credentialsId: 'APIURL_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) { + sh "terraform -chdir=packages/${params.TARGET} apply" + } + } + } + } + } + } + } + } + + stage('Create tag') { + when { expression { !release.equals("SNAPSHOT") } } + steps { + sshagent(credentials: ['sofa-user-automation']) { + sh """ + | git tag $tag + | git push origin $tag + """.stripMargin('| ') + } + } + } + } + + post { + always { + cleanWs() + script { + currentBuild.result = currentBuild.result ?: 'SUCCESS' + notifyBitbucket(projectKey: 'packages') + } + } + } +} \ No newline at end of file diff --git a/packages/eu-west-1/int/backend.tf b/packages/eu-west-1/int/backend.tf new file mode 100644 index 0000000..a0a6b8d --- /dev/null +++ b/packages/eu-west-1/int/backend.tf @@ -0,0 +1,9 @@ +terraform { + backend "s3" { + bucket = "vsds-eu-west-1-prelive-tfstate" + key = "acdc/dynatrace/eu-west-1/int/bootstrap.tfstate" + region = "eu-west-1" + encrypt = true + kms_key_id = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" + } +} \ No newline at end of file diff --git a/packages/eu-west-1/int/module.tf b/packages/eu-west-1/int/module.tf new file mode 100644 index 0000000..1cd621c --- /dev/null +++ b/packages/eu-west-1/int/module.tf @@ -0,0 +1,3 @@ +resource "dynatrace_audit_log" "test" { + enabled = false +} \ No newline at end of file diff --git a/packages/eu-west-1/int/provider.tf b/packages/eu-west-1/int/provider.tf new file mode 100644 index 0000000..7024448 --- /dev/null +++ b/packages/eu-west-1/int/provider.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + dynatrace = { + version = "1.31.0" + source = "dynatrace-oss/dynatrace" + } + aws = { + source = "hashicorp/aws" + version = "5.1.0" + } + } +} + +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/packages/eu-west-1/int/terraform.tfvars b/packages/eu-west-1/int/terraform.tfvars new file mode 100644 index 0000000..be917ec --- /dev/null +++ b/packages/eu-west-1/int/terraform.tfvars @@ -0,0 +1,13 @@ +stack = "acdc" +stage = "int" +name = "dynatrace-int" + +aws_region = "eu-west-1" + +tags = { + "Managed_By" : "NTT_team" +} + +kms = { + "s3" = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" +} \ No newline at end of file diff --git a/eu/prelive/variables.tf b/packages/eu-west-1/int/variables.tf similarity index 94% rename from eu/prelive/variables.tf rename to packages/eu-west-1/int/variables.tf index 9654baa..f7a3863 100644 --- a/eu/prelive/variables.tf +++ b/packages/eu-west-1/int/variables.tf @@ -1,5 +1,3 @@ -# AUTO GENERATED, DON'T MODIFY - variable "aws_region" { description = "The aws region to deploy in" } diff --git a/packages/eu-west-1/prelive/backend.tf b/packages/eu-west-1/prelive/backend.tf new file mode 100644 index 0000000..5a5a1ae --- /dev/null +++ b/packages/eu-west-1/prelive/backend.tf @@ -0,0 +1,9 @@ +terraform { + backend "s3" { + bucket = "vsds-eu-west-1-prelive-tfstate" + key = "acdc/dynatrace/eu-west-1/prelive/bootstrap.tfstate" + region = "eu-west-1" + encrypt = true + kms_key_id = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" + } +} \ No newline at end of file diff --git a/packages/eu-west-1/prelive/module.tf b/packages/eu-west-1/prelive/module.tf new file mode 100644 index 0000000..1cd621c --- /dev/null +++ b/packages/eu-west-1/prelive/module.tf @@ -0,0 +1,3 @@ +resource "dynatrace_audit_log" "test" { + enabled = false +} \ No newline at end of file diff --git a/packages/eu-west-1/prelive/provider.tf b/packages/eu-west-1/prelive/provider.tf new file mode 100644 index 0000000..7024448 --- /dev/null +++ b/packages/eu-west-1/prelive/provider.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + dynatrace = { + version = "1.31.0" + source = "dynatrace-oss/dynatrace" + } + aws = { + source = "hashicorp/aws" + version = "5.1.0" + } + } +} + +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/eu/prelive/terraform.tfvars b/packages/eu-west-1/prelive/terraform.tfvars similarity index 75% rename from eu/prelive/terraform.tfvars rename to packages/eu-west-1/prelive/terraform.tfvars index f1a6d80..34b5a09 100644 --- a/eu/prelive/terraform.tfvars +++ b/packages/eu-west-1/prelive/terraform.tfvars @@ -1,5 +1,3 @@ -# AUTO GENERATED, DON'T MODIFY - stack = "acdc" stage = "prelive" name = "dynatrace-prelive" @@ -7,7 +5,7 @@ name = "dynatrace-prelive" aws_region = "eu-west-1" tags = { - "Managed_By" : "NTT_team" # + "Managed_By" : "NTT_team" } kms = { diff --git a/packages/eu-west-1/prelive/variables.tf b/packages/eu-west-1/prelive/variables.tf new file mode 100644 index 0000000..f7a3863 --- /dev/null +++ b/packages/eu-west-1/prelive/variables.tf @@ -0,0 +1,30 @@ +variable "aws_region" { + description = "The aws region to deploy in" +} + +variable "name" { + description = "Name to be used on all the resources as identifier" + type = string + default = "" +} + +variable "stack" { + description = "Environment stack" + default = "vsds" +} + +variable "stage" { + description = "Environment stage" +} + +variable "tags" { + description = "A map of tags to add to all resources" + type = map(string) + default = {} +} + +variable "kms" { + description = "A map of kms keys to be used for any resources." + type = map(string) + default = {} +} \ No newline at end of file diff --git a/packages/us-east-1/prelive/backend.tf b/packages/us-east-1/prelive/backend.tf new file mode 100644 index 0000000..2a6f73b --- /dev/null +++ b/packages/us-east-1/prelive/backend.tf @@ -0,0 +1,9 @@ +terraform { + backend "s3" { + bucket = "vsds-eu-west-1-prelive-tfstate" + key = "acdc/dynatrace/us-east-1/prelive/bootstrap.tfstate" + region = "eu-west-1" + encrypt = true + kms_key_id = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" + } +} \ No newline at end of file diff --git a/packages/us-east-1/prelive/module.tf b/packages/us-east-1/prelive/module.tf new file mode 100644 index 0000000..1cd621c --- /dev/null +++ b/packages/us-east-1/prelive/module.tf @@ -0,0 +1,3 @@ +resource "dynatrace_audit_log" "test" { + enabled = false +} \ No newline at end of file diff --git a/packages/us-east-1/prelive/provider.tf b/packages/us-east-1/prelive/provider.tf new file mode 100644 index 0000000..7024448 --- /dev/null +++ b/packages/us-east-1/prelive/provider.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + dynatrace = { + version = "1.31.0" + source = "dynatrace-oss/dynatrace" + } + aws = { + source = "hashicorp/aws" + version = "5.1.0" + } + } +} + +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/packages/us-east-1/prelive/terraform.tfvars b/packages/us-east-1/prelive/terraform.tfvars new file mode 100644 index 0000000..42bd232 --- /dev/null +++ b/packages/us-east-1/prelive/terraform.tfvars @@ -0,0 +1,13 @@ +stack = "acdc" +stage = "prelive" +name = "dynatrace-prelive" + +aws_region = "us-east-1" + +tags = { + "Managed_By" : "NTT_team" +} + +kms = { + "s3" = "arn:aws:kms:eu-west-1:248567303878:key/8d860956-4033-4782-ab30-6cb84b31964e" +} \ No newline at end of file diff --git a/packages/us-east-1/prelive/variables.tf b/packages/us-east-1/prelive/variables.tf new file mode 100644 index 0000000..f7a3863 --- /dev/null +++ b/packages/us-east-1/prelive/variables.tf @@ -0,0 +1,30 @@ +variable "aws_region" { + description = "The aws region to deploy in" +} + +variable "name" { + description = "Name to be used on all the resources as identifier" + type = string + default = "" +} + +variable "stack" { + description = "Environment stack" + default = "vsds" +} + +variable "stage" { + description = "Environment stage" +} + +variable "tags" { + description = "A map of tags to add to all resources" + type = map(string) + default = {} +} + +variable "kms" { + description = "A map of kms keys to be used for any resources." + type = map(string) + default = {} +} \ No newline at end of file