2024-01-04 16:08:01 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -e -o errexit -o nounset -o pipefail
|
|
|
|
|
2024-01-04 21:55:24 +00:00
|
|
|
: ${ENTRYPOINT_DEBUG:=0}
|
|
|
|
|
|
|
|
[[ ${ENTRYPOINT_DEBUG} == 1 ]] && set -x
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
# Some splash of color for important messages
|
2024-01-04 16:08:01 +00:00
|
|
|
declare -g error_message_color="\033[1;31m"
|
|
|
|
declare -g warn_message_color="\033[1;34m"
|
|
|
|
declare -g color_clear="\033[1;0m"
|
2024-01-04 21:21:00 +00:00
|
|
|
|
|
|
|
# Current and previous log prefix
|
2024-01-04 16:08:01 +00:00
|
|
|
declare -g log_prefix=
|
2024-01-04 21:21:00 +00:00
|
|
|
declare -g log_prefix_previous=
|
|
|
|
|
|
|
|
# dot-env files to source when reading config
|
2024-01-04 16:08:01 +00:00
|
|
|
declare -ra dot_env_files=(
|
|
|
|
/var/www/.env.docker
|
|
|
|
/var/www/.env
|
|
|
|
)
|
2024-01-04 21:21:00 +00:00
|
|
|
|
|
|
|
# environment keys seen when source dot files (so we can [export] them)
|
2024-01-04 20:55:04 +00:00
|
|
|
declare -ga seen_dot_env_variables=()
|
2024-01-04 16:08:01 +00:00
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function entrypoint-set-name() {
|
|
|
|
log_prefix_previous="${log_prefix}"
|
|
|
|
log_prefix="ENTRYPOINT - [$(get-entrypoint-script-name $1)] - "
|
2024-01-04 16:08:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function entrypoint-restore-name() {
|
|
|
|
log_prefix="${log_prefix_previous}"
|
2024-01-04 16:08:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function run-as-runtime-user() {
|
2024-01-04 20:55:04 +00:00
|
|
|
local -i exit_code
|
|
|
|
local target_user
|
|
|
|
|
|
|
|
target_user=$(id -un ${RUNTIME_UID})
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
log-info "👷 Running [${*}] as [${target_user}]"
|
2024-01-04 20:55:04 +00:00
|
|
|
|
|
|
|
su --preserve-environment "${target_user}" --shell /bin/bash --command "${*}"
|
|
|
|
exit_code=$?
|
|
|
|
|
|
|
|
if [[ $exit_code != 0 ]]; then
|
2024-01-04 21:21:00 +00:00
|
|
|
log-error "❌ Error!"
|
2024-01-04 20:55:04 +00:00
|
|
|
return $exit_code
|
|
|
|
fi
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
log-info "✅ OK!"
|
2024-01-04 20:55:04 +00:00
|
|
|
return $exit_code
|
2024-01-04 16:08:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
# @description Print the given error message to stderr
|
2024-01-04 16:08:01 +00:00
|
|
|
# @arg $message string A error message.
|
2024-01-04 21:21:00 +00:00
|
|
|
function log-error() {
|
|
|
|
echo -e "${error_message_color}${log_prefix}ERROR - ${*}${color_clear}" >/dev/stderr
|
2024-01-04 16:08:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
# @description Print the given error message to stderr and exit 1
|
|
|
|
# @arg $@ string A error message.
|
2024-01-04 16:08:01 +00:00
|
|
|
# @exitcode 1
|
2024-01-04 21:21:00 +00:00
|
|
|
function log-error-and-exit() {
|
|
|
|
log-error "$@"
|
2024-01-04 16:08:01 +00:00
|
|
|
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
# @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
|
2024-01-04 16:08:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
# @description Print the given message to stderr unless [ENTRYPOINT_QUIET_LOGS] is set
|
|
|
|
# @arg $@ string A warning message.
|
|
|
|
function log-info() {
|
2024-01-04 16:08:01 +00:00
|
|
|
if [ -z "${ENTRYPOINT_QUIET_LOGS:-}" ]; then
|
2024-01-04 21:21:00 +00:00
|
|
|
echo "${log_prefix}$*"
|
2024-01-04 16:08:01 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function load-config-files() {
|
2024-01-04 20:55:04 +00:00
|
|
|
# Associative array (aka map/dictionary) holding the unique keys found in dot-env files
|
2024-01-04 16:08:01 +00:00
|
|
|
local -A _tmp_dot_env_keys
|
|
|
|
|
|
|
|
for f in "${dot_env_files[@]}"; do
|
|
|
|
if [ ! -e "$f" ]; then
|
2024-01-04 21:21:00 +00:00
|
|
|
log-warning "Could not source file [${f}]: does not exists"
|
2024-01-04 16:08:01 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
log-info "Sourcing ${f}"
|
2024-01-04 16:08:01 +00:00
|
|
|
source "${f}"
|
|
|
|
|
|
|
|
# find all keys in the dot-env file and store them in our temp associative array
|
|
|
|
for k in "$(grep -v '^#' "${f}" | sed -E 's/(.*)=.*/\1/' | xargs)"; do
|
|
|
|
_tmp_dot_env_keys[$k]=1
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
seen_dot_env_variables=(${!_tmp_dot_env_keys[@]})
|
|
|
|
}
|
2024-01-04 20:55:04 +00:00
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function in-array() {
|
|
|
|
local -r needle="\<${1}\>"
|
|
|
|
local -nr haystack=$2
|
|
|
|
|
|
|
|
[[ ${haystack[*]} =~ $needle ]]
|
|
|
|
}
|
2024-01-04 20:55:04 +00:00
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function is-executable() {
|
|
|
|
[[ -x "$1" ]]
|
2024-01-04 20:55:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:55:24 +00:00
|
|
|
function is-writable() {
|
|
|
|
[[ -w "$1" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensure-directory() {
|
|
|
|
mkdir -pv "$@"
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:21:00 +00:00
|
|
|
function get-entrypoint-script-name() {
|
2024-01-04 20:55:04 +00:00
|
|
|
echo "${1#"$ENTRYPOINT_ROOT"}"
|
|
|
|
}
|