Skip to content

Commit afceb7f

Browse files
ltangvaldyosifkit
authored andcommitted
Template: Create functions for starting and stopping server during init
Move the logic for doing temporary startup of the server during init to functions. Also add tags to echo commands to make it clear they are coming from the entrypoint script.
1 parent 5a727ad commit afceb7f

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

template.Debian/docker-entrypoint.sh

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ file_env() {
2727
local fileVar="${var}_FILE"
2828
local def="${2:-}"
2929
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
30-
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
30+
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Both $var and $fileVar are set (but are exclusive)"
3131
exit 1
3232
fi
3333
local val="$def"
@@ -50,20 +50,21 @@ process_init_file() {
5050
local mysql=( "$@" )
5151

5252
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" ;;
53+
*.sh) echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: $0: running $f"; . "$f" ;;
54+
*.sql) echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: $0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: $0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: $0: ignoring $f" ;;
5757
esac
5858
echo
5959
}
6060

6161
_check_config() {
6262
toRun=( "$@" --verbose --help )
6363
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
64+
datestring=$(date --rfc-3339=seconds)
6465
cat >&2 <<-EOM
6566
66-
ERROR: mysqld failed while attempting to check config
67+
$datestring [ERROR] [Entrypoint]: mysqld failed while attempting to check config
6768
command was: "${toRun[*]}"
6869
6970
$errors
@@ -82,6 +83,40 @@ _get_config() {
8283
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
8384
}
8485

86+
_start_server() {
87+
local socket=$1; shift
88+
"$@" --skip-networking --socket="${socket}" &
89+
local pid="$!"
90+
91+
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${socket}" )
92+
93+
for i in {30..0}; do
94+
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
95+
break
96+
fi
97+
sleep 1
98+
done
99+
if [ "$i" = 0 ]; then
100+
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Unable to start server."
101+
exit 1
102+
fi
103+
return $pid
104+
}
105+
106+
_stop_server() {
107+
local server_pid="$1"
108+
kill "$server_pid"
109+
for i in $(seq 1 60); do
110+
sleep 1
111+
if ! $(pidof /usr/sbin/mysqld >/dev/null 2>&1); then
112+
return 0
113+
fi
114+
done
115+
# The server hasn't shut down in a timely manner
116+
echo "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Unable to shut down server with process id $server_pid" >&2
117+
return 1
118+
119+
}
85120
# allow the container to be started with `--user`
86121
if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
87122
_check_config "$@"
@@ -100,50 +135,39 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
100135
if [ ! -d "$DATADIR/mysql" ]; then
101136
file_env 'MYSQL_ROOT_PASSWORD'
102137
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'
138+
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified "
139+
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD"
105140
exit 1
106141
fi
107142

108143
mkdir -p "$DATADIR"
109144

110-
echo 'Initializing database'
145+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Initializing database"
111146
"$@" --initialize-insecure
112-
echo 'Database initialized'
147+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Database initialized"
113148

114149
if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
115150
# https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
116-
echo 'Initializing certificates'
151+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Initializing certificates"
117152
mysql_ssl_rsa_setup --datadir="$DATADIR"
118-
echo 'Certificates initialized'
153+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Certificates initialized"
119154
fi
120155

121156
SOCKET="$(_get_config 'socket' "$@")"
122-
"$@" --skip-networking --socket="${SOCKET}" &
123-
pid="$!"
157+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Starting server"
158+
_start_server "${SOCKET}" "$@" || pid=$?
159+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Server started with pid $pid"
124160

125161
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
126162

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-
139163
if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
140164
# sed is for https://bugs.mysql.com/bug.php?id=20545
141165
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
142166
fi
143167

144168
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
145169
export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
146-
echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
170+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
147171
fi
148172

149173
rootCreate=
@@ -202,13 +226,11 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
202226
ALTER USER 'root'@'%' PASSWORD EXPIRE;
203227
EOSQL
204228
fi
205-
if ! kill -s TERM "$pid" || ! wait "$pid"; then
206-
echo >&2 'MySQL init process failed.'
207-
exit 1
208-
fi
209-
229+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Stopping server"
230+
_stop_server $pid
231+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Server stopped"
210232
echo
211-
echo 'MySQL init process done. Ready for start up.'
233+
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: MySQL init process done. Ready for start up."
212234
echo
213235
fi
214236
fi

0 commit comments

Comments
 (0)