nulib/runphp/runphp.2postamble

525 lines
16 KiB
Plaintext
Raw Normal View History

2024-09-17 13:19:18 +04:00
#EOF:runphp.userconf:ne pas modifier cette ligne
################################################################################
# Ne pas modifier à partir d'ici
if [ -n "$_sourced" ]; then
if [ "${0#-}" != "$0" ]; then
# sourcé depuis la ligne de commande
MYSELF="${BASH_SOURCE[1]}"
else
# sourcé depuis un script
MYSELF="${BASH_SOURCE[0]}"
fi
MYDIR="$(cd "$(dirname -- "$MYSELF")"; pwd)"
MYNAME="$(basename -- "$MYSELF")"
else
MYDIR="$(cd "$(dirname -- "$0")"; pwd)"
MYNAME="$(basename -- "$0")"
fi
if [ -f "$MYDIR/runphp.userconf.local" ]; then
source "$MYDIR/runphp.userconf.local"
fi
2024-10-18 15:28:55 +04:00
DEFAULT_DIST=d12
if [ -n "$RUNPHP_STANDALONE" ]; then
PROJDIR="$RUNPHP_PROJDIR"
2024-09-17 13:19:18 +04:00
COMPOSERDIR=.
COMPOSERPHAR=
VENDORDIR=vendor
BUILDENV0=
BUILDENV=
2024-10-18 15:28:55 +04:00
DIST="${RUNPHP_DIST:-$DEFAULT_DIST}"
IMAGENAME=nulib/
2024-09-17 13:19:18 +04:00
PRIVAREG=docker.io
REGISTRY="$RUNPHP_REGISTRY"
else
[ -n "$PROJDIR" ] || PROJDIR="$(dirname -- "$MYDIR")"
[ "${PROJDIR#/}" != "$PROJDIR" ] || PROJDIR="$(cd "$MYDIR/$PROJDIR"; pwd)"
[ -n "$COMPOSERDIR" ] || COMPOSERDIR=.
[ -n "$COMPOSERPHAR" ] || COMPOSERPHAR=sbin/composer.phar
[ -n "$VENDORDIR" ] || VENDORDIR=vendor
[ -n "$BUILDENV0" ] || BUILDENV0=.build.env.dist
[ -n "$BUILDENV" ] || BUILDENV=build.env
2024-10-18 15:28:55 +04:00
[ -n "$DIST" ] || DIST="$DEFAULT_DIST"
[ -n "$IMAGENAME" ] || IMAGENAME=nulib/
[ "$COMPOSERPHAR" == none ] && COMPOSERPHAR=
[ "$BUILDENV0" == none ] && BUILDENV0=
[ "$BUILDENV" == none ] && BUILDENV=
fi
2024-09-17 13:19:18 +04:00
function after_source_buildenv() {
NDIST="${DIST#d}"
}
after_source_buildenv
[ -n "$_sourced" ] && return 0
function eecho() { echo "$*" 1>&2; }
function eerror() { eecho "ERROR: $*"; }
function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; }
function is_defined() { [ -n "$(declare -p "$1" 2>/dev/null)" ]; }
function in_path() { [ -n "$1" -a -x "$(which "$1" 2>/dev/null)" ]; }
function composer() {
cd "$PROJDIR/$COMPOSERDIR" || exit 1
if [ -n "$COMPOSERPHAR" -a -x "$PROJDIR/$COMPOSERPHAR" ]; then
2024-09-17 13:19:18 +04:00
"$PROJDIR/$COMPOSERPHAR" "$@"
elif in_path composer; then
command composer "$@"
elif [ -x /usr/bin/composer ]; then
/usr/bin/composer "$@"
elif [ -x /usr/local/bin/composer ]; then
/usr/local/bin/composer "$@"
else
die "impossible de trouver composer"
fi
if [ -z "$RUNPHP_STANDALONE" -a -f composer.lock ]; then
2024-09-17 13:19:18 +04:00
cp composer.lock "$PROJDIR/.composer.lock.runphp"
fi
}
2024-10-05 11:41:38 +04:00
function ensure_image() {
local privareg imagename
if [ -z "$IMAGE" ]; then
[[ "$IMAGENAME" == */ ]] && imagename=runphp || imagename="${IMAGENAME%/*}/runphp"
privareg="$PRIVAREG"
if [ "$imagename" == runphp ]; then
[ -z "$privareg" -o "$privareg" == docker.io ] && privareg=docker.io/library
else
[ -z "$privareg" ] && privareg=docker.io
fi
IMAGE="$privareg/$imagename:$DIST"
fi
}
function check_image() {
local image="$IMAGE"
for prefix in docker.io/library/ docker.io; do
if [ "${image#$prefix}" != "$image" ]; then
image="${image#$prefix}"
break
fi
done
[ -n "$(docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$image" 2>/dev/null)" ]
}
2024-09-17 13:19:18 +04:00
2024-10-18 15:28:55 +04:00
## Arguments initiaux
Bootstrap=
2024-09-17 13:19:18 +04:00
ComposerInstall=
2024-10-18 15:28:55 +04:00
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=1
shift
fi
2024-09-17 13:19:18 +04:00
2024-10-18 15:28:55 +04:00
ForcedBootstrap=
if [ -z "$Bootstrap" -a -z "$RUNPHP_STANDALONE" ]; then
2024-09-17 13:19:18 +04:00
# si vendor/ n'existe pas, alors on doit faire bootstrap
if [ ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
ForcedBootstrap=1
elif [ ! -f "$PROJDIR/.composer.lock.runphp" ]; then
ForcedBootstrap=1
elif ! diff -q "$PROJDIR/$COMPOSERDIR/composer.lock" "$PROJDIR/.composer.lock.runphp" >&/dev/null; then
ForcedBootstrap=1
fi
if [ -n "$ForcedBootstrap" ]; then
2024-10-18 15:28:55 +04:00
[ "$RUNPHP_MODE" != docker ] && eecho "== bootstrapping runphp"
Bootstrap=1
ComposerInstall=1
2024-09-17 13:19:18 +04:00
fi
fi
if [ "$RUNPHP_MODE" != docker ]; then
2024-09-17 13:19:18 +04:00
############################################################################
# Lancement depuis l'extérieur du container
2024-10-18 15:28:55 +04:00
############################################################################
2024-09-17 13:19:18 +04:00
## Charger ~/.dkbuild.env
APT_PROXY=
APT_MIRROR=
SEC_MIRROR=
TIMEZONE=
PRIVAREG=
REGISTRY=
PROFILE=
HOST_MAPPINGS=()
function default_profile() {
PROFILE="$1"
}
function profile() {
local profile
for profile in "$@"; do
[ "$profile" == "$PROFILE" ] && return 0
done
return 1
}
function setenv() {
eval "export $1"
}
function default() {
local command="$1"; shift
local nv n v
case "$command" in
docker)
for nv in "$@"; do
[[ "$nv" == *=* ]] || continue
n="${nv%%=*}"
v="${nv#*=}"
case "$n" in
host-mappings)
read -a ns <<<"$v"
for v in "${ns[@]}"; do
HOST_MAPPINGS+=("$v")
done
;;
esac
done
;;
esac
}
[ -f ~/.dkbuild.env ] && source ~/.dkbuild.env
[ -n "$APT_PROXY" ] || APT_PROXY=
[ -n "$APT_MIRROR" ] || APT_MIRROR=default
[ -n "$SEC_MIRROR" ] || SEC_MIRROR=default
[ -n "$TIMEZONE" ] || TIMEZONE=Europe/Paris
[ -n "$PRIVAREG" ] || PRIVAREG=
[ -n "$REGISTRY" ] || REGISTRY=pubdocker.univ-reunion.fr
2024-10-18 15:28:55 +04:00
## Charger la configuration
# Recenser les valeur de proxy
declare -A PROXY_VARS
for var in {HTTPS,ALL,NO}_PROXY {http,https,all,no}_proxy; do
is_defined "$var" && PROXY_VARS[${var,,}]="${!var}"
done
# Paramètres de montage
2024-09-19 21:53:19 +04:00
if [ -n "$RUNPHP_NO_USE_RSLAVE" ]; then
2024-09-17 13:19:18 +04:00
UseRslave=
2024-09-19 21:53:19 +04:00
elif [ -n "$RUNPHP_USE_RSLAVE" ]; then
2024-09-17 13:19:18 +04:00
UseRslave=1
elif [ -e /proc/sys/fs/binfmt_misc/WSLInterop ]; then
# pas de mount propagation sous WSL
UseRslave=
else
UseRslave=1
fi
2024-10-18 15:28:55 +04:00
# Toujours vérifier l'existence de l'image
2024-09-17 13:19:18 +04:00
IMAGE=
2024-10-18 15:28:55 +04:00
if [ -z "$Bootstrap" ]; then
2024-09-19 21:53:19 +04:00
if [ -n "$RUNPHP_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNPHP_FORCE_BUILDENVS)"
2024-09-17 13:19:18 +04:00
for config in "${Configs[@]}"; do
source "$config" || exit 1
done
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
2024-10-05 11:41:38 +04:00
ensure_image
2024-10-18 15:28:55 +04:00
check_image || Bootstrap=1
2024-09-17 13:19:18 +04:00
fi
2024-10-18 15:28:55 +04:00
Chdir=
Verbose=
if [ -n "$Bootstrap" ]; then
## Mode bootstrap de l'image ###########################################
2024-10-18 15:28:55 +04:00
# Ici, on a déterminé que l'image doit être construite
2024-09-17 13:19:18 +04:00
BUILD_ARGS=(
DIST NDIST
REGISTRY
APT_PROXY
APT_MIRROR
SEC_MIRROR
TIMEZONE
)
2024-10-18 15:28:55 +04:00
SOPTS=+d:9876543210:c:UjDx:z:r:p
LOPTS=help,dist:,d19,d18,d17,d16,d15,d14,d13,d12,d11,d10,config:,ue,unless-exists,pull,nc,no-cache,po,plain-output,apt-proxy:,timezone:,privareg:,push,no-use-rslave
2024-09-17 13:19:18 +04:00
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
Dist=
2024-09-19 21:53:19 +04:00
if [ -n "$RUNPHP_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNPHP_FORCE_BUILDENVS)"
2024-09-17 13:19:18 +04:00
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=
Pull=
NoCache=
PlainOutput=
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break;;
--help)
eecho "\
runphp: construire l'image docker
USAGE
2024-10-18 15:28:55 +04:00
$MYNAME --bootstrap [options...]
2024-09-17 13:19:18 +04:00
OPTIONS
-c, --config build.env
--unless-exists
-U, --pull
-j, --no-cache
-D, --plain-output
-x, --apt-proxy APT_PROXY
-z, --timezone TIMEZONE
-r, --privareg PRIVAREG
-p, --push
2024-10-18 15:28:55 +04:00
paramètres pour la consruction de l'image"
2024-09-17 13:19:18 +04:00
exit 0
;;
-d|--dist) shift; Dist="$1";;
-[0-9]) Dist="d1${1#-}";;
--d*) Dist="${1#--}";;
-c|--config) shift; Configs+="$1";;
--ue|--unless-exists) UnlessExists=1;;
-U|--pull) Pull=1;;
-j|--nc|--no-cache) NoCache=1;;
-D|--po|--plain-output) PlainOutput=1;;
-x|--apt-proxy) shift; APT_PROXY="$1";;
-z|--timezone) shift; TIMEZONE="$1";;
-r|--privareg) shift; PRIVAREG="$1";;
-p|--push) Push=1;;
--no-use-rslave) UseRslave=;;
*) die "$1: option non configurée";;
esac
shift
done
for config in "${Configs[@]}"; do
if [ "$config" == none ]; then
Configs=()
break
fi
done
if [ ${#Configs[*]} -gt 0 ]; then
for config in "${Configs[@]}"; do
source "$config" || exit 1
done
after_source_buildenv
fi
[ -n "$Dist" ] && DIST="$Dist"
2024-10-18 15:28:55 +04:00
2024-10-05 11:41:38 +04:00
ensure_image
check_image && exists=1 || exists=
if [ -z "$UnlessExists" -o -z "$exists" ]; then
2024-09-17 13:19:18 +04:00
eecho "== Building $IMAGE"
2024-10-18 15:28:55 +04:00
if [ -n "$RUNPHP_STANDALONE" ]; then
DFDIR="$RUNPHP_STANDALONE"
else
DFDIR="$MYDIR"
fi
2024-09-17 13:19:18 +04:00
dockerfiles=(
2024-10-18 15:28:55 +04:00
"$DFDIR/Dockerfile.runphp.local"
"$DFDIR/Dockerfile.runphp$BUILD_FLAVOUR"
"$DFDIR/Dockerfile.runphp"
2024-09-17 13:19:18 +04:00
)
for dockerfile in "${dockerfiles[@]}"; do
[ -f "$dockerfile" ] && break
done
args=(
-f "$dockerfile"
${Pull:+--pull}
${NoCache:+--no-cache}
${BuildPlain:+--progress plain}
-t "$IMAGE"
)
for arg in "${BUILD_ARGS[@]}"; do
args+=(--build-arg "$arg=${!arg}")
done
for arg in "${!PROXY_VARS[@]}"; do
args+=(--build-arg "$arg=${PROXY_VARS[$arg]}")
done
for host in "${HOST_MAPPINGS[@]}"; do
args+=(--add-host "$host")
done
mkdir -p /tmp/runphp-build
docker build "${args[@]}" /tmp/runphp-build || exit 1
if [ -n "$Push" -a -n "$PRIVAREG" ]; then
eecho "== Pushing $IMAGE"
docker push "$IMAGE" || exit 1
fi
fi
2024-10-18 15:28:55 +04:00
if [ -z "$RUNPHP_STANDALONE" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
# Forcer l'installation des dépendances si nécessaire
ComposerInstall=1
2024-09-17 13:19:18 +04:00
fi
2024-10-18 15:28:55 +04:00
[ -z "$ComposerInstall" ] && exit 0
2024-09-17 13:19:18 +04:00
else
## Mode exécution de commande ##########################################
2024-10-18 15:28:55 +04:00
# Ici, on a déterminé qu'il faut lancer une commande
2024-09-17 13:19:18 +04:00
SOPTS=+w:v
LOPTS=help,chdir:,verbose,no-use-rslave
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break;;
--help)
eecho "\
2024-09-19 21:53:19 +04:00
runphp: lancer une commande dans docker
2024-09-17 13:19:18 +04:00
USAGE
$MYNAME ci|cu|composer
2024-10-18 15:32:37 +04:00
$MYNAME command [args...]
2024-09-17 13:19:18 +04:00
COMMANDES COMPOSER
ci
cu
installer/mettre à jour les dépendances du projet avec composer
composer (args...]
lancer composer avec les arguments spécifiés.
pour les commandes ci-dessus, l'option --chdir est ignorée: le répertoire
courant est forcé au répertoire du projet composer
OPTIONS
-w, --chdir CHDIR
aller dans le répertoire spécifié avant de lancer la commande
-v, --verbose
afficher plus d'informations"
exit 0
;;
-w|--chdir) shift; Chdir="$1";;
-v|--verbose) Verbose=1;;
--no-use-rslave) UseRslave=;;
*) die "$1: option non configurée";;
esac
shift
done
2024-10-18 15:28:55 +04:00
if [ -z "$RUNPHP_STANDALONE" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
2024-09-19 21:53:19 +04:00
# Forcer l'installation des dépendances si nécessaire
2024-10-18 15:28:55 +04:00
ComposerInstall=1
2024-09-17 13:19:18 +04:00
fi
fi
## Lancer la commande
args=(
run -it --rm
--name "runphp-$(basename -- "$1")-$$"
-e RUNPHP_MODE=docker
2024-09-17 13:19:18 +04:00
)
for arg in "${!PROXY_VARS[@]}"; do
2024-10-18 15:32:37 +04:00
args+=(-e "$arg=${PROXY_VARS[$arg]}")
2024-09-17 13:19:18 +04:00
done
2024-10-18 15:32:37 +04:00
if [ -n "$RUNPHP_STANDALONE" ]; then
args+=(
-e "RUNPHP_STANDALONE=1"
-e "RUNPHP_PROJDIR=$PROJDIR"
)
fi
2024-09-17 13:19:18 +04:00
for host in "${HOST_MAPPINGS[@]}"; do
args+=(--add-host "$host")
done
# monter le répertoire qui contient $PROJDIR
mount_composer=
if [ "${PROJDIR#$HOME/}" != "$PROJDIR" -o "$PROJDIR" == "$HOME" ]; then
# bind mount $HOME
args+=(-v "$HOME:$HOME${UseRslave:+:rslave}")
else
# bind mount uniquement le répertoire du projet
args+=(-v "$PROJDIR:$PROJDIR${UseRslave:+:rslave}")
mount_composer=1
fi
if [ -n "$mount_composer" -a -d "$HOME/.composer" ]; then
# monter la configuration de composer
args+=(-v "$HOME/.composer:$HOME/.composer")
fi
args+=(-w "$(pwd)")
# lancer avec l'utilisateur courant
2024-09-19 21:53:19 +04:00
if [ $(id -u) -ne 0 ]; then
2024-09-17 13:19:18 +04:00
# si c'est un utilisateur lambda, il faut monter les informations
# nécessaires. composer est déjà monté via $HOME
args+=(
2024-09-19 23:33:29 +04:00
-e DEVUSER_USERENT="$(getent passwd "$(id -un)")"
-e DEVUSER_GROUPENT="$(getent group "$(id -gn)")"
2024-09-17 13:19:18 +04:00
)
fi
args+=(
"$IMAGE"
exec "$0" ${Chdir:+-w "$Chdir"}
)
[ -n "$ForcedBootstrap" ] && set -- ci
[ -n "$Verbose" ] && eecho "\$ docker ${args[*]} $*"
exec docker "${args[@]}" "$@"
else
############################################################################
# Lancement depuis l'intérieur du container
2024-10-18 15:28:55 +04:00
############################################################################
2024-09-17 13:19:18 +04:00
2024-09-19 23:33:29 +04:00
if [ -n "$DEVUSER_USERENT" ]; then
user="${DEVUSER_USERENT%%:*}"
export DEVUSER_USERENT=
export DEVUSER_GROUPENT=
2024-09-17 13:19:18 +04:00
if in_path su-exec; then
exec su-exec "$user" "$0" "$@"
else
exec runuser -u "$user" -- "$0" "$@"
fi
fi
SOPTS=+w:
LOPTS=chdir:
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
chdir=
action=
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break;;
-w|--chdir) shift; chdir="$1";;
*) die "$1: option non configurée";;
esac
shift
done
2024-10-18 15:28:55 +04:00
if [ -z "$1" ]; then
die "no command specified"
elif [ "$1" == ci ]; then
2024-09-17 13:19:18 +04:00
eecho "== installing composer dependencies"
composer i
elif [ "$1" == cu ]; then
eecho "== upgrading composer dependencies"
composer u
elif [ "$1" == composer ]; then
"$@"
else
if [ -n "$chdir" ]; then
cd "$chdir" || exit 1
fi
2024-10-18 15:28:55 +04:00
exec "$@"
2024-09-17 13:19:18 +04:00
fi
fi