1+ #! /bin/bash
2+
3+ # ==============================
4+ # COMMON FUNCTIONS
5+ # ==============================
6+
7+ RED=" \033[31m"
8+ BLUE=" \033[94m"
9+ GREEN=" \033[32m"
10+ ENDCOLOR=" \033[0m"
11+
12+ function print_error {
13+ echo -e " ${RED} \n==========================================${ENDCOLOR} "
14+ echo -e " ${RED} FAILED${ENDCOLOR} "
15+ echo -e " ${RED} ==========================================\n${ENDCOLOR} "
16+ echo -e " ${RED} $1 ${ENDCOLOR} "
17+ echo -e " "
18+ }
19+ function print_msg {
20+ echo -e " ${BLUE} $1 ${ENDCOLOR} "
21+ }
22+ function print_success {
23+ echo -e " ${GREEN} $1 ${ENDCOLOR} "
24+ }
25+
26+ # Helper function to check whether prerequisites are installed
27+ function check_prerequisites {
28+ # Ensure that jq tool is installed
29+ if ! command -v jq & > /dev/null; then
30+ print_error " 'jq' tool is not installed"
31+ exit 1
32+ fi
33+ }
34+
35+ # ==============================
36+ # COMMON IBMCLOUD HELPERS
37+ # ==============================
38+
39+ # helper function to check whether IBM Cloud CLI plugins should get updated, or not
40+ function ensure_plugin_is_up_to_date() {
41+ echo " Checking $1 ..."
42+ # check whether plugin is installed
43+ if ! ibmcloud plugin show $1 -q > /dev/null; then
44+ # install it
45+ ibmcloud plugin install $1 -f --quiet
46+ else
47+ # check whether there is an update available
48+ ibmcloud plugin update $1 -f --quiet
49+ fi
50+ }
51+
52+ function target_region {
53+ print_msg " \nTargetting IBM Cloud region '$1 ' ..."
54+ current_region=$( ibmcloud target --output JSON | jq -r ' .region|.name' )
55+ if [[ " $current_region " != " $1 " ]]; then
56+ ibmcloud target -r $1 --quiet
57+ fi
58+ }
59+
60+ function target_resource_group {
61+ print_msg " \nTargetting resource group '$1 ' ..."
62+ current_resource_group_guid=$( ibmcloud target --output JSON | jq -r ' .resource_group|.guid' )
63+ new_resource_group_guid=$( ibmcloud resource group $1 -output json| jq -r ' .[0].id' )
64+ if [[ " $current_resource_group_guid " != " $new_resource_group_guid " ]]; then
65+ ibmcloud target -g $1 --quiet
66+ fi
67+ echo " Done"
68+ }
69+
70+ function does_instance_exist {
71+ (( $(ibmcloud resource search "service_name: \"$1 \" AND name: \"$2 \"" -- output JSON| jq - r '.items| length') > 0 ))
72+ }
73+
74+ function does_serviceid_exist {
75+ (( $(ibmcloud resource search "type: serviceid AND name: \"$1 \"" -- output JSON| jq - r '.items| length') > 0 ))
76+ }
77+
78+ function has_bucket_name_with_prefix {
79+ buckets=$( ibmcloud cos buckets -output json)
80+ COS_BUCKET_NAME=" "
81+ if [[ " $( echo " ${buckets} " | jq -r ' .Buckets' ) " != " null" ]]; then
82+ for bucket in $( echo " ${buckets} " | jq -r ' .Buckets|.[] | @base64' ) ; do
83+ _jq () {
84+ echo ${bucket} | base64 --decode | jq -r ${1}
85+ }
86+ bucket_name=$( _jq ' .Name' )
87+ if [[ " $bucket_name " =~ ^$1 -* ]]; then
88+ COS_BUCKET_NAME=$bucket_name
89+ fi
90+ done
91+ fi
92+ echo $COS_BUCKET_NAME
93+ }
94+
95+
96+ function abortScript() {
97+ if [[ " ${CLEANUP_ON_ERROR} " == true ]]; then
98+ clean
99+ else
100+ print_msg " \nSkipping deletion of the created IBM Cloud resources. Please be aware that the created resources will occur costs in your account."
101+ echo " $ ibmcloud resource service-instances --type all -g $resource_group_name --location $REGION "
102+ ibmcloud resource service-instances --type all -g $resource_group_name --location $REGION
103+ fi
104+ exit 1
105+ }
0 commit comments