forked from stolostron/bootstrap-ks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy.sh
executable file
·64 lines (52 loc) · 1.72 KB
/
destroy.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
#!/bin/bash
# Color codes for bash output
BLUE='\e[36m'
GREEN='\e[32m'
RED='\e[31m'
YELLOW='\e[33m'
CLEAR='\e[39m'
#----LOAD ENV VARS FROM INPUT FILE----#
if [ -z "$1" ]; then
printf "Usage: ./destroy.sh <path-to-json-file>\n"
exit 1
fi
if [ ! -f "$1" ]; then
printf "$1 does not exist, exiting\n"
exit 1
fi
CLUSTER_NAME=$(cat $1 | jq -r '.CLUSTER_NAME')
CLUSTER_ID=$(cat $1 | jq -r '.CLUSTER_ID')
REGION=$(cat $1 | jq -r '.REGION')
OCM_URL=$(cat $1 | jq -r '.OCM_URL')
#----VALIDATE ENV VARS----#
# Validate that we have all required env vars and exit with a failure if any are missing
missing=0
if [ -z "$OCM_TOKEN" ]; then
printf "${RED}OCM_TOKEN env var not set. flagging for exit.${CLEAR}\n"
missing=1
fi
if [ "$missing" -ne 0 ]; then
exit $missing
fi
#----VERIFY ocm CLI----#
if [ -z "$(which ocm)" ]; then
printf "${RED}Could not find the ocm cli, exiting. Try running ./install.sh.${CLEAR}\n"
exit 1
fi
#----SIGN IN TO ocm----#
if [ -f ~/.ocm.json ]; then
REFRESH_TOKEN=`cat ~/.ocm.json | jq -r '.refresh_token'`
CLIENT_ID=`cat ~/.ocm.json | jq -r '.client_id'`
curl --silent https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token -d grant_type=refresh_token -d client_id=$CLIENT_ID -d refresh_token=$REFRESH_TOKEN > /dev/null
else
ocm login --token=$OCM_TOKEN --url $OCM_URL
fi
#----DELETE OCM-AWS CLUSTER----#
printf "${BLUE}Deleting the OCM-AWS cluster named ${CLUSTER_NAME}.${CLEAR}\n"
ocm delete /api/clusters_mgmt/v1/clusters/$CLUSTER_ID
if [ "$?" -ne 0 ]; then
printf "${RED}Failed to delete cluster ${CLUSTER_NAME}, exiting${CLEAR}\n"
exit 1
fi
printf "${CLEAR}"
printf "${GREEN}Successfully cleaned up the cluster named ${CLUSTER_NAME}.${CLEAR}\n"