forked from ids/cluster-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster-update
executable file
·187 lines (156 loc) · 5.56 KB
/
cluster-update
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
#!/bin/bash
INVENTORY_FILE=$1
SERVER_WAIT=90s
REMOTE_USER=root
export ANSIBLE_DISPLAY_SKIPPED_HOSTS=false
export ANSIBLE_CONFIG=./ansible.cfg
function k8s_patch () {
echo
echo -e " ${TC}Verifying host availability:${NC}"
echo
ansible all -i $INVENTORY_FILE -m ping -u $REMOTE_USER
if [ $? -ne 0 ]; then
echo "FAIL: ${CLUSTER_TYPE_NAME} host access verification failed!"
exit 1
fi
echo
echo -e " ${TC}Updating cluster: ${CLUSTER_TYPE_NAME}${NC}"
echo
ansible-playbook -i $INVENTORY_FILE ansible/k8s-update.yml --extra-vars="cluster_pkg_folder=${CLUSTER_PKG_FOLDER}"
if [ $? -ne 0 ]; then
echo "FAIL: ${CLUSTER_TYPE_NAME} update failed!"
exit 1
fi
if [ -f clusters/$CLUSTER_PKG_FOLDER/kubeadm_upgrade_results ]; then
cat clusters/$CLUSTER_PKG_FOLDER/kubeadm_upgrade_results
echo
fi
if [ -f clusters/$CLUSTER_PKG_FOLDER/test-cluster ]; then
bash clusters/$CLUSTER_PKG_FOLDER/test-cluster
fi
echo -e "${TC}------------------------------------------------------------${NC}"
echo -e "${TCX}UPDATE SUCCESS${NC}: ${CLUSTER_TYPE_NAME}!"
}
function cluster_info_display() {
echo
echo -e "${TC}-----------------------------------------------------------------------------------${NC}"
echo
echo -e " ${TCX}Cluster Update${NC}"
echo
echo -e " Cluster Name : ${TC}${CLUSTER_NAME}${NC}"
echo -e " Cluster Package : ${TC}${CLUSTER_PKG_FOLDER}${NC}"
echo -e " Cluster Type : ${TC}${CLUSTER_TYPE} - ${CLUSTER_TYPE_NAME}${NC}"
echo -e " VMware Target : ${TC}${VMWARE_TARGET}${NC}"
if [ "$CLUSTER_TYPE" == "centos-k8s" ] || [ "$CLUSTER_TYPE" == "centos8-k8s" ] || [ "$CLUSTER_TYPE" == "fedora-k8s" ] || [ "$CLUSTER_TYPE" == "ubuntu-k8s" ]; then
if [ "$K8S_VERSION" == "" ]; then
K8S_VERSION=default
fi
if [ "$K8S_CNI" == "" ]; then
K8S_CNI=default
fi
if [ "$K8S_CRI" == "" ]; then
K8S_CRI=default
fi
echo -e " K8s Version : ${TC}${K8S_VERSION}${NC}"
echo -e " K8s CRI : ${TC}${K8S_CRI}${NC}"
echo -e " K8s CNI : ${TC}${K8S_CNI}${NC}"
fi
echo
echo -e "${TC}-----------------------------------------------------------------------------------${NC}"
echo
}
PLATFORM=`uname -ra`
VMWARE_APP="VMware Workstation"
if [[ $PLATFORM == *"Microsoft"* ]]; then
export ANSIBLE_CONFIG=$PWD/ansible.cfg
elif [[ $PLATFORM == *"Darwin"* ]]; then
VMWARE_APP="VMware Fusion"
fi
if [ -z "${INVENTORY_FILE}" ]; then
read -s "Enter the target cluster folder: " INVENTORY_FILE
fi
INVENTORY_FILE=./clusters/$INVENTORY_FILE/hosts
if [ ! -f $INVENTORY_FILE ]; then
echo
echo "Unable to find inventory file @: ${INVENTORY_FILE}"
exit 1
fi
CLUSTER_TYPE=`sed -n '/^cluster_type=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
VMWARE_TARGET=`sed -n '/^vmware_target=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
CLUSTER_NAME=`sed -n '/^cluster_name=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
REMOTE_USER=`sed -n '/^remote_user=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
CLUSTER_PROTECTED=`sed -n '/^protected=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
K8S_VERSION=`sed -n '/^k8s_version=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
K8S_CRI=`sed -n '/^k8s_container_runtime=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
K8S_CNI=`sed -n '/^k8s_network_cni=/ {s///p;q;}' $INVENTORY_FILE | tr -d '\n' | tr -d '\r'`
if [ "$CLUSTER_PROTECTED" == "true" ]; then
echo
echo -e "${TC}Cluster Patching Blocked!${NC}"
echo "The cluster has been marked protected=true"
echo
echo "To PATCH the cluster ${CLUSTER_NAME} remove this flag, "
echo "temporarily from the ${CLUSTER_PKG_FOLDER}/hosts file."
echo
exit 1
fi
CLUSTER_PKG_PATH=`dirname $INVENTORY_FILE`
CLUSTER_PKG_FOLDER=${CLUSTER_PKG_PATH//.\/clusters\//}
if [ -f clusters/$CLUSTER_PKG_FOLDER/.passwords ]; then
source clusters/$CLUSTER_PKG_FOLDER/.passwords
fi
TEMP_SCRIPT_PATH=./tmp/$CLUSTER_PKG_FOLDER
if [ "$CLUSTER_TYPE" == 'centos-dcos' ]; then
echo "** DC/OS Not Supported! **"
exit;
elif [ "$CLUSTER_TYPE" == 'centos-k8s' ]; then
CLUSTER_TYPE_NAME='CentOS 7 Kubernetes'
REMOTE_USER=admin
elif [ "$CLUSTER_TYPE" == 'centos8-k8s' ]; then
CLUSTER_TYPE_NAME='CentOS 8 Kubernetes'
REMOTE_USER=admin
elif [ "$CLUSTER_TYPE" == 'fedora-k8s' ]; then
CLUSTER_TYPE_NAME='Fedora Kubernetes'
REMOTE_USER=admin
elif [ "$CLUSTER_TYPE" == 'ubuntu-k8s' ]; then
CLUSTER_TYPE_NAME='Ubuntu Kubernetes'
REMOTE_USER=sysop
elif [ "$CLUSTER_TYPE" == 'targetd-server' ]; then
echo "** Targetd patching not supported using this script, please patch manually! **"
exit;
fi
TC=''
NC=''
if [ "$TERM" == "xterm-256color" ]; then
TC='\033[1;34m'
NC='\033[0m' # No Color
fi
TCR=''
if [ "$TERM" == "xterm-256color" ]; then
TCR='\033[33;4m'
fi
TCX=''
if [ "$TERM" == "xterm-256color" ]; then
TCX='\033[2;1m'
fi
cluster_info_display
echo
echo -e " ${TCR}ATTENTION!${NC}"
echo
echo " Cluster update uses the --delete-local-data flag when draining."
echo -e " All PODs using ${TC}local data${NC} will be re-initialized with all EXISTING DATA LOST!"
echo
read -p " Are you sure you want to begin updating this cluster? (y/n): " patch_confirm
if [ "$patch_confirm" != "y" ] && [ "$patch_confirm" != "Y" ]; then
echo
echo -e "${TC}!! Cluster update aborted!${NC}"
echo
exit 1
fi
START_TIME=$SECONDS
k8s_patch
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Cluster Updated in: $(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) sec"
echo -e "${TC}------------------------------------------------------------${NC}"
echo
echo -e "Enjoy your updated ${TCX}${CLUSTER_TYPE_NAME}${NC}!"
echo