réorganiser le code runphp

This commit is contained in:
Jephté Clain 2025-02-13 11:19:45 +04:00
parent 682e4048b1
commit ca8f0a8492

View File

@ -117,9 +117,12 @@ function eerror() { eecho "ERROR: $*"; }
function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; } function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; }
function is_defined() { [ -n "$(declare -p "$1" 2>/dev/null)" ]; } function is_defined() { [ -n "$(declare -p "$1" 2>/dev/null)" ]; }
function in_path() { [ -n "$1" -a -x "$(which "$1" 2>/dev/null)" ]; } function in_path() { [ -n "$1" -a -x "$(which "$1" 2>/dev/null)" ]; }
function composer() { function composer() {
if [ -n "$PROJDIR" -a -n "$COMPOSERDIR" ]; then
cd "$PROJDIR/$COMPOSERDIR" || exit 1 cd "$PROJDIR/$COMPOSERDIR" || exit 1
if [ -n "$COMPOSERPHAR" -a -x "$PROJDIR/$COMPOSERPHAR" ]; then fi
if [ -n "$PROJDIR" -a -n "$COMPOSERPHAR" -a -x "$PROJDIR/$COMPOSERPHAR" ]; then
"$PROJDIR/$COMPOSERPHAR" "$@" "$PROJDIR/$COMPOSERPHAR" "$@"
elif in_path composer; then elif in_path composer; then
command composer "$@" command composer "$@"
@ -130,11 +133,40 @@ function composer() {
else else
die "impossible de trouver composer" die "impossible de trouver composer"
fi fi
if [ -z "$RUNPHP_STANDALONE" -a -f composer.lock ]; then if [ -n "$PROJDIR" -a -z "$RUNPHP_STANDALONE" -a -f composer.lock ]; then
cp composer.lock "$PROJDIR/.composer.lock.runphp" cp composer.lock "$PROJDIR/.composer.lock.runphp"
fi fi
} }
function ensure_image() {
function host_parse_args() {
# analyser les arguments et calculer les opérations à lancer: bootstrap,
# composer install et/ou commande
# faut-il lancer bootstrap?
Bootstrap=auto
# faut-il installer les dépendances?
Composer=
# commande à lancer
Cmd=()
if [ "$1" == --runphp-bootstrap -o "$1" == --bs ]; then
Bootstrap=1
shift
elif [ "$1" == --runphp-exec ]; then
Bootstrap=
shift
elif [ "$1" == --runphp-install -o "$1" == --ci ]; then
Composer=ci
shift
elif [ "$1" == --runphp-update -o "$1" == --cu ]; then
Composer=cu
shift
fi
Cmd=("$@")
}
function host_ensure_image() {
# calculer le nom de l'image runphp
local dfdir suffix dockerfiles dockerfile local dfdir suffix dockerfiles dockerfile
local privareg imagename local privareg imagename
if [ -z "$Image" ]; then if [ -z "$Image" ]; then
@ -161,7 +193,9 @@ function ensure_image() {
Image="$privareg/$imagename$suffix:$DIST" Image="$privareg/$imagename$suffix:$DIST"
fi fi
} }
function check_image() {
function host_check_image() {
# vérifier que l'image runphp existe
local image="$Image" local image="$Image"
for prefix in docker.io/library/ docker.io; do for prefix in docker.io/library/ docker.io; do
if [ "${image#$prefix}" != "$image" ]; then if [ "${image#$prefix}" != "$image" ]; then
@ -172,46 +206,25 @@ function check_image() {
[ -n "$(docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$image" 2>/dev/null)" ] [ -n "$(docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$image" 2>/dev/null)" ]
} }
## Arguments initiaux function host_check_projdir() {
# vérifier le projet pour voir s'il faut installer les dépendances
[ -n "$RUNPHP_STANDALONE" ] && return
Bootstrap= local install
ComposerInstall=
if [ "$1" == --runphp-bootstrap -o "$1" == --bs ]; then
Bootstrap=1
shift
elif [ "$1" == --runphp-exec ]; then
Bootstrap=
shift
elif [ "$1" == --runphp-install -o "$1" == --ci ]; then
ComposerInstall=ci
shift
elif [ "$1" == --runphp-update -o "$1" == --cu ]; then
ComposerInstall=cu
shift
fi
ForcedBootstrap=
if [ -z "$Bootstrap" -a -z "$RUNPHP_STANDALONE" ]; then
# si vendor/ n'existe pas, alors on doit faire bootstrap
if [ ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then if [ ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
ForcedBootstrap=1 install=1
elif [ ! -f "$PROJDIR/.composer.lock.runphp" ]; then elif [ ! -f "$PROJDIR/.composer.lock.runphp" ]; then
ForcedBootstrap=1 install=1
elif ! diff -q "$PROJDIR/$COMPOSERDIR/composer.lock" "$PROJDIR/.composer.lock.runphp" >&/dev/null; then elif ! diff -q "$PROJDIR/$COMPOSERDIR/composer.lock" "$PROJDIR/.composer.lock.runphp" >&/dev/null; then
ForcedBootstrap=1 install=1
fi fi
if [ -n "$ForcedBootstrap" ]; then if [ -n "$install" ]; then
[ "$RUNPHP_MODE" != docker ] && eecho "== bootstrapping runphp" eecho "== bootstrapping runphp"
Bootstrap=1 [ -n "$Composer" ] || Composer=ci
ComposerInstall=ci
fi fi
fi }
if [ "$RUNPHP_MODE" != docker ]; then
############################################################################
# Lancement depuis l'extérieur du container
############################################################################
function host_init_env() {
## Charger ~/.dkbuild.env ## Charger ~/.dkbuild.env
APT_PROXY= APT_PROXY=
@ -284,35 +297,26 @@ if [ "$RUNPHP_MODE" != docker ]; then
UseRslave=1 UseRslave=1
fi fi
# Toujours vérifier l'existence de l'image
Image= Image=
if [ -z "$Bootstrap" ]; then
if [ -n "$RUNPHP_FORCE_BUILDENVS" ]; then if [ -n "$RUNPHP_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNPHP_FORCE_BUILDENVS)" eval "Configs=($RUNPHP_FORCE_BUILDENVS)"
elif [ -n "$BUILDENV" -a -f "$PROJDIR/$BUILDENV" ]; then
Configs=("$PROJDIR/$BUILDENV")
elif [ -n "$BUILDENV0" -a -f "$PROJDIR/$BUILDENV0" ]; then
Configs=("$PROJDIR/$BUILDENV0")
else
Configs=()
fi
for config in "${Configs[@]}"; do for config in "${Configs[@]}"; do
source "$config" || exit 1 source "$config" || exit 1
done done
after_source_buildenv after_source_buildenv
elif [ -n "$BUILDENV" -a -f "$PROJDIR/$BUILDENV" ]; then
source "$PROJDIR/$BUILDENV" || exit 1
after_source_buildenv
elif [ -n "$BUILDENV0" -a -f "$PROJDIR/$BUILDENV0" ]; then
source "$PROJDIR/$BUILDENV0" || exit 1
after_source_buildenv
fi
ensure_image
if ! check_image; then
ForcedBootstrap=1
Bootstrap=1
fi
fi
Chdir= Chdir=
Verbose="$RUNPHP_VERBOSE" Verbose="$RUNPHP_VERBOSE"
if [ -n "$Bootstrap" ]; then }
## Mode bootstrap de l'image ###########################################
# Ici, on a déterminé que l'image doit être construite
function host_docker_build() {
BUILD_ARGS=( BUILD_ARGS=(
DIST NDIST DIST NDIST
REGISTRY REGISTRY
@ -327,15 +331,6 @@ if [ "$RUNPHP_MODE" != docker ]; then
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args" args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
Dist= Dist=
if [ -n "$RUNPHP_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNPHP_FORCE_BUILDENVS)"
elif [ -n "$BUILDENV" -a -f "$PROJDIR/$BUILDENV" ]; then
Configs=("$PROJDIR/$BUILDENV")
elif [ -n "$BUILDENV0" -a -f "$PROJDIR/$BUILDENV0" ]; then
Configs=("$PROJDIR/$BUILDENV0")
else
Configs=()
fi
UnlessExists= UnlessExists=
Pull= Pull=
NoCache= NoCache=
@ -348,25 +343,31 @@ if [ "$RUNPHP_MODE" != docker ]; then
runphp: construire l'image docker runphp: construire l'image docker
USAGE USAGE
$MYNAME --runphp-bootstrap [options...] $MYNAME --bs [options...]
OPTIONS OPTIONS
-c, --config build.env -c, --config build.env
--unless-exists -d, --dist DIST
--ue, --unless-exists
-U, --pull -U, --pull
-j, --no-cache -j, --nc, --no-cache
-D, --plain-output -D, --po, --plain-output
-x, --apt-proxy APT_PROXY -x, --apt-proxy APT_PROXY
-z, --timezone TIMEZONE -z, --timezone TIMEZONE
-r, --privareg PRIVAREG -r, --privareg PRIVAREG
-p, --push -p, --push
paramètres pour la consruction de l'image" paramètres pour la consruction de l'image
--ci
--cu
lancer composer install (resp. update) après bootstrap
--no-use-rslave
paramètre montage des volumes"
exit 0 exit 0
;; ;;
-c|--config) shift; Configs+=("$1");;
-d|--dist) shift; Dist="$1";; -d|--dist) shift; Dist="$1";;
-[0-9]) Dist="d1${1#-}";; -[0-9]) Dist="d1${1#-}";;
--d*) Dist="${1#--}";; --d*) Dist="${1#--}";;
-c|--config) shift; Configs+="$1";;
--ue|--unless-exists) UnlessExists=1;; --ue|--unless-exists) UnlessExists=1;;
-U|--pull) Pull=1;; -U|--pull) Pull=1;;
-j|--nc|--no-cache) NoCache=1;; -j|--nc|--no-cache) NoCache=1;;
@ -375,13 +376,14 @@ OPTIONS
-z|--timezone) shift; TIMEZONE="$1";; -z|--timezone) shift; TIMEZONE="$1";;
-r|--privareg) shift; PRIVAREG="$1";; -r|--privareg) shift; PRIVAREG="$1";;
-p|--push) Push=1;; -p|--push) Push=1;;
--ci) ComposerInstall=ci;; --ci) Composer=ci;;
--cu) ComposerInstall=cu;; --cu) Composer=cu;;
--no-use-rslave) UseRslave=;; --no-use-rslave) UseRslave=;;
*) die "$1: option non configurée";; *) die "$1: option non configurée";;
esac esac
shift shift
done done
Cmd=("$@")
for config in "${Configs[@]}"; do for config in "${Configs[@]}"; do
if [ "$config" == none ]; then if [ "$config" == none ]; then
@ -397,8 +399,10 @@ OPTIONS
fi fi
[ -n "$Dist" ] && DIST="$Dist" [ -n "$Dist" ] && DIST="$Dist"
ensure_image Image=
check_image && exists=1 || exists= host_ensure_image
host_check_image && exists=1 || exists=
if [ -z "$UnlessExists" -o -z "$exists" ]; then if [ -z "$UnlessExists" -o -z "$exists" ]; then
eecho "== Building $Image" eecho "== Building $Image"
args=( args=(
@ -425,24 +429,10 @@ OPTIONS
docker push "$Image" || exit 1 docker push "$Image" || exit 1
fi fi
fi fi
if [ -z "$RUNPHP_STANDALONE" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then }
# Forcer l'installation des dépendances si nécessaire
[ -n "$ComposerInstall" ] || ComposerInstall=ci
fi
if [ -n "$ForcedBootstrap" ]; then
# si le bootstrap est forcé, ne continuer que s'il y a des arguments
[ $# -eq 0 ] && exit 0
else
# si le bootstrap a été demandé, ne continuer que s'il faut
# installer les dépendances
[ -z "$ComposerInstall" ] && exit 0
fi
fi
## Mode exécution de commande ##########################################
# Ici, on a déterminé qu'il faut lancer une commande
function host_docker_run() {
# lancer une commande avec docker
SOPTS=+w: SOPTS=+w:
LOPTS=help,chdir:,no-use-rslave LOPTS=help,chdir:,no-use-rslave
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args" args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
@ -470,7 +460,9 @@ courant est forcé au répertoire du projet composer
OPTIONS OPTIONS
-w, --chdir CHDIR -w, --chdir CHDIR
aller dans le répertoire spécifié avant de lancer la commande" aller dans le répertoire spécifié avant de lancer la commande
--no-use-rslave
paramètre montage des volumes"
exit 0 exit 0
;; ;;
-w|--chdir) shift; Chdir="$1";; -w|--chdir) shift; Chdir="$1";;
@ -480,13 +472,6 @@ OPTIONS
shift shift
done done
if [ -z "$RUNPHP_STANDALONE" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
# Forcer l'installation des dépendances si nécessaire
ComposerInstall=ci
fi
## Lancer la commande
args=( args=(
run -it --rm run -it --rm
--name "runphp-$(basename -- "$1")-$$" --name "runphp-$(basename -- "$1")-$$"
@ -514,7 +499,7 @@ OPTIONS
[ -n "$RUNPHP_STANDALONE" ] && [ -n "$RUNPHP_STANDALONE" ] &&
[ "${RUNPHP_STANDALONE#$HOME/}" != "$RUNPHP_STANDALONE" ] && [ "${RUNPHP_STANDALONE#$HOME/}" != "$RUNPHP_STANDALONE" ] &&
mount_runphp= mount_runphp=
else elif [ -n "$PROJDIR" ]; then
# bind mount uniquement le répertoire du projet # bind mount uniquement le répertoire du projet
args+=(-v "$PROJDIR:$PROJDIR${UseRslave:+:rslave}") args+=(-v "$PROJDIR:$PROJDIR${UseRslave:+:rslave}")
mount_composer=1 mount_composer=1
@ -543,15 +528,13 @@ OPTIONS
"$Image" "$Image"
exec "$0" ${Chdir:+-w "$Chdir"} exec "$0" ${Chdir:+-w "$Chdir"}
) )
[ -n "$ComposerInstall" ] && set -- "$ComposerInstall" "$@"
[ -n "$Verbose" ] && eecho "\$ docker ${args[*]} $*" [ -n "$Verbose" ] && eecho "\$ docker ${args[*]} $*"
exec docker "${args[@]}" "$@" docker "${args[@]}" "$@"
}
else
############################################################################
# Lancement depuis l'intérieur du container
############################################################################
function container_exec() {
# lancer la commande $@. cette fonction doit être lancée dans le container
# docker
if [ -n "$DEVUSER_USERENT" ]; then if [ -n "$DEVUSER_USERENT" ]; then
user="${DEVUSER_USERENT%%:*}" user="${DEVUSER_USERENT%%:*}"
export DEVUSER_USERENT= export DEVUSER_USERENT=
@ -578,7 +561,7 @@ else
shift shift
done done
if [ -z "$1" ]; then if [ $# -eq 0 ]; then
die "no command specified" die "no command specified"
elif [ "$1" == ci ]; then elif [ "$1" == ci ]; then
eecho "== installing composer dependencies" eecho "== installing composer dependencies"
@ -596,4 +579,32 @@ else
fi fi
exec "$@" exec "$@"
fi fi
}
################################################################################
if [ "$RUNPHP_MODE" != docker ]; then
# Lancement depuis l'extérieur du container
host_parse_args "$@"
host_init_env
Image=
if [ "$Bootstrap" == auto ]; then
host_ensure_image
host_check_image && Bootstrap= || Bootstrap=1
fi
if [ -n "$Bootstrap" ]; then
host_docker_build "${Cmd[@]}"
else
host_ensure_image
fi
host_check_projdir
[ -n "$Composer" ] && host_docker_run "$Composer"
[ ${#Cmd[*]} -gt 0 ] && host_docker_run "${Cmd[@]}"
else
# Lancement depuis l'intérieur du container
container_exec "$@"
fi fi