Skip to content

Commit 5a727ad

Browse files
ltangvaldyosifkit
authored andcommitted
Make template for 5.7+ entrypoint script
5.7 and 8.0 can use the exact same entrypoint script, so simply have the update script copy it from a template location.
1 parent f16150e commit 5a727ad

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

template.Debian/docker-entrypoint.sh

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
shopt -s nullglob
4+
5+
# if command starts with an option, prepend mysqld
6+
if [ "${1:0:1}" = '-' ]; then
7+
set -- mysqld "$@"
8+
fi
9+
10+
# skip setup if they want an option that stops mysqld
11+
wantHelp=
12+
for arg; do
13+
case "$arg" in
14+
-'?'|--help|--print-defaults|-V|--version)
15+
wantHelp=1
16+
break
17+
;;
18+
esac
19+
done
20+
21+
# usage: file_env VAR [DEFAULT]
22+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
23+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
24+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
25+
file_env() {
26+
local var="$1"
27+
local fileVar="${var}_FILE"
28+
local def="${2:-}"
29+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
30+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
31+
exit 1
32+
fi
33+
local val="$def"
34+
if [ "${!var:-}" ]; then
35+
val="${!var}"
36+
elif [ "${!fileVar:-}" ]; then
37+
val="$(< "${!fileVar}")"
38+
fi
39+
export "$var"="$val"
40+
unset "$fileVar"
41+
}
42+
43+
# usage: process_init_file FILENAME MYSQLCOMMAND...
44+
# ie: process_init_file foo.sh mysql -uroot
45+
# (process a single initializer file, based on its extension. we define this
46+
# function here, so that initializer scripts (*.sh) can use the same logic,
47+
# potentially recursively, or override the logic used in subsequent calls)
48+
process_init_file() {
49+
local f="$1"; shift
50+
local mysql=( "$@" )
51+
52+
case "$f" in
53+
*.sh) echo "$0: running $f"; . "$f" ;;
54+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$0: ignoring $f" ;;
57+
esac
58+
echo
59+
}
60+
61+
_check_config() {
62+
toRun=( "$@" --verbose --help )
63+
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
64+
cat >&2 <<-EOM
65+
66+
ERROR: mysqld failed while attempting to check config
67+
command was: "${toRun[*]}"
68+
69+
$errors
70+
EOM
71+
exit 1
72+
fi
73+
}
74+
75+
# Fetch value from server config
76+
# We use mysqld --verbose --help instead of my_print_defaults because the
77+
# latter only show values present in config files, and not server defaults
78+
_get_config() {
79+
local conf="$1"; shift
80+
"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
81+
| awk '$1 == "'"$conf"'" && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
82+
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
83+
}
84+
85+
# allow the container to be started with `--user`
86+
if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
87+
_check_config "$@"
88+
DATADIR="$(_get_config 'datadir' "$@")"
89+
mkdir -p "$DATADIR"
90+
chown -R mysql:mysql "$DATADIR"
91+
exec gosu mysql "$BASH_SOURCE" "$@"
92+
fi
93+
94+
if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
95+
# still need to check config, container may have started with --user
96+
_check_config "$@"
97+
# Get config
98+
DATADIR="$(_get_config 'datadir' "$@")"
99+
100+
if [ ! -d "$DATADIR/mysql" ]; then
101+
file_env 'MYSQL_ROOT_PASSWORD'
102+
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
103+
echo >&2 'error: database is uninitialized and password option is not specified '
104+
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
105+
exit 1
106+
fi
107+
108+
mkdir -p "$DATADIR"
109+
110+
echo 'Initializing database'
111+
"$@" --initialize-insecure
112+
echo 'Database initialized'
113+
114+
if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
115+
# https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
116+
echo 'Initializing certificates'
117+
mysql_ssl_rsa_setup --datadir="$DATADIR"
118+
echo 'Certificates initialized'
119+
fi
120+
121+
SOCKET="$(_get_config 'socket' "$@")"
122+
"$@" --skip-networking --socket="${SOCKET}" &
123+
pid="$!"
124+
125+
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
126+
127+
for i in {30..0}; do
128+
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
129+
break
130+
fi
131+
echo 'MySQL init process in progress...'
132+
sleep 1
133+
done
134+
if [ "$i" = 0 ]; then
135+
echo >&2 'MySQL init process failed.'
136+
exit 1
137+
fi
138+
139+
if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
140+
# sed is for https://bugs.mysql.com/bug.php?id=20545
141+
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
142+
fi
143+
144+
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
145+
export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
146+
echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
147+
fi
148+
149+
rootCreate=
150+
# default root to listen for connections from anywhere
151+
file_env 'MYSQL_ROOT_HOST' '%'
152+
if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
153+
# no, we don't care if read finds a terminating character in this heredoc
154+
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
155+
read -r -d '' rootCreate <<-EOSQL || true
156+
CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
157+
GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
158+
EOSQL
159+
fi
160+
161+
"${mysql[@]}" <<-EOSQL
162+
-- What's done in this file shouldn't be replicated
163+
-- or products like mysql-fabric won't work
164+
SET @@SESSION.SQL_LOG_BIN=0;
165+
166+
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
167+
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
168+
${rootCreate}
169+
DROP DATABASE IF EXISTS test ;
170+
FLUSH PRIVILEGES ;
171+
EOSQL
172+
173+
if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
174+
mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
175+
fi
176+
177+
file_env 'MYSQL_DATABASE'
178+
if [ "$MYSQL_DATABASE" ]; then
179+
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
180+
mysql+=( "$MYSQL_DATABASE" )
181+
fi
182+
183+
file_env 'MYSQL_USER'
184+
file_env 'MYSQL_PASSWORD'
185+
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
186+
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
187+
188+
if [ "$MYSQL_DATABASE" ]; then
189+
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
190+
fi
191+
192+
echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
193+
fi
194+
195+
echo
196+
for f in /docker-entrypoint-initdb.d/*; do
197+
process_init_file "$f" "${mysql[@]}"
198+
done
199+
200+
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
201+
"${mysql[@]}" <<-EOSQL
202+
ALTER USER 'root'@'%' PASSWORD EXPIRE;
203+
EOSQL
204+
fi
205+
if ! kill -s TERM "$pid" || ! wait "$pid"; then
206+
echo >&2 'MySQL init process failed.'
207+
exit 1
208+
fi
209+
210+
echo
211+
echo 'MySQL init process done. Ready for start up.'
212+
echo
213+
fi
214+
fi
215+
216+
exec "$@"

update.sh

+7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ declare -A debianVariants=(
1414
#[5.5]='jessie'
1515
)
1616

17+
# Copy entrypoint template
18+
templateVersions=( "5.7 8.0" )
19+
for version in ${templateVersions}; do
20+
cp "template.Debian/docker-entrypoint.sh" "${version}/"
21+
done
22+
1723
for version in "${versions[@]}"; do
24+
if [ "${version}" = "template.Debian" ]; then continue; fi # If update.sh is run without arguments, the template directory is included in the list
1825
debianVariant="${debianVariants[$version]:-$defaultDebianVariant}"
1926
debianSuite="${debianVariant%%-*}" # "stretch", etc
2027

0 commit comments

Comments
 (0)