forked from TheRealHaoLiu/hyper-acm-deploy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.sh
executable file
·152 lines (139 loc) · 4.46 KB
/
helpers.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
#!/bin/bash
# $1 - type e.g "error", "info" (yellow - 33; red -31 )
# $2 - message
function comment() {
if [ "$1" = "error" ]; then
echo -e '\033[0;31m>>> '$2' <<<\033[0m'
else
echo -e '\033[0;33m>>> '$2' <<<\033[0m'
fi
}
function usage () {
echo "hyper-acm-install.sh is to install ACM components on the hosted cluster namespace according to the given configuration file"
echo ""
echo "Options:"
echo " -f Configuration ini file name"
echo " -n Hosted cluster Namespace"
echo " -c Hosted cluster Name"
echo " -h Help"
echo ""
echo "Example: ./hyper-acm-install.sh -f acm.conf -n hypershift-clusters -c acm-1"
}
function uninstall_usage () {
echo "hyper-acm-uninstall.sh is to uninstall ACM components from the hosted cluster and management cluster"
echo ""
echo "Options:"
echo " -n Hosted cluster Namespace"
echo " -c Hosted cluster Name"
echo " -h Help"
echo ""
echo "Example: ./hyper-acm-uninstall.sh -n hypershift-clusters -c acm-1"
}
function import_cluster_usage () {
echo "import-cluster.sh is to import a managed cluster to the hosted cluster"
echo ""
echo "Options:"
echo " -f Configuration ini file name"
echo " -n Hosted cluster Namespace"
echo " -c Hosted cluster Name"
echo " -m Managed cluster Name"
echo " -k Managed cluster kubeconfig file name"
echo " -h Help"
echo ""
echo "Example: ./import-cluster.sh -f acm.conf -n hypershift-clusters -c acm-1 -m cluster1 -k ~/.kube/kubeconfig.kind"
}
function detach_cluster_usage () {
echo "detach-cluster.sh is to detach a managed cluster from the hosted cluster"
echo ""
echo "Options:"
echo " -n Hosted cluster Namespace"
echo " -c Hosted cluster Name"
echo " -m Managed cluster Name"
echo " -k Managed cluster kubeconfig file name"
echo " -h Help"
echo ""
echo "Example: ./detach-cluster.sh -n hypershift-clusters -c acm-1 -m cluster1 -k ~/.kube/kubeconfig.kind"
}
function check_dependency () {
which oc > /dev/null
if [ $? -ne 0 ]; then
echo "oc is not installed."
exit 1
fi
}
waitForNoPods() {
MINUTE=0
resNamespace=$1
while [ true ]; do
# Wait up to 3min
if [ $MINUTE -gt 180 ]; then
echo "Timeout waiting for addons to be removed"
exit 1
fi
operatorRes=`oc get pods -n ${resNamespace} | wc`
if [ $? -eq 0 ]; then
echo "All pods in the ${resNamespace} namespace removed"
break
fi
echo "* STATUS: Pods still running in the ${resNamespace} namespace removed. Retry in 5 sec"
sleep 5
(( MINUTE = MINUTE + 5 ))
done
}
waitForCMD() {
eval CMD="$1"
eval WAIT_MSG="$2"
MINUTE=0
while [ true ]; do
# Wait up to 3min
if [ $MINUTE -gt 180 ]; then
echo "Timeout waiting for ${CMD}"
exit 1
fi
echo ${CMD}
eval ${CMD}
if [ $? -eq 0 ]; then
break
fi
echo "* STATUS: ${WAIT_MSG}. Retry in 5 sec"
sleep 5
(( MINUTE = MINUTE + 5 ))
done
}
waitForRes() {
FOUND=1
MINUTE=0
resKinds=$1
resName=$2
resNamespace=$3
ignore=$4
running="\([0-9]\+\)\/\1"
printf "\n#####\nWait for ${resNamespace}/${resName} to reach running state (4min).\n"
while [ ${FOUND} -eq 1 ]; do
# Wait up to 3min, should only take about 20-30s
if [ $MINUTE -gt 180 ]; then
echo "Timeout waiting for the ${resNamespace}\/${resName}."
echo "List of current resources:"
oc get ${resKinds} -n ${resNamespace} ${resName}
echo "You should see ${resNamespace}/${resName} ${resKinds}"
if [ "${resKinds}" == "pods" ]; then
oc describe deployments -n ${resNamespace} ${resName}
fi
exit 1
fi
if [ "$ignore" == "" ]; then
operatorRes=`oc get ${resKinds} -n ${resNamespace} | grep ${resName}`
else
operatorRes=`oc get ${resKinds} -n ${resNamespace} | grep ${resName} | grep -v ${ignore}`
fi
if [[ $(echo $operatorRes | grep "${running}") ]]; then
echo "* ${resName} is running"
break
elif [ "$operatorRes" == "" ]; then
operatorRes="Waiting"
fi
echo "* STATUS: $operatorRes"
sleep 5
(( MINUTE = MINUTE + 5 ))
done
}