nulib-base/runphp/update-runphp.sh
2025-06-04 19:35:14 +04:00

204 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname "$0")/../load.sh" || exit 1
function parse_runphp() {
local runphp="$1" preamble="$2" conf="$3" postamble="$4"
local conf0
ac_set_tmpfile conf0
# extraire la configuration depuis le fichier
<"$runphp" awk -v preamble="$preamble" -v conf="$conf0" -v postamble="$postamble" '
BEGIN { out = preamble }
{
if ($0 ~ /SOF:runphp.userconf:/) {
if (out != "") print >out
out = conf
} else if ($0 ~ /EOF:runphp.userconf:/) {
out = postamble
if (out != "") print >out
} else {
if (out != "") print >out
}
}
'
# mettre en forme le fichier conf: pas de lignes vides avant et après
<"$conf0" >"$conf" awk '
BEGIN { p = 0; have_pending = 0; pending = "" }
$0 != "" { p = 1 }
p == 1 {
if ($0 != "") {
if (have_pending) print pending
print
have_pending = 0
pending = ""
} else {
if (!have_pending) have_pending = 1
else pending = pending "\n"
}
}
'
ac_clean "$conf0"
}
declare -A PHPWRAPPER_DESTDIRS=(
[_bg_launcher.php]=@@SBIN@@
[.launcher.php]=@@CLI@@
[_wrapper.sh]=@@CLI@@
)
declare -A PHPWRAPPER_MODES=(
[_bg_launcher.php]=+x
[.launcher.php]=
[_wrapper.sh]=+x
)
projdir=
install_phpwrappers=auto
args=(
"Mettre à jour le script runphp"
"[path/to/runphp]"
-d:,--projdir:PROJDIR . "\
Copier les fichiers pour un projet de l'université de la Réunion:
- BUILDENV0 et BUILDENV sont fixés à ..env.dist et .env
- les fichiers ..env.dist et .runphp.conf sont créés le cas échéant
- le script build est mis à jour
- les wrappers PHP pour la gestion des tâches de fond sont mis à jour le cas
échéant"
-p,--phpwrappers-install install_phpwrappers=1 "forcer l'installation des wrappers PHP"
--np,--no-phpwrappers-install install_phpwrappers= "ne pas installer les wrappers PHP"
)
parse_args "$@"; set -- "${args[@]}"
if [ -n "$projdir" ]; then
setx projdir=abspath "$projdir"
set -- "$projdir/sbin/runphp"
fi
runphp="${1:-.}"
if [ -d "$runphp" ]; then
runphp="$runphp/runphp"
elif [ ! -e "$runphp" ]; then
[ "${runphp%/runphp}" != "$runphp" ] || runphp="$runphp/runphp"
fi
setx runphp=abspath "$runphp"
setx rundir=dirname -- "$runphp"
[ "$rundir" == "$MYDIR" ] && exit 0
[ -d "$rundir" ] || mkdir -p "$rundir"
# Tout d'abord, isoler chaque partie du fichier local
ac_set_tmpfile preamble
ac_set_tmpfile conf
ac_set_tmpfile postamble
parse_runphp "$MYDIR/runphp" "$preamble" "$conf" "$postamble"
# Puis analyser le fichier destination
if [ -f "$runphp" ]; then
ac_set_tmpfile userconf
parse_runphp "$runphp" "" "$userconf" ""
initial_config=
elif [ -n "$projdir" ]; then
# forcer BUILDENV0=..env.dist et BUILDENV=.env pour les projets de
# l'université de la Réunion
ac_set_tmpfile userconf
sed <"$conf" >"$userconf" '
/^BUILDENV0=/s/=.*/=..env.dist/
/^BUILDENV=/s/=.*/=.env/
'
initial_config=1
else
userconf="$conf"
initial_config=1
fi
# (Re)construire le fichier destination
estep "$(relpath "$runphp")"
(
cat "$preamble"
echo
cat "$userconf"
echo
cat "$postamble"
) >"$runphp"
[ -x "$runphp" ] || chmod +x "$runphp"
eval "$(
vars=(PROJDIR COMPOSERDIR COMPOSERPHAR VENDORDIR BUILDENV0 BUILDENV BUILD_FLAVOUR DIST IMAGENAME)
arrays=(BUILD_IMAGES DISTFILES TEMPLATEFILES VARFILES)
for var in "${vars[@]}"; do eval "$var="; done
for array in "${arrays[@]}"; do eval "$array=()"; done
source "$runphp"
for var in "${vars[@]}"; do echo_setv2 "$var"; done
for array in "${arrays[@]}"; do echo_seta2 "$array"; done
)"
estep "$(relpath "$rundir/Dockerfile.runphp")"
rsync -lpt "$MYDIR/Dockerfile.runphp" "$rundir/"
if [ -n "$projdir" ]; then
if testdiff "$rundir/build" "$MYDIR/build"; then
estep "$(relpath "$rundir/build")"
cp "$MYDIR/build" "$rundir/build"
chmod +x "$rundir/build"
fi
if [ ! -f "$projdir/..env.dist" ]; then
estep "$(relpath "$projdir/..env.dist")"
sed <"$MYDIR/dot-build.env.dist" >"$projdir/..env.dist" '
/^IMAGENAME=/s/=.*\//='"$(basename -- "$projdir")"'\//
'
initial_config=1
fi
if [ ! -f "$projdir/.runphp.conf" ]; then
estep "$(relpath "$projdir/.runphp.conf")"
sed <"$MYDIR/dot-runphp.conf" >"$projdir/.runphp.conf" '
/^RUNPHP=/s/=.*/=sbin\/runphp/
'
fi
sbin_path=sbin
cli_path=cli_config
if [ "$install_phpwrappers" == auto ]; then
if [ ! -f "$PROJDIR/$COMPOSERDIR/composer.json" ]; then
# ce doit être un projet PHP
install_phpwrappers=
elif [ -d "$projdir/cli_config" ]; then
install_phpwrappers=1
sbin_path=sbin
cli_path=cli_config
elif [ -d "$projdir/_cli" ]; then
install_phpwrappers=1
sbin_path=sbin
cli_path=_cli
else
install_phpwrappers=
fi
fi
if [ -n "$install_phpwrappers" ]; then
setx -a phpwrappers=ls_files "$MYDIR" "phpwrapper-*"
for phpwrapper in "${phpwrappers[@]}"; do
destname="${phpwrapper#phpwrapper-}"
destdir="${PHPWRAPPER_DESTDIRS[$destname]}"
[ -n "$destdir" ] || die "$phpwrapper: la destination n'est pas configurée"
mode="${PHPWRAPPER_MODES[$destname]}"
case "$destdir" in
@@SBIN@@) destdir="$PROJDIR/$sbin_path";;
@@CLI@@) destdir="$PROJDIR/$cli_path";;
*) destdir="$PROJDIR/$destdir";;
esac
estep "$(relpath "$destdir/$destname")"
mkdir -p "$destdir"
tail -n+4 "$MYDIR/$phpwrapper" | sed "
s|/@@SBIN@@/|/$sbin_path/|
s|/@@CLI@@/|/$cli_path/|
" >"$destdir/$destname"
[ -n "$mode" ] && chmod "$mode" "$destdir/$destname"
done
fi
fi
[ -n "$initial_config" ]