Skip to content

Commit a8aa1cf

Browse files
ltangvaldyosifkit
authored andcommitted
Template: Create logging functions
Logging is done by calling _note, _warn or _error with the message string. _warn and _error print to stderr, and _error causes the script to exit with return code 1
1 parent 34f3ef2 commit a8aa1cf

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

.template.Debian/docker-entrypoint.sh

+35-32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
set -eo pipefail
33
shopt -s nullglob
44

5+
# logging functions
6+
_log() {
7+
local type=$1;shift
8+
printf "$(date --rfc-3339=seconds) [${type}] [Entrypoint]: $@\n"
9+
}
10+
_note() {
11+
_log Note "$@"
12+
}
13+
_warn() {
14+
_log Warn "$@" >&2
15+
}
16+
_error() {
17+
_log ERROR "$@" >&2
18+
exit 1
19+
}
20+
521
# if command starts with an option, prepend mysqld
622
if [ "${1:0:1}" = '-' ]; then
723
set -- mysqld "$@"
@@ -27,8 +43,7 @@ file_env() {
2743
local fileVar="${var}_FILE"
2844
local def="${2:-}"
2945
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
30-
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Both $var and $fileVar are set (but are exclusive)"
31-
exit 1
46+
_error "Both $var and $fileVar are set (but are exclusive)"
3247
fi
3348
local val="$def"
3449
if [ "${!var:-}" ]; then
@@ -50,26 +65,18 @@ process_init_file() {
5065
local mysql=( "$@" )
5166

5267
case "$f" in
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" ;;
68+
*.sh) _note "$0: running $f"; . "$f" ;;
69+
*.sql) _note "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
70+
*.sql.gz) _note "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
71+
*) _warn "$0: ignoring $f" ;;
5772
esac
5873
echo
5974
}
6075

6176
_check_config() {
6277
toRun=( "$@" --verbose --help )
6378
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
64-
datestring=$(date --rfc-3339=seconds)
65-
cat >&2 <<-EOM
66-
67-
$datestring [ERROR] [Entrypoint]: mysqld failed while attempting to check config
68-
command was: "${toRun[*]}"
69-
70-
$errors
71-
EOM
72-
exit 1
79+
_error "mysqld failed while attempting to check config\n\tcommand was: ${toRun[*]}\n\t$errors"
7380
fi
7481
}
7582

@@ -97,8 +104,7 @@ _start_server() {
97104
sleep 1
98105
done
99106
if [ "$i" = 0 ]; then
100-
echo >&2 "$(date --rfc-3339=seconds) [ERROR] [Entrypoint]: Unable to start server."
101-
exit 1
107+
_error "Unable to start server."
102108
fi
103109
return $pid
104110
}
@@ -113,8 +119,7 @@ _stop_server() {
113119
fi
114120
done
115121
# 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
122+
_error "Unable to shut down server with process id $server_pid"
118123

119124
}
120125
# allow the container to be started with `--user`
@@ -135,28 +140,26 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
135140
if [ ! -d "$DATADIR/mysql" ]; then
136141
file_env 'MYSQL_ROOT_PASSWORD'
137142
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
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"
140-
exit 1
143+
_error "Database is uninitialized and password option is not specified \n\tYou need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD"
141144
fi
142145

143146
mkdir -p "$DATADIR"
144147

145-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Initializing database"
148+
_note "Initializing database"
146149
"$@" --initialize-insecure
147-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Database initialized"
150+
_note "Database initialized"
148151

149152
if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
150153
# https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
151-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Initializing certificates"
154+
_note "Initializing certificates"
152155
mysql_ssl_rsa_setup --datadir="$DATADIR"
153-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Certificates initialized"
156+
_note "Certificates initialized"
154157
fi
155158

156159
SOCKET="$(_get_config 'socket' "$@")"
157-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Starting server"
160+
_note "Starting server"
158161
_start_server "${SOCKET}" "$@" || pid=$?
159-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Server started with pid $pid"
162+
_note "Server started with pid $pid"
160163

161164
mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
162165

@@ -167,7 +170,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
167170

168171
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
169172
export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
170-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
173+
_note "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
171174
fi
172175

173176
rootCreate=
@@ -226,11 +229,11 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
226229
ALTER USER 'root'@'%' PASSWORD EXPIRE;
227230
EOSQL
228231
fi
229-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Stopping server"
232+
_note "Stopping server"
230233
_stop_server $pid
231-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: Server stopped"
234+
_note "Server stopped"
232235
echo
233-
echo "$(date --rfc-3339=seconds) [Note] [Entrypoint]: MySQL init process done. Ready for start up."
236+
_note "MySQL init process done. Ready for start up."
234237
echo
235238
fi
236239
fi

0 commit comments

Comments
 (0)