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 ## adjust paths in config directory
cd /etc/angie || exit 1 ; \ cd /etc/angie || exit 1 ; \
ln -sv /run/angie run ; \ 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 ; \ ln -sv ${ANGIE_MODULES_DIR} modules.dist ; \
## hyper-modular paths: ## hyper-modular paths:
data='conf j2cfg mod modules site snip static' ; \ data='conf j2cfg mod modules site snip static' ; \

View File

@ -1,23 +1,22 @@
daemon off; daemon off;
pid run/angie.pid; pid run/angie.pid;
lock_file lock.d/angie.lock;
## almost useless ## almost useless
include mod.d/core-*.load; include load/mod-core-*.conf;
# mod-http.conf.in # mod-http.conf
# mod-mail.conf.in # mod-mail.conf
# mod-stream.conf.in # mod-stream.conf
include run/mod-*.conf; include run/mod-*.conf;
events { events {
include conf.d/core_ev-*.conf; include conf.d/core_ev-*.conf;
include snip.d/core_ev-*.load; include load/core_ev-*.conf;
} }
include conf.d/core-*.conf; include conf.d/core-*.conf;
include snip.d/core-*.load; include load/core-*.conf;
# ctx-http.conf.in # ctx-http.conf
# ctx-mail.conf.in # ctx-mail.conf
# ctx-stream.conf.in # ctx-stream.conf
include run/ctx-*.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}; user ${NGX_USER} ${NGX_GROUP};

View File

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

View File

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

View File

@ -1,5 +1,5 @@
stream { stream {
include conf.d/stream-*.conf; include conf.d/stream-*.conf;
include snip.d/stream-*.load; include load/stream-*.conf;
include site.d/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 set +a
unset default_dirs_merge default_dirs_link 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 set -a
NGX_CORE_MODULES="${NGX_CORE_MODULES:-}" NGX_CORE_MODULES="${NGX_CORE_MODULES:-}"
NGX_CORE_EVENTS_SNIPPETS="${NGX_CORE_EVENTS_SNIPPETS:-}" NGX_CORE_CONFLOAD="${NGX_CORE_CONFLOAD:-}"
NGX_CORE_SNIPPETS="${NGX_CORE_SNIPPETS:-}" NGX_CORE_EVENTS_CONFLOAD="${NGX_CORE_EVENTS_CONFLOAD:-}"
set +a set +a

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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