1
0
Files
angie-conv-image/image-entry.d/73-expand-templates.sh
Konstantin Demin 612532576b treewide: improve template expansion
also: support *.toml as extra configuration dictionaries
2025-06-19 06:00:31 +03:00

121 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
set -f
. /run/ngx/iep/00-common.envsh
## Angie: unset core variables
unset ANGIE ANGIE_BPF_MAPS
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
cd "${target_root}/"
expand_error_delim() {
IEP_DEBUG=0 log_always ' ----------------------------------- '
}
unset expand_error_seen
expand_error() {
[ "${expand_error_seen:-}" != 1 ] || return
expand_error_seen=1
expand_error_delim
log_always 'template expansion has failed'
if [ "${NGX_STRICT_LOAD}" = 1 ] ; then
t=15
log_always "injecting delay for $t seconds"
expand_error_delim
sleep $t
exit 1
fi
expand_error_delim
}
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
unset ENVSUBST_ARGS
ENVSUBST_ARGS="${volume_root}/diag.envsubst.txt"
envsubst-args.sh > "${ENVSUBST_ARGS}"
export ENVSUBST_ARGS
## envsubst is simple and fast
envsubst-dirs j2cfg/ || expand_error
## j2cfg is more complex
unset J2CFG_CONFIG
set -a
J2CFG_PATH="${target_root}/j2cfg"
J2CFG_SEARCH_PATH="${target_root}"
set +a
## expand j2cfg/ first
j2cfg-dirs j2cfg/ || expand_error
## dump [merged] j2cfg config
j2cfg_dump="${volume_root}/diag.j2cfg.yml"
j2cfg-dump-yml > "${j2cfg_dump}" || expand_error
export J2CFG_CONFIG="${j2cfg_dump}"
## expand other directories
## NB: j2cfg/ and static/ are handled separately
merge_dirs=$(mktemp)
{
set +e
find ./ -follow -mindepth 1 -maxdepth 1 -type d -printf '%P/\0' \
| grep -zFxv -e j2cfg/ -e static/ | sort -zuV
} > "${merge_dirs}"
xargs -0r -n 1000 -a "${merge_dirs}" \
envsubst-dirs < /dev/null || expand_error
xargs -0r -n 1000 -a "${merge_dirs}" \
j2cfg-dirs < /dev/null || expand_error
rm -f "${merge_dirs}" ; unset merge_dirs
## expand static/
## NB: template sources are removed unless IEP_DEBUG is set!
if [ "${NGX_HTTP_STATIC_TEMPLATE}" = 1 ] ; then
template_list=$(mktemp)
find static/ -follow -name '*.in' -type f -printf '%p\0' \
| {
set +e
if [ -n "${NGX_STATIC_EXCLUDE_REGEX:-}" ] ; then
grep -zEv -e "${NGX_STATIC_EXCLUDE_REGEX}"
elif [ -n "${NGX_STATIC_INCLUDE_REGEX:-}" ] ; then
grep -zE -e "${NGX_STATIC_INCLUDE_REGEX}"
else
cat
fi
} \
| sort -zuV > "${template_list}"
if [ -s "${template_list}" ] ; then
xargs -0r -n 1000 -a "${template_list}" \
envsubst-multi < /dev/null || expand_error
fi
find static/ -follow -name '*.j2' -type f -printf '%p\0' \
| {
set +e
if [ -n "${NGX_STATIC_EXCLUDE_REGEX:-}" ] ; then
grep -zEv -e "${NGX_STATIC_EXCLUDE_REGEX}"
elif [ -n "${NGX_STATIC_INCLUDE_REGEX:-}" ] ; then
grep -zE -e "${NGX_STATIC_INCLUDE_REGEX}"
else
cat
fi
} \
| sort -zuV > "${template_list}"
if [ -s "${template_list}" ] ; then
xargs -0r -n 1000 -a "${template_list}" \
j2cfg-multi < /dev/null || expand_error
fi
rm -f "${template_list}"
fi
exit 0