1
0

improve dynamic configuration snippets

This commit is contained in:
Konstantin Demin 2024-07-23 21:59:49 +03:00
parent 3c0a74f206
commit 7425ac403a
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0
26 changed files with 142 additions and 104 deletions

View File

@ -138,7 +138,8 @@ RUN install -d -o angie -g angie -m 03777 /angie /run/angie ; \
## adjust paths in config directory
cd /etc/angie || exit 1 ; \
ln -sv /run/angie run ; \
ln -sv /run/angie/lock lock.d ; \
ln -sv /run/angie/load load ; \
ln -sv /run/angie/lock lock ; \
ln -sv ${ANGIE_MODULES_DIR} modules.dist ; \
## hyper-modular paths:
data='conf j2cfg mod modules site snip static' ; \

View File

@ -1,23 +1,22 @@
daemon off;
pid run/angie.pid;
lock_file lock.d/angie.lock;
## almost useless
include mod.d/core-*.load;
include load/mod-core-*.conf;
# mod-http.conf.in
# mod-mail.conf.in
# mod-stream.conf.in
# mod-http.conf
# mod-mail.conf
# mod-stream.conf
include run/mod-*.conf;
events {
include conf.d/core_ev-*.conf;
include snip.d/core_ev-*.load;
include load/core_ev-*.conf;
}
include conf.d/core-*.conf;
include snip.d/core-*.load;
include load/core-*.conf;
# ctx-http.conf.in
# ctx-mail.conf.in
# ctx-stream.conf.in
# ctx-http.conf
# ctx-mail.conf
# ctx-stream.conf
include run/ctx-*.conf;

View File

@ -0,0 +1 @@
lock_file lock/angie.lock;

View File

@ -1 +1,3 @@
## if container is running in non-privileged mode,
## then this file is going to be removed by /image-entry.d/76-adjust-core-user.sh
user ${NGX_USER} ${NGX_GROUP};

View File

@ -1,5 +1,5 @@
http {
include conf.d/http-*.conf;
include snip.d/http-*.load;
include load/http-*.conf;
include site.d/http-*.conf;
}

View File

@ -1,5 +1,5 @@
mail {
include conf.d/mail-*.conf;
include snip.d/mail-*.load;
include load/mail-*.conf;
include site.d/mail-*.conf;
}

View File

@ -1,5 +1,5 @@
stream {
include conf.d/stream-*.conf;
include snip.d/stream-*.load;
include load/stream-*.conf;
include site.d/stream-*.conf;
}

View File

@ -1 +1 @@
include mod.d/http-*.load;
include load/mod-http-*.conf;

View File

@ -1 +1 @@
include mod.d/mail-*.load;
include load/mod-mail-*.conf;

View File

@ -1 +1 @@
include mod.d/stream-*.load;
include load/mod-stream-*.conf;

View File

@ -34,3 +34,23 @@ NGX_DIRS_LINK=$(sort_dedup_list "${default_dirs_link} ${NGX_DIRS_LINK:-}")
set +a
unset default_dirs_merge default_dirs_link
unset i dirs_link
dirs_link=
for i in ${NGX_DIRS_LINK:-} ; do
[ -n "$i" ] || continue
## naive deduplication
case " ${NGX_DIRS_MERGE} " in
*" $i "* )
log "$i is already specified in NGX_DIRS_MERGE - removing from NGX_DIRS_LINK"
continue
;;
esac
dirs_link="${dirs_link}${dirs_link:+ }$i"
done
unset i
export NGX_DIRS_LINK="${dirs_link}"
unset dirs_link

View File

@ -3,7 +3,7 @@
set -a
NGX_CORE_MODULES="${NGX_CORE_MODULES:-}"
NGX_CORE_EVENTS_SNIPPETS="${NGX_CORE_EVENTS_SNIPPETS:-}"
NGX_CORE_SNIPPETS="${NGX_CORE_SNIPPETS:-}"
NGX_CORE_CONFLOAD="${NGX_CORE_CONFLOAD:-}"
NGX_CORE_EVENTS_CONFLOAD="${NGX_CORE_EVENTS_CONFLOAD:-}"
set +a

View File

@ -1,8 +1,8 @@
#!/bin/sh
unset core_modules core_snippets
unset core_modules core_confload
core_modules=
core_snippets="${NGX_CORE_SNIPPETS:-}"
core_confload="${NGX_CORE_CONFLOAD:-}"
## filter out builtin core modules
unset i
@ -10,8 +10,8 @@ for i in ${NGX_CORE_MODULES:-} ; do
[ -n "$i" ] || continue
if is_builtin_module core "$i" ; then
log "$i is builtin module, moving to snippets"
core_snippets="${core_snippets} $i"
log "$i is builtin module, moving to confload"
core_confload="${core_confload} $i"
continue
fi
@ -29,8 +29,8 @@ unset i
set -a
NGX_CORE_MODULES="${core_modules}"
NGX_CORE_SNIPPETS=$(sort_dedup_list "${core_snippets}")
NGX_CORE_EVENTS_SNIPPETS=$(sort_dedup_list "${NGX_CORE_EVENTS_SNIPPETS}")
NGX_CORE_CONFLOAD=$(sort_dedup_list "${core_confload}")
NGX_CORE_EVENTS_CONFLOAD=$(sort_dedup_list "${NGX_CORE_EVENTS_CONFLOAD}")
set +a
unset core_modules core_snippets
unset core_modules core_confload

