From 91785a50c332947c3963018606680acc2608a405 Mon Sep 17 00:00:00 2001 From: Joe Ferguson Date: Tue, 17 Sep 2019 17:05:44 -0700 Subject: [PATCH] Add --dont-use-mysql-root-password flag for docker_process_sql - also remove bash generating bash --- .template.Debian/docker-entrypoint.sh | 79 +++++++++++++++------- 5.6/docker-entrypoint.sh | 96 ++++++++++++++++++--------- 5.7/docker-entrypoint.sh | 95 +++++++++++++++++--------- 8.0/docker-entrypoint.sh | 95 +++++++++++++++++--------- update.sh | 21 +----- 5 files changed, 251 insertions(+), 135 deletions(-) diff --git a/.template.Debian/docker-entrypoint.sh b/.template.Debian/docker-entrypoint.sh index c2d37e202..ed2d88e48 100755 --- a/.template.Debian/docker-entrypoint.sh +++ b/.template.Debian/docker-entrypoint.sh @@ -84,20 +84,18 @@ mysql_get_config() { # Do a temporary startup of the MySQL server, for init purposes docker_temp_server_start() { - local result=0 - %%SERVERSTARTUP%% - if [ "$result" != "0" ];then - mysql_error "Unable to start server. Status code $result." - fi - - # For 5.7+ the server is ready for use as soon as startup command unblocks - if [ "${MYSQL_MAJOR}" = "5.6" ]; then + if [ "${MYSQL_MAJOR}" = '5.6' ]; then + "$@" --skip-networking --socket="${SOCKET}" & mysql_note "Waiting for server startup" local i for i in {30..0}; do - # unset MYSQL_ROOT_PASSWORD for just docker_process_sql + # only use the root password if the database has already been initializaed # so that it won't try to fill in a password file when it hasn't been set yet - if MYSQL_ROOT_PASSWORD= docker_process_sql --database=mysql <<<'SELECT 1' &> /dev/null; then + extraArgs=() + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + extraArgs+=( '--dont-use-mysql-root-password' ) + fi + if docker_process_sql "${extraArgs[@]}" --database=mysql <<<'SELECT 1' &> /dev/null; then break fi sleep 1 @@ -105,6 +103,11 @@ docker_temp_server_start() { if [ "$i" = 0 ]; then mysql_error "Unable to start server." fi + else + # For 5.7+ the server is ready for use as soon as startup command unblocks + if ! "$@" --daemonize --skip-networking --socket="${SOCKET}"; then + mysql_error "Unable to start server." + fi fi } @@ -143,7 +146,11 @@ docker_create_db_directories() { # initializes the database directory docker_init_database_dir() { mysql_note "Initializing database files" - %%DATABASEINIT%% + if [ "$MYSQL_MAJOR" = '5.6' ]; then + mysql_install_db --datadir="$DATADIR" --rpm --keep-my-cnf "${@:2}" + else + "$@" --initialize-insecure + fi mysql_note "Database files initialized" if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then @@ -168,19 +175,29 @@ docker_setup_env() { file_env 'MYSQL_USER' file_env 'MYSQL_PASSWORD' file_env 'MYSQL_ROOT_PASSWORD' + + declare -g DATABASE_ALREADY_EXISTS + if [ -d "$DATADIR/mysql" ]; then + DATABASE_ALREADY_EXISTS='true' + fi } # Execute sql script, passed via stdin -# usage: docker_process_sql [mysql-cli-args] +# usage: docker_process_sql [--dont-use-mysql-root-password] [mysql-cli-args] # ie: docker_process_sql --database=mydb <<<'INSERT ...' -# ie: docker_process_sql --database=mydb /dev/null; then + extraArgs=() + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + extraArgs+=( '--dont-use-mysql-root-password' ) + fi + if docker_process_sql "${extraArgs[@]}" --database=mysql <<<'SELECT 1' &> /dev/null; then break fi sleep 1 @@ -99,6 +103,11 @@ docker_temp_server_start() { if [ "$i" = 0 ]; then mysql_error "Unable to start server." fi + else + # For 5.7+ the server is ready for use as soon as startup command unblocks + if ! "$@" --daemonize --skip-networking --socket="${SOCKET}"; then + mysql_error "Unable to start server." + fi fi } @@ -137,7 +146,11 @@ docker_create_db_directories() { # initializes the database directory docker_init_database_dir() { mysql_note "Initializing database files" - mysql_install_db --datadir="$DATADIR" --rpm --keep-my-cnf "${@:2}" + if [ "$MYSQL_MAJOR" = '5.6' ]; then + mysql_install_db --datadir="$DATADIR" --rpm --keep-my-cnf "${@:2}" + else + "$@" --initialize-insecure + fi mysql_note "Database files initialized" if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then @@ -149,8 +162,10 @@ docker_init_database_dir() { } # Loads various settings that are used elsewhere in the script +# This should be called after mysql_check_config, but before any other functions docker_setup_env() { # Get config + declare -g DATADIR SOCKET DATADIR="$(mysql_get_config 'datadir' "$@")" SOCKET="$(mysql_get_config 'socket' "$@")" @@ -160,19 +175,29 @@ docker_setup_env() { file_env 'MYSQL_USER' file_env 'MYSQL_PASSWORD' file_env 'MYSQL_ROOT_PASSWORD' + + declare -g DATABASE_ALREADY_EXISTS + if [ -d "$DATADIR/mysql" ]; then + DATABASE_ALREADY_EXISTS='true' + fi } # Execute sql script, passed via stdin -# usage: docker_process_sql [mysql-cli-args] +# usage: docker_process_sql [--dont-use-mysql-root-password] [mysql-cli-args] # ie: docker_process_sql --database=mydb <<<'INSERT ...' -# ie: docker_process_sql --database=mydb /dev/null; then + extraArgs=() + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + extraArgs+=( '--dont-use-mysql-root-password' ) + fi + if docker_process_sql "${extraArgs[@]}" --database=mysql <<<'SELECT 1' &> /dev/null; then break fi sleep 1 @@ -99,6 +103,11 @@ docker_temp_server_start() { if [ "$i" = 0 ]; then mysql_error "Unable to start server." fi + else + # For 5.7+ the server is ready for use as soon as startup command unblocks + if ! "$@" --daemonize --skip-networking --socket="${SOCKET}"; then + mysql_error "Unable to start server." + fi fi } @@ -137,7 +146,11 @@ docker_create_db_directories() { # initializes the database directory docker_init_database_dir() { mysql_note "Initializing database files" - "$@" --initialize-insecure + if [ "$MYSQL_MAJOR" = '5.6' ]; then + mysql_install_db --datadir="$DATADIR" --rpm --keep-my-cnf "${@:2}" + else + "$@" --initialize-insecure + fi mysql_note "Database files initialized" if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then @@ -149,8 +162,10 @@ docker_init_database_dir() { } # Loads various settings that are used elsewhere in the script +# This should be called after mysql_check_config, but before any other functions docker_setup_env() { # Get config + declare -g DATADIR SOCKET DATADIR="$(mysql_get_config 'datadir' "$@")" SOCKET="$(mysql_get_config 'socket' "$@")" @@ -160,19 +175,29 @@ docker_setup_env() { file_env 'MYSQL_USER' file_env 'MYSQL_PASSWORD' file_env 'MYSQL_ROOT_PASSWORD' + + declare -g DATABASE_ALREADY_EXISTS + if [ -d "$DATADIR/mysql" ]; then + DATABASE_ALREADY_EXISTS='true' + fi } # Execute sql script, passed via stdin -# usage: docker_process_sql [mysql-cli-args] +# usage: docker_process_sql [--dont-use-mysql-root-password] [mysql-cli-args] # ie: docker_process_sql --database=mydb <<<'INSERT ...' -# ie: docker_process_sql --database=mydb /dev/null; then + extraArgs=() + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + extraArgs+=( '--dont-use-mysql-root-password' ) + fi + if docker_process_sql "${extraArgs[@]}" --database=mysql <<<'SELECT 1' &> /dev/null; then break fi sleep 1 @@ -99,6 +103,11 @@ docker_temp_server_start() { if [ "$i" = 0 ]; then mysql_error "Unable to start server." fi + else + # For 5.7+ the server is ready for use as soon as startup command unblocks + if ! "$@" --daemonize --skip-networking --socket="${SOCKET}"; then + mysql_error "Unable to start server." + fi fi } @@ -137,7 +146,11 @@ docker_create_db_directories() { # initializes the database directory docker_init_database_dir() { mysql_note "Initializing database files" - "$@" --initialize-insecure + if [ "$MYSQL_MAJOR" = '5.6' ]; then + mysql_install_db --datadir="$DATADIR" --rpm --keep-my-cnf "${@:2}" + else + "$@" --initialize-insecure + fi mysql_note "Database files initialized" if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then @@ -149,8 +162,10 @@ docker_init_database_dir() { } # Loads various settings that are used elsewhere in the script +# This should be called after mysql_check_config, but before any other functions docker_setup_env() { # Get config + declare -g DATADIR SOCKET DATADIR="$(mysql_get_config 'datadir' "$@")" SOCKET="$(mysql_get_config 'socket' "$@")" @@ -160,19 +175,29 @@ docker_setup_env() { file_env 'MYSQL_USER' file_env 'MYSQL_PASSWORD' file_env 'MYSQL_ROOT_PASSWORD' + + declare -g DATABASE_ALREADY_EXISTS + if [ -d "$DATADIR/mysql" ]; then + DATABASE_ALREADY_EXISTS='true' + fi } # Execute sql script, passed via stdin -# usage: docker_process_sql [mysql-cli-args] +# usage: docker_process_sql [--dont-use-mysql-root-password] [mysql-cli-args] # ie: docker_process_sql --database=mydb <<<'INSERT ...' -# ie: docker_process_sql --database=mydb "$version/docker-entrypoint.sh" - debianVariant="${debianVariants[$version]:-$defaultDebianVariant}" debianSuite="${debianVariant%%-*}" # "stretch", etc + cp -a .template.Debian/docker-entrypoint.sh "$version/docker-entrypoint.sh" + fullVersion="$( curl -fsSL "https://repo.mysql.com/apt/debian/dists/$debianSuite/mysql-$version/binary-amd64/Packages.gz" \ | gunzip \