|
| 1 | +#!/bin/bash |
| 2 | +set -x |
| 3 | +[ "$(pwd)" != "/etc/mysql/conf.d" ] && cp * /etc/mysql/conf.d/ |
| 4 | + |
| 5 | +HOST_ID=${HOSTNAME##*-} |
| 6 | + |
| 7 | +STATEFULSET_SERVICE=$(dnsdomainname -d) |
| 8 | +POD_FQDN=$(dnsdomainname -A) |
| 9 | + |
| 10 | +echo "This is pod $HOST_ID ($POD_FQDN) for statefulset $STATEFULSET_SERVICE" |
| 11 | + |
| 12 | +[ -z "$WSREP_CLUSTER_ADDRESS" ] && echo "Missing WSREP_CLUSTER_ADDRESS env" && exit 1 |
| 13 | +sed -i "s|^#init-wsrep#.*|wsrep_cluster_address=$WSREP_CLUSTER_ADDRESS|" /etc/mysql/conf.d/galera.cnf |
| 14 | + |
| 15 | +[ -z "$DATADIR" ] && exit "Missing DATADIR variable" && exit 1 |
| 16 | + |
| 17 | +SUGGEST_EXEC_COMMAND="kubectl --namespace=$POD_NAMESPACE exec -c init-config $POD_NAME --" |
| 18 | + |
| 19 | +function wsrepNewCluster { |
| 20 | + sed -i 's|^#init-new-cluster#||' /etc/mysql/conf.d/galera.cnf |
| 21 | +} |
| 22 | + |
| 23 | +function wsrepRecover { |
| 24 | + sed -i 's|^#init-recover#||' /etc/mysql/conf.d/galera.cnf |
| 25 | +} |
| 26 | + |
| 27 | +function wsrepForceBootstrap { |
| 28 | + sed -i 's|safe_to_bootstrap: 0|safe_to_bootstrap: 1|' /data/db/grastate.dat |
| 29 | +} |
| 30 | + |
| 31 | +[[ $STATEFULSET_SERVICE = mariadb.* ]] || echo "WARNING: unexpected service name $STATEFULSET_SERVICE, Peer detection below may fail falsely." |
| 32 | + |
| 33 | +if [ $HOST_ID -eq 0 ]; then |
| 34 | + echo "This is the 1st statefulset pod. Checking if the statefulset is down ..." |
| 35 | + getent hosts mariadb-ready |
| 36 | + [ $? -eq 2 ] && { |
| 37 | + # https://github.com/docker-library/mariadb/commit/f76084f0f9dc13f29cce48c727440eb79b4e92fa#diff-b0fa4b30392406b32de6b8ffe36e290dR80 |
| 38 | + if [ ! -d "$DATADIR/mysql" ]; then |
| 39 | + echo "No database in $DATADIR; configuring $POD_NAME for initial start" |
| 40 | + wsrepNewCluster |
| 41 | + else |
| 42 | + set +x |
| 43 | + echo "----- ACTION REQUIRED -----" |
| 44 | + echo "No peers found, but data exists. To start in wsrep_new_cluster mode, run:" |
| 45 | + echo " $SUGGEST_EXEC_COMMAND touch /tmp/confirm-new-cluster" |
| 46 | + echo "Or to start in recovery mode, to see replication state, run:" |
| 47 | + echo " $SUGGEST_EXEC_COMMAND touch /tmp/confirm-recover" |
| 48 | + echo "Or to force bootstrap on this node, potentially losing writes, run:" |
| 49 | + echo " $SUGGEST_EXEC_COMMAND touch /tmp/confirm-force-bootstrap" |
| 50 | + #echo " NOTE This bypasses the following warning from new cluster mode:" |
| 51 | + #echo " It may not be safe to bootstrap the cluster from this node. It was not the last one to leave the cluster and may not contain all the updates. To force cluster bootstrap with this node, edit the grastate.dat file manually and set safe_to_bootstrap to 1 ." |
| 52 | + echo "Or to try a regular start (for example after recovery + manual intervention), run:" |
| 53 | + echo " $SUGGEST_EXEC_COMMAND touch /tmp/confirm-resume" |
| 54 | + if [ ! -z "$AUTO_RECOVERY_MODE" ]; then |
| 55 | + echo "The AUTO_RECOVERY_MODE env was set to $AUTO_RECOVERY_MODE, will trigger that choice" |
| 56 | + touch /tmp/$AUTO_RECOVERY_MODE |
| 57 | + else |
| 58 | + echo "Waiting for response ..." |
| 59 | + fi |
| 60 | + while [ ! -f /tmp/confirm-resume ]; do |
| 61 | + if [ "$AUTO_NEW_CLUSTER" = "true" ]; then |
| 62 | + echo "The AUTO_NEW_CLUSTER env was set to $AUTO_NEW_CLUSTER, will proceed without confirmation" |
| 63 | + echo "NOTE this env is deprecated, use AUTO_RECOVERY_MODE instead" |
| 64 | + wsrepNewCluster |
| 65 | + touch /tmp/confirm-resume |
| 66 | + elif [ -f /tmp/confirm-new-cluster ]; then |
| 67 | + echo "Confirmation received. Resuming new cluster start ..." |
| 68 | + wsrepNewCluster |
| 69 | + touch /tmp/confirm-resume |
| 70 | + elif [ -f /tmp/confirm-force-bootstrap ]; then |
| 71 | + echo "Forcing bootstrap on this node ..." |
| 72 | + wsrepForceBootstrap |
| 73 | + touch /tmp/confirm-new-cluster |
| 74 | + elif [ -f /tmp/confirm-recover ]; then |
| 75 | + echo "Confirmation received. Resuming in recovery mode." |
| 76 | + echo "Note: to start the other pods you need to edit OrderedReady and add a command: --wsrep-recover" |
| 77 | + wsrepRecover |
| 78 | + touch /tmp/confirm-resume |
| 79 | + fi |
| 80 | + sleep 1 |
| 81 | + done |
| 82 | + rm /tmp/confirm-* |
| 83 | + set -x |
| 84 | + fi |
| 85 | + } |
| 86 | +else |
| 87 | + getent hosts mariadb-ready |
| 88 | + [ $? -eq 2 ] && { |
| 89 | + echo "This is NOT the 1st statefulset pod. Must not go up as primary." |
| 90 | + echo "Found no ready pods. Will exit to trigger a crash loop back off." |
| 91 | + exit 1 |
| 92 | + } |
| 93 | +fi |
| 94 | + |
| 95 | +# https://github.com/docker-library/mariadb/blob/master/10.2/docker-entrypoint.sh#L62 |
| 96 | +mysqld --verbose --help --log-bin-index="$(mktemp -u)" | tee /tmp/mariadb-start-config | grep -e ^version -e ^datadir -e ^wsrep -e ^binlog -e ^character-set -e ^collation |
0 commit comments