-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathpxc-configure-pxc.sh
executable file
·128 lines (113 loc) · 5.34 KB
/
pxc-configure-pxc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /bin/bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script writes out a mysql galera config using a list of newline seperated
# peer DNS names it accepts through stdin.
# /etc/mysql is assumed to be a shared volume so we can modify my.cnf as required
# to keep the config up to date, without wrapping mysqld in a custom pid1.
# The config location is intentionally not /etc/mysql/my.cnf because the
# standard base image clobbers that location.
set -o errexit
set -o xtrace
function join {
local IFS="$1"
shift
echo "$*"
}
function mysql_root_exec() {
local server="$1"
local query="$2"
{ set +x; } 2>/dev/null
MYSQL_PWD="${OPERATOR_ADMIN_PASSWORD:-operator}" timeout 600 mysql -h "${server}" -P 33062 -uoperator -s -NB -e "${query}"
set -x
}
NODE_IP=$(hostname -I | awk ' { print $1 } ')
CLUSTER_NAME="$(hostname -f | cut -d'.' -f2)"
SERVER_NUM=${HOSTNAME/$CLUSTER_NAME-/}
SERVER_ID=${CLUSTER_HASH}${SERVER_NUM}
NODE_NAME=$(hostname -f)
NODE_PORT=3306
while read -ra LINE; do
echo "read line $LINE"
LINE_IP=$(getent hosts "$LINE" | awk '{ print $1 }')
if [ "$LINE_IP" != "$NODE_IP" ]; then
LINE_HOST=$(mysql_root_exec "$LINE_IP" 'select @@hostname' || :)
if [ -n "$LINE_HOST" ]; then
PEERS=("${PEERS[@]}" $LINE_HOST)
PEERS_FULL=("${PEERS_FULL[@]}" "$LINE_HOST.$CLUSTER_NAME")
else
PEERS_FULL=("${PEERS_FULL[@]}" $LINE_IP)
fi
fi
done
if [ "${#PEERS[@]}" != 0 ]; then
DONOR_ADDRESS="$(printf '%s\n' "${PEERS[@]}" "${HOSTNAME}" | sort --version-sort | uniq | grep -v -- '-0$' | sed '$d' | tr '\n' ',' | sed 's/^,$//')"
fi
if [ "${#PEERS_FULL[@]}" != 0 ]; then
WSREP_CLUSTER_ADDRESS="$(printf '%s\n' "${PEERS_FULL[@]}" | sort --version-sort | tr '\n' ',' | sed 's/,$//')"
fi
CFG=/etc/mysql/node.cnf
MYSQL_VERSION=$(mysqld -V | awk '{print $3}' | awk -F'.' '{print $1"."$2}')
if [[ "$MYSQL_VERSION" =~ ^(8\.0|8\.4)$ ]]; then
grep -E -q "^[#]?admin-address" "$CFG" || sed '/^\[mysqld\]/a admin-address=\n' ${CFG} 1<>${CFG}
grep -E -q "^[#]?log_error_suppression_list" "$CFG" || sed '/^\[mysqld\]/a log_error_suppression_list="MY-010055"\n' ${CFG} 1<>${CFG}
else
grep -E -q "^[#]?extra_max_connections" "$CFG" || sed '/^\[mysqld\]/a extra_max_connections=\n' ${CFG} 1<>${CFG}
grep -E -q "^[#]?extra_port" "$CFG" || sed '/^\[mysqld\]/a extra_port=\n' ${CFG} 1<>${CFG}
fi
if [ "$IS_LOGCOLLECTOR" == 'yes' ]; then
grep -E -q "^[#]?log-error" "$CFG" || sed "/^\[mysqld\]/a log-error=$LOG_DATA_DIR/mysqld-error.log\n" ${CFG} 1<>${CFG}
fi
grep -E -q "^[#]?wsrep_sst_donor" "$CFG" || sed '/^\[mysqld\]/a wsrep_sst_donor=\n' ${CFG} 1<>${CFG}
grep -E -q "^[#]?wsrep_node_incoming_address" "$CFG" || sed '/^\[mysqld\]/a wsrep_node_incoming_address=\n' ${CFG} 1<>${CFG}
grep -E -q "^[#]?wsrep_provider_options" "$CFG" || sed '/^\[mysqld\]/a wsrep_provider_options="pc.weight=10"\n' ${CFG} 1<>${CFG}
sed -r "s|^[#]?server_id=.*$|server_id=${SERVER_ID}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?coredumper$|coredumper|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?wsrep_node_address=.*$|wsrep_node_address=${NODE_IP}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?wsrep_cluster_name=.*$|wsrep_cluster_name=${CLUSTER_NAME}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?wsrep_sst_donor=.*$|wsrep_sst_donor=${DONOR_ADDRESS}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?wsrep_cluster_address=.*$|wsrep_cluster_address=gcomm://${WSREP_CLUSTER_ADDRESS}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?wsrep_node_incoming_address=.*$|wsrep_node_incoming_address=${NODE_NAME}:${NODE_PORT}|" ${CFG} 1<>${CFG}
{ set +x; } 2>/dev/null
ESCAPED_XTRABACKUP_PASSWORD=$(printf '%s\n' "$XTRABACKUP_PASSWORD" | sed -e 's/[]\|\&\!$*.^[]/\\&/g')
sed -r "s|^[#]?wsrep_sst_auth=.*$|wsrep_sst_auth='xtrabackup:$ESCAPED_XTRABACKUP_PASSWORD'|" ${CFG} 1<>${CFG}
set -x
sed -r "s|^[#]?admin-address=.*$|admin-address=${NODE_IP}|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?extra_max_connections=.*$|extra_max_connections=100|" ${CFG} 1<>${CFG}
sed -r "s|^[#]?extra_port=.*$|extra_port=33062|" ${CFG} 1<>${CFG}
CA=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
if [ -f /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt ]; then
CA=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt
fi
SSL_DIR=${SSL_DIR:-/etc/mysql/ssl}
if [ -f "${SSL_DIR}"/ca.crt ]; then
CA=${SSL_DIR}/ca.crt
fi
SSL_INTERNAL_DIR=${SSL_INTERNAL_DIR:-/etc/mysql/ssl-internal}
if [ -f "${SSL_INTERNAL_DIR}"/ca.crt ]; then
CA=${SSL_INTERNAL_DIR}/ca.crt
fi
KEY=${SSL_DIR}/tls.key
CERT=${SSL_DIR}/tls.crt
if [ -f "${SSL_INTERNAL_DIR}"/tls.key -a -f "${SSL_INTERNAL_DIR}"/tls.crt ]; then
KEY=${SSL_INTERNAL_DIR}/tls.key
CERT=${SSL_INTERNAL_DIR}/tls.crt
fi
if [ -f "$CA" -a -f "$KEY" -a -f "$CERT" ]; then
sed "/^\[mysqld\]/a pxc-encrypt-cluster-traffic=ON\nssl-ca=$CA\nssl-key=$KEY\nssl-cert=$CERT" ${CFG} 1<>${CFG}
else
sed "/^\[mysqld\]/a pxc-encrypt-cluster-traffic=OFF" ${CFG} 1<>${CFG}
fi
# don't need a restart, we're just writing the conf in case there's an
# unexpected restart on the node.