fix param setup to use env vars for root and desync issues

This commit is contained in:
elianddb
2025-09-25 14:28:42 -07:00
parent 6d12ff7562
commit 3a9a96ad6f
3 changed files with 172 additions and 144 deletions
+160 -135
View File
@@ -1,7 +1,12 @@
#!/bin/bash
set -eo pipefail
# logging functions
# mysql_log logs messages with a timestamp and optional color formatting.
# Arguments:
# $1 - Log type (e.g., Note, Warn, ERROR)
# $@ - Log message (if empty, reads from stdin)
# Output:
# Prints a formatted log line to stdout or stderr, with color for Warn and ERROR.
mysql_log() {
local type="$1"
shift
@@ -18,34 +23,61 @@ mysql_log() {
esac
printf '%b%s [%s] [Entrypoint]: %s%b\n' "$color" "$dt" "$type" "$text" "$color_reset"
}
# mysql_note logs a message of type 'Note' using mysql_log.
mysql_note() {
mysql_log Note "$@"
}
# mysql_warn logs a message of type 'Warn' using mysql_log and writes to stderr.
mysql_warn() {
mysql_log Warn "$@" >&2
}
# mysql_error logs a message of type 'ERROR' using mysql_log, write to stderr, prints a container removal hint, and
# exits with status 1.
mysql_error() {
mysql_log ERROR "$@" >&2
if [ -f "/tmp/server.log" ]; then
mysql_note "Server log:"
cat /tmp/server.log >&2
fi
mysql_note "Remove this container with 'docker rm -f <container_name>' before retrying"
exit 1
}
docker_process_sql() {
dolt sql
}
# Helper function to execute SQL with error capture
# exec_mysql executes a SQL query using Dolt, retrying until it succeeds or a timeout is reached.
# On timeout, it prints the provided error message prefix followed by the command output.
# Containers and system resources can hang during startup, so this function helps ensure
# that the SQL command is executed successfully before proceeding.
# Arguments:
# $1 - SQL command to execute
# $2 - Error message prefix to print on timeout
# $3 - (Optional) If set to 1, prints command output on success
exec_mysql() {
local sql_command="$1"
local error_message="$2"
local output
local show_output="${3:-0}"
local timeout="${DOLT_SERVER_TIMEOUT:-300}"
local start_time now output
if ! output=$(dolt sql -q "$sql_command" 2>&1); then
mysql_error "$error_message$output"
fi
start_time=$(date +%s)
while true; do
if output=$(dolt sql -q "$sql_command" 2>&1); then
[ "$show_output" -eq 1 ] && echo "$output"
return 0
fi
if echo "$output" | grep -qi "syntax error"; then
mysql_error "$error_message$output"
fi
if [ "$timeout" -ne 0 ]; then
now=$(date +%s)
if [ $((now - start_time)) -ge "$timeout" ]; then
mysql_error "$error_message$output"
fi
fi
sleep 1
done
}
CONTAINER_DATA_DIR="/var/lib/dolt"
@@ -54,55 +86,75 @@ DOLT_CONFIG_DIR="/etc/dolt/doltcfg.d"
SERVER_CONFIG_DIR="/etc/dolt/servercfg.d"
DOLT_ROOT_PATH="/.dolt"
check_for_dolt() {
local dolt_bin=$(which dolt)
# check_for_dolt_binary verifies that the dolt binary is present and executable in the system PATH.
# If not found or not executable, it logs an error and exits.
check_for_dolt_binary() {
local dolt_bin
dolt_bin=$(which dolt)
if [ ! -x "$dolt_bin" ]; then
mysql_error "dolt binary executable not found"
fi
}
# get_env_var returns the value of an environment variable, preferring DOLT_* over MYSQL_*.
# Arguments:
# $1 - The base variable name (e.g., "USER" for MYSQL_USER or DOLT_USER)
# Output:
# Prints the value of the first set variable, or an empty string if neither is set.
get_env_var() {
local var_name="$1"
local mysql_var="MYSQL_${var_name}"
local dolt_var="DOLT_${var_name}"
local mysql_var="MYSQL_${var_name}"
if [ -n "${!mysql_var}" ]; then
echo "${!mysql_var}"
elif [ -n "${!dolt_var}" ]; then
if [ -n "${!dolt_var}" ]; then
echo "${!dolt_var}"
elif [ -n "${!mysql_var}" ]; then
echo "${!mysql_var}"
else
echo ""
fi
}
# get_env_var_name returns the name of the environment variable that is set, preferring DOLT_* over MYSQL_*.
# Arguments:
# $1 - The base variable name (e.g., "USER" for MYSQL_USER or DOLT_USER)
# Output:
# Prints the name of the first set variable, or both names if neither is set.
get_env_var_name() {
local var_name="$1"
local mysql_var="MYSQL_${var_name}"
local dolt_var="DOLT_${var_name}"
local mysql_var="MYSQL_${var_name}"
if [ -n "${!mysql_var}" ]; then
echo "MYSQL_${var_name}"
elif [ -n "${!dolt_var}" ]; then
if [ -n "${!dolt_var}" ]; then
echo "DOLT_${var_name}"
elif [ -n "${!mysql_var}" ]; then
echo "MYSQL_${var_name}"
else
echo "MYSQL_${var_name}/DOLT_${var_name}"
fi
}
# arg $1 is the directory to search in
# arg $2 is the type file to search for
# get_config_file_path_if_exists checks for config files of a given type in a directory.
# Arguments:
# $1 - Directory to search in
# $2 - File type/extension to search for (e.g., 'json', 'yaml')
# Output:
# Sets CONFIG_PROVIDED to the path of the config file if exactly one is found, or empty otherwise.
# Logs a warning if multiple config files are found and uses the default config.
get_config_file_path_if_exists() {
CONFIG_PROVIDED=
local CONFIG_DIR=$1
local FILE_TYPE=$2
if [ -d "$CONFIG_DIR" ]; then
mysql_note "Checking for config provided in $CONFIG_DIR"
local number_of_files_found=$(find "$CONFIG_DIR" -type f -name "*.$FILE_TYPE" | wc -l)
local number_of_files_found
number_of_files_found=$(find "$CONFIG_DIR" -type f -name "*.$FILE_TYPE" | wc -l)
if [ "$number_of_files_found" -gt 1 ]; then
CONFIG_PROVIDED=
mysql_warn "multiple config file found in $CONFIG_DIR, using default config"
mysql_warn "Multiple config files found in $CONFIG_DIR, using default config"
elif [ "$number_of_files_found" -eq 1 ]; then
local files_found=$(ls "$CONFIG_DIR"/*."$FILE_TYPE")
local files_found
files_found=$(ls "$CONFIG_DIR"/*."$FILE_TYPE")
mysql_note "$files_found file is found"
CONFIG_PROVIDED=$files_found
else
@@ -111,19 +163,19 @@ get_config_file_path_if_exists() {
fi
}
# taken from https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh
# this function will run files found in /docker-entrypoint-initdb.d directory BEFORE server is started
# usage: docker_process_init_files [file [file [...]]]
# ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions
# docker_process_init_files Runs files found in /docker-entrypoint-initdb.d before the server is started.
# Taken from https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh
# Usage:
# docker_process_init_files [file [file ...]]
# e.g., docker_process_init_files /always-initdb.d/*
# Processes initializer files based on file extensions.
docker_process_init_files() {
mysql_note "Running init scripts"
local f
mysql_note "Running init script...s"
local f sql
for f; do
case "$f" in
*.sh)
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
mysql_note "$0: running $f"
if ! "$f"; then
@@ -138,37 +190,40 @@ docker_process_init_files() {
;;
*.sql)
mysql_note "$0: running $f"
docker_process_sql <"$f"
echo
sql=$(cat "$f")
exec_mysql "$sql" "Failed to execute $f: "
;;
*.sql.bz2)
mysql_note "$0: running $f"
bunzip2 -c "$f" | docker_process_sql
echo
sql=$(bunzip2 -c "$f")
exec_mysql "$sql" "Failed to execute $f: "
;;
*.sql.gz)
mysql_note "$0: running $f"
gunzip -c "$f" | docker_process_sql
echo
sql=$(gunzip -c "$f")
exec_mysql "$sql" "Failed to execute $f: "
;;
*.sql.xz)
mysql_note "$0: running $f"
xzcat "$f" | docker_process_sql
echo
sql=$(xzcat "$f")
exec_mysql "$sql" "Failed to execute $f: "
;;
*.sql.zst)
mysql_note "$0: running $f"
zstd -dc "$f" | docker_process_sql
echo
sql=$(zstd -dc "$f")
exec_mysql "$sql" "Failed to execute $f: "
;;
*)
mysql_warn "$0: ignoring $f"
;;
*) mysql_warn "$0: ignoring $f" ;;
esac
echo
done
}
# if there is config file provided through /etc/dolt/doltcfg.d,
# we overwrite $HOME/.dolt/config_global.json file with this file.
# set_dolt_config_if_defined checks for a user-provided Dolt config file in $DOLT_CONFIG_DIR.
# If a single JSON config file is found, it copies it to $HOME/$DOLT_ROOT_PATH/config_global.json,
# overwriting the default config. Logs an error and exits if the copy fails.
set_dolt_config_if_defined() {
get_config_file_path_if_exists "$DOLT_CONFIG_DIR" "json"
if [ ! -z "$CONFIG_PROVIDED" ]; then
@@ -178,29 +233,35 @@ set_dolt_config_if_defined() {
fi
}
create_default_database_from_env() {
# create_database_from_env creates a database if the DATABASE environment variable is set.
# It retrieves the database name from environment variables (preferring DOLT_DATABASE over MYSQL_DATABASE)
# and attempts to create the database using exec_mysql.
create_database_from_env() {
local database
database=$(get_env_var "DATABASE")
if [ -n "$database" ]; then
mysql_note "Creating database '${database}'"
exec_mysql "CREATE DATABASE IF NOT EXISTS \`$database\`;" "Failed to create database '$database': "
fi
}
# create_user_from_env creates a new database user from environment variables.
# It prefers DOLT_USER/PASSWORD over MYSQL_USER/PASSWORD, and optionally grants access to a database.
# Requires both USER and PASSWORD to be set; if only the password is set, it logs a warning and does nothing.
# It does not allow creating a 'root' user via these environment variables.
create_user_from_env() {
local user
local password
local database
database=$(get_env_var "DATABASE")
user=$(get_env_var "USER")
password=$(get_env_var "PASSWORD")
if [ -n "$database" ]; then
mysql_note "Creating database '${database}'"
local db_output
if ! db_output=$(dolt sql -q "CREATE DATABASE IF NOT EXISTS \`$database\`;" 2>&1); then
mysql_error "Failed to create database '$database'. Error: $db_output"
fi
fi
database=$(get_env_var "DATABASE")
if [ "$user" = 'root' ]; then
# TODO: add ALLOW_EMPTY_PASSWORD and RANDOM_ROOT_PASSWORD support
mysql_error <<-EOF
$(get_env_var_name "USER")="root", $(get_env_var_name "USER") and $(get_env_var_name "PASSWORD") are for configuring a regular user and cannot be used for the root user
Remove $(get_env_var_name "USER")="root" and use the following to control the root user password:
- DOLT_ROOT_PASSWORD
$(get_env_var_name "USER")="root", $(get_env_var_name "USER") and $(get_env_var_name "PASSWORD") are for configuring the regular user and cannot be used for the root user.
EOF
fi
@@ -212,61 +273,26 @@ EOF
fi
if [ -n "$user" ]; then
# Get user host from DOLT_USER_HOST/MYSQL_USER_HOST, fall back to DOLT_ROOT_HOST, then localhost
local user_host
user_host=$(get_env_var "USER_HOST")
user_host="${user_host:-${DOLT_ROOT_HOST:-localhost}}"
mysql_note "Creating user '${user}'"
exec_mysql "CREATE USER IF NOT EXISTS '$user'@'$user_host' IDENTIFIED BY '$password';" "Failed to create user '$user': "
# Grant basic server access
mysql_note "Granting server access to user '${user}'"
mysql_note "Creating user '${user}@${user_host}'..."
exec_mysql "CREATE USER IF NOT EXISTS '$user'@'$user_host' IDENTIFIED BY '$password';" "Failed to create user '$user': "
exec_mysql "GRANT USAGE ON *.* TO '$user'@'$user_host';" "Failed to grant server access to user '$user': "
if [ -n "$database" ]; then
mysql_note "Giving user '${user}' access to schema '${database}'"
mysql_note "Giving user '${user}@${user_host}' access to schema '${database}'..."
exec_mysql "GRANT ALL ON \`$database\`.* TO '$user'@'$user_host';" "Failed to grant permissions to user '$user' on database '$database': "
fi
mysql_note "'${user}@${user_host}' user successfully created!"
fi
}
wait_for_dolt_server() {
local timeout="${DOLT_SERVER_TIMEOUT:-300}" # default 5 minutes
local start_time now output
wait_for_check() {
local description="$1"
local query="$2"
start_time=$(date +%s)
while true; do
if output=$(dolt sql -q "$query" 2>&1); then
mysql_note "...$description is ready"
return 0
fi
sleep 1
if [ "$timeout" -ne 0 ]; then
now=$(date +%s)
if [ $((now - start_time)) -ge "$timeout" ]; then
mysql_error "Timed out after ${timeout}s waiting for $description: $output"
fi
fi
done
}
wait_for_check "basic server connection" "SELECT 1;"
wait_for_check "mysql.user table" "SELECT COUNT(*) FROM mysql.user;"
wait_for_check "database operations" "CREATE DATABASE IF NOT EXISTS __health_check_db__; DROP DATABASE __health_check_db__;"
wait_for_check "privilege queries" "SELECT COUNT(*) FROM mysql.db;"
mysql_note "Server initialization complete!"
}
# _main is the main entrypoint for the Dolt Docker container initialization.
_main() {
# check for dolt binary executable
check_for_dolt
check_for_dolt_binary
local dolt_version
dolt_version=$(dolt version | grep 'dolt version' | cut -f3 -d " ")
@@ -285,51 +311,50 @@ _main() {
set -- "$@" --config="$CONFIG_PROVIDED"
fi
# TODO: add support for MYSQL_ROOT_HOST and MYSQL_ROOT_PASSWORD
# Note: User creation will happen after server starts to avoid conflicts with default users
if [[ ! -f $INIT_COMPLETED ]]; then
# run any file provided in /docker-entrypoint-initdb.d directory before the server starts
if ls /docker-entrypoint-initdb.d/* >/dev/null 2>&1; then
docker_process_init_files /docker-entrypoint-initdb.d/*
else
mysql_warn "No files found in /docker-entrypoint-initdb.d/ to process"
fi
touch $INIT_COMPLETED
touch "$INIT_COMPLETED"
fi
# Start server in background for user setup
mysql_note "Starting Dolt server in background..."
dolt sql-server --host=0.0.0.0 --port=3306 "$@" >/tmp/server.log 2>&1 &
local server_pid=$!
create_database_from_env
# Wait for server to be ready for connections and user setup
wait_for_dolt_server
mysql_note "Starting Dolt server..."
# Configure the root user first since the environment var initialization breaks
# if certain queries are ran before the root user is ready.
DOLT_ROOT_HOST="${DOLT_ROOT_HOST:-localhost}"
mysql_note "Configuring user 'root@${DOLT_ROOT_HOST}'..."
# Create root user with the specified host (defaults to localhost if not specified)
local root_host="${DOLT_ROOT_HOST:-localhost}"
mysql_note "Ensuring root@${root_host} superuser exists with password"
dolt sql-server --host=0.0.0.0 --port=3306 "$@" &
# Ensure root user exists with correct password and permissions
mysql_note "Configuring root@${root_host} user"
exec_mysql "CREATE USER IF NOT EXISTS 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; ALTER USER 'root'@'${root_host}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}'; GRANT ALL ON *.* TO 'root'@'${root_host}' WITH GRANT OPTION;" "Failed to configure root@${root_host} user: "
local SERVER_PID=$!
# If DOLT_DATABASE or MYSQL_DATABASE has been specified, create the database if it does not exist
create_default_database_from_env
exec_mysql "SELECT 1 FROM mysql.user WHERE User='root' LIMIT 1;" "The root user did not initialize: "
# Show what users exist for debugging
mysql_note "Current users in the system:"
dolt sql -q "SELECT User, Host FROM mysql.user;" 2>&1 | grep -v "^$" || mysql_warn "Could not list users"
# Ran in a subshell to avoid exiting the main script, capture output and use fallback if query fails
local has_correct_host
has_correct_host=$(exec_mysql \
"SELECT User, Host FROM mysql.user WHERE User='root' AND Host='${DOLT_ROOT_HOST}' LIMIT 1;" \
"Could not check root host: " 1 | grep -c "$DOLT_ROOT_HOST" || true)
mysql_note "Reattaching to server process..."
cat /tmp/server.log
# docker-entrypoint-initdb.d scripts may conflict with environment variable initialization if they create users
if [ "$has_correct_host" -eq 0 ]; then
mysql_warn "Environment variables failed to initialize 'root@${DOLT_ROOT_HOST}'; docker-entrypoint-initdb.d scripts queries may have conflicted. Overriding root user..."
exec_mysql "CREATE USER IF NOT EXISTS 'root'@'${DOLT_ROOT_HOST}' IDENTIFIED BY '${DOLT_ROOT_PASSWORD}';" "Could not create root user: " # override password
exec_mysql "GRANT ALL PRIVILEGES ON *.* TO 'root'@'${DOLT_ROOT_HOST}' WITH GRANT OPTION;" "Could not set root privileges: "
fi
mysql_note "'root@${DOLT_ROOT_HOST}' user successfully configured!"
# Kill the background process and restart in foreground to show live output
kill $server_pid 2>/dev/null || true
wait $server_pid 2>/dev/null || true
create_user_from_env
# Start server in foreground to show live output
exec dolt sql-server --host=0.0.0.0 --port=3306 "$@"
exec_mysql "SELECT User, Host FROM mysql.user;" "Could not list users: " 1
mysql_note "Server initialization complete!"
wait "$SERVER_PID"
}
_main "$@"
+1 -1
View File
@@ -179,6 +179,6 @@ The Dolt SQL Server image supports the following environment variables:
- `DOLT_USER` / `MYSQL_USER`: Creates a user with this name if it doesn't exist
- `DOLT_PASSWORD` / `MYSQL_PASSWORD`: Sets the password for the user specified in `DOLT_USER`/`MYSQL_USER`
- `DOLT_USER_HOST` / `MYSQL_USER_HOST`: Specifies a host for the custom user (default: falls back to `DOLT_ROOT_HOST`, then localhost)
- `DOLT_SERVER_TIMEOUT`: Sets the server startup timeout in seconds (default: 300)
- `DOLT_SERVER_TIMEOUT`: Sets the server startup timeout in seconds (default: 300). A value of 0 means no timeout.
The user will be granted all privileges on the database specified by `DOLT_DATABASE`/`MYSQL_DATABASE` if provided.
+11 -8
View File
@@ -37,8 +37,7 @@ run_container() {
name="$1"; shift
docker run -d --name "$name" "$@" "$TEST_IMAGE" >/dev/null
wait_for_log "$name" "Server ready. Accepting connections."
wait_for_log "$name" "Reattaching to server process..."
wait_for_log "$name" "Server initialization complete!"
# Verify container is running
run docker ps --filter "name=$name" --format "{{.Names}}"
@@ -54,9 +53,8 @@ run_container_with_port() {
port="$1"; shift
docker run -d --name "$name" -p "$port:3306" "$@" "$TEST_IMAGE" >/dev/null
wait_for_log "$name" "Server ready. Accepting connections."
wait_for_log "$name" "Server initialization complete!"
wait_for_log "$name" "Reattaching to server process"
# Verify container is running
run docker ps --filter "name=$name" --format "{{.Names}}"
[ $status -eq 0 ]
@@ -163,6 +161,9 @@ wait_for_log() {
[ $status -eq 0 ]
echo "$output" | grep -Fx "$kw_db" >/dev/null
run docker exec "$cname" dolt sql --result-format csv -q "SHOW FULL TABLES FROM versioning;"
# Can use the database for operations
run docker exec "$cname" dolt sql -q "USE \`$kw_db\`; CREATE TABLE test_table (id INT);"
[ $status -eq 0 ]
@@ -552,6 +553,8 @@ EOF
[[ "$output" =~ "Bash script executed" ]] || false
[[ "$output" =~ "Compressed SQL executed" ]] || false
docker logs "$cname"
rm -rf "$temp_dir"
}
@@ -597,7 +600,7 @@ EOF
docker run -d --name "$cname" -e DOLT_ROOT_PASSWORD=rootpass -e DOLT_ROOT_HOST=% "$LATEST_IMAGE" >/dev/null
wait_for_log "$cname" "Server ready. Accepting connections."
wait_for_log "$cname" "Reattaching to server process..."
wait_for_log "$name" "Server initialization complete!"
run docker exec "$cname" dolt version
[ $status -eq 0 ]
@@ -623,7 +626,7 @@ EOF
docker run -d --name "$cname" -e DOLT_ROOT_PASSWORD=rootpass -e DOLT_ROOT_HOST=% "$SPECIFIC_IMAGE" >/dev/null
wait_for_log "$cname" "Server ready. Accepting connections."
wait_for_log "$cname" "Reattaching to server process..."
wait_for_log "$name" "Server initialization complete!"
run docker exec "$cname" dolt version
[ $status -eq 0 ]
@@ -668,7 +671,7 @@ EOF
# Wait for all servers to be ready
for cycle in {1..20}; do
wait_for_log "$cname-$cycle" "Server ready. Accepting connections." 30
wait_for_log "$cname-$cycle" "Reattaching to server process" 15
wait_for_log "$name" "Server initialization complete!" 15
done
# Verify no errors in any container logs
@@ -678,7 +681,7 @@ EOF
# Should not contain ERROR messages (but allow warnings)
! echo "$output" | grep -i "ERROR" >/dev/null || false
# Should contain success indicators
[[ "$output" =~ "Server initialization complete" ]] || false
[[ "$output" =~ "Server initialization complete!" ]] || false
[[ "$output" =~ "Server ready. Accepting connections" ]] || false
done