1
0
angie-conv-image/image-entry.d/74-combine-tree.sh
2024-07-31 14:04:15 +03:00

237 lines
5.3 KiB
Bash
Executable File

#!/bin/sh
set -f
. /image-entry.d/00-common.envsh
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
load_error_delim() {
IEP_DEBUG=0 log_always ' ----------------------------------- '
}
unset load_error_seen
load_error() {
[ "${load_error_seen:-}" != 1 ] || return
load_error_seen=1
load_error_delim
log_always 'tree combine has failed'
if [ "${NGX_STRICT_LOAD}" = 1 ] ; then
t=15
log_always "injecting delay for $t seconds"
load_error_delim
sleep $t
exit 1
fi
load_error_delim
}
dirs='cache lib log'
for n in ${dirs} ; do
s="/angie/$n"
d="${target_root}/$n"
if [ -d "$s" ] ; then
ln_s "$s" "$d"
else
[ -d "$d" ] || install_userdir "$d"
fi
done
d="${target_root}/cache"
for p in ${NGX_HTTP_CACHES:-} ; do
[ -d "$d/$p" ] || install_userdir "$d/$p" || load_error
done
d="${target_root}/lib"
dirs='acme'
[ "${NGX_HTTP_WITH_MODSECURITY:-}" != 1 ] || dirs="${dirs} modsecurity"
for p in ${dirs} ; do
[ -d "$d/$p" ] || install_userdir "$d/$p" || load_error
done
if [ "${NGX_HTTP_WITH_MODSECURITY:-}" = 1 ] ; then
d="${target_root}/log"
for p in modsecurity modsecurity/concurrent ; do
[ -d "$d/$p" ] || install_userdir "$d/$p" || load_error
done
fi
## provide same symlinks as upstream (both Angie and nginx) docker images do
d="${target_root}/log"
[ -e "$d/access.log" ] || ln_s /dev/stdout "$d/access.log" || load_error
[ -e "$d/error.log" ] || ln_s /dev/stderr "$d/error.log" || load_error
## NB: if any error occurs above then configuration is merely empty and/or broken
remap_path() {
[ -n "$1" ] || return
case "$1" in
"${merged_root}"/* ) echo "${target_root}${1#"${merged_root}"}" ;;
## misbehavior!
* )
log_always "remap_path() doesn't know how to handle this path: $1"
return 1
;;
esac
}
while read -r old_path ; do
[ -n "${old_path}" ] || continue
new_path=$(remap_path "${old_path}")
[ -n "${new_path}" ]
new_dir="${new_path%/*}"
[ -d "${new_dir}" ] || mkdir -p "${new_dir}"
ln_cp "${old_path}" "${new_path}"
done <<-EOF
$(
set +e
for n in j2cfg ${NGX_DIRS_MERGE} ; do
[ -n "$n" ] || continue
[ -d "${merged_root}/$n" ] || continue
find "${merged_root}/$n/" ! -type d
done \
| grep -Ev \
-e "^${merged_root}/mod/[^/]+\.preseed\$" \
| sort -V
)
EOF
for n in ${NGX_DIRS_LINK} ; do
[ -n "$n" ] || continue
if [ -e "${target_root}/$n" ] ; then continue ; fi
for d in "/angie/$n" "/etc/angie/$n" "/etc/angie/$n.dist" ; do
[ -d "$d" ] || continue
ln_s "$d" "${target_root}/$n"
break
done
[ -d "${target_root}/$n" ] || {
log "missing required directory: ${target_root}/$n"
}
done
## Angie modules are loaded in [strict] order!
combine_modules() {
[ -n "$1" ] || return 1
local n
n="$1" ; shift
[ $# -ne 0 ] || return 0
local i m src_dir dst_dir src_name dst_name src_path dst_path dst_dir
src_dir="${merged_root}/mod"
dst_dir="${volume_root}/load"
i=0
for m ; do
[ -n "$m" ] || continue
case "$m" in
/* | */../* | *\** | *\?* )
log_always "module config filename '$m' is not legal, skipping"
continue
;;
esac
case "$m" in
*/* ) src_name="$m.conf" ;;
* ) src_name="$n-$m.conf" ;;
esac
dst_name=$(printf 'mod-%s-%02d-%s.conf' "$n" "$i" "$m" | tr -s '/_' '_')
src_path="${src_dir}/${src_name}"
if ! [ -f "${src_path}" ] ; then
log_always "file ${src_name} is not found in ${src_dir}/"
load_error
log "file ${src_name} is skipped"
continue
fi
dst_path="${dst_dir}/${dst_name}"
ln_cp "${src_path}" "${dst_path}"
i=$((i+1))
done
}
combine_confload() {
[ -n "$1" ] || return 1
local n
n="$1" ; shift
[ $# -ne 0 ] || return 0
local s src_dir dst_dir src_name dst_name src_path dst_path
src_dir="${merged_root}/conf"
dst_dir="${volume_root}/load"
for s ; do
[ -n "$s" ] || continue
case "$s" in
/* | */../* | *\** | *\?* )
log_always "config filename '$s' is not legal, skipping"
continue
;;
esac
case "$s" in
*/* ) src_name="$s.conf" ;;
* ) src_name="$n-$s.conf" ;;
esac
dst_name=$(printf '%s-%s.conf' "$n" "$s" | tr -s '/_' '_')
dst_path="${dst_dir}/${dst_name}"
if [ -e "${dst_path}" ] ; then
log "${dst_path} already exists, skipping"
continue
fi
src_path="${src_dir}/${src_name}"
if ! [ -f "${src_path}" ] ; then
log_always "file ${src_name} is not found in ${src_dir}/"
if [ "${NGX_ALLOW_MISSING_CONFLOAD:-}" != 1 ] ; then
load_error
log "file ${src_name} is skipped"
fi
continue
fi
ln_cp "${src_path}" "${dst_path}"
done
}
[ -d "${volume_root}/load" ] || install -d "${volume_root}/load"
find "${volume_root}/load/" -mindepth 1 -exec rm -rf {} +
combine_modules core ${NGX_CORE_MODULES:-}
combine_modules http ${NGX_HTTP_MODULES:-}
combine_modules mail ${NGX_MAIL_MODULES:-}
combine_modules stream ${NGX_STREAM_MODULES:-}
loose=$(( 1 - NGX_STRICT_LOAD ))
NGX_ALLOW_MISSING_CONFLOAD=$(gobool_to_int "${NGX_ALLOW_MISSING_CONFLOAD:-${loose}}" ${loose})
unset loose
combine_confload core ${NGX_CORE_CONFLOAD:-}
combine_confload core_ev ${NGX_CORE_EVENTS_CONFLOAD:-}
combine_confload http ${NGX_HTTP_CONFLOAD:-}
combine_confload mail ${NGX_MAIL_CONFLOAD:-}
combine_confload stream ${NGX_STREAM_CONFLOAD:-}
## some modules doesn't have configuration at all
NGX_ALLOW_MISSING_CONFLOAD=1
combine_confload core ${NGX_CORE_MODULES:-}
combine_confload http ${NGX_HTTP_MODULES:-}
combine_confload mail ${NGX_MAIL_MODULES:-}
combine_confload stream ${NGX_STREAM_MODULES:-}
exit 0