-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathcommon.sh
More file actions
executable file
·99 lines (87 loc) · 2.94 KB
/
common.sh
File metadata and controls
executable file
·99 lines (87 loc) · 2.94 KB
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
#!/bin/bash
# ==============================
# COMMON FUNCTIONS
# ==============================
RED="\033[31m"
BLUE="\033[94m"
GREEN="\033[32m"
ENDCOLOR="\033[0m"
function print_error {
echo -e "${RED}\n==========================================${ENDCOLOR}"
echo -e "${RED} FAILED${ENDCOLOR}"
echo -e "${RED}==========================================\n${ENDCOLOR}"
echo -e "${RED}$1${ENDCOLOR}"
echo -e ""
}
function print_msg {
echo -e "${BLUE}$1${ENDCOLOR}"
}
function print_success {
echo -e "${GREEN}$1${ENDCOLOR}"
}
# Helper function to check whether prerequisites are installed
function check_prerequisites {
# Ensure that jq tool is installed
if ! command -v jq &>/dev/null; then
print_error "'jq' tool is not installed"
exit 1
fi
# Ensure that uuidgen tool is installed
if ! command -v uuidgen &>/dev/null; then
print_error "'uuidgen' tool is not installed"
exit 1
fi
}
# ==============================
# COMMON IBMCLOUD HELPERS
# ==============================
# helper function to check whether IBM Cloud CLI plugins should get updated, or not
function ensure_plugin_is_up_to_date() {
echo "Checking $1 ..."
# check whether plugin is installed
if ! ibmcloud plugin show $1 -q >/dev/null; then
# install it
ibmcloud plugin install $1 -f --quiet
else
# check whether there is an update available
ibmcloud plugin update $1 -f --quiet
fi
}
function target_region {
print_msg "\nTargetting IBM Cloud region '$1' ..."
current_region=$(ibmcloud target --output JSON |jq -r '.region|.name')
if [[ "$current_region" != "$1" ]]; then
ibmcloud target -r $1 --quiet
fi
}
function target_resource_group {
print_msg "\nTargetting resource group '$1' ..."
current_resource_group_guid=$(ibmcloud target --output JSON |jq -r '.resource_group|.guid')
new_resource_group_guid=$(ibmcloud resource group $1 -output json|jq -r '.[0].id')
if [[ "$current_resource_group_guid" != "$new_resource_group_guid" ]]; then
ibmcloud target -g $1 --quiet
fi
echo "Done"
}
function does_instance_exist {
(( $(ibmcloud resource search "service_name:\"$1\" AND name:\"$2\"" --output JSON|jq -r '.items|length') > 0 ))
}
function does_serviceid_exist {
(( $(ibmcloud resource search "type:serviceid AND name:\"$1\"" --output JSON|jq -r '.items|length') > 0 ))
}
function has_bucket_name_with_prefix {
buckets=$(ibmcloud cos buckets -output json)
COS_BUCKET_NAME=""
if [[ "$(echo "${buckets}" | jq -r '.Buckets')" != "null" ]]; then
for bucket in $(echo "${buckets}" | jq -r '.Buckets|.[] | @base64'); do
_jq() {
echo ${bucket} | base64 --decode | jq -r ${1}
}
bucket_name=$(_jq '.Name')
if [[ "$bucket_name" =~ ^$1-* ]]; then
COS_BUCKET_NAME=$bucket_name
fi
done
fi
echo $COS_BUCKET_NAME
}