blob: ff44606e2308ef5502ce6ee18b7900ff0407cb49 [file] [log] [blame]
tringuyeneb9eb592020-04-01 17:51:06 +00001#!/bin/bash
2MYSQL_USER=$(kubectl exec -n $NAMESPACE_ENV \
3 {{ include "common.fullname" . }}-0 -- printenv MYSQL_USER)
4
5MYSQL_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV \
6 {{ include "common.fullname" . }}-0 -- printenv MYSQL_PASSWORD)
7
8MYSQL_ROOT_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV \
9 {{ include "common.fullname" . }}-0 -- printenv MYSQL_ROOT_PASSWORD)
10
11kubectl create secret generic \
12 '{{ include "common.fullname" . }}'-temp-upgrade-root \
13 --from-literal=password=$MYSQL_ROOT_PASSWORD
14
15kubectl create secret generic \
16 '{{ include "common.fullname" . }}'-temp-upgrade-usercred \
17 --from-literal=login=$MYSQL_USER --from-literal=password=$MYSQL_PASSWORD
18
19kubectl create -f /upgrade/create-deployment.yml
20
21TEMP_POD=$(kubectl get pod -n $NAMESPACE_ENV --selector \
22 app='{{ include "common.fullname" . }}' -o \
23 jsonpath='{.items[?(@.metadata.ownerReferences[].kind=="ReplicaSet")].metadata.name}')
24
25CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \
26 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
27 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \
28 awk '{print $2}')
29
30CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \
31 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
32 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
33 | awk '{print $2}')
34
35STS_REPLICA=$(kubectl get statefulsets -n $NAMESPACE_ENV \
36 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
37
38DEPLOYMENT_REPLICA=$(kubectl get deployment -n $NAMESPACE_ENV \
39 {{ include "common.fullname" . }}-upgrade-deployment -o \
40 jsonpath='{.status.replicas}')
41
42while [[ ! $CLUSTER_NO == $((STS_REPLICA+DEPLOYMENT_REPLICA)) ]] \
43 || [[ ! $CLUSTER_STATE == "Synced" ]]
44do
45 echo "$CLUSTER_NO and $CLUSTER_STATE"
46 CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \
47 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
48 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \
49 | awk '{print $2}')
50 CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \
51 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
52 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
53 | awk '{print $2}')
54 sleep 2
55 if [[ $CLUSTER_NO == $((STS_REPLICA+DEPLOYMENT_REPLICA)) ]] \
56 && [[ $CLUSTER_STATE == "Synced" ]]
57 then
58 echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state."
59 break
60 fi
61done
62
63MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysqladmin \
64 -uroot -p$MYSQL_ROOT_PASSWORD ping)
65
66while [[ ! $MYSQL_STATUS == "mysqld is alive" ]]
67do
68 echo "Mariadb deployment is not ready yet."
69 sleep 2
70 MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysqladmin \
71 -uroot -p$MYSQL_ROOT_PASSWORD ping)
72 if [[ $MYSQL_STATUS == "mysqld is alive" ]]
73 then
74 echo "Mariadb deployment is ready."
75 break
76 fi
77done
78
79kubectl scale statefulsets {{ include "common.fullname" . }} --replicas=0
80MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \
81 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
82echo "The the cluster has $MY_REPLICA_NUMBER replicas."
83
84while [[ ! $MY_REPLICA_NUMBER == "0" ]]
85do
86 echo "The cluster is not scaled to 0 yet. Please wait ..."
87 MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \
88 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
89 echo "The current status of the cluster is $MY_REPLICA_NUMBER"
90 sleep 2
91 if [[ $MY_REPLICA_NUMBER == "0" ]]
92 then
93 break
94 fi
95done
96
97for (( index=0; index<$STS_REPLICA; index+=1 ))
98do
99 kubectl delete pvc \
100 "{{ include "common.fullname" . }}-data-{{ include "common.fullname" . }}-$index"
101done