-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_seed_nodes.jf
218 lines (193 loc) · 7.81 KB
/
deploy_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!groovy
def TERRAFORM_EXE_PATH = "terraform"
pipeline {
environment {
AWS_REGION = "us-east-1"
CONFIG_BUCKET_NAME = "wizards-cassandra-config"
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
),
string(
name: 'ami',
description: '[Mandatory] Ubuntu AMI of underlying EC2 instance',
defaultValue: 'ami-07ebfd5b3428b6f4d',
trim: true
),
string(
name: 'vpc_id',
description: '[Mandatory] ID of VPC to deploy into',
defaultValue: 'vpc-d630b2ab',
trim: true
),
string(
name: 'subnet_ids',
description: '[Mandatory] Comma Separated List of Subnet IDs of VPC to deploy into',
defaultValue: 'subnet-cff6f782,subnet-cff6f782',
trim: true
),
string(
name: 'instance_type',
description: '[Mandatory] Instance Type of underlying EC2',
defaultValue: 't2.small',
trim: true
)
])
])
}
}
}
stage('Initialize Ingredients'){
steps{
script{
checkout scm
}
}
}
stage('Execute Terraform'){
steps{
script {
check_terraform()
def terraform_vars = get_terraform_vars(params)
def terraform_init_vars = get_terraform_init_vars()
execute_terraform(terraform_init_vars, terraform_vars)
}
}
}
stage('Create Config Files'){
steps{
script{
def seed_nodes_private_ips = get_seed_nodes_private_ips()
prepare_config_files(params.name, seed_nodes_private_ips)
}
}
}
stage('Upload Config Files to S3'){
steps{
script{
upload_config_files()
}
}
}
stage('Audit'){
steps{
script{
print_audit_information(params)
}
}
}
}
post {
always {
cleanWs()
}
}
}
def get_terraform_vars(params){
def terraform_vars = ""
terraform_vars += " -var 'NAME=${params.name}'"
terraform_vars += " -var 'ENVIRONMENT=${params.environment}'"
terraform_vars += " -var 'VPC_ID=${params.vpc_id}'"
def subnet_ids_list = params.subnet_ids.split(',')
def subnet_ids_string = ""
for (subnet_id in subnet_ids_list) {
subnet_ids_string += "\"" + subnet_id + "\"" + ","
}
subnet_ids_string = subnet_ids_string.substring(0, subnet_ids_string.length()-1)
terraform_vars += " -var 'SUBNET_IDs=[${subnet_ids_string}]'"
terraform_vars += " -var 'AMI=${params.ami}'"
terraform_vars += " -var 'INSTANCE_TYPE=${params.instance_type}'"
terraform_vars += " -var 'CONFIG_BUCKET_NAME=${env.CONFIG_BUCKET_NAME}'"
terraform_vars += " -var 'CONFIG_BUCKET_KEY=${env.COMMON_BUCKET_KEY}'"
return terraform_vars
}
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, terraform_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 ${terraform_vars} -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 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 get_seed_nodes_private_ips(){
dir("terraform"){
def terraform_output = sh(script: "${TERRAFORM_EXE_PATH} output -json cassandra_seed_nodes_private_ips", returnStdout: true)
def seed_nodes_private_ips = readJSON text: "${terraform_output}"
print "seed_nodes_private_ips : ${seed_nodes_private_ips}"
return seed_nodes_private_ips
}
}
def prepare_config_files(name, seed_nodes_private_ips){
def return_status = sh(script: "sed -i 's/@@NAME_OF_CLUSTER@@/${name}/g' config/cassandra/cassandra.yaml", returnStatus: true)
if(return_status!=0){
error("Unable to replace NAME_OF_CLUSTER")
}
def join_private_ips = ""
for (private_ip in seed_nodes_private_ips) {
join_private_ips = join_private_ips + private_ip + ","
}
join_private_ips = join_private_ips.substring(0, join_private_ips.length()-1)
return_status = sh(script: "sed -i 's/@@LIST_OF_SEED_NODES@@/${join_private_ips}/g' config/cassandra/cassandra.yaml", returnStatus: true)
if(return_status!=0){
error("Unable to replace LIST_OF_SEED_NODES")
}
}
def upload_config_files(){
sh(script: "aws s3 cp config/cassandra/ s3://${env.CONFIG_BUCKET_NAME}/${env.COMMON_BUCKET_KEY}/ --recursive")
}
def print_audit_information(params){
print "#############################################"
print "NAME : ${params.name}"
print "ENVIRONMENT : ${params.environment}"
print "CONFIGURATION BUCKET NAME : ${env.CONFIG_BUCKET_NAME}"
print "STATE BUCKET NAME : ${env.STATE_BUCKET_NAME}"
print "BUCKET KEY : ${env.COMMON_BUCKET_KEY}"
print "#############################################"
}