|
1 | | -#!/bin/bash -e |
| 1 | +#!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | <% if p('cf_mysql_enabled') == true %> |
| 4 | +set -e -o pipefail |
| 5 | + |
| 6 | +<% |
| 7 | + cluster_ips = link('mysql').instances.map(&:address) |
| 8 | + if_link('arbitrator') do |
| 9 | + cluster_ips += link('arbitrator').instances.map(&:address) |
| 10 | + end |
| 11 | +%> |
| 12 | + |
| 13 | +CLUSTER_NODES=(<%= cluster_ips.map{|e| e }.join(' ') %>) |
| 14 | +MYSQL_PORT=<%= p("cf_mysql.mysql.port") %> |
| 15 | +GALERA_HEALTHCHECK_PORT=<%= p("cf_mysql.mysql.galera_healthcheck.port") %> |
| 16 | +LOG_DIR="/var/vcap/sys/log/mysql" |
| 17 | + |
| 18 | +# If the node is not running, exit drain successfully |
| 19 | +if ! ps -p "$(</var/vcap/sys/run/mysql/mysql.pid)" >/dev/null; then |
| 20 | + echo "$(date): mysql is not running: OK to drain" >> "${LOG_DIR}/drain.log" |
| 21 | + echo 0; exit 0 # drain success |
| 22 | +fi |
| 23 | + |
| 24 | +# Check the galera healthcheck endpoint on all of the nodes. If the http status returned is 000, there |
| 25 | +# is no node at that IP, so we assume we are scaling down. If the http status returned is 200 from all nodes |
| 26 | +# it will continue to drain. If it detects any other nodes to be unhealthy, it will fail to drain |
| 27 | +# and exit. |
| 28 | +for NODE in "${CLUSTER_NODES[@]}"; do |
| 29 | + set +e |
| 30 | + status_code=$(curl -s -o "/dev/null" -w "%{http_code}" "$NODE:$GALERA_HEALTHCHECK_PORT") |
| 31 | + set -e |
| 32 | + if [[ $status_code -eq 000 || $status_code -eq 200 ]]; then |
| 33 | + continue |
| 34 | + else |
| 35 | + echo "$(date): galera heathcheck returned $status_code; drain failed on node ${NODE}" >> "${LOG_DIR}/drain.err.log" |
| 36 | + exit -1 |
| 37 | + fi |
| 38 | +done |
| 39 | + |
| 40 | +# Actually drain with a kill_and_wait on the mysql pid |
4 | 41 | PIDFILE=/var/vcap/sys/run/mariadb_ctl/mariadb_ctl.pid |
5 | 42 | source /var/vcap/packages/cf-mysql-common/pid_utils.sh |
6 | 43 |
|
7 | 44 | set +e |
8 | | -kill_and_wait ${PIDFILE} 300 0 > /dev/null |
| 45 | +kill_and_wait "${PIDFILE}" 300 0 > /dev/null |
9 | 46 | return_code=$? |
10 | 47 |
|
11 | 48 | echo 0 |
12 | 49 | exit ${return_code} |
| 50 | + |
13 | 51 | <% else %> |
14 | 52 | echo 0 |
15 | 53 | exit 0 |
|
0 commit comments