-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-accelerator-script.sh
executable file
·516 lines (417 loc) · 17.6 KB
/
import-accelerator-script.sh
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
#!/bin/bash
#################################################################################################################################################
# Copyright IBM Corp. 2020,2021
# All Rights Reserved
#################################################################################################################################################
# Log handling function
clear
log_msg()
{
declare l_sev=$1
declare l_msg=$2
declare l_logfile=$3
TIMESTAMP=`date "+%Y-%m-%d-%H:%M:%S"`
RED='\033[0;31m'
NC='\033[0m' # No Color
GREEN='\033[32m'
YELLOW='\033[33m'
case $l_sev in
"ERROR") l_colour=$RED ;;
"WARNING") l_colour=$YELLOW ;;
"SUCCESS") l_colour=$GREEN ;;
*) l_colour=$NC ;;
esac
echo -e "${l_colour}[$TIMESTAMP][$l_sev] : $l_msg ${NC}" >> $l_logfile
echo -e "${l_colour}[$l_sev] : $l_msg ${NC}"
}
#################################################################################################################################################
# Process command line arguments and flag parameters
#################################################################################################################################################
for arg in "$@"
do
case $arg in
-h|--hostname)
CPD_HOST="$2"
shift
shift
;;
-u|--username)
username="$2"
shift
shift
;;
-p|--password)
password="$2"
shift
shift
;;
-n|--name)
analytics_project="$2"
shift
shift
;;
#-j|--run_jobs)
# run_notebook_jobs="Y"
# shift
# ;;
-v|--version)
echo
echo "[INFO] : Industry Accelerator Import Script - Version 3.0"
shift
exit 0
;;
--help)
echo
echo "Cloud Pak for Data - Industry Accelerator Import"
echo
echo
echo "Usage: ./import-acclerator-script.sh [flags]"
echo
echo
echo "Flags :"
echo
echo " -h, --hostname : The host URL of the Cloud Pak for Data cluster e.g. https://<hostname:port>"
echo " -u, --username : The username for the Cloud Pak for Data cluster"
echo " -p, --password : The password for the Cloud Pak for Data cluster"
echo " -n, --name : The name you wish to use for the analytics project"
echo " -v, --version : Display version information for this script"
# echo " -j, --run_jobs : Execute the notebook jobs specified in the analytics project during the import process (optional)"
echo " --help : Help for accelerator import process"
echo
echo "These arguments are optional and can be declared in any order."
echo "You can also execute the script without arguments, in which case you will be prompted for the required information."
echo
echo "Example syntax without arguments..."
echo
echo "Example syntax Bash Users : ./import-accelerator-script.sh "
echo "Example syntax Windows Users : bash -c ./import-accelerator-script.sh "
echo
echo "Example syntax if you have extracted the .tar.gz file and are executing the import script that was included..."
echo "Note that in this scenario the script and the artefacts are in the same path"
echo "Example syntax Bash Users : ./import-accelerator-script.sh --hostname https://hostname:port --username username --password password --name name-of-project"
echo "Example syntax Windows Users : bash -c ./import-accelerator-script.sh --hostname https://hostname:port --username username --password password --name name-of-project"
echo
#echo "Example syntax if you are importing an unextracted .tar.gz file (this method can be useful if you have multiple .tar.gz files to process..."
#echo "Example syntax Bash Users : ./import-accelerator-script.sh --file accelerator-file-name.tar.gz --hostname https://hostname:port --username username --password password --name name-of-project"
#echo "Example syntax Windows Users : bash -c ./import-accelerator-script.sh --file accelerator-file-name.tar.gz --hostname https://hostname:port --username username --password password --name name-of-project"
shift
exit 0
;;
$1)
echo "[ERROR] : Invalid parameter(s). User --help argument to get assistance on usage of the parameter(s)."
exit 0
shift
;;
esac
done
echo
##################################################################################################################################
# Notebook Job Run section
##################################################################################################################################
# Set accelerator name & create logfile
TIMESTAMP=`date "+%Y-%m-%d-%H:%M:%S"`
logtime=$TIMESTAMP
current_path=$PWD
while [[ -z "$analytics_project" ]]
do
echo
echo "[INFO] : Please enter name of the accelerator. "
echo "[INFO] : Run ./import-accelerator.sh --help for further help."
echo
read -t300 -p ' Enter Accelerator Name : ' analytics_project
if [[ $? -gt 300 || -z "$analytics_project" ]]
then
echo
log_msg "ERROR" "No input entered by user for username. Aborting import..." "$logfile"
exit 0
fi
done
accelerator_name=$analytics_project
accelerator=${analytics_project%-industry-accelerator}
project_zipfile="$current_path/$analytics_project.zip"
logfile=$accelerator-$logtime.log
#echo zipfile : $project_zipfile
#echo accelerator name : $accelerator_name
#echo accelerator : $accelerator
if [[ ! -f $project_zipfile ]]
then
echo
log_msg "ERROR" "Aborting as analytics project file does not exist. Download the zip file and try again." "$logfile"
exit 0
elif [[ -z "$project_zipfile" ]]
then
echo
log_msg "INFO" "Please ensure that the script and the accelerator zip file are in the same directory and try again." "$logfile"
rm -rf output.txt
exit 0
elif [[ `stat -c %s $project_zipfile` == 0 ]]
then
echo
log_msg "WARNING" "The analytics project is empty. Aborting..." "$logfile"
rm -rf output.txt
exit 0
fi
log_msg "INFO" "$accelerator_name import process" "$logfile"
log_msg "INFO" "Starting script...." "$logfile"
#if [[ $run_notebook_jobs == 'Y' ]]
#then
# log_msg "WARNING" "Note that the notebook job run duration may vary from 8 to 15 minutes depending on the accelerator and CPD cluster..." "$logfile"
#fi
#################################################################################################################################################
# Config section & argument checks
#################################################################################################################################################
# Check host is reachable
while [[ -z "$CPD_HOST" ]]
do
echo
# Read Variables from User
echo "[INFO] : Script parameter Cloud Pak for Data Hostname required."
echo "[INFO] : Run ./import-accelerator.sh --help for further help."
echo "Enter host URL for your Cloud Pak for Data Cluster in this format : https://<hostname:port> "
echo
read -t300 -p 'Enter Host URL : ' CPD_HOST
if [[ $? -gt 300 || -z "$CPD_HOST" ]]
then
echo
log_msg "ERROR" "No input entered by user for host URL. Aborting import..." "$logfile"
exit 0
fi
done
if [[ $CPD_HOST != https://* ]]
then
CPD_HOST=https://$CPD_HOST
fi
if [[ $CPD_HOST == */ ]]
then
CPD_HOST=${CPD_HOST%?}
fi
# Cluster Sanity Check
if curl -k --output /dev/null --silent --head --fail "$CPD_HOST"; then
echo
log_msg "INFO" "Cloud Pak for Data cluster exists and is reachable... " "$logfile"
else
echo
log_msg "ERROR" "Cannot reach Cloud Pak for Data cluster..." "$logfile"
exit 0
fi
# Endpoint & headers for authentication
declare -a authURL="${CPD_HOST}/icp4d-api/v1/authorize"
declare -a curlArgs1=('-H' "Content-Type: application/json" \
'-H' "cache-control: no-cache")
#################################################################################################################################################
# Gather host and credentials
#################################################################################################################################################
while [[ -z "$username" ]]
do
echo
echo "[INFO] : Script parameter Username required. "
echo "[INFO] : Run ./import-accelerator.sh --help for further help."
read -t300 -p 'Enter USERNAME : ' username
if [[ $? -gt 300 || -z "$username" ]]
then
echo
log_msg "ERROR" "No input entered by user for username. Aborting import..." "$logfile"
exit 0
fi
done
while [[ -z "$password" ]]
do
echo
echo "[INFO] : Script parameter Password required. "
echo "[INFO] : Run ./import-accelerator.sh --help for further help."
read -t300 -sp 'Enter PASSWORD : ' password
if [[ $? -gt 300 || -z "$password" ]]
then
echo
log_msg "ERROR" "No input entered by user for password. Aborting import..." "$logfile"
exit 0
fi
done
#################################################################################################################################################
# Check authentication is successful
#################################################################################################################################################
credentials='{"username":"'$username'","password":"'$password'"}'
HTTP_CODE=$(curl -s --write-out "%{http_code}\n" -k -X POST "${curlArgs1[@]}" \
-d "$credentials" \
${authURL} \
--output output.txt)
if [[ $HTTP_CODE == 200 ]]
then
echo
log_msg "SUCCESS" "Authentication completed and successful..." "$logfile"
else
echo
log_msg "ERROR" "Authentication check returned error... $HTTP_CODE" "$logfile"
log_msg "ERROR" "Please check your username/password. Please also check your Cloud Pak for Data Host URL." "$logfile"
rm -rf output.txt
exit 0
fi
#################################################################################################################################################
# Check if user has required permissions to import the accelerator
#################################################################################################################################################
echo
json=$(curl -s -k "$authURL" \
-X POST "${curlArgs1[@]}" \
-d "$credentials") \
&& token=$(echo $json | sed "s/{.*\"token\":\"\([^\"]*\).*}/\1/g") \
log_msg "INFO" "Checking for user permissions...." "$logfile"
user_permissions=$(curl -s -k -X GET -H "Authorization: Bearer $token" \
-H "cache-control: no-cache" "${CPD_HOST}/icp4d-api/v1/users/$username")
#if echo $user_permissions | grep -q -e "Administrator" -e "Data Steward" -e "Data Engineer" -e "Data Quality Analyst" -e "zen_administrator_role" -e "wkc_data_steward_role" -e "wkc_data_scientist_role" -e "zen_data_engineer_role"
if echo $user_permissions | grep -q -e "Developer" -e "Business Analyst" -e "Data Scientist" -e "zen_developer_role" -e "wkc_business_analyst_role" -e "wkc_data_scientist_role"
then
echo
log_msg "INFO" "User has necessary privileges for project import and execution." "$logfile"
project_import_flag=1
else
log_msg "ERROR" "Aborting as Permissions not sufficient to import or execute a project." "$logfile"
project_import_flag=0
rm -rf output.txt
exit 0
fi
#################################################################################################################################################
# Provide name for project and start import of project
#################################################################################################################################################
if [[ $project_import_flag == 1 ]]
then
echo
log_msg "INFO" "Starting accelerator import process..." "$logfile"
#if [[ -z "$analytics_project" ]]
#then
# echo "[INFO] : Optional script parameter project name required."
# echo "[INFO] : Run ./import-accelerator.sh --help for further help."
# read -t 30 -p 'Enter a name for the analytics project. If no name is entered, the script will auto-generate a name : ' project
# if [[ ! -z "$project" ]]
# then
# analytics_project=$project
# else
# analytics_project=$accelerator_name
# fi
#fi
#echo $analytics_project
#exit 0
if [[ ${#analytics_project} -gt 95 ]]
then
log_msg "WARNING" "Project name too large." "$logfile"
analytics_project=`echo ${analytics_project:0:95}`
log_msg "INFO" "Project name has been trimmed down to $analytics_project" "$logfile"
fi
# analytics project metadata
METADATA='metadata={
"name": "'${analytics_project}'",
"description": "Industry Accelerator.",
"generator": "IndAcc-Projects",
"public": false,
"tags": [
"string"
],
"storage": {
"type": "assetfiles",
"guid": "d0e410a0-b358-42fc-b402-dba83316413b"
}
}'
if [[ -z "${METADATA}" ]]; then
log_msg "ERROR" "Metadata generation failed." "$logfile"
rm -rf output.txt
exit 0
else
log_msg "INFO" "Metadata created for project." "$logfile"
fi
declare -a metadata="${METADATA}"
# endpoint and headers for analytics project import
if [ $HTTP_CODE == 200 ]
then
declare -a curlArgs2=('-H' "Authorization: Bearer $token" \
'-H' "content-type:multipart/form-data")
log_msg "INFO" "Importing the analytics project..." "$logfile"
http_proj=$(curl -s --write-out "%{http_code}\n" -X POST -k \
"${CPD_HOST}/transactional/v2/projects" \
"${curlArgs2[@]}" \
-F file=@$project_zipfile \
-F "$metadata" \
--output output.txt )
echo "$TIMESTAMP">>$logfile
cat output.txt>>$logfile
if [[ $http_proj == 202 ]]
then
log_msg "INFO" "Importing Project Artefacts..." "$logfile"
guid=$(cat output.txt | grep -Po '"location":"\K[^"]*' | awk -F / '{print $4}')
trans_id=$(cat output.txt | sed "s/{.*\"id\":\"\([^\"]*\).*}/\1/g")
elif [[ $http_proj == 400 ]]
then
echo
log_msg "WARNING" "Project with this name already exists.... $http_proj" "$logfile"
log_msg "WARNING" "Attempting to import with unique name..." "$logfile"
while [[ $http_proj == 400 ]]
do
current_version=$analytics_project
n=${current_version##*[!0-9]}; p=${current_version%%$n}
analytics_project=$p$((n+1))
METADATA='metadata={
"name": "'${analytics_project}'",
"description": "Industry Accelerator.",
"generator": "IndAcc-Projects",
"public": false,
"tags": [
"string"
],
"storage": {
"type": "assetfiles",
"guid": "d0e410a0-b358-42fc-b402-dba83316413b"
}
}'
declare -a metadata="${METADATA}"
http_proj=$(curl -s --write-out "%{http_code}\n" -X POST -k \
"${CPD_HOST}/transactional/v2/projects" \
"${curlArgs2[@]}" \
-F file=@$project_zipfile \
-F "$metadata" \
--output output.txt )
echo "$TIMESTAMP">>$logfile
if [[ $http_proj == 202 ]]
then
cat output.txt>>$logfile
guid=$(cat output.txt | grep -Po '"location":"\K[^"]*' | awk -F / '{print $4}')
fi
trans_id=$(cat output.txt | sed "s/{.*\"id\":\"\([^\"]*\).*}/\1/g")
done
log_msg "INFO" "Project with this name created : $analytics_project" "$logfile"
else
echo
log_msg "ERROR" "Aborting Import... failed to create $http_proj" "$logfile"
rm -rf output.txt
exit 0
fi
else
echo
log_msg "ERROR" "Aborting Import... incorrect details/cluster unreachable. $HTTP_CODE " "$logfile"
rm -rf output.txt
exit 0
fi
echo
log_msg "SUCCESS" "Analytics project imported...." "$logfile"
fi
################## END OF NOTEBOOK JOB SECTION #####################
rm -rf output.txt
echo
echo
######################################################################################################################################
# SUMMARY SECTION FOR IMPORT PROCESS
######################################################################################################################################
log_msg "INFO" "INDUSTRY ACCELERATOR IMPORT PROCESS SUMMARY" "$logfile"
echo
log_msg "INFO" "Host Cluster URL : $CPD_HOST" "$logfile"
log_msg "INFO" "Cloud Pak for Data username : $username" "$logfile"
if [[ $project_import_flag == 1 ]]
then
log_msg "INFO" "Imported analytics project name : $analytics_project" "$logfile"
log_msg "INFO" "Project Link : $CPD_HOST/projects/$guid" "$logfile"
fi
#if [[ $run_all_notebook_jobs == 1 && $notebook_status != "Failed" && $dashboard_link != "" && $notebook_exe != 99 ]]
#then
# log_msg "INFO" "All notebook jobs have run successfully." "$logfile"
# log_msg "INFO" "Dashboard link : $dashboard_link" "$logfile"
#fi
echo
echo "[INFO] : Please visit the Cloud Pak for Data cluster above to access all the imported artefacts. View "$logfile" to view the import process log."
echo