-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
439 lines (413 loc) · 16.6 KB
/
Jenkinsfile
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
def val = ''
if (params.Datacenter == 'PNQ-Datacenter') {
val = 'PNQ-Datacenter-Agent'
} else if (params.Datacenter == 'HR-Datacenter') {
val = 'HR-Datacenter-Agent'
}
pipeline {
agent {
label val
}
stages {
stage('Set parameters') {
steps {
script {
currentBuild.displayName = '#' + currentBuild.number +
'-' + params.Datacenter
properties([
parameters([
[
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'Environment',
randomName: 'choice-parameter-4317672109824',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
],
script: [
classpath: [],
sandbox: true,
script: 'return [\'Select an Environment\',\'PROD\', \'DEV\']'
]
]
],
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'Datacenter',
randomName: 'choice-parameter-1434774074254',
referencedParameters: 'Environment',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
], script: [
classpath: [],
sandbox: true,
script: '''
def choices
switch(Environment){
case \'PROD\':
choices = [\'Select a Datacenter\', \'PNQ-Datacenter\', \'HR-Datacenter\']
break
case \'DEV\':
choices = [\'Select a Datacenter\', \'PNQ-Datacenter\', \'HR-Datacenter\']
break
default:
choices = [\'N/A\']
break
}
return choices'''
]
]
],
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'Provision_Server',
randomName: 'choice-parameter-143132192342342254',
referencedParameters: 'Datacenter',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
], script: [
classpath: [],
sandbox: true,
script: '''
def choices
if (Datacenter=='PNQ-Datacenter') {
choices=['PNQ-Datacenter-Agent']
} else if (Datacenter=='HR-Datacenter') {
choices=['HR-Datacenter-Agent']
} else {
choices=['None']
}
return choices'''
]
]
],
string(
defaultValue: '',
description: 'Enter your vCenter IP or FQDN',
name: 'vCenter_URL',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'vSphere_ResourcePool',
trim: true
),
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'vSphere_Datacenter',
randomName: 'choice-parameter-1434323202365329',
referencedParameters: 'Environment, Datacenter',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
], script: [
classpath: [],
sandbox: true,
script: '''
def choices
if (Environment=='PROD') {
choices=['PNQ-Datacenter','HR-Datacenter']
} else if (Datacenter=='DEV') {
choices=['PNQ-Datacenter','HR-Datacenter']
} else {
choices=['None']
}
return choices'''
]
]
],
string(
defaultValue: 'VM Network',
description: 'Enter the Network for kubernetes node deployment',
name: 'Network_Subnet',
trim: true
),
string(
defaultValue: '255.255.255.0',
description: '',
name: 'Subnet_Netmask',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'Default_Gateway',
trim: true
),
choice(
choices: [
'CentOS-7.9-temp'
],
description: '',
name: 'Template'
),
string(
defaultValue: '',
description: '',
name: 'DNS_Server1',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'DNS_Server2',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'DNS_Domain',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'k8s_master_ip',
trim: true
),
string(
defaultValue: 'k8s-master',
description: '',
name: 'k8s_master_hostname',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'k8s_worker01_ip',
trim: true
),
string(
defaultValue: 'k8s-worker01',
description: '',
name: 'k8s_worker01_hostname',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'k8s_worker02_ip',
trim: true
),
string(
defaultValue: 'k8s-worker02',
description: '',
name: 'k8s_worker02_hostname',
trim: true
),
string(
defaultValue: '',
description: '',
name: 'k8s_worker03_ip',
trim: true
),
string(
defaultValue: 'k8s-worker03',
description: '',
name: 'k8s_worker03_hostname',
trim: true
),
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'Timezone',
randomName: 'choice-parameter-435671232309824',
referencedParameters: 'Datacenter',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
],
script: [
classpath: [],
sandbox: true,
script: '''
def choices
if (Datacenter=='PNQ-Datacenter') {
choices=['Asia/Kolkata']
} else if (Datacenter=='HR-Datacenter') {
choices=['america/denver']
} else {
choices=['None']
}
return choices'''
]
]
]
])
])
}
}
}
stage ('Replace values in inventory file') {
steps {
withCredentials([
[$class: 'UsernamePasswordMultiBinding', credentialsId: getCredentialsId(),
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'],
[$class: 'UsernamePasswordMultiBinding', credentialsId: 'centos-login',
usernameVariable: 'linuxos_username', passwordVariable: 'linuxos_userpassword']]) {
sh '''
cp -rf CodePull/* .
# Variable assignment
masterip=${k8s_master_ip}
node01ip=${k8s_worker01_ip}
node02ip=${k8s_worker02_ip}
node03ip=${k8s_worker03_ip}
masterfqdn=${k8s_master_hostname}
node01fqdn=${k8s_worker01_hostname}
node02fqdn=${k8s_worker02_hostname}
node03fqdn=${k8s_worker03_hostname}
mastervm=${k8s_master_hostname}_${k8s_master_ip}
workernode01=${k8s_worker01_hostname}_${k8s_worker01_ip}
workernode02=${k8s_worker02_hostname}_${k8s_worker02_ip}
workernode03=${k8s_worker03_hostname}_${k8s_worker03_ip}
cat >inventory/dynamic_inventory << EOL
[containerlab]
${mastervm} Mgmt_network_ipv4=${masterip} vsphere_vm_hostname=${masterfqdn}
${workernode01} Mgmt_network_ipv4=${node01ip} vsphere_vm_hostname=${node01fqdn}
${workernode02} Mgmt_network_ipv4=${node02ip} vsphere_vm_hostname=${node02fqdn}
${workernode03} Mgmt_network_ipv4=${node03ip} vsphere_vm_hostname=${node03fqdn}
[master]
${masterip}
[workers]
${node01ip}
${node02ip}
${node03ip}
# Node Groups
[nodes:children]
master
workers
[nodes:vars]
ansible_connection=ssh
ansible_user=$linuxos_username
ansible_python_interpreter=/usr/bin/python2.7
ansible_password=$linuxos_userpassword
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
EOL
cat >inventory/group_vars/all.yaml << EOL
# vCenter Login Information
vcenter_hostname: '$vCenter_URL'
vcenter_username: '$USERNAME'
vcenter_password: '$PASSWORD'
# Variable entries for Linux virtual machine
vsphere_datacenter: '$vSphere_Datacenter'
resource_pool: '$vSphere_ResourcePool'
# VM Network Components
Mgmt_network: '$Network_Subnet'
Mgmt_network_nmv4: '255.255.255.0'
Mgmt_network_gwv4: '$Default_Gateway'
# Tempalate of CentOS 7.9 Linux Server
lin_temp: '$Template'
wdc_vm_name: '{{ lin_temp }}'
# Timezone and NTP Server
# Use below command on a linux system to get timezone:
## timedatectl list-timezones
timezone: '$Timezone'
ntp_server:
- '$DNS_Server1'
- '$DNS_Server2'
# DNS server details
dns_server1: '$DNS_Server1'
dns_server2: '$DNS_Server2'
dns_domain: '$DNS_Domain'
# Kubernetes Specific Parameters
pod_network: '172.16.0.0/16'
k8s_master_ip: '${masterip}'
'''
}
}
}
stage('Nodes Deployment') {
steps {
echo 'Starting the Kubernetes Nodes deployment....'
sh """
ansible-playbook -i inventory containerlab.yaml --tags "deploy"
"""
}
}
stage('Guest OS Customization') {
steps {
echo 'Starting the guest os customization'
sh """
ansible-playbook -i inventory containerlab.yaml --tags "oscustom"
"""
}
}
stage('Kubernetes Master Setup') {
steps {
echo 'Starting the Kubernetes Master Setup'
sh """
ansible-playbook -i inventory containerlab.yaml --tags "mastersetup"
"""
}
}
stage('Kubernetes Nodes Setup') {
steps {
echo 'Starting the Kubernetes Node Setup'
sh """
ansible-playbook -i inventory containerlab.yaml --tags "nodesetup"
"""
}
}
stage('Kubernetes Nodes labeling') {
steps {
echo 'Starting the Kubernetes Nodes Labeling'
sh """
ansible-playbook -i inventory containerlab.yaml --tags "nodelabel"
"""
}
}
}
}
String getCredentialsId() {
if (params.Environment == "PROD") {
"vCenter-Login"
} else if (params.Environment == "DEV") {
"vCenter-Login"
} else {
"N/A"
}
}
String getGitUrl() {
if (params.Environment == "PROD") {
"https://github.com/Udayendu/vmugdemo03.git"
} else if (params.Environment == "DEV") {
"https://github.com/Udayendu/vmugdemo03.git"
} else {
"N/A"
}
}