Merge "[DMAAP] DMaaP ServiceMesh compatibility"
[oom.git] / kubernetes / portal / components / portal-mariadb / resources / config / mariadb / docker-entrypoint.sh
index a363ab3..63d266b 100644 (file)
@@ -1,11 +1,11 @@
 #!/bin/bash
 
 set -eo pipefail
-shopt -s nullglob
 
 # logging functions
 mysql_log() {
-    local type="$1"; shift
+    local type
+    type="$1"; shift
     printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
 }
 mysql_note() {
@@ -24,13 +24,17 @@ mysql_error() {
 # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
 #  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
 file_env() {
-    local var="$1"
-    local fileVar="${var}_FILE"
-    local def="${2:-}"
+    local var
+    var="$1"
+    local fileVar
+    fileVar="${var}_FILE"
+    local def
+    def="${2:-}"
     if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
         mysql_error "Both $var and $fileVar are set (but are exclusive)"
     fi
-    local val="$def"
+    local val
+    val="$def"
     # val="${!var}"
     # val="$(< "${!fileVar}")"
     # eval replacement of the bashism equivalents above presents no security issue here
@@ -45,13 +49,6 @@ file_env() {
     unset "$fileVar"
 }
 
-# check to see if this file is being run or sourced from another script
-_is_sourced() {
-    # https://unix.stackexchange.com/a/215279
-    [ "${#FUNCNAME[@]}" -ge 2 ] \
-        && [ "${FUNCNAME[0]}" = '_is_sourced' ] \
-        && [ "${FUNCNAME[1]}" = 'source' ]
-}
 
 # usage: docker_process_init_files [file [file [...]]]
 #    ie: docker_process_init_files /always-initdb.d/*
@@ -85,7 +82,9 @@ docker_process_init_files() {
 }
 
 mysql_check_config() {
-    local toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" ) errors
+    local toRun
+    local errors
+    toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
     if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
         mysql_error "$(printf 'mysqld failed while attempting to check config\n\tcommand was: ')${toRun[*]}$(printf'\n\t')$errors"
     fi
@@ -95,7 +94,8 @@ mysql_check_config() {
 # We use mysqld --verbose --help instead of my_print_defaults because the
 # latter only show values present in config files, and not server defaults
 mysql_get_config() {
-    local conf="$1"; shift
+    local conf
+    conf="$1"; shift
     "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
         | awk -v conf="$conf" '$1 == conf && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
     # match "datadir      /some/path with/spaces in/it here" but not "--xyz=abc\n     datadir (xyz)"
@@ -141,7 +141,8 @@ docker_verify_minimum_env() {
 # creates folders for the database
 # also ensures permission for user mysql of run as root
 docker_create_db_directories() {
-    local user; user="$(id -u)"
+    local user
+    user="$(id -u)"
 
     # TODO other directories that are used by default? like /var/lib/mysql-files
     # see https://github.com/docker-library/mysql/issues/562
@@ -168,11 +169,20 @@ docker_init_database_dir() {
     mysql_note "Database files initialized"
 }
 
+if [ -z "$DATADIR" ]; then
+    DATADIR='unknown'
+fi
+if [ -z "$SOCKET" ]; then
+    SOCKET='unknown'
+fi
+if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
+    DATABASE_ALREADY_EXISTS='false'
+fi
+
 # Loads various settings that are used elsewhere in the script
 # This should be called after mysql_check_config, but before any other functions
 docker_setup_env() {
     # Get config
-    declare -g DATADIR SOCKET
     DATADIR="$(mysql_get_config 'datadir' "$@")"
     SOCKET="$(mysql_get_config 'socket' "$@")"
 
@@ -184,7 +194,6 @@ docker_setup_env() {
     file_env 'MYSQL_ROOT_PASSWORD'
     file_env 'PORTAL_DB_TABLES'
 
-    declare -g DATABASE_ALREADY_EXISTS
     if [ -d "$DATADIR/mysql" ]; then
         DATABASE_ALREADY_EXISTS='true'
     fi
@@ -216,7 +225,8 @@ docker_setup_db() {
             # Aria in 10.4+ is slow due to "transactional" (crash safety)
             # https://jira.mariadb.org/browse/MDEV-23326
             # https://github.com/docker-library/mariadb/issues/262
-            local tztables=( time_zone time_zone_leap_second time_zone_name time_zone_transition time_zone_transition_type )
+            local tztables
+            tztables=( time_zone time_zone_leap_second time_zone_name time_zone_transition time_zone_transition_type )
             for table in "${tztables[@]}"; do
                 echo "/*!100400 ALTER TABLE $table TRANSACTIONAL=0 */;"
             done
@@ -237,7 +247,8 @@ docker_setup_db() {
         mysql_note "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
     fi
     # Sets root password and creates root users for non-localhost hosts
-    local rootCreate=
+    local rootCreate
+    rootCreate=
     # default root to listen for connections from anywhere
     if [ -n "$MYSQL_ROOT_HOST" ] && [ "$MYSQL_ROOT_HOST" != 'localhost' ]; then
         # no, we don't care if read finds a terminating character in this heredoc
@@ -367,6 +378,7 @@ _main() {
 }
 
 # If we are sourced from elsewhere, don't perform any further actions
-if ! _is_sourced; then
+# https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced/2942183#2942183
+if [ "$(basename $0)" = "docker-entrypoint.sh" ]; then
     _main "$@"
 fi