MariaDB startup issue
[oom.git] / kubernetes / portal / charts / portal-mariadb / resources / config / mariadb / docker-entrypoint.sh
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 _check_config() {
44         toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
45         if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
46                 cat >&2 <<-EOM
47
48                                 ERROR: mysqld failed while attempting to check config
49                                 command was: "${toRun[*]}"
50
51                                 $errors
52                                 EOM
53                 exit 1
54         fi
55 }
56
57 # Fetch value from server config
58 # We use mysqld --verbose --help instead of my_print_defaults because the
59 # latter only show values present in config files, and not server defaults
60 _get_config() {
61         local conf="$1"; shift
62         "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }'
63 }
64
65 # allow the container to be started with `--user`
66 if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
67         _check_config "$@"
68         DATADIR="$(_get_config 'datadir' "$@")"
69         mkdir -p "$DATADIR"
70         find "$DATADIR" \! -user mysql -exec chown mysql '{}' +
71         exec gosu mysql "$BASH_SOURCE" "$@"
72 fi
73
74 if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
75         # still need to check config, container may have started with --user
76         _check_config "$@"
77         # Get config
78         DATADIR="$(_get_config 'datadir' "$@")"
79
80         if [ ! -d "$DATADIR/mysql" ]; then
81                 file_env 'MYSQL_ROOT_PASSWORD'
82                 if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
83                         echo >&2 'error: database is uninitialized and password option is not specified '
84                         echo >&2 '  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
85                         exit 1
86                 fi
87
88                 mkdir -p "$DATADIR"
89
90                 echo 'Initializing database'
91                 # "Other options are passed to mysqld." (so we pass all "mysqld" arguments directly here)
92                 mysql_install_db --datadir="$DATADIR" --rpm "${@:2}"
93                 echo 'Database initialized'
94
95                 SOCKET="$(_get_config 'socket' "$@")"
96                 "$@" --skip-networking --socket="${SOCKET}" &
97                 pid="$!"
98
99                 mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
100
101                 for i in {60..0}; do
102                         if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
103                                 break
104                         fi
105                         echo 'MySQL init process in progress...'
106                         sleep 1
107                 done
108                 if [ "$i" = 0 ]; then
109                         echo >&2 'MySQL init process failed.'
110                         exit 1
111                 fi
112
113                 if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
114                         # sed is for https://bugs.mysql.com/bug.php?id=20545
115                         mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
116                 fi
117
118                 if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
119                         export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
120                         echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
121                 fi
122
123                 rootCreate=
124                 # default root to listen for connections from anywhere
125                 file_env 'MYSQL_ROOT_HOST' '%'
126                 if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
127                         # no, we don't care if read finds a terminating character in this heredoc
128                         # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
129                         read -r -d '' rootCreate        <<-EOSQL || true
130                                                 CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
131                                                 GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
132                                                 EOSQL
133                 fi
134
135                 "${mysql[@]}"   <<-EOSQL
136                                                 -- What's done in this file shouldn't be replicated
137                                                 --  or products like mysql-fabric won't work
138                                                 SET @@SESSION.SQL_LOG_BIN=0;
139
140                                                 DELETE FROM mysql.user WHERE user NOT IN ('mysql.sys', 'mysqlxsys', 'root') OR host NOT IN ('localhost') ;
141                                                 SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
142                                                 GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
143                                                 ${rootCreate}
144                                                 DROP DATABASE IF EXISTS test ;
145                                                 FLUSH PRIVILEGES ;
146                                         EOSQL
147
148                 if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
149                         mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
150                 fi
151
152                 file_env 'MYSQL_DATABASE'
153                 if [ "$MYSQL_DATABASE" ]; then
154                         echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
155                         mysql+=( "$MYSQL_DATABASE" )
156                 fi
157
158                 file_env 'MYSQL_USER'
159                 file_env 'MYSQL_PASSWORD'
160                 if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
161                         echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
162
163                         if [ "$MYSQL_DATABASE" ]; then
164                                 echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
165                         fi
166                 fi
167
168                 echo
169                 for f in /docker-entrypoint-initdb.d/*; do
170                         case "$f" in
171                                 *.sh)     echo "$0: running $f"; . "$f" ;;
172                                 *.sql)    echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
173                                 *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
174                                 *)        echo "$0: ignoring $f" ;;
175                         esac
176                         echo
177                 done
178
179                 if ! kill -s TERM "$pid" || ! wait "$pid"; then
180                         echo >&2 'MySQL init process failed.'
181                         exit 1
182                 fi
183
184                 echo
185                 echo 'MySQL init process done. Ready for start up.'
186                 echo
187         fi
188 fi
189
190 exec "$@"