nulib/support/rundk.2postamble

503 lines
15 KiB
Plaintext
Raw Normal View History

#EOF:rundk.userconf:ne pas modifier cette ligne
2024-04-08 18:46:01 +04:00
################################################################################
2024-03-27 12:40:36 +04:00
# Ne pas modifier à partir d'ici
2024-04-19 12:52:26 +04:00
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
2024-04-19 11:46:31 +04:00
MYDIR="$(cd "$(dirname -- "$MYSELF")"; pwd)"
MYNAME="$(basename -- "$MYSELF")"
else
MYDIR="$(cd "$(dirname -- "$0")"; pwd)"
MYNAME="$(basename -- "$0")"
fi
2024-05-15 16:23:53 +04:00
if [ -f "$MYDIR/rundk.userconf.local" ]; then
source "$MYDIR/rundk.userconf.local"
fi
2024-04-19 11:46:31 +04:00
2024-03-27 12:40:36 +04:00
[ -n "$PROJDIR" ] || PROJDIR="$(dirname -- "$MYDIR")"
2024-04-19 11:46:31 +04:00
[ "${PROJDIR#/}" != "$PROJDIR" ] || PROJDIR="$(cd "$MYDIR/$PROJDIR"; pwd)"
2024-03-27 12:40:36 +04:00
[ -n "$COMPOSERDIR" ] || COMPOSERDIR=.
[ -n "$COMPOSERPHAR" ] || COMPOSERPHAR=sbin/composer.phar
[ -n "$VENDORDIR" ] || VENDORDIR=vendor
2024-03-27 18:19:14 +04:00
[ -n "$BUILDENV0" ] || BUILDENV0=.build.env.dist
2024-03-27 12:40:36 +04:00
[ -n "$BUILDENV" ] || BUILDENV=build.env
2024-03-27 18:19:14 +04:00
[ -n "$DIST" ] || DIST=d12
2024-03-27 19:32:24 +04:00
[ -n "$IMAGENAME" ] || IMAGENAME=nulib/rundk
2024-03-27 12:40:36 +04:00
[ "$COMPOSERPHAR" == none ] && COMPOSERPHAR=
2024-03-27 18:19:14 +04:00
[ "$BUILDENV0" == none ] && BUILDENV0=
2024-03-27 12:40:36 +04:00
[ "$BUILDENV" == none ] && BUILDENV=
2024-05-12 22:09:50 +04:00
function after_source_buildenv() {
NDIST="${DIST#d}"
}
2024-05-12 23:41:53 +04:00
after_source_buildenv
2024-04-19 11:46:31 +04:00
2024-04-19 12:52:26 +04:00
[ -n "$_sourced" ] && return 0
2024-04-19 11:46:31 +04:00
2024-03-27 12:40:36 +04:00
function eecho() { echo "$*" 1>&2; }
function eerror() { eecho "ERROR: $*"; }
function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; }
2024-03-27 19:40:54 +04:00
function is_defined() { [ -n "$(declare -p "$1" 2>/dev/null)" ]; }
2024-03-27 12:40:36 +04:00
function in_path() { [ -n "$1" -a -x "$(which "$1" 2>/dev/null)" ]; }
function composer() {
cd "$PROJDIR/$COMPOSERDIR" || exit 1
if [ -x "$PROJDIR/$COMPOSERPHAR" ]; then
"$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 [ -f composer.lock ]; then
cp composer.lock "$PROJDIR/.composer.lock.rundk"
fi
}
bootstrap=
BootstrapOnly=1
2024-03-27 21:39:01 +04:00
ComposerInstall=
2024-03-27 12:40:36 +04:00
ForcedBootstrap=
2024-03-27 18:19:14 +04:00
parse_opts=1
2024-03-27 12:40:36 +04:00
args=()
for arg in "$@"; do
2024-03-27 18:19:14 +04:00
if [ -z "$parse_opts" ]; then
args+=("$arg")
2024-03-27 21:39:01 +04:00
elif [ "$arg" == --bootstrap -o "$arg" == --bs ]; then
2024-03-27 12:40:36 +04:00
bootstrap=1
elif [ "$arg" == --exec ]; then
BootstrapOnly=
2024-03-27 21:39:01 +04:00
elif [ "$arg" == --composer-install -o "$arg" == --ci ]; then
ComposerInstall=1
2024-03-27 18:19:14 +04:00
elif [[ "$arg" == -* ]]; then
args+=("$arg")
2024-03-27 12:40:36 +04:00
else
args+=("$arg")
2024-03-27 18:19:14 +04:00
parse_opts=
2024-03-27 12:40:36 +04:00
fi
done
set -- "${args[@]}"
if [ -z "$bootstrap" ]; then
# 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.rundk" ]; then
ForcedBootstrap=1
elif ! diff -q "$PROJDIR/$COMPOSERDIR/composer.lock" "$PROJDIR/.composer.lock.rundk" >&/dev/null; then
ForcedBootstrap=1
elif [ -n "$bootstrap" -a -n "$BootstrapOnly" ]; then
# bootstrap inutile
exit 0
fi
if [ -n "$ForcedBootstrap" ]; then
2024-04-19 12:27:08 +04:00
[ -z "$_RUNDK_IN_DOCKER" ] && eecho "== rundk is needed: bootstrapping"
2024-03-27 12:40:36 +04:00
bootstrap=1
BootstrapOnly=
fi
fi
if [ -z "$_RUNDK_IN_DOCKER" ]; then
############################################################################
# Lancement depuis l'extérieur du container
2024-03-27 19:40:54 +04:00
# 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
2024-03-27 12:40:36 +04:00
## Charger ~/.dkbuild.env
2024-03-27 19:32:24 +04:00
APT_PROXY=
APT_MIRROR=
SEC_MIRROR=
TIMEZONE=
PRIVAREG=
REGISTRY=
2024-03-27 12:40:36 +04:00
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
## Construire l'image
if [ -n "$RUNDK_NO_USE_RSLAVE" ]; then
UseRslave=
elif [ -n "$RUNDK_USE_RSLAVE" ]; then
UseRslave=1
elif [ -e /proc/sys/fs/binfmt_misc/WSLInterop ]; then
# pas de mount propagation sous WSL
UseRslave=
else
UseRslave=1
fi
2024-03-27 19:32:24 +04:00
IMAGE=
2024-03-27 15:33:59 +04:00
if [ -z "$bootstrap" ]; then
2024-05-15 17:27:48 +04:00
if [ -n "$RUNDK_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNDK_FORCE_BUILDENVS)"
for config in "${Configs[@]}"; do
source "$config" || exit 1
done
after_source_buildenv
elif [ -n "$BUILDENV" -a -f "$PROJDIR/$BUILDENV" ]; then
2024-03-27 15:33:59 +04:00
source "$PROJDIR/$BUILDENV" || exit 1
2024-05-12 22:09:50 +04:00
after_source_buildenv
2024-03-27 18:19:14 +04:00
elif [ -n "$BUILDENV0" -a -f "$PROJDIR/$BUILDENV0" ]; then
source "$PROJDIR/$BUILDENV0" || exit 1
2024-05-12 22:09:50 +04:00
after_source_buildenv
2024-03-27 15:33:59 +04:00
fi
if [ -z "$IMAGE" ]; then
2024-03-27 18:19:14 +04:00
[ -n "$PRIVAREG" ] && IMAGE="$PRIVAREG/$IMAGENAME:$DIST" || IMAGE="$REGISTRY/$IMAGENAME:$DIST"
2024-03-27 15:33:59 +04:00
fi
if [ -z "$(docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$IMAGE" 2>/dev/null)" ]; then
bootstrap=1
fi
fi
2024-03-27 12:40:36 +04:00
if [ -n "$bootstrap" ]; then
BUILD_ARGS=(
2024-04-19 11:46:31 +04:00
DIST NDIST
2024-03-27 12:40:36 +04:00
REGISTRY
APT_PROXY
APT_MIRROR
SEC_MIRROR
TIMEZONE
)
2024-03-27 18:19:14 +04:00
SOPTS=+d:9876543210:c:UjDx:z:r:pw:v
2024-03-27 21:39:01 +04:00
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,chdir:,verbose,no-use-rslave
2024-03-27 12:40:36 +04:00
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
2024-03-27 18:19:14 +04:00
Dist=
2024-05-15 17:27:48 +04:00
if [ -n "$RUNDK_FORCE_BUILDENVS" ]; then
eval "Configs=($RUNDK_FORCE_BUILDENVS)"
elif [ -n "$BUILDENV" -a -f "$PROJDIR/$BUILDENV" ]; then
2024-05-15 17:27:48 +04:00
Configs=("$PROJDIR/$BUILDENV")
2024-03-27 18:19:14 +04:00
elif [ -n "$BUILDENV0" -a -f "$PROJDIR/$BUILDENV0" ]; then
2024-05-15 17:27:48 +04:00
Configs=("$PROJDIR/$BUILDENV0")
2024-03-27 18:19:14 +04:00
else
2024-05-15 17:27:48 +04:00
Configs=()
2024-03-27 18:19:14 +04:00
fi
2024-03-27 12:40:36 +04:00
UnlessExists=
Pull=
NoCache=
PlainOutput=
Chdir=
Verbose=
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break;;
--help)
eecho "\
rundk: construire l'image docker
USAGE
$MYNAME --bootstrap [options...] [--exec command [args...]]
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
paramètres pour la consruction de l'image
-w, --chdir CHDIR
aller dans le répertoire spécifié avant de lancer la commande
-v, --verbose
afficher plus d'informations"
exit 0
;;
2024-03-27 18:19:14 +04:00
-d|--dist) shift; Dist="$1";;
-[0-9]) Dist="d1${1#-}";;
--d*) Dist="${1#--}";;
2024-05-15 17:27:48 +04:00
-c|--config) shift; Configs+="$1";;
2024-03-27 21:39:01 +04:00
--ue|--unless-exists) UnlessExists=1;;
2024-03-27 12:40:36 +04:00
-U|--pull) Pull=1;;
2024-03-27 21:39:01 +04:00
-j|--nc|--no-cache) NoCache=1;;
-D|--po|--plain-output) PlainOutput=1;;
2024-03-27 12:40:36 +04:00
-x|--apt-proxy) shift; APT_PROXY="$1";;
-z|--timezone) shift; TIMEZONE="$1";;
-r|--privareg) shift; PRIVAREG="$1";;
-p|--push) Push=1;;
-w|--chdir) shift; Chdir="$1";;
-v|--verbose) Verbose=1;;
--no-use-rslave) UseRslave=;;
*) die "$1: option non configurée";;
esac
shift
done
2024-05-15 17:27:48 +04:00
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
2024-05-12 22:09:50 +04:00
after_source_buildenv
2024-03-27 12:40:36 +04:00
fi
2024-03-27 18:19:14 +04:00
[ -n "$Dist" ] && DIST="$Dist"
2024-03-27 12:40:36 +04:00
if [ -z "$IMAGE" ]; then
2024-03-27 18:19:14 +04:00
[ -n "$PRIVAREG" ] && IMAGE="$PRIVAREG/$IMAGENAME:$DIST" || IMAGE="$REGISTRY/$IMAGENAME:$DIST"
2024-03-27 12:40:36 +04:00
fi
if [ -z "$UnlessExists" -o -z "$(docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$IMAGE" 2>/dev/null)" ]; then
eecho "== Building $IMAGE"
2024-05-15 16:23:53 +04:00
dockerfiles=(
"$MYDIR/Dockerfile.rundk.local"
"$MYDIR/Dockerfile.rundk$BUILD_FLAVOUR"
"$MYDIR/Dockerfile.rundk"
)
for dockerfile in "${dockerfiles[@]}"; do
[ -f "$dockerfile" ] && break
done
2024-03-27 12:40:36 +04:00
args=(
2024-05-15 16:23:53 +04:00
-f "$dockerfile"
2024-03-27 12:40:36 +04:00
${Pull:+--pull}
${NoCache:+--no-cache}
${BuildPlain:+--progress plain}
-t "$IMAGE"
)
for arg in "${BUILD_ARGS[@]}"; do
args+=(--build-arg "$arg=${!arg}")
done
2024-03-27 19:40:54 +04:00
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
2024-03-27 12:40:36 +04:00
mkdir -p /tmp/rundk-build
docker build "${args[@]}" /tmp/rundk-build || exit 1
if [ -n "$Push" -a -n "$PRIVAREG" ]; then
eecho "== Pushing $IMAGE"
docker push "$IMAGE" || exit 1
fi
fi
2024-03-27 21:39:01 +04:00
if [ -n "$ComposerInstall" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
BootstrapOnly=
ForcedBootstrap=1
fi
2024-03-27 12:40:36 +04:00
[ -n "$BootstrapOnly" ] && exit 0
else
SOPTS=+w:v
LOPTS=help,chdir:,verbose,no-use-rslave
args="$(getopt -n "$MYNAME" -o "$SOPTS" -l "$LOPTS" -- "$@")" || exit 1; eval "set -- $args"
Chdir=
Verbose=
while [ $# -gt 0 ]; do
case "$1" in
--) shift; break;;
--help)
eecho "\
rundk: construire l'image docker
USAGE
$MYNAME ci|cu|composer
$MYNAME --exec [options...] command [args...]
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-03-27 21:39:01 +04:00
if [ -n "$ComposerInstall" -a ! -f "$PROJDIR/$VENDORDIR/nulib/php/load.sh" ]; then
ForcedBootstrap=1
fi
2024-03-27 12:40:36 +04:00
fi
## Lancer la commande
uid="$(id -u)"
gid="$(id -g)"
args=(
run -it --rm
2024-03-27 20:44:37 +04:00
--name "rundk-$(basename -- "$1")-$$"
2024-03-27 12:40:36 +04:00
-e _RUNDK_IN_DOCKER=1
-e _RUNDK_UID="$uid"
-e _RUNDK_GID="$gid"
)
2024-03-27 19:40:54 +04:00
for arg in "${!PROXY_VARS[@]}"; do
args+=(--e "$arg=${PROXY_VARS[$arg]}")
done
2024-03-27 12:40:36 +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
if [ $uid -ne 0 ]; then
# si c'est un utilisateur lambda, il faut monter les informations
# nécessaires. composer est déjà monté via $HOME
user="$(id -un)"
userent="$(getent passwd "$user")"
group="$(id -gn)"
groupent="$(getent group "$group")"
args+=(
-e _RUNDK_USER="$user"
-e _RUNDK_USERENT="$userent"
-e _RUNDK_GROUPENT="$groupent"
)
fi
args+=(
"$IMAGE"
2024-03-27 20:44:37 +04:00
exec "$0" ${Chdir:+-w "$Chdir"}
2024-03-27 12:40:36 +04:00
)
[ -n "$ForcedBootstrap" ] && set -- ci
[ -n "$Verbose" ] && eecho "\$ docker ${args[*]} $*"
exec docker "${args[@]}" "$@"
else
############################################################################
# Lancement depuis l'intérieur du container
# Ajouter les informations utilisateur le cas échéant
if [ -n "$_RUNDK_USERENT" ]; then
grep -q "^$_RUNDK_USER:" /etc/passwd || echo "$_RUNDK_USERENT" >>/etc/passwd
fi
if [ -n "$_RUNDK_GROUPENT" ]; then
grep -q "^$_RUNDK_GROUP:" /etc/group || echo "$_RUNDK_GROUPENT" >>/etc/group
fi
if [ -n "$_RUNDK_USER" ]; then
user="$_RUNDK_USER"
export _RUNDK_USER=
export _RUNDK_USERENT=
export _RUNDK_GROUPENT=
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
if [ "$1" == ci ]; then
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-03-27 20:44:37 +04:00
exec "${@:-bash}"
2024-03-27 12:40:36 +04:00
fi
fi