Skip to content

Commit

Permalink
move init-file processing into a function
Browse files Browse the repository at this point in the history
files found within /docker-entrypoint-initdb.d/* are processed using
specific rules based on their filename. It was previously difficult to
re-use this logic recursively, or to add logic for handling additional
filetypes.

By moving this logic into a shell function, we both enable inner shell
scripts to easily re-use the same logic (for example, for including
files from additional directories or sub-directories), and the
possibility of overriding these processing rules entirely, for example
to enable handling of additional filename patterns.

There is a potential change of functionality compared to the previous
version: the mysql command can no-longer be overridden by changing the
mysql variable. Instead, the mysql command may be overridden only by
overriding the process_init_file() function.

There is a potentially loss of functionality in that included scripts
can no-longer use "break" to escape the file loop. There is presently
no work-around for this, as this is not expected to have been an
intended feature.
  • Loading branch information
wpalmer committed Apr 10, 2018
1 parent 30bf2b7 commit 597ac5b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
26 changes: 19 additions & 7 deletions 5.5/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ file_env() {
unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

_check_config() {
toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
Expand Down Expand Up @@ -169,13 +187,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
process_init_file "$f" "${mysql[@]}"
done

if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
Expand Down
26 changes: 19 additions & 7 deletions 5.6/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ file_env() {
unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

_check_config() {
toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
Expand Down Expand Up @@ -169,13 +187,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
process_init_file "$f" "${mysql[@]}"
done

if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
Expand Down
26 changes: 19 additions & 7 deletions 5.7/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ file_env() {
unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

_check_config() {
toRun=( "$@" --verbose --help )
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
Expand Down Expand Up @@ -174,13 +192,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
process_init_file "$f" "${mysql[@]}"
done

if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
Expand Down
26 changes: 19 additions & 7 deletions 8.0/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ file_env() {
unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

_check_config() {
toRun=( "$@" --verbose --help )
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
Expand Down Expand Up @@ -174,13 +192,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
process_init_file "$f" "${mysql[@]}"
done

if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
Expand Down

0 comments on commit 597ac5b

Please sign in to comment.