1
0
angie-conv-image/scripts/apt-install-angie-mod.sh

170 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
set -ef
[ $# -gt 0 ] || exit 1
if [ -z "${NGX_DEBUG:-}" ] ; then
cat >&2 <<-EOF
$0:
NGX_DEBUG is not set - defaulting to NGX_DEBUG=0
EOF
NGX_DEBUG=0
fi
set -a
ANGIE_MODULES_DIR="${ANGIE_MODULES_DIR:-/usr/lib/angie/modules}"
set +a
## produce package list
pkgs=
for i ; do
[ -n "$i" ] || continue
i="angie-module-$i"
printf '%s' "$i" | grep -zEq '^[a-z0-9.+-]+$' || {
env printf 'package name %q is not legal, quitting!\n' "$i" >&2
exit 1
}
pkgs="${pkgs}${pkgs:+ }$i"
done
[ -n "${pkgs}" ] || exit 0
normalize_list() {
[ -n "$1" ] || return 0
printf '%s' "$1" \
| tr -s '[:space:]' ' ' \
| sed -zE 's/^ //;s/ $//'
}
sort_dedup_list() {
[ -n "$1" ] || return 0
printf '%s' "$1" \
| tr -s '[:space:]' '\n' | sort -uV | paste -sd ' ' \
| sed -zE 's/^\s+//;s/\s+$//'
}
pkgs=$(sort_dedup_list "${pkgs}")
dirs='cache lib log'
for n in ${dirs} ; do
d="/run/angie/$n"
[ -d "$d" ] || install -d "$d"
done
apt-install.sh ${pkgs}
ANGIE_MODCONF_DIR=/etc/angie/mod.dist
[ -d "${ANGIE_MODCONF_DIR}" ] || install -d "${ANGIE_MODCONF_DIR}"
list_ngx_modules() {
set +e
dpkg-query -L "$1" \
| grep -F -e "${ANGIE_MODULES_DIR}/" \
| grep -E -e '/[^/]+_module(-debug)?\.so$' \
| sed -E '\,^(.+)-debug\.so$,{p;s//\1.so/;p;d}' \
| sort -uV \
| xargs -r ls -U1d 2>/dev/null
set -e
}
is_same_file() {
find -L "$1" -samefile "$2" -printf . -quit 2>/dev/null | grep -Fq . || return 1
}
gen_mod_config() {
if [ -s "$2" ] ; then
printf '%s: configuration already exists: %s\n' "$1" "$2" >&2
return
fi
[ -n "$3" ] || return
local __m
for __m in $3 ; do
echo "load_module ${__m};"
done > "$2"
}
for p in ${pkgs} ; do
[ -n "$p" ] || continue
i="${p#angie-module-}"
## adjust modules:
## - remove debug module if not in debug image
## - move debug module to usual location otherwise
while read -r fmod_debug ; do
# [ -n "${fmod_debug}" ] || continue
case "${fmod_debug}" in
*-debug.so ) ;;
* ) continue ;;
esac
fmod="${fmod_debug%-debug.so}.so"
fmod_tmp=$(mktemp -u "${fmod}.XXXXXXXXXX")
if [ "${NGX_DEBUG}" = 0 ] ; then
if [ -f "${fmod}" ] ; then
fmod_real=$(readlink -f "${fmod}")
else
env printf 'missing (non-debug) file: %q\n' "${fmod}" >&2
env printf 'falling back to (debug) file: %q\n' "${fmod_debug}" >&2
fmod_real=$(readlink -f "${fmod_debug}")
fi
else
fmod_real=$(readlink -f "${fmod_debug}")
fi
[ -n "${fmod_real}" ] || exit 1
ln -v "${fmod_real}" "${fmod_tmp}"
rm -fv "${fmod}" "${fmod_debug}"
mv -fv "${fmod_tmp}" "${fmod}"
done <<-EOF
$(list_ngx_modules "$p")
EOF
if [ -e "${ANGIE_MODCONF_DIR}/.$i.preseed" ] ; then
printf '%s: skipping generation of module load config (preseed is in effect)\n' "$p" >&2
continue
fi
## produce attachable module configs
http_modules=
mail_modules=
stream_modules=
while read -r fmod ; do
[ -n "${fmod}" ] || continue
fmod_short="modules.d/${fmod#"${ANGIE_MODULES_DIR}/"}"
fname=${fmod##*/}
case "${fname}" in
ngx_http_* )
http_modules="${http_modules}${http_modules:+ }${fmod_short}"
;;
ngx_mail_* )
mail_modules="${mail_modules}${mail_modules:+ }${fmod_short}"
;;
ngx_stream_* )
stream_modules="${stream_modules}${stream_modules:+ }${fmod_short}"
;;
## damn NDK
ndk_http_* )
http_modules="${http_modules}${http_modules:+ }${fmod_short}"
;;
* )
env printf '%s: unable to determine module type for file (skipping): %q\n' "$p" "${fmod}" >&2
continue
;;
esac
done <<-EOF
$(list_ngx_modules "$p")
EOF
[ -z "${http_modules}" ] || gen_mod_config "$p" "${ANGIE_MODCONF_DIR}/http-$i.conf" "${http_modules}"
[ -z "${mail_modules}" ] || gen_mod_config "$p" "${ANGIE_MODCONF_DIR}/mail-$i.conf" "${mail_modules}"
[ -z "${stream_modules}" ] || gen_mod_config "$p" "${ANGIE_MODCONF_DIR}/stream-$i.conf" "${stream_modules}"
done