View File

@ -1,10 +1,10 @@
#!/bin/sh
if [ "${NGX_HTTP}" = 0 ] ; then
unset NGX_HTTP_MODULES NGX_HTTP_SNIPPETS
unset NGX_HTTP_MODULES NGX_HTTP_CONFLOAD
else
set -a
NGX_HTTP_MODULES="${NGX_HTTP_MODULES:-}"
NGX_HTTP_SNIPPETS="${NGX_HTTP_SNIPPETS:-}"
NGX_HTTP_CONFLOAD="${NGX_HTTP_CONFLOAD:-}"
set +a
fi

View File

@ -1,9 +1,9 @@
#!/bin/sh
if [ "${NGX_HTTP}" = 1 ] ; then
unset http_modules http_snippets
unset http_modules http_confload
http_modules=
http_snippets="${NGX_HTTP_SNIPPETS:-}"
http_confload="${NGX_HTTP_CONFLOAD:-}"
if [ -n "${NGX_HTTP_MODULES}" ] ; then
## angie-module-lua: depends on angie-module-ndk
@ -20,8 +20,8 @@ if [ "${NGX_HTTP}" = 1 ] ; then
[ -n "$i" ] || continue
if is_builtin_module http "$i" ; then
log "$i is builtin module, moving to snippets"
http_snippets="${http_snippets} $i"
log "$i is builtin module, moving to confload"
http_confload="${http_confload} $i"
continue
fi
@ -39,8 +39,8 @@ if [ "${NGX_HTTP}" = 1 ] ; then
set -a
NGX_HTTP_MODULES="${http_modules}"
NGX_HTTP_SNIPPETS=$(sort_dedup_list "${http_snippets}")
NGX_HTTP_CONFLOAD=$(sort_dedup_list "${http_confload}")
set +a
unset http_modules http_snippets
unset http_modules http_confload
fi

View File

@ -1,10 +1,10 @@
#!/bin/sh
if [ "${NGX_MAIL}" = 0 ] ; then
unset NGX_MAIL_MODULES NGX_MAIL_SNIPPETS
unset NGX_MAIL_MODULES NGX_MAIL_CONFLOAD
else
set -a
NGX_MAIL_MODULES="${NGX_MAIL_MODULES:-}"
NGX_MAIL_SNIPPETS="${NGX_MAIL_SNIPPETS:-}"
NGX_MAIL_CONFLOAD="${NGX_MAIL_CONFLOAD:-}"
set +a
fi

View File

@ -1,9 +1,9 @@
#!/bin/sh
if [ "${NGX_MAIL}" = 1 ] ; then
unset mail_modules mail_snippets
unset mail_modules mail_confload
mail_modules=
mail_snippets="${NGX_MAIL_SNIPPETS:-}"
mail_confload="${NGX_MAIL_CONFLOAD:-}"
## filter out builtin mail modules
unset i
@ -11,8 +11,8 @@ if [ "${NGX_MAIL}" = 1 ] ; then
[ -n "$i" ] || continue
if is_builtin_module mail "$i" ; then
log "$i is builtin module, moving to snippets"
mail_snippets="${mail_snippets} $i"
log "$i is builtin module, moving to confload"
mail_confload="${mail_confload} $i"
continue
fi
@ -30,8 +30,8 @@ if [ "${NGX_MAIL}" = 1 ] ; then
set -a
NGX_MAIL_MODULES="${mail_modules}"
NGX_MAIL_SNIPPETS=$(sort_dedup_list "${mail_snippets}")
NGX_MAIL_CONFLOAD=$(sort_dedup_list "${mail_confload}")
set +a
unset mail_modules mail_snippets
unset mail_modules mail_confload
fi

View File

@ -1,10 +1,10 @@
#!/bin/sh
if [ "${NGX_STREAM}" = 0 ] ; then
unset NGX_STREAM_MODULES NGX_STREAM_SNIPPETS
unset NGX_STREAM_MODULES NGX_STREAM_CONFLOAD
else
set -a
NGX_STREAM_MODULES="${NGX_STREAM_MODULES:-}"
NGX_STREAM_SNIPPETS="${NGX_STREAM_SNIPPETS:-}"
NGX_STREAM_CONFLOAD="${NGX_STREAM_CONFLOAD:-}"
set +a
fi

View File

