1
0
Fork 0
pixelfed/contrib/docker/shared/root/docker/helpers.sh

169 lines
5.2 KiB
Bash
Raw Normal View History

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 22:16:25 +00:00
# @description Restore the log prefix to the previous value that was captured in [entrypoint-set-name]
# @arg $1 string The name (or path) of the entrypoint script being run
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 22:16:25 +00:00
# @description Restore the log prefix to the previous value that was captured in [entrypoint-set-name]
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 22:16:25 +00:00
# @description Run a command as the [runtime user]
# @arg $@ string The command to run
# @exitcode 0 if the command succeeeds
# @exitcode 1 if the command fails
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 22:16:25 +00:00
# @stderr The error message provided with log prefix
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 22:16:25 +00:00
# @stderr The error message provided with log prefix
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.
2024-01-04 22:16:25 +00:00
# @stderr The warning message provided with log prefix
2024-01-04 21:21:00 +00:00
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 22:16:25 +00:00
# @description Print the given message to stdout unless [ENTRYPOINT_QUIET_LOGS] is set
# @arg $@ string A info message.
# @stdout The info message provided with log prefix unless $ENTRYPOINT_QUIET_LOGS
2024-01-04 21:21:00 +00:00
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
}
2024-01-04 22:16:25 +00:00
# @description Loads the dot-env files used by Docker and track the keys present in the configuration.
# @sets seen_dot_env_variables array List of config keys discovered during loading
2024-01-04 16:08:01 +00:00
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 22:16:25 +00:00
# @description Checks if $needle exists in $haystack
# @arg $1 string The needle (value) to search for
# @arg $2 array The haystack (array) to search in
# @exitcode 0 If $needle was found in $haystack
# @exitcode 1 If $needle was *NOT* found in $haystack
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 22:16:25 +00:00
# @description Checks if $1 has executable bit set or not
# @arg $1 string The path to check
# @exitcode 0 If $1 has executable bit
# @exitcode 1 If $1 does *NOT* have executable bit
2024-01-04 21:21:00 +00:00
function is-executable() {
[[ -x "$1" ]]
2024-01-04 20:55:04 +00:00
}
2024-01-04 22:16:25 +00:00
# @description Checks if $1 is writable or not
# @arg $1 string The path to check
# @exitcode 0 If $1 is writable
# @exitcode 1 If $1 is *NOT* writable
2024-01-04 21:55:24 +00:00
function is-writable() {
[[ -w "$1" ]]
}
2024-01-04 22:16:25 +00:00
# @description Checks if $1 contains any files or not
# @arg $1 string The path to check
# @exitcode 0 If $1 contains files
# @exitcode 1 If $1 does *NOT* contain files
function is-directory-empty() {
! find "${1}" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v
}
# @description Ensures a directory exists (via mkdir)
# @arg $1 string The path to create
# @exitcode 0 If $1 If the path exists *or* was created
# @exitcode 1 If $1 If the path does *NOT* exists and could *NOT* be created
function ensure-directory-exists() {
2024-01-04 21:55:24 +00:00
mkdir -pv "$@"
}
2024-01-04 22:16:25 +00:00
# @description Find the relative path for a entrypoint script by removing the ENTRYPOINT_ROOT prefix
# @arg $1 string The path to manipulate
# @stdout The relative path to the entrypoint script
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"}"
}