Skip to content

Commit 344a5f5

Browse files
authored
fix: handle nonempty password in Docker entrypoint (#308)
1 parent be912e8 commit 344a5f5

File tree

7 files changed

+51
-39
lines changed

7 files changed

+51
-39
lines changed

devtools/replica-setup-mysql/checker.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ check_server_params() {
1313
echo "Checking MySQL server parameters..."
1414

1515
# Retrieve the required MySQL server variables using mysqlsh
16-
result=$(mysqlsh --uri="$SOURCE_DSN" $SOURCE_NO_PASSWORD_OPTION --sql -e "
16+
result=$(mysqlsh --uri="$SOURCE_DSN" $SOURCE_PASSWORD_OPTION --sql -e "
1717
SHOW VARIABLES WHERE variable_name IN ('binlog_format', 'enforce_gtid_consistency', 'gtid_mode', 'gtid_strict_mode', 'log_bin');
1818
")
1919

@@ -65,7 +65,7 @@ check_user_privileges() {
6565
echo "Checking privileges for the current user '$SOURCE_USER'..."
6666

6767
# Check the user grants for the currently authenticated user using mysqlsh
68-
result=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_NO_PASSWORD_OPTION --sql -e "
68+
result=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_PASSWORD_OPTION --sql -e "
6969
SHOW GRANTS FOR CURRENT_USER();
7070
")
7171

@@ -98,7 +98,7 @@ check_mysql_config() {
9898
# Function to check if source MySQL server is empty
9999
check_if_source_mysql_is_empty() {
100100
# Run the query using mysqlsh and capture the output
101-
OUTPUT=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_NO_PASSWORD_OPTION --sql -e "SHOW DATABASES;" 2>/dev/null)
101+
OUTPUT=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_PASSWORD_OPTION --sql -e "SHOW DATABASES;" 2>/dev/null)
102102

103103
check_command "retrieving database list"
104104

@@ -114,7 +114,7 @@ check_if_source_mysql_is_empty() {
114114

115115
# Function to check if there is ongoing replication on MyDuck Server
116116
check_if_myduck_has_replica() {
117-
REPLICA_STATUS=$(mysqlsh --sql --host=$MYDUCK_HOST --port=$MYDUCK_PORT --user=root --password='' -e "SHOW REPLICA STATUS\G")
117+
REPLICA_STATUS=$(mysqlsh --sql --host=$MYDUCK_HOST --port=$MYDUCK_PORT --user=${MYDUCK_USER} ${MYDUCK_PASSWORD_OPTION} -e "SHOW REPLICA STATUS\G")
118118
check_command "retrieving replica status"
119119

120120
SOURCE_HOST_EXISTS=$(echo "$REPLICA_STATUS" | awk '/Source_Host/ {print $2}')

devtools/replica-setup-mysql/prepare.sh

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
#!/bin/bash
22

3-
if ! mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password -e "SELECT 1 FROM mysql.user WHERE user = '${MYDUCK_USER}'" | grep -q 1; then
4-
echo "Creating user ${MYDUCK_USER} for replication..."
5-
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
6-
CREATE USER '${MYDUCK_USER}'@'%' IDENTIFIED BY '${MYDUCK_PASSWORD}';
7-
GRANT ALL PRIVILEGES ON *.* TO '${MYDUCK_USER}'@'%';
8-
EOF
9-
fi
10-
11-
if [[ $? -ne 0 ]]; then
12-
echo "Failed to create user '${MYDUCK_USER}'. Exiting."
13-
exit 1
14-
fi
15-
163
echo "Setting local_infile and server_id..."
17-
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
4+
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=${MYDUCK_USER} ${MYDUCK_PASSWORD_OPTION} <<EOF
185
SET GLOBAL local_infile = 1;
196
SET GLOBAL server_id = ${MYDUCK_SERVER_ID};
207
SET GLOBAL replica_is_loading_snapshot = ON;

devtools/replica-setup-mysql/replica_setup.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ while [[ $# -gt 0 ]]; do
5757
esac
5858
done
5959

60-
# if SOURCE_PASSWORD is empty, set SOURCE_NO_PASSWORD_OPTION to "--no-password"
60+
# if SOURCE_PASSWORD is empty, set SOURCE_PASSWORD_OPTION to "--no-password"
6161
if [[ -z "$SOURCE_PASSWORD" ]]; then
62-
SOURCE_NO_PASSWORD_OPTION="--no-password"
62+
SOURCE_PASSWORD_OPTION="--no-password"
6363
else
64-
SOURCE_NO_PASSWORD_OPTION=""
64+
SOURCE_PASSWORD_OPTION=""
65+
fi
66+
67+
# if MYDUCK_PASSWORD is empty, set MYDUCK_PASSWORD_OPTION to "--no-password"
68+
if [[ -z "$MYDUCK_PASSWORD" ]]; then
69+
MYDUCK_PASSWORD_OPTION="--no-password"
70+
else
71+
MYDUCK_PASSWORD_OPTION="--password=$MYDUCK_PASSWORD"
6572
fi
6673

6774
# Check if all parameters are set

devtools/replica-setup-mysql/snapshot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ echo "Thread count set to: $THREAD_COUNT"
3636

3737
echo "Copying data from MySQL to MyDuck..."
3838
# Run mysqlsh command and capture the output
39-
output=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_NO_PASSWORD_OPTION -- util copy-instance "mysql://${MYDUCK_USER}:${MYDUCK_PASSWORD}@${MYDUCK_HOST}:${MYDUCK_PORT}" \
39+
output=$(mysqlsh --uri "$SOURCE_DSN" $SOURCE_PASSWORD_OPTION -- util copy-instance "mysql://${MYDUCK_USER}:${MYDUCK_PASSWORD}@${MYDUCK_HOST}:${MYDUCK_PORT}" \
4040
--users false \
4141
--consistent false \
4242
--ignore-existing-objects true \
@@ -84,6 +84,6 @@ fi
8484
echo "Snapshot completed successfully."
8585

8686
echo "Reset replica_is_loading_snapshot..."
87-
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
87+
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=${MYDUCK_USER} {MYDUCK_PASSWORD_OPTION} <<EOF
8888
SET GLOBAL replica_is_loading_snapshot = OFF;
8989
EOF

devtools/replica-setup-mysql/start_replication.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OS=$(uname -s)
1313

1414
# Use the EXECUTED_GTID_SET variable from the previous steps
1515
if [ $GTID_MODE == "ON" ] && [ ! -z "$EXECUTED_GTID_SET" ]; then
16-
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
16+
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=${MYDUCK_USER} ${MYDUCK_PASSWORD_OPTION} <<EOF
1717
SET GLOBAL gtid_purged = "${EXECUTED_GTID_SET}";
1818
EOF
1919
fi
@@ -31,7 +31,7 @@ if [ $GTID_MODE == "OFF" ]; then
3131
SOURCE_LOG_POS=${BINLOG_POS}"
3232
fi
3333

34-
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
34+
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=${MYDUCK_USER} ${MYDUCK_PASSWORD_OPTION} <<EOF
3535
${REPLICATION_CMD};
3636
START REPLICA;
3737
EOF

devtools/replica-setup-postgres/replica_setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ CREATE_SUBSCRIPTION_SQL="CREATE SUBSCRIPTION ${SUBSCRIPTION_NAME} \
8080
CONNECTION 'dbname=${SOURCE_DATABASE} host=${SOURCE_HOST} port=${SOURCE_PORT} user=${SOURCE_USER} password=${SOURCE_PASSWORD}' \
8181
PUBLICATION ${PUBLICATION_NAME};"
8282

83-
psql -h $MYDUCK_HOST -p $MYDUCK_PORT -U $MYDUCK_USER <<EOF
83+
PGPASSWORD="$MYDUCK_PASSWORD" psql -h $MYDUCK_HOST -p $MYDUCK_PORT -U $MYDUCK_USER <<EOF
8484
${CREATE_SUBSCRIPTION_SQL}
8585
EOF
8686

docker/entrypoint.sh

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ cleanup() {
9393
fi
9494
}
9595

96+
# Define MYSQL_PASSWORD_OPTION based on SUPERUSER_PASSWORD
97+
if [ -z "$SUPERUSER_PASSWORD" ]; then
98+
MYSQL_PASSWORD_OPTION="--no-password"
99+
else
100+
MYSQL_PASSWORD_OPTION="--password=$SUPERUSER_PASSWORD"
101+
fi
102+
96103
# Function to run replica setup
97104
run_replica_setup() {
98105
case "$SOURCE_TYPE" in
@@ -116,6 +123,8 @@ run_replica_setup() {
116123
;;
117124
esac
118125

126+
export MYDUCK_PASSWORD="${SUPERUSER_PASSWORD}"
127+
119128
# Run replica_setup.sh and check for errors
120129
if source replica_setup.sh; then
121130
echo "Replica setup completed."
@@ -127,7 +136,16 @@ run_replica_setup() {
127136

128137
run_server_in_background() {
129138
cd "$DATA_PATH" || { echo "Error: Could not change directory to ${DATA_PATH}"; exit 1; }
130-
nohup myduckserver $DEFAULT_DB $SUPERUSER_PASSWORD $LOG_LEVEL $PROFILER_PORT $RESTORE_FILE $RESTORE_ENDPOINT $RESTORE_ACCESS_KEY_ID $RESTORE_SECRET_ACCESS_KEY | tee -a "${LOG_PATH}/server.log" 2>&1 &
139+
nohup myduckserver \
140+
${DEFAULT_DB_OPTION} \
141+
${SUPERUSER_PASSWORD_OPTION} \
142+
${LOG_LEVEL_OPTION} \
143+
${PROFILER_PORT_OPTION} \
144+
${RESTORE_FILE_OPTION} \
145+
${RESTORE_ENDPOINT_OPTION} \
146+
${RESTORE_ACCESS_KEY_ID_OPTION} \
147+
${RESTORE_SECRET_ACCESS_KEY_OPTION} \
148+
| tee -a "${LOG_PATH}/server.log" 2>&1 &
131149
echo "$!" > "${PID_FILE}"
132150
}
133151

@@ -141,10 +159,10 @@ wait_for_my_duck_server_ready() {
141159

142160
echo "Waiting for MyDuck Server at $host:$port to be ready..."
143161

144-
until mysqlsh --sql --host "$host" --port "$port" --user "$user" --no-password --execute "SELECT VERSION();" &> /dev/null; do
162+
until mysqlsh --sql --host "$host" --port "$port" --user "$user" ${MYSQL_PASSWORD_OPTION} --execute "SELECT VERSION();" &> /dev/null; do
145163
attempt=$((attempt+1))
146164
if [ "$attempt" -ge "$max_attempts" ]; then
147-
echo "Error: MySQL connection timed out after $max_attempts attempts."
165+
echo "Error: MySQL connection timeout after $max_attempts attempts."
148166
exit 1
149167
fi
150168
echo "Attempt $attempt/$max_attempts: MyDuck Server is unavailable - retrying in $wait_time seconds..."
@@ -186,14 +204,14 @@ execute_init_sqls() {
186204
echo "Executing init SQL scripts from $INIT_SQLS_DIR/mysql..."
187205
for file in "$INIT_SQLS_DIR/mysql"/*.sql; do
188206
echo "Executing $file..."
189-
mysqlsh --sql --host "$host" --port "$mysql_port" --user "$mysql_user" --no-password --file="$file"
207+
mysqlsh --sql --host "$host" --port "$mysql_port" --user "$mysql_user" $MYSQL_PASSWORD_OPTION --file="$file"
190208
done
191209
fi
192210
if [ -d "$INIT_SQLS_DIR/postgres" ] && [ "$(find "$INIT_SQLS_DIR/postgres" -maxdepth 1 -name '*.sql' -type f | head -n 1)" ]; then
193211
echo "Executing init SQL scripts from $INIT_SQLS_DIR/postgres..."
194212
for file in "$INIT_SQLS_DIR/postgres"/*.sql; do
195213
echo "Executing $file..."
196-
psql -h "$host" -p "$postgres_port" -U "$postgres_user" -f "$file"
214+
PGPASSWORD="$SUPERUSER_PASSWORD" psql -h "$host" -p "$postgres_port" -U "$postgres_user" -f "$file"
197215
done
198216
fi
199217
}
@@ -204,35 +222,35 @@ setup() {
204222
trap cleanup SIGTERM SIGINT SIGQUIT
205223

206224
if [ -n "$DEFAULT_DB" ]; then
207-
export DEFAULT_DB="--default-db=$DEFAULT_DB"
225+
export DEFAULT_DB_OPTION="--default-db=$DEFAULT_DB"
208226
fi
209227

210228
if [ -n "$SUPERUSER_PASSWORD" ]; then
211-
export SUPERUSER_PASSWORD="--superuser-password=$SUPERUSER_PASSWORD"
229+
export SUPERUSER_PASSWORD_OPTION="--superuser-password=$SUPERUSER_PASSWORD"
212230
fi
213231

214232
if [ -n "$LOG_LEVEL" ]; then
215-
export LOG_LEVEL="--loglevel=$LOG_LEVEL"
233+
export LOG_LEVEL_OPTION="--loglevel=$LOG_LEVEL"
216234
fi
217235

218236
if [ -n "$PROFILER_PORT" ]; then
219-
export PROFILER_PORT="--profiler-port=$PROFILER_PORT"
237+
export PROFILER_PORT_OPTION="--profiler-port=$PROFILER_PORT"
220238
fi
221239

222240
if [ -n "$RESTORE_FILE" ]; then
223-
export RESTORE_FILE="--restore-file=$RESTORE_FILE"
241+
export RESTORE_FILE_OPTION="--restore-file=$RESTORE_FILE"
224242
fi
225243

226244
if [ -n "$RESTORE_ENDPOINT" ]; then
227-
export RESTORE_ENDPOINT="--restore-endpoint=$RESTORE_ENDPOINT"
245+
export RESTORE_ENDPOINT_OPTION="--restore-endpoint=$RESTORE_ENDPOINT"
228246
fi
229247

230248
if [ -n "$RESTORE_ACCESS_KEY_ID" ]; then
231-
export RESTORE_ACCESS_KEY_ID="--restore-access-key-id=$RESTORE_ACCESS_KEY_ID"
249+
export RESTORE_ACCESS_KEY_ID_OPTION="--restore-access-key-id=$RESTORE_ACCESS_KEY_ID"
232250
fi
233251

234252
if [ -n "$RESTORE_SECRET_ACCESS_KEY" ]; then
235-
export RESTORE_SECRET_ACCESS_KEY="--restore-secret-access-key=$RESTORE_SECRET_ACCESS_KEY"
253+
export RESTORE_SECRET_ACCESS_KEY_OPTION="--restore-secret-access-key=$RESTORE_SECRET_ACCESS_KEY"
236254
fi
237255

238256
# Ensure required directories exist

0 commit comments

Comments
 (0)