forked from microsoft/pai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
622 lines (567 loc) · 17 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
pipeline {
agent {
node {
label 'pipeline'
}
}
stages {
stage('Choose Deployment Bed') {
steps {
script {
sh (
script: '''#!/bin/bash
set -ex
IFS=", " read -r -a labels <<< "$NODE_NAME"
#echo ${labels[@]}
echo ${labels[0]} > ${WORKSPACE}/BED.txt
''',
returnStdout: true
)
env.BED = readFile("${WORKSPACE}/BED.txt").trim()
echo "Select CI Cluster: ${BED}"
sh '''#!/bin/bash
set -ex
echo ${GIT_BRANCH//\\//-}-$(git rev-parse --short HEAD)-${BUILD_ID} > ${WORKSPACE}/IMAGE_TAG.txt
'''
env.IMAGE_TAG = readFile("${WORKSPACE}/IMAGE_TAG.txt").trim()
echo "Image tag: ${IMAGE_TAG}"
}
//sh 'printenv'
}
}
stage('Clean dev-box') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#!/bin/bash
set -ex
sudo --preserve-env $JENKINS_HOME/scripts/prepare_build_env.sh'''
}
}
stage('Build Images') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#! /bin/bash
set -ex
# prepare path
sudo mkdir -p ${JENKINS_HOME}/${BED}/singlebox/quick-start
sudo mkdir -p ${JENKINS_HOME}/${BED}/singlebox/cluster-configuration
sudo chown core:core -R $JENKINS_HOME
sudo chown core:core -R /pathHadoop/
QUICK_START_PATH=${JENKINS_HOME}/${BED}/singlebox/quick-start
CONFIG_PATH=${JENKINS_HOME}/${BED}/singlebox/cluster-configuration
# Install necessary python package
sudo pip install GitPython
# generate quick-start
$JENKINS_HOME/scripts/${BED}-gen_single-box.sh ${QUICK_START_PATH}
# ! fix permission
sudo chown core:core -R /mnt/jenkins/workspace
# generate config
ls $CONFIG_PATH/
rm -rf $CONFIG_PATH/*.yaml
./paictl.py config generate -i ${QUICK_START_PATH}/quick-start.yaml -o $CONFIG_PATH
# update image tag
sed -i "s/tag: \\(latest\\|v[[:digit:]]\\+.[[:digit:]]\\+.[[:digit:]]\\+\\)/tag: ${IMAGE_TAG}/" ${CONFIG_PATH}/services-configuration.yaml
# setup registry
$JENKINS_HOME/scripts/setup_azure_int_registry_new_com.sh $CONFIG_PATH
# build images
cd build/
sudo ./pai_build.py build -c $CONFIG_PATH
# push images
sudo ./pai_build.py push -c $CONFIG_PATH
'''
}
}
stage('Deploy') {
parallel {
stage('Install A SingleBox') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#!/bin/bash
set -ex
CONFIG_PATH=${JENKINS_HOME}/${BED}/singlebox/cluster-configuration
QUICK_START_PATH=${JENKINS_HOME}/${BED}/singlebox/quick-start
# Run your dev-box
# Assume the path of custom-hadoop-binary-path in your service-configuration is /pathHadoop.
# Assume the directory path of your cluster-configuration is /pathConfiguration.
# By now, you can leave it as it is, we only mount those two directories into docker container for later usage.
sudo docker run -itd \
-e COLUMNS=$COLUMNS \
-e LINES=$LINES \
-e TERM=$TERM \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/jenkins/scripts:/jenkins/scripts \
-v /pathHadoop:/pathHadoop \
-v ${CONFIG_PATH}:/cluster-configuration \
-v ${QUICK_START_PATH}:/quick-start \
--pid=host \
--privileged=true \
--net=host \
--name=dev-box-singlebox \
10.0.1.5:5000/openpai/dev-box:${IMAGE_TAG} nofetch > SINGLE_BOX_DEV_BOX.txt
sudo docker exec $(cat SINGLE_BOX_DEV_BOX.txt) rm -rf /pai
sudo docker cp ${WORKSPACE} $(cat SINGLE_BOX_DEV_BOX.txt):/pai
'''
script {
env.SINGLE_BOX_DEV_BOX = readFile("$WORKSPACE/SINGLE_BOX_DEV_BOX.txt").trim()
}
script {
try {
sh '''#!/bin/bash
set -ex
# Working in your dev-box
sudo docker exec -i ${SINGLE_BOX_DEV_BOX} /bin/bash <<EOF_DEV_BOX
set -ex
# prepare directory
rm -rf /cluster-configuration/layout.yaml
rm -rf /cluster-configuration/k8s-role-definition.yaml
rm -rf /cluster-configuration/kubernetes-configuration.yaml
rm -rf /cluster-configuration/services-configuration.yaml
cd /pai
# Create quick-start.yaml
/jenkins/scripts/${BED}-gen_single-box.sh /quick-start
# Step 1. Generate config
./paictl.py config generate -i /quick-start/quick-start.yaml -o /cluster-configuration
# update image tag
sed -i "s/tag: \\(latest\\|v[[:digit:]]\\+.[[:digit:]]\\+.[[:digit:]]\\+\\)/tag: ${IMAGE_TAG}/" /cluster-configuration/services-configuration.yaml
# setup registry
/jenkins/scripts/setup_azure_int_registry_new_com.sh /cluster-configuration
# Step 2. Boot up Kubernetes
# install k8s
./paictl.py cluster k8s-bootup -p /cluster-configuration
# ! TODO wait for cluster ready
sleep 6s
# Step 3. Upload cluster-configuration into kubernetes cluster. And set cluster-id
./paictl.py config push -p /cluster-configuration << EOF
pai
EOF
# Step 4. Start all PAI services
# start pai services
./paictl.py service start << EOF
pai
EOF
EOF_DEV_BOX
sudo chown core:core -R $JENKINS_HOME
sudo chown core:core -R /pathHadoop/
sudo chown core:core -R /mnt/jenkins/workspace
'''
} catch (err) {
echo "Deploy SingleBox Failed: ${err}"
currentBuild.result = 'FAILURE'
}
}
}
}
stage('Install Cluster') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#!/bin/bash
set -ex
CONFIG_PATH=${JENKINS_HOME}/${BED}/cluster/cluster-configuration
QUICK_START_PATH=${JENKINS_HOME}/${BED}/cluster/quick-start
# Run your dev-box
# Assume the path of custom-hadoop-binary-path in your service-configuration is /pathHadoop.
# Assume the directory path of your cluster-configuration is /pathConfiguration.
# By now, you can leave it as it is, we only mount those two directories into docker container for later usage.
sudo docker run -itd \
-e COLUMNS=$COLUMNS \
-e LINES=$LINES \
-e TERM=$TERM \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/jenkins/scripts:/jenkins/scripts \
-v /pathHadoop:/pathHadoop \
-v ${CONFIG_PATH}:/cluster-configuration \
-v ${QUICK_START_PATH}:/quick-start \
--pid=host \
--privileged=true \
--net=host \
--name=dev-box-cluster \
10.0.1.5:5000/openpai/dev-box:${IMAGE_TAG} nofetch > CLUSTER_DEV_BOX.txt
sudo docker exec $(cat CLUSTER_DEV_BOX.txt) rm -rf /pai
sudo docker cp ${WORKSPACE} $(cat CLUSTER_DEV_BOX.txt):/pai
'''
script {
env.CLUSTER_DEV_BOX = readFile("$WORKSPACE/CLUSTER_DEV_BOX.txt").trim()
}
script {
try {
sh '''#!/bin/bash
set -ex
# Working in your dev-box
sudo docker exec -i ${CLUSTER_DEV_BOX} /bin/bash <<EOF_DEV_BOX
set -ex
# prepare directory
rm -rf /cluster-configuration/layout.yaml
rm -rf /cluster-configuration/k8s-role-definition.yaml
rm -rf /cluster-configuration/kubernetes-configuration.yaml
rm -rf /cluster-configuration/services-configuration.yaml
cd /pai
# Create quick-start.yaml
/jenkins/scripts/${BED}-gen_cluster.sh /quick-start
# Step 1. Generate config
./paictl.py config generate -i /quick-start/quick-start.yaml -o /cluster-configuration
# update image tag
sed -i "s/tag: \\(latest\\|v[[:digit:]]\\+.[[:digit:]]\\+.[[:digit:]]\\+\\)/tag: ${IMAGE_TAG}/" /cluster-configuration/services-configuration.yaml
# setup registry
/jenkins/scripts/setup_azure_int_registry_new_com.sh /cluster-configuration
# Step 2. Boot up Kubernetes
# install k8s
./paictl.py cluster k8s-bootup -p /cluster-configuration
# ! TODO wait for cluster ready
sleep 6s
# Step 3. Upload cluster configuration into kubernetes cluster. And set cluster-id
./paictl.py config push -p /cluster-configuration << EOF
pai
EOF
# Step 4. Start all PAI services
# start pai services
./paictl.py service start << EOF
pai
EOF
EOF_DEV_BOX
sudo chown core:core -R $JENKINS_HOME
sudo chown core:core -R /pathHadoop/
sudo chown core:core -R /mnt/jenkins/workspace
'''
} catch (err) {
echo "Deploy Cluster Failed: ${err}"
currentBuild.result = 'FAILURE'
}
}
}
}
}
}
stage('Test') {
parallel {
stage('Test A SingleBox') {
agent {
node {
label 'dev-box'
}
}
steps {
script {
env.SINGLE_BOX_URL = readFile("${JENKINS_HOME}/${BED}/singlebox/quick-start/pai_url.txt").trim()
echo "${SINGLE_BOX_URL}"
// env.PAI_ENDPOINT = readFile("${JENKINS_HOME}/${BED}/singlebox/quick-start/pai_url.txt").trim()
if(currentBuild.result == 'FAILURE') {
echo "Deploy Failed!!! Skip test!"
} else {
try {
timeout(time: 15, unit: 'MINUTES'){
def responseCode = 500
while(!responseCode.equals(200)){
try {
sleep(30)
echo "Waiting for PAI to be ready ..."
def response = httpRequest(env.SINGLE_BOX_URL + "/rest-server/api/v1")
println("Status: "+response.status)
println("Content: "+response.content)
responseCode = response.status
echo "Response code: ${responseCode}."
} catch (err) {
if(err instanceof org.jenkinsci.plugins.workflow.steps.FlowInterruptedException){
echo "Timout!"
currentBuild.result = 'FAILURE'
break;
}
echo "PAI is not ready: ${err}"
}
}
}
sh (
//returnStatus: true,
script: '''#!/bin/bash
set -ex
#set -euxo pipefail
sleep 60
TOKEN=$(
curl --silent --verbose \
$SINGLE_BOX_URL/rest-server/api/v1/authn/basic/login \
--header 'Content-Type: application/json' \
--data '{
"username": "admin",
"password": "admin-password",
"expiration": 3600
}' \\
| python -c "import sys,json;sys.stdout.write(json.loads(sys.stdin.read())['token'])"
)
# Submit a job
JOB_NAME="e2e-test-$RANDOM-$RANDOM"
curl --silent --verbose \
--request POST \
$SINGLE_BOX_URL/rest-server/api/v2/user/admin/jobs/ \
--header "Authorization: Bearer $TOKEN" \
--header 'Content-Type: application/json' \
--data "{
\\"jobName\\": \\"$JOB_NAME\\",
\\"image\\": \\"docker.io/openpai/alpine:bash\\",
\\"taskRoles\\": [
{
\\"name\\": \\"Master\\",
\\"taskNumber\\": 1,
\\"cpuNumber\\": 1,
\\"memoryMB\\": 2048,
\\"command\\": \\"/bin/bash --version\\"
}
]
}"
while :; do
sleep 10
STATUS=$(
curl --silent --verbose $SINGLE_BOX_URL/rest-server/api/v2/user/admin/jobs/$JOB_NAME \
| python -c "import sys,json;sys.stdout.write(json.loads(sys.stdin.read())['jobStatus']['state'])"
)
if [ "$STATUS" == 'SUCCEEDED' ]; then break; fi
if [ "$STATUS" != 'WAITING' ] && [ "$STATUS" != 'RUNNING' ]; then exit 1; fi
done
'''
)
} catch (err) {
echo "Failed: ${err}"
currentBuild.result = 'FAILURE'
}
}
}
}
}
stage('Test Cluster') {
agent {
node {
label 'dev-box'
}
}
steps {
script {
env.CLUSTER_URL = readFile("${JENKINS_HOME}/${BED}/cluster/quick-start/pai_url.txt").trim()
echo "${CLUSTER_URL}"
// env.PAI_ENDPOINT = readFile("${JENKINS_HOME}/${BED}/cluster/quick-start/pai_url.txt").trim()
if(currentBuild.result == 'FAILURE') {
echo "Deploy Failed!!! Skip test!"
} else {
try {
timeout(time: 15, unit: 'MINUTES'){
def responseCode = 500
while(!responseCode.equals(200)){
try {
sleep(30)
echo "Waiting for PAI to be ready ..."
def response = httpRequest(env.CLUSTER_URL + "/rest-server/api/v1")
println("Status: "+response.status)
println("Content: "+response.content)
responseCode = response.status
echo "Response code: ${responseCode}."
} catch (err) {
if(err instanceof org.jenkinsci.plugins.workflow.steps.FlowInterruptedException){
echo "Timout!"
currentBuild.result = 'FAILURE'
break;
}
echo "PAI is not ready: ${err}"
}
}
}
sh (
//returnStatus: true,
script: '''#!/bin/bash
set -ex
#set -euxo pipefail
sleep 60
TOKEN=$(
curl --silent --verbose \
$CLUSTER_URL/rest-server/api/v1/authn/basic/login \
--header 'Content-Type: application/json' \
--data '{
"username": "admin",
"password": "admin-password",
"expiration": 3600
}' \\
| python -c "import sys,json;sys.stdout.write(json.loads(sys.stdin.read())['token'])"
)
# Submit a job
JOB_NAME="e2e-test-$RANDOM-$RANDOM"
curl --silent --verbose \
--request POST \
$CLUSTER_URL/rest-server/api/v2/user/admin/jobs \
--header "Authorization: Bearer $TOKEN" \
--header 'Content-Type: application/json' \
--data "{
\\"jobName\\": \\"$JOB_NAME\\",
\\"image\\": \\"docker.io/openpai/alpine:bash\\",
\\"taskRoles\\": [
{
\\"name\\": \\"Master\\",
\\"taskNumber\\": 1,
\\"cpuNumber\\": 1,
\\"memoryMB\\": 2048,
\\"command\\": \\"/bin/bash --version\\"
}
]
}"
while :; do
sleep 10
STATUS=$(
curl --silent --verbose $CLUSTER_URL/rest-server/api/v2/user/admin/jobs/$JOB_NAME \
| python -c "import sys,json;sys.stdout.write(json.loads(sys.stdin.read())['jobStatus']['state'])"
)
if [ "$STATUS" == 'SUCCEEDED' ]; then break; fi
if [ "$STATUS" != 'WAITING' ] && [ "$STATUS" != 'RUNNING' ]; then exit 1; fi
done
'''
)
} catch (err) {
echo "Failed: ${err}"
currentBuild.result = 'FAILURE'
}
}
}
}
}
}
}
stage('Pause on failure') {
agent {
node {
label 'dev-box'
}
}
steps {
script {
try {
if(currentBuild.result == 'FAILURE'){
def pauseNow
timeout(time: 15, unit: 'MINUTES'){
pauseNow= input(message: 'Do you want to reserve the environment for debug?', ok: 'Yes',
parameters: [booleanParam(defaultValue: true,
description: 'If you want to debug, click the Yes', name: 'Yes?')])
echo "pauseNow:" + pauseNow
}
if(pauseNow){
input (
message: 'Click "Proceed" to finish!'
)
}
}
} catch (err) {
echo "Encountered error: ${err}"
echo "Whatever, Will cleanup now!"
}
}
}
}
stage('Clean Up') {
parallel {
stage('Clean up A SingleBox') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#!/bin/bash
set -x
# Working in your dev-box
sudo docker exec -i ${SINGLE_BOX_DEV_BOX} /bin/bash <<EOF_DEV_BOX
set -x
cd /pai
# delete service for next install
./paictl.py service start -n cluster-configuration << EOF
pai
EOF
./paictl.py service delete << EOF
Y
pai
EOF
# clean k8s
./paictl.py cluster k8s-clean -p /cluster-configuration -f << EOF
Y
EOF
EOF_DEV_BOX
sudo docker rm -f ${SINGLE_BOX_DEV_BOX}
'''
}
}
stage('Clean up Cluster') {
agent {
node {
label 'dev-box'
}
}
steps {
sh '''#!/bin/bash
set -x
# Working in your dev-box
sudo docker exec -i ${CLUSTER_DEV_BOX} /bin/bash <<EOF_DEV_BOX
set -x
cd /pai
# delete service for next install
./paictl.py service start -n cluster-configuration << EOF
pai
EOF
./paictl.py service delete << EOF
Y
pai
EOF
# clean k8s
./paictl.py cluster k8s-clean -p /cluster-configuration -f << EOF
Y
EOF
EOF_DEV_BOX
sudo docker rm -f ${CLUSTER_DEV_BOX}
'''
}
}
}
}
}
post {
always {
echo 'I am the end of pipeline'
}
success {
echo 'I succeeeded!'
office365ConnectorSend(
status: "Build success",
webhookUrl: "${env.HOOK}"
)
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
office365ConnectorSend(
status: "Build failure",
webhookUrl: "${env.HOOK}"
)
step([
$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']]),
to: '[email protected]'
])
}
changed {
echo 'Things were different before...'
}
}
options {
disableConcurrentBuilds()
}
}