-
Notifications
You must be signed in to change notification settings - Fork 0
/
destory_seed_nodes.jf
123 lines (107 loc) · 3.79 KB
/
destory_seed_nodes.jf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!groovy
def TERRAFORM_EXE_PATH = "terraform"
pipeline {
environment {
AWS_REGION = "us-east-1"
COMMON_BUCKET_KEY = "${params.environment}/cassandra/${name}"
STATE_BUCKET_NAME = "wizards-terraform-state"
}
agent any
stages{
stage("Setup Parameters"){
steps {
script {
properties([
parameters([
string(
name: 'name',
description: '[Mandatory] Name of cluster',
defaultValue: 'cassandra-cluster',
trim: true
),
choice(
name: 'environment',
description: '[Mandatory] Environment to deploy into',
choices: ['development', 'staging', 'production'],
defaultValue: 'production',
trim: true
)
])
])
}
}
}
stage('Initialize Ingredients'){
steps{
script{
checkout scm
}
}
}
stage('Execute Terraform'){
steps{
script {
check_terraform()
def terraform_init_vars = get_terraform_init_vars()
execute_terraform(terraform_init_vars)
}
}
}
stage('Audit'){
steps{
script{
print_audit_information(params)
}
}
}
}
post {
always {
cleanWs()
}
}
}
def get_terraform_init_vars(){
def terraform_init_vars = ""
terraform_init_vars += " -backend-config=\"bucket=${env.STATE_BUCKET_NAME}\" "
terraform_init_vars += " -backend-config=\"key=${env.COMMON_BUCKET_KEY}\" "
terraform_init_vars += " -backend-config=\"region=${env.AWS_REGION}\" "
return terraform_init_vars
}
def check_terraform(){
def returnStatus = sh(script: "terraform", returnStatus: true)
if(returnStatus!=0){
TERRAFORM_EXE_PATH = "/usr/local/bin/terraform"
}else{
TERRAFORM_EXE_PATH = "terraform"
}
print "TERRAFORM_EXE_PATH is set to ${TERRAFORM_EXE_PATH}"
}
def execute_terraform(terraform_init_vars){
dir("terraform"){
def returnStatus = sh(script: "${TERRAFORM_EXE_PATH} init ${terraform_init_vars}", returnStatus: true)
if(returnStatus!=0){
error("Unable to execute ${TERRAFORM_EXE_PATH} init")
}
returnStatus = sh(script: "/usr/local/bin/terraform plan -destroy -out=terraform_plan.tf", returnStatus: true)
if(returnStatus!=0){
error("Unable to execute ${TERRAFORM_EXE_PATH} plan")
}
returnStatus = sh(script: "/usr/local/bin/terraform apply -auto-approve -destroy terraform_plan.tf", returnStatus: true)
if(returnStatus!=0){
error("Unable to execute ${TERRAFORM_EXE_PATH} apply")
}
returnStatus = sh(script: "rm terraform_plan.tf", returnStatus: true)
if(returnStatus!=0){
error("Unable to remove terraform_plan.tf")
}
}
}
def print_audit_information(params){
print "#############################################"
print "NAME : ${params.name}"
print "ENVIRONMENT : ${params.environment}"
print "STATE BUCKET NAME : ${env.STATE_BUCKET_NAME}"
print "BUCKET KEY : ${env.COMMON_BUCKET_KEY}"
print "#############################################"
}