-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·328 lines (250 loc) · 11.5 KB
/
deploy.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
#!/usr/bin/env bash
: '
Copyright (C) 2021 IBM Corporation
Elayaraja Dhanapal <[email protected]> - Initial implementation.
Rafael Sene <[email protected]> - Initial implementation.
'
# Trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo "bye!"
exit 1
}
function check_dependencies() {
DEPENDENCIES=(oc curl)
for i in "${DEPENDENCIES[@]}"
do
if ! command -v $i &> /dev/null; then
echo "ERROR: $i could not be found."
exit 1
fi
done
}
function check_connectivity() {
curl --output /dev/null --silent --head --fail http://cloud.ibm.com
if [ ! $? -eq 0 ]; then
echo
echo "ERROR: please, check your internet connection."
exit 1
fi
}
function set_cluster_id() {
CLUSTER=$1
FILE=$2
# sets the cluster ID for a given deployment
sed -i -e "s/CLUSTERID/$CLUSTER/g" $FILE
}
function set_ocp_secrets() {
CLUSTER=$1
# check if the OCP secret is set and replace its value in
# the respective yaml file
if [ -s "$(pwd)"/cluster-configuration/ocp-secrets ]; then
SECRET=$(cat "$(pwd)"/cluster-configuration/ocp-secrets)
# copies the base cluster secrets file to the directory of a new deployment
cp -rp "$(pwd)"/yamls/secret.yaml "$(pwd)"/clusters/$CLUSTER
# the cluster secrets file for the new cluster
CLUSTER_SECRETS="$(pwd)"/clusters/$CLUSTER/secret.yaml
# sets the ocp secret for a given deployment
sed -i -e "s/ocp-secret/$SECRET/g" "$CLUSTER_SECRETS"
# sets the exclusive cluster id for the secrets of a given deployment
set_cluster_id "$CLUSTER" "$CLUSTER_SECRETS"
else
echo
echo "ERROR: ensure you have added the OpenShift Secrets at /cluster-configuration/ocp-secrets"
echo " you can get it from bit.ly/ocp-secrets"
echo
exit 1
fi
}
function set_volume_claim() {
CLUSTER=$1
# copies the base cluster pvc file to the directory of a new deployment
cp -rp "$(pwd)"/yamls/pvc.yaml "$(pwd)"/clusters/$CLUSTER
# the new cluster's pvc file
CLUSTER_PVC="$(pwd)"/clusters/$CLUSTER/pvc.yaml
# sets the exclusive cluster id for the secrets of a given deployment
set_cluster_id "$CLUSTER" "$CLUSTER_PVC"
}
function set_cluster_config() {
CLUSTER=$1
# copies the base cluster config file to the directory of a new deployment
cp -rp "$(pwd)"/yamls/ocp-cluster.yaml "$(pwd)"/clusters/$CLUSTER
# the new cluster's pvc file
CLUSTER_CONFIG="$(pwd)"/clusters/$CLUSTER/ocp-cluster.yaml
# sets the exclusive cluster id for the secrets of a given deployment
set_cluster_id "$CLUSTER" "$CLUSTER_CONFIG"
}
function set_variables() {
local PREFIX=$1
local SUFIX=$2
local OCP_VERSION=$3
local ACTION=$4
local CLUSTER=$PREFIX"-"$SUFIX
local CLUSTER_SECRETS="$(pwd)"/clusters/$CLUSTER/secret.yaml
local CLUSTER_PVC="$(pwd)"/clusters/$CLUSTER/pvc.yaml
local CLUSTER_CONFIG="$(pwd)"/clusters/$CLUSTER/ocp-cluster.yaml
local CLUSTER_VARIABLES="$(pwd)"/cluster-configuration/ocp-variables
### set variables related to the secrets
# sets the API Key for a given cluster deployment, converting it to base64
local IBMCLOUD_API_KEY=$(grep "IBMCLOUD_API_KEY" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
local IBMCLOUD_API_KEY_B64=$(echo -n $IBMCLOUD_API_KEY | base64)
sed -i -e "s/ibm-cloud-api-key/$IBMCLOUD_API_KEY_B64/g" "$CLUSTER_SECRETS"
# sets the RHEL subscription username for a given cluster deployment, converting it to base64
local RHEL_SUBS_USERNAME=$(grep "RHEL_SUBS_USERNAME" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
local RHEL_SUBS_USERNAME64=$(echo -n $RHEL_SUBS_USERNAME | base64)
sed -i -e "s/redhat-subscription-username/$RHEL_SUBS_USERNAME64/g" "$CLUSTER_SECRETS"
# sets the RHEL subscription password for a given cluster deployment, converting it to base64
local RHEL_SUBS_PASSWORD=$(grep "RHEL_SUBS_PASSWORD" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
local RHEL_SUBS_PASSWORD64=$(echo -n $RHEL_SUBS_PASSWORD | base64)
sed -i -e "s/redhat-subscription-password/$RHEL_SUBS_PASSWORD64/g" "$CLUSTER_SECRETS"
### end secrets variables
### set variables related to the pvc
local STORAGE_CLASS_NAME=$(grep "STORAGE_CLASS_NAME" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/STORAGE_CLASS_NAME/$STORAGE_CLASS_NAME/g" "$CLUSTER_PVC"
local PVC_SIZE=$(grep "PVC_SIZE" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/PVC_SIZE/$PVC_SIZE/g" "$CLUSTER_PVC"
### end pvc variables
### set cluster deployment variables
local IBMCLOUD_REGION=$(grep "IBMCLOUD_REGION" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/ibmcloud_region/$IBMCLOUD_REGION/g" "$CLUSTER_CONFIG"
local IBMCLOUD_ZONE=$(grep "IBMCLOUD_ZONE" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/ibmcloud_zone/$IBMCLOUD_ZONE/g" "$CLUSTER_CONFIG"
local POWERVS_INSTANCE_ID=$(grep "POWERVS_INSTANCE_ID" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/powervs_instance_id/$POWERVS_INSTANCE_ID/g" "$CLUSTER_CONFIG"
local PRIVATE_NETWORK_NAME=$(grep "PRIVATE_NETWORK_NAME" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/private_network_name/$PRIVATE_NETWORK_NAME/g" "$CLUSTER_CONFIG"
local BASTION_IMAGE_NAME=$(grep "BASTION_IMAGE_NAME" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/bastion_image_name/$BASTION_IMAGE_NAME/g" "$CLUSTER_CONFIG"
local RHCOS_IMAGE_NAME=$(grep "RHCOS_IMAGE_NAME" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/rhcos_image_name/$RHCOS_IMAGE_NAME/g" "$CLUSTER_CONFIG"
local PROCESSOR_TYPE=$(grep "PROCESSOR_TYPE" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/processor_type/$PROCESSOR_TYPE/g" "$CLUSTER_CONFIG"
local SYSTEM_TYPE=$(grep "SYSTEM_TYPE" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/system_type/$SYSTEM_TYPE/g" "$CLUSTER_CONFIG"
local CLUSTER_DOMAIN=$(grep "CLUSTER_DOMAIN" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s/cluster_domain/$CLUSTER_DOMAIN/g" "$CLUSTER_CONFIG"
sed -i -e "s/sufix/$SUFIX/g" "$CLUSTER_CONFIG"
sed -i -e "s/prefix/$PREFIX/g" "$CLUSTER_CONFIG"
sed -i -e "s/VERSION/$OCP_VERSION/g" "$CLUSTER_CONFIG"
sed -i -e "s/OCP_VERSION/$OCP_VERSION/g" "$CLUSTER_VARIABLES"
sed -i -e "s/action/$ACTION/g" "$CLUSTER_CONFIG"
local HTTP_PROXY=$(grep "HTTP_PROXY" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s|http_proxy|$HTTP_PROXY|g" "$CLUSTER_CONFIG"
local HTTPS_PROXY=$(grep "HTTPS_PROXY" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s|https_proxy|$HTTPS_PROXY|g" "$CLUSTER_CONFIG"
local OPENSHIFT_INSTALL_TARBALL=$(grep "OPENSHIFT_INSTALL_TARBALL" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s|openshift_install_tarball|$OPENSHIFT_INSTALL_TARBALL|g" "$CLUSTER_CONFIG"
local OPENSHIFT_CLIENT_TARBALL=$(grep "OPENSHIFT_CLIENT_TARBALL" "$(pwd)"/cluster-configuration/ocp-variables | tr -d " " | awk -F "=" '{print $2}')
sed -i -e "s|openshift_client_tarball|$OPENSHIFT_CLIENT_TARBALL|g" "$CLUSTER_CONFIG"
### end cluster deployment variables
}
function check_variables() {
INPUT="$(pwd)"/cluster-configuration/ocp-variables
while IFS= read -r line; do
VAR=$(echo "$line" | awk '{split($0,var,"="); print var[1]}')
VALUE=$(echo "$line" | awk '{split($0,var,"="); print var[2]}')
if [ -z $VALUE ]; then
echo
echo "ERROR: $VAR is not set."
echo " check the $INPUT file and try again."
echo
exit 1
fi
done < "$INPUT"
}
function create_project() {
echo "creating deployment secrets..."
#oc new-project $1
}
function create_secrets() {
echo "creating deployment secrets..."
#oc create -f $1/secret.yaml
}
function create_pvc_claim() {
echo "creating pvc claim...."
#oc create -f $1/pvc.yaml
}
function act() {
echo "deploying new cluster..."
#oc create -f $1/ocp-cluster.yaml
}
function run (){
#check_dependencies
check_connectivity
local OCP_VERSIONS=("4.5" "4.6")
local ACTIONS=("apply" "destroy")
local ACTION=$1
if [ -z $ACTION ]; then
echo
echo "ERROR: please, select one of the supported actions ${ACTIONS[@]}."
echo " e.g: ./deploy apply"
echo
exit 1
elif [[ ! " ${ACTIONS[@]} " =~ " ${ACTION} " ]]; then
echo
echo "ERROR: this action is not supported."
echo " pick one of the following ${ACTIONS[@]}."
echo
exit 1
fi
if [[ "$ACTION" == "apply" ]]; then
local OCP_VERSION=$2
if [ -z $OCP_VERSION ]; then
echo
echo "ERROR: please, select one of the supported versions: ${OCP_VERSIONS[@]}."
echo " e.g: ./deploy apply 4.6"
echo
exit 1
elif [[ ! " ${OCP_VERSIONS[@]} " =~ " ${OCP_VERSION} " ]]; then
echo
echo "ERROR: this version of OpenShift ($OCP_VERSION) is not supported."
echo " pick one of the following: ${OCP_VERSIONS[@]}."
echo
exit 1
else
local TODAY=$(date "+%Y%m%d-%H%M%S")
local SUFIX=$(openssl rand -hex 5)
local PREFIX=$(echo "ocp-"$OCP_VERSION"-"$TODAY | tr -d .)
local CLUSTERID=$PREFIX"-"$SUFIX
local CLUSTER_CONFIG_LOCATION="$(pwd)"/clusters/$CLUSTERID
check_variables
mkdir -p "$(pwd)"/clusters/$CLUSTERID
# prepar cluster configurations
set_ocp_secrets "$CLUSTERID"
set_volume_claim "$CLUSTERID"
set_cluster_config "$CLUSTERID"
set_variables "$PREFIX" "$SUFIX" "$OCP_VERSION" "$ACTION"
#TODO automate the apply
# deploy cluster
# create_project "$CLUSTERID"
# create_secrets "$CLUSTER_CONFIG_LOCATION"
# create_pvc_claim "$CLUSTER_CONFIG_LOCATION"
# act "$CLUSTER_CONFIG_LOCATION"
echo $CLUSTERID
fi
elif [[ "$ACTION" == "destroy" ]]; then
local CLUSTERS_LOCATION="$(pwd)"/clusters/
local CLUSTERS=( $( ls $CLUSTERS_LOCATION ) )
local CLUSTER=$2
if [ -z $CLUSTER ]; then
echo
echo "ERROR: please, set the cluster you want to destroy."
echo " e.g: ./deploy destroy ocp-46-20210108-185057-4eb0cfab8a"
echo
exit 1
elif [[ ! " ${CLUSTERS[@]} " =~ " ${CLUSTER} " ]]; then
echo
echo "ERROR: looks like this cluster ($1) was not deployed."
echo " pick one of the following: ${CLUSTERS[@]}."
echo
exit 1
else
local CLUSTER_CONFIG_LOCATION="$(pwd)"/clusters/$CLUSTER
local CLUSTER_CONFIG=$CLUSTER_CONFIG_LOCATION/ocp-cluster.yaml
sed -i -e "s/value: \"apply\"/value: \"destroy\"/g" "$CLUSTER_CONFIG"
#TODO automate the destroy
# act "$CLUSTER_CONFIG_LOCATION"
fi
fi
}
run "$@"