From 00702c1d25216112f42482d70b1d8e78b1126942 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Thu, 27 Feb 2025 12:33:49 +0300 Subject: [PATCH] [DO-1617] add 2 pod templates and add Terraform class (!64) Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/64 Reviewed-by: Vasiliy Chipizhin Reviewed-by: Aleksandr Vodyanov Reviewed-by: Denis Patrakeev --- src/tech/avroid/kube/PodTemplates.groovy | 68 ++++++++++++++++++++++ src/tech/avroid/terraform/Terraform.groovy | 49 ++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/tech/avroid/terraform/Terraform.groovy diff --git a/src/tech/avroid/kube/PodTemplates.groovy b/src/tech/avroid/kube/PodTemplates.groovy index 7a67cd5..e1765a5 100644 --- a/src/tech/avroid/kube/PodTemplates.groovy +++ b/src/tech/avroid/kube/PodTemplates.groovy @@ -237,6 +237,74 @@ class PodTemplates implements Serializable { } } + public void terraform(imageVersion='1.10.5', body) { + this.script.podTemplate( + cloud: this.cloud, + runAsUser: "0", + containers: [ + this.script.containerTemplate( + alwaysPullImage: true, + name: 'terraform', + image: "${this.registry}/docker-hub-proxy/hashicorp/terraform:${imageVersion}", + envVars: [ + this.script.containerEnvVar(key: 'HOME', value: '/home/jenkins'), + ], + ttyEnabled: true, + command: "cat", + resourceRequestCpu: '500m', + resourceLimitCpu: '500m', + resourceRequestMemory: '2Gi', + resourceLimitMemory: '12Gi', + workingDir: '/jenkins', + ), + ], + instanceCap: 1, + showRawYaml: false, + volumes: [ + this.script.emptyDirVolume(memory: false, mountPath: '/tmp'), + ], + workspaceVolume: this.script.emptyDirWorkspaceVolume(false), + ) + + { + body.call() + } + } + + public void vault(imageVersion='1.17.6-0', body) { + this.script.podTemplate( + cloud: this.cloud, + imagePullSecrets: this.dockerCreds, + containers: [ + this.script.containerTemplate( + alwaysPullImage: true, + name: 'vault', + image: "${this.registry}/devops/vault:${imageVersion}", + envVars: [ + this.script.containerEnvVar(key: 'HOME', value: '/home/jenkins'), + ], + ttyEnabled: true, + command: "cat", + resourceRequestCpu: '300m', + resourceLimitCpu: '300m', + resourceRequestMemory: '1Gi', + resourceLimitMemory: '12Gi', + workingDir: '/jenkins', + ), + ], + instanceCap: 1, + showRawYaml: false, + volumes: [ + this.script.emptyDirVolume(memory: false, mountPath: '/tmp'), + ], + workspaceVolume: this.script.emptyDirWorkspaceVolume(false), + ) + + { + body.call() + } + } + public void nodejs(imageVersion='18.16-alpine3.18', body) { this.script.podTemplate( runAsUser: "0", diff --git a/src/tech/avroid/terraform/Terraform.groovy b/src/tech/avroid/terraform/Terraform.groovy new file mode 100644 index 0000000..e867827 --- /dev/null +++ b/src/tech/avroid/terraform/Terraform.groovy @@ -0,0 +1,49 @@ +package tech.avroid.terraform + +/** + * Class for work with Terraform + */ +class Terraform implements Serializable { + + private Script script + + Terraform(Map args = [:]) { + this.script = args.script + } + + /** + * Method terraform init + * @return exit code + */ + public Integer init() { + return this.script.sh( + script: "set +x && source .creds && terraform init", + returnStatus: true + ) + } + + /** + * Method terraform plan + */ + public void plan() { + script.sh "terraform plan" + } + + /** + * Method cheking status plan + * @return exit code + */ + public Integer checkStatusPlan(Boolean debug = false) { + String hideOutput = '> /dev/null' + + if (debug) { + hideOutput = '' + } + + return this.script.sh( + script: "set +x && source .creds && terraform plan " + + "-lock=false -detailed-exitcode -input=false ${hideOutput}", + returnStatus: true + ) + } +}