|
@@ -1,70 +1,79 @@
|
|
#!/bin/bash
|
|
#!/bin/bash
|
|
set -e -o errexit -o nounset -o pipefail
|
|
set -e -o errexit -o nounset -o pipefail
|
|
|
|
|
|
|
|
+# Some splash of color for important messages
|
|
declare -g error_message_color="\033[1;31m"
|
|
declare -g error_message_color="\033[1;31m"
|
|
declare -g warn_message_color="\033[1;34m"
|
|
declare -g warn_message_color="\033[1;34m"
|
|
declare -g color_clear="\033[1;0m"
|
|
declare -g color_clear="\033[1;0m"
|
|
|
|
+
|
|
|
|
+# Current and previous log prefix
|
|
declare -g log_prefix=
|
|
declare -g log_prefix=
|
|
-declare -g old_log_prefix=
|
|
|
|
|
|
+declare -g log_prefix_previous=
|
|
|
|
+
|
|
|
|
+# dot-env files to source when reading config
|
|
declare -ra dot_env_files=(
|
|
declare -ra dot_env_files=(
|
|
/var/www/.env.docker
|
|
/var/www/.env.docker
|
|
/var/www/.env
|
|
/var/www/.env
|
|
)
|
|
)
|
|
|
|
+
|
|
|
|
+# environment keys seen when source dot files (so we can [export] them)
|
|
declare -ga seen_dot_env_variables=()
|
|
declare -ga seen_dot_env_variables=()
|
|
|
|
|
|
-function set_identity() {
|
|
|
|
- old_log_prefix="${log_prefix}"
|
|
|
|
- log_prefix="ENTRYPOINT - [$(get_script_name $1)] - "
|
|
|
|
|
|
+function entrypoint-set-name() {
|
|
|
|
+ log_prefix_previous="${log_prefix}"
|
|
|
|
+ log_prefix="ENTRYPOINT - [$(get-entrypoint-script-name $1)] - "
|
|
}
|
|
}
|
|
|
|
|
|
-function resetore_identity() {
|
|
|
|
- log_prefix="${old_log_prefix}"
|
|
|
|
|
|
+function entrypoint-restore-name() {
|
|
|
|
+ log_prefix="${log_prefix_previous}"
|
|
}
|
|
}
|
|
|
|
|
|
-function as_runtime_user() {
|
|
|
|
|
|
+function run-as-runtime-user() {
|
|
local -i exit_code
|
|
local -i exit_code
|
|
local target_user
|
|
local target_user
|
|
|
|
|
|
target_user=$(id -un ${RUNTIME_UID})
|
|
target_user=$(id -un ${RUNTIME_UID})
|
|
|
|
|
|
- log "👷 Running [${*}] as [${target_user}]"
|
|
|
|
|
|
+ log-info "👷 Running [${*}] as [${target_user}]"
|
|
|
|
|
|
su --preserve-environment "${target_user}" --shell /bin/bash --command "${*}"
|
|
su --preserve-environment "${target_user}" --shell /bin/bash --command "${*}"
|
|
exit_code=$?
|
|
exit_code=$?
|
|
|
|
|
|
if [[ $exit_code != 0 ]]; then
|
|
if [[ $exit_code != 0 ]]; then
|
|
- log_error "❌ Error!"
|
|
|
|
|
|
+ log-error "❌ Error!"
|
|
return $exit_code
|
|
return $exit_code
|
|
fi
|
|
fi
|
|
|
|
|
|
- log "✅ OK!"
|
|
|
|
|
|
+ log-info "✅ OK!"
|
|
return $exit_code
|
|
return $exit_code
|
|
}
|
|
}
|
|
|
|
|
|
-# @description Display the given error message with its line number on stderr and exit with error.
|
|
|
|
|
|
+# @description Print the given error message to stderr
|
|
# @arg $message string A error message.
|
|
# @arg $message string A error message.
|
|
-function log_error() {
|
|
|
|
- echo -e "${error_message_color}${log_prefix}ERROR - ${1}${color_clear}" >/dev/stderr
|
|
|
|
|
|
+function log-error() {
|
|
|
|
+ echo -e "${error_message_color}${log_prefix}ERROR - ${*}${color_clear}" >/dev/stderr
|
|
}
|
|
}
|
|
|
|
|
|
-# @description Display the given error message with its line number on stderr and exit with error.
|
|
|
|
-# @arg $message string A error message.
|
|
|
|
|
|
+# @description Print the given error message to stderr and exit 1
|
|
|
|
+# @arg $@ string A error message.
|
|
# @exitcode 1
|
|
# @exitcode 1
|
|
-function log_error_and_exit() {
|
|
|
|
- log_error "$1"
|
|
|
|
|
|
+function log-error-and-exit() {
|
|
|
|
+ log-error "$@"
|
|
|
|
|
|
exit 1
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
|
|
-# @description Display the given warning message with its line number on stderr.
|
|
|
|
-# @arg $message string A warning message.
|
|
|
|
-function log_warning() {
|
|
|
|
- echo -e "${warn_message_color}${log_prefix}WARNING - ${1}${color_clear}" >/dev/stderr
|
|
|
|
|
|
+# @description Print the given warning message to stderr
|
|
|
|
+# @arg $@ string A warning message.
|
|
|
|
+function log-warning() {
|
|
|
|
+ echo -e "${warn_message_color}${log_prefix}WARNING - ${*}${color_clear}" >/dev/stderr
|
|
}
|
|
}
|
|
|
|
|
|
-function log() {
|
|
|
|
|
|
+# @description Print the given message to stderr unless [ENTRYPOINT_QUIET_LOGS] is set
|
|
|
|
+# @arg $@ string A warning message.
|
|
|
|
+function log-info() {
|
|
if [ -z "${ENTRYPOINT_QUIET_LOGS:-}" ]; then
|
|
if [ -z "${ENTRYPOINT_QUIET_LOGS:-}" ]; then
|
|
- echo "${log_prefix}$@"
|
|
|
|
|
|
+ echo "${log_prefix}$*"
|
|
fi
|
|
fi
|
|
}
|
|
}
|
|
|
|
|
|
@@ -74,11 +83,11 @@ function load-config-files() {
|
|
|
|
|
|
for f in "${dot_env_files[@]}"; do
|
|
for f in "${dot_env_files[@]}"; do
|
|
if [ ! -e "$f" ]; then
|
|
if [ ! -e "$f" ]; then
|
|
- log_warning "Could not source file [${f}]: does not exists"
|
|
|
|
|
|
+ log-warning "Could not source file [${f}]: does not exists"
|
|
continue
|
|
continue
|
|
fi
|
|
fi
|
|
|
|
|
|
- log "Sourcing ${f}"
|
|
|
|
|
|
+ log-info "Sourcing ${f}"
|
|
source "${f}"
|
|
source "${f}"
|
|
|
|
|
|
# find all keys in the dot-env file and store them in our temp associative array
|
|
# find all keys in the dot-env file and store them in our temp associative array
|
|
@@ -90,13 +99,17 @@ function load-config-files() {
|
|
seen_dot_env_variables=(${!_tmp_dot_env_keys[@]})
|
|
seen_dot_env_variables=(${!_tmp_dot_env_keys[@]})
|
|
}
|
|
}
|
|
|
|
|
|
-function array_value_exists() {
|
|
|
|
- local -nr validOptions=$1
|
|
|
|
- local -r providedValue="\<${2}\>"
|
|
|
|
|
|
+function in-array() {
|
|
|
|
+ local -r needle="\<${1}\>"
|
|
|
|
+ local -nr haystack=$2
|
|
|
|
+
|
|
|
|
+ [[ ${haystack[*]} =~ $needle ]]
|
|
|
|
+}
|
|
|
|
|
|
- [[ ${validOptions[*]} =~ $providedValue ]]
|
|
|
|
|
|
+function is-executable() {
|
|
|
|
+ [[ -x "$1" ]]
|
|
}
|
|
}
|
|
|
|
|
|
-function get_script_name() {
|
|
|
|
|
|
+function get-entrypoint-script-name() {
|
|
echo "${1#"$ENTRYPOINT_ROOT"}"
|
|
echo "${1#"$ENTRYPOINT_ROOT"}"
|
|
}
|
|
}
|