Skip to content

Commit

Permalink
Add logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mnagy committed Feb 4, 2016
1 parent b8a1697 commit 569a31c
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 46 deletions.
19 changes: 15 additions & 4 deletions 5.5/root/usr/bin/run-mysqld
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
source ${CONTAINER_SCRIPTS_PATH}/common.sh
set -eu

[ -f ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh

log_volume_info $MYSQL_DATADIR

# Process the MySQL configuration files
log_info 'Processing MySQL configuration files ...'
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf

Expand All @@ -14,10 +18,17 @@ else
start_local_mysql "$@"
fi

[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && . ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
if [ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ]; then
log_info 'Setting passwords ...'
source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
fi
if [ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ]; then
log_info 'Sourcing post-init.sh ...'
source ${CONTAINER_SCRIPTS_PATH}/post-init.sh
fi

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE "$@" 2>&1
18 changes: 12 additions & 6 deletions 5.5/root/usr/bin/run-mysqld-master
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ set -eu

export MYSQL_RUNNING_AS_MASTER=1

[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh

log_volume_info $MYSQL_DATADIR

# The 'server-id' for master needs to be constant
export MYSQL_SERVER_ID=1
echo "The 'master' server-id is ${MYSQL_SERVER_ID}"
log_info "The 'master' server-id is ${MYSQL_SERVER_ID}"

# Process the MySQL configuration files
log_info 'Processing MySQL configuration files ...'
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-master.cnf.template > /etc/my.cnf.d/master.cnf
Expand All @@ -25,7 +28,8 @@ else
start_local_mysql "$@"
fi

[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && . ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
log_info 'Setting passwords ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh

# Setup the 'master' replication on the MySQL server
# Grant REPLICATION CLIENT so slave can get master status
Expand All @@ -35,9 +39,11 @@ mysql $mysql_flags <<EOSQL
FLUSH PRIVILEGES;
EOSQL

[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
log_info 'Sourcing post-init.sh ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && source ${CONTAINER_SCRIPTS_PATH}/post-init.sh

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE "$@" 2>&1
14 changes: 10 additions & 4 deletions 5.5/root/usr/bin/run-mysqld-slave
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
source ${CONTAINER_SCRIPTS_PATH}/common.sh
set -eu

log_volume_info $MYSQL_DATADIR

# Just run normal server if the data directory is already initialized
if [ -d "${MYSQL_DATADIR}/mysql" ]; then
log_info "Datadir already exists, execing ordinary run script"
exec /usr/bin/run-mysqld "$@"
fi

export MYSQL_RUNNING_AS_SLAVE=1

[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh

# Generate the unique 'server-id' for this master
export MYSQL_SERVER_ID=$(server_id)
echo "The 'slave' server-id is ${MYSQL_SERVER_ID}"
log_info "The 'slave' server-id is ${MYSQL_SERVER_ID}"

# Process the MySQL configuration files
log_info 'Processing MySQL configuration files ...'
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-slave.cnf.template > /etc/my.cnf.d/slave.cnf
Expand All @@ -43,10 +47,12 @@ mysql $mysql_flags <<EOSQL
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START;
EOSQL

[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
log_info 'Sourcing post-init.sh ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && source ${CONTAINER_SCRIPTS_PATH}/post-init.sh

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE \
--report-host=$(hostname -i) "$@" 2>&1
37 changes: 28 additions & 9 deletions 5.5/root/usr/share/container-scripts/mysql/common.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

source ${CONTAINER_SCRIPTS_PATH}/helpers.sh

# Data directory where MySQL database files live. The data subdirectory is here
# because .bashrc and my.cnf both live in /var/lib/mysql/ and we don't want a
# volume to override it.
Expand All @@ -24,6 +26,7 @@ admin_flags="--defaults-file=$MYSQL_DEFAULTS_FILE $mysql_flags"

# Make sure env variables don't propagate to mysqld process.
function unset_env_vars() {
log_info 'Cleaning up environment variables MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE and MYSQL_ROOT_PASSWORD ...'
unset MYSQL_USER MYSQL_PASSWORD MYSQL_DATABASE MYSQL_ROOT_PASSWORD
}

Expand All @@ -33,42 +36,54 @@ function wait_for_mysql() {

while [ true ]; do
if [ -d "/proc/$pid" ]; then
mysqladmin --socket=/tmp/mysql.sock ping &>/dev/null && return 0
mysqladmin --socket=/tmp/mysql.sock ping &>/dev/null && log_info "MySQL started successfully" && return 0
else
return 1
fi
echo "Waiting for MySQL to start ..."
log_info "Waiting for MySQL to start ..."
sleep 1
done
}

# Start local MySQL server with a defaults file
function start_local_mysql() {
# Now start mysqld and add appropriate users.
echo 'Starting local mysqld server ...'
log_info 'Starting MySQL server with disabled networking ...'
${MYSQL_PREFIX}/libexec/mysqld \
--defaults-file=$MYSQL_DEFAULTS_FILE \
--skip-networking --socket=/tmp/mysql.sock "$@" &
mysql_pid=$!
wait_for_mysql $mysql_pid
}

# Shutdown mysql flushing privileges
function shutdown_local_mysql() {
log_info 'Shutting down MySQL ...'
mysqladmin $admin_flags flush-privileges shutdown
}

# Initialize the MySQL database (create user accounts and the initial database)
function initialize_database() {
echo 'Running mysql_install_db ...'
log_info 'Initializing database ...'
log_info 'Running mysql_install_db ...'
# Using --rpm since we need mysql_install_db behaves as in RPM
mysql_install_db --rpm --datadir=$MYSQL_DATADIR
start_local_mysql "$@"

log_info 'Dropping the "test" database ...'
mysqladmin $admin_flags -f drop test

[ -v MYSQL_RUNNING_AS_SLAVE ] && return
if [ -v MYSQL_RUNNING_AS_SLAVE ]; then
log_info 'Initialization finished'
return 0
fi

if [ -v MYSQL_RUNNING_AS_MASTER ]; then
# Save master status into a separate database.
STATUS_INFO=$(mysql $admin_flags -e 'SHOW MASTER STATUS\G')
BINLOG_POSITION=$(echo "$STATUS_INFO" | grep 'Position:' | head -n 1 | sed -e 's/^\s*Position: //')
BINLOG_FILE=$(echo "$STATUS_INFO" | grep 'File:' | head -n 1 | sed -e 's/^\s*File: //')

log_info "Creating replication table (status: $BINLOG_FILE:$BINLOG_POSITION) ..."
mysqladmin $admin_flags create replication
mysql $admin_flags <<EOSQL
use replication
Expand All @@ -79,15 +94,17 @@ EOSQL

# Do not care what option is compulsory here, just create what is specified
if [ -v MYSQL_USER ]; then
log_info "Creating user specified by MYSQL_USER (${MYSQL_USER}) ..."
mysql $mysql_flags <<EOSQL
CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
EOSQL
fi

if [ -v MYSQL_DATABASE ]; then
log_info "Creating database ${MYSQL_DATABASE} ..."
mysqladmin $admin_flags create "${MYSQL_DATABASE}"

if [ -v MYSQL_USER ]; then
log_info "Granting privileges to user ${MYSQL_USER} for ${MYSQL_DATABASE} ..."
mysql $mysql_flags <<EOSQL
GRANT ALL ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%' ;
FLUSH PRIVILEGES ;
Expand All @@ -96,10 +113,12 @@ EOSQL
fi

if [ -v MYSQL_ROOT_PASSWORD ]; then
log_info "Setting password for MySQL root user ..."
mysql $mysql_flags <<EOSQL
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
EOSQL
fi
log_info 'Initialization finished'
}

# The 'server_id' number for slave needs to be within 1-4294967295 range.
Expand All @@ -114,9 +133,9 @@ function server_id() {

function wait_for_mysql_master() {
while true; do
echo "Waiting for MySQL master (${MYSQL_MASTER_SERVICE_NAME}) to accept connections ..."
log_info "Waiting for MySQL master (${MYSQL_MASTER_SERVICE_NAME}) to accept connections ..."
mysqladmin --host=${MYSQL_MASTER_SERVICE_NAME} --user="${MYSQL_MASTER_USER}" \
--password="${MYSQL_MASTER_PASSWORD}" ping &>/dev/null && return 0
--password="${MYSQL_MASTER_PASSWORD}" ping &>/dev/null && log_info "MySQL master is ready" && return 0
sleep 1
done
}
Expand Down
24 changes: 24 additions & 0 deletions 5.5/root/usr/share/container-scripts/mysql/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function log_info {
echo "---> `date +%T` $@"
}

function log_and_run {
log_info "Running $@"
"$@"
}

function log_volume_info {
CONTAINER_DEBUG=${CONTAINER_DEBUG:-}
if [[ "${CONTAINER_DEBUG,,}" != "true" ]]; then
return
fi

log_info "Volume info for $@:"
set +e
log_and_run mount
while [ $# -gt 0 ]; do
log_and_run ls -alZ $1
shift
done
set -e
}
18 changes: 14 additions & 4 deletions 5.6/root/usr/bin/run-mysqld
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
source ${CONTAINER_SCRIPTS_PATH}/common.sh
set -eu

[ -f ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate-variables.sh

# Process the MySQL configuration files
log_info 'Processing MySQL configuration files ...'
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf

Expand All @@ -14,10 +16,18 @@ else
start_local_mysql "$@"
fi

[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && . ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
if [ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ]; then
log_info 'Setting passwords ...'
source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
fi
if [ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ]; then
log_info 'Sourcing post-init.sh ...'
source ${CONTAINER_SCRIPTS_PATH}/post-init.sh
fi

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_volume_info $MYSQL_DATADIR
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE "$@" 2>&1
17 changes: 11 additions & 6 deletions 5.6/root/usr/bin/run-mysqld-master
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
source ${CONTAINER_SCRIPTS_PATH}/common.sh
set -eu

[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_variables.sh

# The 'server-id' for master needs to be constant
export MYSQL_SERVER_ID=1
echo "The 'master' server-id is ${MYSQL_SERVER_ID}"
log_info "The 'master' server-id is ${MYSQL_SERVER_ID}"

# Process the MySQL configuration files
log_info 'Processing MySQL configuration files ...'
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-master.cnf.template > /etc/my.cnf.d/master.cnf
Expand All @@ -24,17 +25,21 @@ else
start_local_mysql "$@"
fi

[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && . ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh
log_info 'Setting passwords ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh ] && source ${CONTAINER_SCRIPTS_PATH}/passwd-change.sh

# Setup the 'master' replication on the MySQL server
mysql $mysql_flags <<EOSQL
GRANT REPLICATION SLAVE ON *.* TO '${MYSQL_MASTER_USER}'@'%' IDENTIFIED BY '${MYSQL_MASTER_PASSWORD}';
FLUSH PRIVILEGES;
EOSQL

[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
log_info 'Sourcing post-init.sh ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && source ${CONTAINER_SCRIPTS_PATH}/post-init.sh

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_volume_info $MYSQL_DATADIR
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE "$@" 2>&1
11 changes: 7 additions & 4 deletions 5.6/root/usr/bin/run-mysqld-slave
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ fi

export MYSQL_RUNNING_AS_SLAVE=1

[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && . ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh

# Generate the unique 'server-id' for this master
export MYSQL_SERVER_ID=$(server_id)
echo "The 'slave' server-id is ${MYSQL_SERVER_ID}"
log_info "The 'slave' server-id is ${MYSQL_SERVER_ID}"

# Process the MySQL configuration files
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
Expand All @@ -33,10 +33,13 @@ mysql $mysql_flags <<EOSQL
CHANGE MASTER TO MASTER_HOST='${MYSQL_MASTER_SERVICE_NAME}',MASTER_USER='${MYSQL_MASTER_USER}', MASTER_PASSWORD='${MYSQL_MASTER_PASSWORD}', MASTER_AUTO_POSITION = 1;
EOSQL

[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && . ${CONTAINER_SCRIPTS_PATH}/post-init.sh
log_info 'Sourcing post-init.sh ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && source ${CONTAINER_SCRIPTS_PATH}/post-init.sh

# Restart the MySQL server with public IP bindings
mysqladmin $admin_flags flush-privileges shutdown
shutdown_local_mysql
unset_env_vars
log_volume_info $MYSQL_DATADIR
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE \
--report-host=$(hostname -i) "$@" 2>&1
Loading

0 comments on commit 569a31c

Please sign in to comment.