1
0
angie-conv-image/image-entry.d/74-combine-tree.sh

223 lines
5.1 KiB
Bash
Raw Normal View History

2024-07-11 13:37:35 +03:00
#!/bin/sh
set -f
. /image-entry.d/00-common.envsh
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
2024-07-17 13:50:13 +03:00
load_error_delim() {
IEP_DEBUG=0 log_always ' ----------------------------------- '
2024-07-17 13:50:13 +03:00
}
2024-07-11 13:37:35 +03:00
load_error() {
2024-07-17 13:50:13 +03:00
[ "${load_error_seen:-}" != 1 ] || return
2024-07-11 13:37:35 +03:00
load_error_seen=1
2024-07-17 13:50:13 +03:00
load_error_delim
log_always 'tree combine has failed'
2024-07-11 13:37:35 +03:00
if [ "${NGX_STRICT_LOAD}" = 1 ] ; then
2024-07-20 16:35:39 +03:00
t=15
2024-07-11 13:37:35 +03:00
log_always "injecting delay for $t seconds"
2024-07-17 13:50:13 +03:00
load_error_delim
2024-07-11 13:37:35 +03:00
sleep $t
exit 1
fi
2024-07-17 13:50:13 +03:00
load_error_delim
2024-07-11 13:37:35 +03:00
}
2024-07-15 16:15:47 +03:00
dirs='cache lib log'
for n in ${dirs} ; do
s="/angie/$n"
d="${target_root}/$n"
if [ -d "$s" ] ; then
ln_s "$s" "$d"
2024-07-11 13:37:35 +03:00
else
2024-07-15 16:15:47 +03:00
[ -d "$d" ] || install_userdir "$d"
2024-07-11 13:37:35 +03:00
fi
2024-07-15 16:15:47 +03:00
done
2024-07-11 13:37:35 +03:00
2024-07-24 22:47:53 +03:00
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
2024-07-15 16:15:47 +03:00
## provide same symlinks as upstream (both Angie and nginx) docker images do
d="${target_root}/log"
2024-07-20 16:35:39 +03:00
[ -e "$d/access.log" ] || ln_s /dev/stdout "$d/access.log" || load_error
2024-07-23 21:59:49 +03:00
[ -e "$d/error.log" ] || ln_s /dev/stderr "$d/error.log" || load_error
2024-07-15 16:15:47 +03:00
## NB: if any error occurs above then configuration is merely empty and/or broken
2024-07-11 13:37:35 +03:00
while read -r old_path ; do
[ -n "${old_path}" ] || continue
new_path=$(combine_remap_path "${old_path}")
[ -n "${new_path}" ]
new_dir="${new_path%/*}"
[ -d "${new_dir}" ] || mkdir -p "${new_dir}"
2024-07-15 16:15:47 +03:00
ln_cp "${old_path}" "${new_path}"
2024-07-11 13:37:35 +03:00
done <<-EOF
$(
2024-07-15 16:15:47 +03:00
set +e
2024-07-20 16:35:39 +03:00
for n in ${NGX_DIRS_MERGE} ; do
[ -n "$n" ] || continue
2024-07-11 13:37:35 +03:00
[ -d "${merged_root}/$n" ] || continue
find "${merged_root}/$n/" ! -type d
2024-07-15 16:15:47 +03:00
done \
| grep -Ev \
-e "^${merged_root}/mod/[^/]+\.preseed\$" \
| sort -V
2024-07-11 13:37:35 +03:00
)
EOF
2024-07-20 16:35:39 +03:00
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
2024-07-11 13:37:35 +03:00
[ -d "$d" ] || continue
2024-07-20 16:35:39 +03:00
ln_s "$d" "${target_root}/$n"
2024-07-11 13:37:35 +03:00
break
done
2024-07-20 16:35:39 +03:00
[ -d "${target_root}/$n" ] || {
log "missing required directory: ${target_root}/$n"
}
done
2024-07-11 13:37:35 +03:00
## Angie modules are loaded in [strict] order!
combine_modules() {
[ -n "$1" ] || return 1
2024-07-23 21:59:49 +03:00
local n
2024-07-11 13:37:35 +03:00
n="$1" ; shift
2024-07-15 16:15:47 +03:00
[ $# -ne 0 ] || return 0
2024-07-23 21:59:49 +03:00
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"
2024-07-15 16:15:47 +03:00
2024-07-11 13:37:35 +03:00
i=0
for m ; do
[ -n "$m" ] || continue
2024-07-23 21:59:49 +03:00
case "$m" in
/* | */../* | *\** | *\?* )
2024-07-24 22:47:53 +03:00
log_always "module config filename '$m' is not legal, skipping"
2024-07-23 21:59:49 +03:00
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}/"
2024-07-11 13:37:35 +03:00
load_error
2024-07-23 21:59:49 +03:00
log "file ${src_name} is skipped"
2024-07-11 13:37:35 +03:00
continue
fi
2024-07-23 21:59:49 +03:00
dst_path="${dst_dir}/${dst_name}"
2024-07-11 13:37:35 +03:00
2024-07-23 21:59:49 +03:00
ln_cp "${src_path}" "${dst_path}"
2024-07-11 13:37:35 +03:00
i=$((i+1))
2024-07-23 21:59:49 +03:00
done
2024-07-11 13:37:35 +03:00
}
2024-07-23 21:59:49 +03:00
combine_confload() {
2024-07-11 13:37:35 +03:00
[ -n "$1" ] || return 1
2024-07-23 21:59:49 +03:00
local n
2024-07-11 13:37:35 +03:00
n="$1" ; shift
2024-07-15 16:15:47 +03:00
[ $# -ne 0 ] || return 0
2024-07-23 21:59:49 +03:00
local s src_dir dst_dir src_name dst_name src_path dst_path
2024-07-24 22:47:53 +03:00
src_dir="${merged_root}/conf"
2024-07-23 21:59:49 +03:00
dst_dir="${volume_root}/load"
2024-07-15 16:15:47 +03:00
2024-07-11 13:37:35 +03:00
for s ; do
[ -n "$s" ] || continue
2024-07-23 21:59:49 +03:00
case "$s" in
/* | */../* | *\** | *\?* )
2024-07-24 22:47:53 +03:00
log_always "config filename '$s' is not legal, skipping"
2024-07-23 21:59:49 +03:00
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"
2024-07-15 16:15:47 +03:00
continue
fi
2024-07-11 13:37:35 +03:00
2024-07-23 21:59:49 +03:00
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
2024-07-17 13:50:13 +03:00
load_error
2024-07-23 21:59:49 +03:00
log "file ${src_name} is skipped"
2024-07-17 13:50:13 +03:00
fi
2024-07-11 13:37:35 +03:00
continue
fi
2024-07-23 21:59:49 +03:00
ln_cp "${src_path}" "${dst_path}"
done
2024-07-11 13:37:35 +03:00
}
2024-07-23 21:59:49 +03:00
[ -d "${volume_root}/load" ] || install -d "${volume_root}/load"
find "${volume_root}/load/" -mindepth 1 -exec rm -rf {} +
2024-07-15 16:15:47 +03:00
combine_modules core ${NGX_CORE_MODULES:-}
combine_modules http ${NGX_HTTP_MODULES:-}
combine_modules mail ${NGX_MAIL_MODULES:-}
combine_modules stream ${NGX_STREAM_MODULES:-}
2024-07-23 21:59:49 +03:00
loose=$(( 1 - NGX_STRICT_LOAD ))
NGX_ALLOW_MISSING_CONFLOAD=$(gobool_to_int "${NGX_ALLOW_MISSING_CONFLOAD:-${loose}}" ${loose})
unset loose
2024-07-17 13:50:13 +03:00
2024-07-23 21:59:49 +03:00
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:-}
2024-07-11 13:37:35 +03:00
2024-07-17 13:50:13 +03:00
## some modules doesn't have configuration at all
2024-07-23 21:59:49 +03:00
NGX_ALLOW_MISSING_CONFLOAD=1
2024-07-17 13:50:13 +03:00
2024-07-23 21:59:49 +03:00
combine_confload core ${NGX_CORE_MODULES:-}
combine_confload http ${NGX_HTTP_MODULES:-}
combine_confload mail ${NGX_MAIL_MODULES:-}
combine_confload stream ${NGX_STREAM_MODULES:-}
2024-07-11 13:37:35 +03:00
exit 0