@ -1,9 +1,9 @@
#!/bin/sh
if [ "${NGX_STREAM}" = 1 ] ; then
unset stream_modules stream_snippets
unset stream_modules stream_confload
stream_modules=
stream_snippets="${NGX_STREAM_SNIPPETS:-}"
stream_confload="${NGX_STREAM_CONFLOAD:-}"
## filter out builtin stream modules
unset i
@ -11,8 +11,8 @@ if [ "${NGX_STREAM}" = 1 ] ; then
[ -n "$i" ] || continue
if is_builtin_module stream "$i" ; then
log "$i is builtin module, moving to snippets"
stream_snippets="${stream_snippets} $i"
log "$i is builtin module, moving to confload"
stream_confload="${stream_confload} $i"
continue
fi
@ -30,8 +30,8 @@ if [ "${NGX_STREAM}" = 1 ] ; then
set -a
NGX_STREAM_MODULES="${stream_modules}"
NGX_STREAM_SNIPPETS=$(sort_dedup_list "${stream_snippets}")
NGX_STREAM_CONFLOAD=$(sort_dedup_list "${stream_confload}")
set +a
unset stream_modules stream_snippets
unset stream_modules stream_confload
fi

View File

@ -3,22 +3,6 @@ set -f
. /image-entry.d/00-common.envsh
## sanity check
t=$(mktemp) || exit 1
for n in mod snip ; do
[ -d "${merged_root}/$n" ] || continue
find "${merged_root}/$n/" ! -type d
done \
| grep -E "^${merged_root}/(mod|snip)/.+\.load\$" \
| sort -V > "$t"
if [ -s "$t" ] ; then
log_always 'these files will be EXPLICITLY skipped:'
log_file "$t"
fi
rm -f "$t" ; unset t
[ "${NGX_STRICT_LOAD}" = 0 ] || set -e
load_error_delim() {
@ -54,7 +38,7 @@ done
## 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
[ -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
@ -78,7 +62,6 @@ $(
find "${merged_root}/$n/" ! -type d
done \
| grep -Ev \
-e "^${merged_root}/(mod|snip)/.+\.load\$" \
-e "^${merged_root}/mod/[^/]+\.preseed\$" \
| sort -V
)
@ -102,87 +85,119 @@ done
## Angie modules are loaded in [strict] order!
combine_modules() {
[ -n "$1" ] || return 1
local n
n="$1" ; shift
[ $# -ne 0 ] || return 0
old_dir="${merged_root}/mod"
new_dir="${target_root}/mod"
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
old_name="$n-$m.conf"
old_path="${old_dir}/${old_name}"
if ! [ -f "${old_path}" ] ; then
log_always "file ${old_name} is not found in ${old_dir}/"
case "$m" in
/* | */../* | *\** | *\?* )
log_always "module name '$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 ${old_name} is skipped"
log "file ${src_name} is skipped"
continue
fi
new_name=$(printf "%s-%02d-%s.load" "$n" "$i" "$m")
new_path="${new_dir}/${new_name}"
dst_path="${dst_dir}/${dst_name}"
ln_cp "${old_path}" "${new_path}"
ln_cp "${src_path}" "${dst_path}"
i=$((i+1))
done ; unset m
done
}
combine_snippets() {
combine_confload() {
[ -n "$1" ] || return 1
local n
n="$1" ; shift
[ $# -ne 0 ] || return 0
old_dir="${merged_root}/snip"
new_dir="${target_root}/snip"
local s src_dir dst_dir src_name dst_name src_path dst_path
src_dir="${merged_root}/snip"
dst_dir="${volume_root}/load"
for s ; do
[ -n "$s" ] || continue
new_path="${new_dir}/$n-$s.load"
if [ -e "${new_path}" ] ; then
log "${new_path} already exists, skipping"
case "$s" in
/* | */../* | *\** | *\?* )
log_always "snippet name '$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
old_name="$n-$s.conf"
old_path="${old_dir}/${old_name}"
if ! [ -f "${old_path}" ] ; then
log_always "file ${old_name} is not found in ${old_dir}/"
if [ "${NGX_ALLOW_MISSING_SNIPPETS:-}" != 1 ] ; then
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 ${old_name} is skipped"
log "file ${src_name} is skipped"
fi
continue
fi
ln_cp "${old_path}" "${new_path}"
done ; unset s
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:-}
unset NGX_ALLOW_MISSING_SNIPPETS
loose=$(( 1 - NGX_STRICT_LOAD ))
NGX_ALLOW_MISSING_CONFLOAD=$(gobool_to_int "${NGX_ALLOW_MISSING_CONFLOAD:-${loose}}" ${loose})
unset loose
combine_snippets core ${NGX_CORE_SNIPPETS:-}
combine_snippets core_ev ${NGX_CORE_EVENTS_SNIPPETS:-}
combine_snippets http ${NGX_HTTP_SNIPPETS:-}
combine_snippets mail ${NGX_MAIL_SNIPPETS:-}
combine_snippets stream ${NGX_STREAM_SNIPPETS:-}
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_SNIPPETS=1
NGX_ALLOW_MISSING_CONFLOAD=1
combine_snippets core ${NGX_CORE_MODULES:-}
combine_snippets http ${NGX_HTTP_MODULES:-}
combine_snippets mail ${NGX_MAIL_MODULES:-}
combine_snippets stream ${NGX_STREAM_MODULES:-}
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