1
0
angie-conv-image/image-entry.d/00-common.envsh

230 lines
4.3 KiB
Plaintext
Raw Normal View History

2024-07-11 13:37:35 +03:00
#!/bin/sh
2024-07-15 16:15:47 +03:00
volume_root='/run/angie'
tmp_dir="${volume_root}/tmp"
merged_root="${volume_root}/merged"
target_root="${volume_root}"
2024-07-11 13:37:35 +03:00
have_envvar() {
[ -n "$1" ] || return 1
grep -Ezq "^$1=" /proc/self/environ || return
}
## unexporting variable in (POSIX) sh is PITA =/
unexport() {
local ___k ___v
2024-07-11 13:37:35 +03:00
for ___k ; do
[ -n "${___k}" ] || continue
have_envvar "${___k}" || continue
___v=$(eval printf '%s' "\"\${${___k}}\"")
eval "unset ${___k}"
eval "${___k}=$(env printf '%s' \"\${___v}\")"
done
}
## likely the same as in https://pkg.go.dev/strconv#ParseBool
gobool_to_int() {
## local value=$1
## local default=$2
case "${1:-_}" in
1 | [Tt] | [Tt][Rr][Uu][Ee] ) echo 1 ;;
0 | [Ff] | [Ff][Aa][Ll][Ss][Ee] ) echo 0 ;;
* ) echo "${2:-error}" ;;
esac
}
[ -n "${__IEP_SRC:-}" ] || __IEP_SRC="$0"
2024-07-17 13:50:13 +03:00
log_always() {
if [ "${IEP_DEBUG}" = 1 ] ; then
2024-07-17 13:50:13 +03:00
echo "# $(date +'%Y-%m-%d %H:%M:%S.%03N %z'): ${__IEP_SRC}${*:+: $*}"
else
echo "# ${__IEP_SRC}${*:+: $*}"
fi >&2
}
2024-07-11 13:37:35 +03:00
2024-07-17 13:50:13 +03:00
log() {
[ "${IEP_VERBOSE}" = 0 ] || log_always "$@"
}
2024-07-15 16:15:47 +03:00
ln_s() {
if [ "${IEP_VERBOSE}" = 0 ] ; then
ln -s "$@" || return
else
ln -sv "$@" || return
fi
}
cp_a() {
if [ "${IEP_VERBOSE}" = 0 ] ; then
cp -a "$@" || return
else
cp -av "$@" || return
fi
}
2024-07-11 13:37:35 +03:00
2024-07-15 16:15:47 +03:00
ln_cp() {
if [ -h "$1" ] ; then
ln_s "$(readlink -e "$1")" "$2"
else
cp_a "$1" "$2"
fi
}
2024-07-11 13:37:35 +03:00
have_cmd() { command -v "$1" >/dev/null 2>&1 || return ; }
strip_suffix() { printf '%s' "${1%"$2"}" | tr -s '/' ; }
install_userdir() {
if [ "${IEP_ROOT}" = 1 ] ; then
install -d -o "${NGX_USER}" -g "${NGX_GROUP}" "$@"
else
install -d "$@"
fi
}
2024-07-16 02:43:08 +03:00
expand_file_envsubst() {
local __ret __src __dst
__ret=0
2024-07-16 02:43:08 +03:00
for __src ; do
[ -n "${__src}" ] || continue
if ! [ -f "${__src}" ] ; then
__ret=1
2024-07-16 02:43:08 +03:00
log_always "file not found: ${__src}"
continue
fi
case "${__src}" in
*.in ) ;;
* )
__ret=1
2024-07-16 02:43:08 +03:00
log "expand_file_envsubst: file name extension mismatch: ${__src}"
continue
;;
esac
__dst=$(strip_suffix "${__src}" '.in')
if [ -e "${__dst}" ] ; then
__ret=1
log "expand_file_envsubst: destination file already exists: ${__dst}"
2024-07-16 02:43:08 +03:00
continue
fi
log "Running envsubst: ${__src} -> ${__dst}"
envsubst.sh < "${__src}" > "${__dst}" || __ret=1
2024-07-16 02:43:08 +03:00
done
return ${__ret}
2024-07-11 13:37:35 +03:00
}
2024-07-20 16:35:39 +03:00
expand_file_j2cfg() {
j2cfg-single "$@" || return $?
2024-07-11 13:37:35 +03:00
}
2024-07-16 02:43:08 +03:00
expand_dir_envsubst() {
local __template_list __have_args __ret __orig_file
2024-07-11 13:37:35 +03:00
__template_list=$(mktemp) || return
2024-07-16 02:43:08 +03:00
find "$@" -follow -type f -name '*.in' \
2024-07-11 13:37:35 +03:00
| sort -uV > "${__template_list}"
__have_args="${ENVSUBST_ARGS:+1}"
if [ -z "${__have_args}" ] ; then
## optimize envsubst.sh invocation by caching argument list
## ref: envsubst.sh
ENVSUBST_ARGS=$(mktemp) || return
envsubst-args.sh > "${ENVSUBST_ARGS}"
export ENVSUBST_ARGS
fi
2024-07-16 02:43:08 +03:00
__ret=0
2024-07-15 16:15:47 +03:00
while read -r __orig_file ; do
[ -n "${__orig_file}" ] || continue
2024-07-16 02:43:08 +03:00
expand_file_envsubst "${__orig_file}" || __ret=1
2024-07-11 13:37:35 +03:00
done < "${__template_list}"
if [ -z "${__have_args}" ] ; then
rm -f "${ENVSUBST_ARGS}" ; unset ENVSUBST_ARGS
fi
unset __have_args
rm -f "${__template_list}" ; unset __template_list
2024-07-16 02:43:08 +03:00
return ${__ret}
2024-07-11 13:37:35 +03:00
}
2024-07-20 16:35:39 +03:00
expand_dir_j2cfg() {
local __template_list __ret
2024-07-11 13:37:35 +03:00
__template_list=$(mktemp) || return
2024-07-16 02:43:08 +03:00
find "$@" -follow -type f -name '*.j2' -printf '%p\0' \
| sort -zuV > "${__template_list}"
2024-07-11 13:37:35 +03:00
2024-07-16 02:43:08 +03:00
__ret=0
if [ -s "${__template_list}" ] ; then
xargs -0r -n 1000 -a "${__template_list}" \
2024-07-20 16:35:39 +03:00
j2cfg-multi < /dev/null || __ret=1
2024-07-16 02:43:08 +03:00
fi
2024-07-11 13:37:35 +03:00
rm -f "${__template_list}" ; unset __template_list
2024-07-16 02:43:08 +03:00
return ${__ret}
2024-07-11 13:37:35 +03:00
}
2024-07-15 16:15:47 +03:00
is_builtin_module() {
[ -n "${1:-}" ] || return 1
[ -n "${2:-}" ] || return 1
[ -f "/etc/angie/builtin.$1" ] || return 1
[ -s "/etc/angie/builtin.$1" ] || return 1
grep -Fxq -e "$2" "/etc/angie/builtin.$1" || return 1
}
2024-07-26 01:18:59 +03:00
append_list() {
if [ -n "$2" ] ; then
printf '%s' "${1:-}${1:+ }$2"
else
printf '%s' "$1"
fi
}
prepend_list() {
if [ -n "$2" ] ; then
printf '%s' "$2${1:+ }${1:-}"
else
printf '%s' "$1"
fi
}
list_have_item() {
[ -n "$2" ] || return 1
case " $1 " in
*" $2 "* ) return 0 ;;
* ) return 1 ;;
esac
}
2024-07-24 22:47:53 +03:00
normalize_list() {
[ -n "$1" ] || return 0
printf '%s' "$1" \
| tr -s '[:space:]' ' ' \
| sed -zE 's/^ //;s/ $//'
}
2024-07-15 16:15:47 +03:00
sort_dedup_list() {
[ -n "$1" ] || return 0
printf '%s' "$1" \
| tr -s '[:space:]' '\n' | sort -uV | paste -sd ' ' \
| sed -zE 's/^\s+//;s/\s+$//'
}
2024-07-26 01:18:59 +03:00
float_div() {
mawk -v "a=$1" -v "b=$2" 'BEGIN{print a/b;exit;}' </dev/null
}