blob: ec09df3a86141e0c31252eacf438297cbb93c6fe [file] [log] [blame]
tringuyeneb9eb592020-04-01 17:51:06 +00001#!/bin/bash
2
3TEMP_POD=$(kubectl get pod -n $NAMESPACE_ENV --selector \
4 app='{{ include "common.fullname" . }}' -o \
5 jsonpath='{.items[?(@.metadata.ownerReferences[].kind=="ReplicaSet")].metadata.name}')
6
7tmp_MYSQL_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv \
8 MYSQL_PASSWORD) | base64)
9
10tmp_ROOT_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv \
11 MYSQL_ROOT_PASSWORD) | base64)
12
13FLAG_EX_ROOT_SEC='{{ include "common.secret.getSecretNameFast" (dict "global" . "uid" (include "common.mariadb.secret.rootPassUID" .)) }}'
14
15FLAG_EX_SEC='{{ include "common.secret.getSecretNameFast" (dict "global" . "uid" (include "common.mariadb.secret.userCredentialsUID" .)) }}'
16
17kubectl patch secret $FLAG_EX_ROOT_SEC -p \
18 '{"data":{"password":"'"$tmp_ROOT_PASSWORD"'"}}'
19
20kubectl patch secret $FLAG_EX_SEC -p \
21 '{"data":{"password":"'"$tmp_MYSQL_PASSWORD"'"}}'
22
Marat Salakhutdinova72170b2020-06-27 23:18:17 -040023MYSQL_USER=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_USER)
24
25MYSQL_PASSWORD=$(echo -n $(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_PASSWORD))
26
27MYSQL_ROOT_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- printenv MYSQL_ROOT_PASSWORD)
28
29CURRENT_STS_REPLICA=$(kubectl get statefulsets -n $NAMESPACE_ENV \
30 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
31
32DEPLOYMENT_REPLICA=$(kubectl get deployment -n $NAMESPACE_ENV \
33 {{ include "common.fullname" . }}-upgrade-deployment -o \
34 jsonpath='{.status.replicas}')
35
36if [[ $CURRENT_STS_REPLICA == "0" ]]
37then
38 echo "Seems there was no upgrade of cluster and we will scale up cluster replicas back to $REPLICA_COUNT now"
39 kubectl scale statefulsets {{ include "common.fullname" . }} --replicas=$REPLICA_COUNT
40fi
41
42MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \
43 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
44
45while [[ ! $MY_REPLICA_NUMBER == $REPLICA_COUNT ]]
46do
47 echo "The cluster is not scaled up to $REPLICA_COUNT yet. Please wait ..."
48 MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \
49 {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}')
50 echo "The current status of the cluster is $MY_REPLICA_NUMBER"
51 sleep 2
52 if [[ $MY_REPLICA_NUMBER == $REPLICA_COUNT ]]
53 then
54 break
55 fi
56done
57
58CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \
59 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
60 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \
61 awk '{print $2}')
62
63CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \
64 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
65 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
66 | awk '{print $2}')
67
68while [[ ! $CLUSTER_NO == $((REPLICA_COUNT+DEPLOYMENT_REPLICA)) ]] \
69 || [[ ! $CLUSTER_STATE == "Synced" ]]
70do
71 echo "$CLUSTER_NO and $CLUSTER_STATE"
72 CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \
73 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
74 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \
75 | awk '{print $2}')
76 CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \
77 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
78 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
79 | awk '{print $2}')
80 sleep 2
81 if [[ $CLUSTER_NO == $((REPLICA_COUNT+DEPLOYMENT_REPLICA)) ]] \
82 && [[ $CLUSTER_STATE == "Synced" ]]
83 then
84 echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state."
85 break
86 fi
87done
88
89MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysqladmin \
90 -uroot -p$MYSQL_ROOT_PASSWORD ping)
91
92while [[ ! $MYSQL_STATUS == "mysqld is alive" ]]
93do
94 echo "Mariadb deployment is not ready yet."
95 sleep 2
96 MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysqladmin \
97 -uroot -p$MYSQL_ROOT_PASSWORD ping)
98 if [[ $MYSQL_STATUS == "mysqld is alive" ]]
99 then
100 echo "Mariadb deployment is ready and cluster size is $CLUSTER_NO"
101 break
102 fi
103done
104
105echo "Deleting upgrade deployment now"
106
tringuyeneb9eb592020-04-01 17:51:06 +0000107kubectl delete deployment -n $NAMESPACE_ENV {{ include "common.fullname" . }}-upgrade-deployment
108kubectl delete secret -n $NAMESPACE_ENV {{ include "common.fullname" . }}-temp-upgrade-root
Marat Salakhutdinova72170b2020-06-27 23:18:17 -0400109kubectl delete secret -n $NAMESPACE_ENV {{ include "common.fullname" . }}-temp-upgrade-usercred
110
111CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- \
112 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
113 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \
114 awk '{print $2}')
115
116CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- \
117 mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
118 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
119 | awk '{print $2}')
120
121while [[ ! $CLUSTER_NO == $REPLICA_COUNT ]] \
122 || [[ ! $CLUSTER_STATE == "Synced" ]]
123do
124 echo "$CLUSTER_NO and $CLUSTER_STATE"
125 CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysql \
126 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
127 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \
128 | awk '{print $2}')
129 CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV {{ include "common.fullname" . }}-0 -- mysql \
130 --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \
131 -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \
132 | awk '{print $2}')
133 sleep 2
134 if [[ $CLUSTER_NO == $REPLICA_COUNT ]] \
135 && [[ $CLUSTER_STATE == "Synced" ]]
136 then
137 echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state."
138 break
139 fi
140done
141
142echo "The cluster upgrade is finished now"