|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# Copyright 2016 The Kubernetes Authors All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# This script writes out a mysql galera config using a list of newline seperated |
| 18 | +# peer DNS names it accepts through stdin. |
| 19 | + |
| 20 | +# /etc/mysql is assumed to be a shared volume so we can modify my.cnf as required |
| 21 | +# to keep the config up to date, without wrapping mysqld in a custom pid1. |
| 22 | +# The config location is intentionally not /etc/mysql/my.cnf because the |
| 23 | +# standard base image clobbers that location. |
| 24 | +CFG=/etc/mysql/my-galera.cnf |
| 25 | + |
| 26 | +function join { |
| 27 | + local IFS="$1"; shift; echo "$*"; |
| 28 | +} |
| 29 | + |
| 30 | +HOSTNAME=$(hostname) |
| 31 | +# Parse out cluster name, formatted as: petset_name-index |
| 32 | +IFS='-' read -ra ADDR <<< "$(hostname)" |
| 33 | +CLUSTER_NAME="${ADDR[0]}" |
| 34 | + |
| 35 | +while read -ra LINE; do |
| 36 | + if [[ "${LINE}" == *"${HOSTNAME}"* ]]; then |
| 37 | + MY_NAME=$LINE |
| 38 | + fi |
| 39 | + PEERS=("${PEERS[@]}" $LINE) |
| 40 | +done |
| 41 | + |
| 42 | +if [ "${#PEERS[@]}" = 1 ]; then |
| 43 | + WSREP_CLUSTER_ADDRESS="" |
| 44 | +else |
| 45 | + WSREP_CLUSTER_ADDRESS=$(join , "${PEERS[@]}") |
| 46 | +fi |
| 47 | +sed -i -e "s|^wsrep_node_address=.*$|wsrep_node_address=${MY_NAME}|" ${CFG} |
| 48 | +sed -i -e "s|^wsrep_cluster_name=.*$|wsrep_cluster_name=${CLUSTER_NAME}|" ${CFG} |
| 49 | +sed -i -e "s|^wsrep_cluster_address=.*$|wsrep_cluster_address=gcomm://${WSREP_CLUSTER_ADDRESS}|" ${CFG} |
| 50 | + |
| 51 | +# don't need a restart, we're just writing the conf in case there's an |
| 52 | +# unexpected restart on the node. |
0 commit comments