From e7abfee16da68ccf460152fafb293ddb38b3f36e Mon Sep 17 00:00:00 2001 From: Pete Birley Date: Sat, 29 Jul 2017 15:58:59 -0500 Subject: [PATCH] MariaDB: Update start script to be more robust This PS updates the MariaDB start script remove templated values from being directly inserted into the script. It also moves the root connection params to a secret to prevent the root password being exposed in logs. Change-Id: I10977ef267877a5913bf265280ff5439c7bf268c --- mariadb/templates/bin/_readiness.sh.tpl | 2 +- mariadb/templates/bin/_start.sh.tpl | 51 +++++++++---------- mariadb/templates/etc/_00-base.cnf.tpl | 2 - mariadb/templates/secrets-etc.yaml | 24 +++++++++ mariadb/templates/secrets/_admin_user.cnf.tpl | 19 +++++++ mariadb/templates/statefulset.yaml | 14 ++++- 6 files changed, 80 insertions(+), 32 deletions(-) create mode 100644 mariadb/templates/secrets-etc.yaml create mode 100644 mariadb/templates/secrets/_admin_user.cnf.tpl diff --git a/mariadb/templates/bin/_readiness.sh.tpl b/mariadb/templates/bin/_readiness.sh.tpl index e13c3b86ac..a761f7de4b 100644 --- a/mariadb/templates/bin/_readiness.sh.tpl +++ b/mariadb/templates/bin/_readiness.sh.tpl @@ -16,7 +16,7 @@ set -o pipefail -MYSQL="mysql -u root --password=${MYSQL_ROOT_PASSWORD}" +MYSQL="mysql --defaults-file=/etc/mysql/admin_user.cnf --host=localhost" if [ ! $($MYSQL -e 'select 1') ]; then echo "Could not SELECT 1" 1>&2 diff --git a/mariadb/templates/bin/_start.sh.tpl b/mariadb/templates/bin/_start.sh.tpl index 9442bf30cd..6ecff20b81 100644 --- a/mariadb/templates/bin/_start.sh.tpl +++ b/mariadb/templates/bin/_start.sh.tpl @@ -13,38 +13,41 @@ # See the License for the specific language governing permissions and # limitations under the License. -# -# Bootstrap database -# -CLUSTER_INIT_ARGS= +set -xe +# Bootstrap database +CLUSTER_INIT_ARGS="" if [ ! -d /var/lib/mysql/mysql ]; then if [ "x${POD_NAME}" = "x${SERVICE_NAME}-0" ]; then echo No data found for pod 0 - if [ "xtrue" = "x{{ .Values.force_bootstrap }}" ]; then - echo force_bootstrap set, so will force-initialize node 0. + if [ "x${FORCE_BOOTSTRAP}" = "xtrue" ]; then + echo 'force_bootstrap set, so will force-initialize node 0.' CLUSTER_INIT_ARGS=--wsrep-new-cluster - elif ! mysql -h ${SERVICE_NAME} -u root --password=${MYSQL_ROOT_PASSWORD} -e 'select 1'; then - echo No other nodes found, so will initialize cluster. + elif ! mysql --defaults-file=/etc/mysql/admin_user.cnf \ + --connect-timeout 2 \ + -e 'select 1'; then + echo 'No other nodes found, so will initialize cluster.' CLUSTER_INIT_ARGS=--wsrep-new-cluster else - echo Found other live nodes, will attempt to join them. + echo 'Found other live nodes, will attempt to join them.' mkdir /var/lib/mysql/mysql fi else - echo Not pod 0, so will avoid upstream database initialization. + echo 'Not pod 0, so will avoid upstream database initialization.' mkdir /var/lib/mysql/mysql fi fi -# # Construct cluster config -# CLUSTER_CONFIG_PATH=/etc/mysql/conf.d/10-cluster-config.cnf -MEMBERS= -for i in $(seq 1 {{ .Values.pod.replicas.server }}); do - NUM=$(expr $i - 1) +MEMBERS="" +for i in $(seq 1 ${MARIADB_REPLICAS}); do + if [ "$i" -eq "1" ]; then + NUM="0" + else + NUM="$(expr $i - 1)" + fi CANDIDATE_POD="${SERVICE_NAME}-$NUM.${DISCOVERY_SERVICE_NAME}" if [ "x${CANDIDATE_POD}" != "x${POD_NAME}.${DISCOVERY_SERVICE_NAME}" ]; then if [ -n "${MEMBERS}" ]; then @@ -54,22 +57,14 @@ for i in $(seq 1 {{ .Values.pod.replicas.server }}); do fi done -echo -echo Writing cluster config for ${POD_NAME} to ${CLUSTER_CONFIG_PATH} -echo vvv - -cat <> ${CLUSTER_CONFIG_PATH} <