nulib/support/build

179 lines
5.4 KiB
Plaintext
Raw Normal View History

2024-04-19 11:46:31 +04:00
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
MYDIR="$(cd "$(dirname -- "$0")"; pwd)"
"$MYDIR/rundk" --bs --ue --ci || exit 1
2024-05-15 16:31:45 +04:00
PROJDIR=; COMPOSERDIR=; COMPOSERPHAR=; VENDORDIR=; BUILDENV0=; BUILDENV=
BUILD_IMAGES=(php-apache mariadb10); export BUILD_FLAVOUR=; DIST=; IMAGENAME=
2024-04-19 11:46:31 +04:00
source "$MYDIR/rundk" || exit 1
source "$PROJDIR/$VENDORDIR/nulib/php/load.sh" || exit 1
2024-04-19 11:59:16 +04:00
require: template
2024-04-19 11:46:31 +04:00
BUILD_ARGS=(
DIST NDIST
REGISTRY
APT_PROXY
APT_MIRROR
SEC_MIRROR
TIMEZONE
)
function dklsnet() {
docker network ls --no-trunc --format '{{.Name}}' -f name="$1" 2>/dev/null
}
function dklsimg() {
local image="$1" version="$2"
docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$image${version:+:$version}" 2>/dev/null
}
function dklsct() {
# afficher le container dont l'image correspondante est $1
docker ps --no-trunc --format '{{.Image}} {{.Names}}' | awk -v image="$1" '$1 == image { print $2 }'
}
function dkrunning() {
# vérifier si le container d'image $1 tourne
[ -n "$(dklsct "$@")" ]
}
function dclsct() {
# afficher les containers correspondant à $1(=docker-compose.yml)
docker compose ${1:+-f "$1"} ps -q
}
function dcrunning() {
# vérifier si les containers correspondant à $1(=docker-compose.yml) tournent
# si $2 est spécifié, c'est le nombre de service qui doit tourner
if [ -n "$2" ]; then
[ "$(dclsct "${@:1:1}" | wc -l)" -eq "$2" ]
else
[ -n "$(dclsct "${@:1:1}")" ]
fi
}
function build_check_env() {
eval "$(template_locals)"
template_copy_missing "$PROJDIR/$BUILDENV0" && updated=1
template_process_userfiles
if [ -n "$updated" ]; then
enote "IMPORTANT: Veuillez faire le paramétrage en éditant le fichier $BUILDENV
${EDITOR:-nano} $BUILDENV
ENSUITE, vous pourrez relancer la commande"
return 1
fi
}
function _build() {
2024-04-19 12:03:22 +04:00
local dockerfile image="${PRIVAREG:+$PRIVAREG/}${IMAGENAME%/*}/$1"
2024-04-19 11:46:31 +04:00
if [ -n "$ForceBuild" -o -z "$(dklsimg "$image")" ]; then
estep "Construction de $image"
2024-05-15 16:23:53 +04:00
dockerfiles=(
"$MYDIR/Dockerfile.$1.local"
"$MYDIR/Dockerfile.$1$BUILD_FLAVOUR"
"$PROJDIR/$VENDORDIR/nulib/php/support/Dockerfile.$1$BUILD_FLAVOUR"
"$MYDIR/Dockerfile.$1"
"$PROJDIR/$VENDORDIR/nulib/php/support/Dockerfile.$1"
)
for dockerfile in "${dockerfiles[@]}"; do
[ -f "$dockerfile" ] && break
done
2024-04-19 11:46:31 +04:00
args=(
-f "$dockerfile"
${Pull:+--pull}
${NoCache:+--no-cache}
${PlainOutput:+--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
docker build "${args[@]}" "$PROJDIR" || die
if [ -n "$Push" ]; then
if [ -n "$PRIVAREG" ]; then
estep "Poussement de $image"
docker push "$image" || die
else
ewarn "PRIVAREG non défini: impossible de pousser l'image"
fi
fi
fi
}
function build_images() {
local image sourced
2024-04-19 12:19:14 +04:00
[ $# -gt 0 ] || set -- rundk "${BUILD_IMAGES[@]}"
2024-04-19 11:46:31 +04:00
for image in "$@"; do
case "$image" in
rundk)
2024-05-15 17:27:48 +04:00
[ ${#Configs[*]} -gt 0 ] && export RUNDK_FORCE_BUILDENVS="${Configs[*]}"
2024-04-19 11:46:31 +04:00
local -a args=(--bootstrap)
[ -z "$ForceBuild" ] && args+=(--unless-exists)
[ -n "$Pull" ] && args+=(--pull)
[ -n "$NoCache" ] && args+=(--no-cache)
"$MYDIR/rundk" "${args[@]}" || die
;;
*)
if [ -z "$sourced" ]; then
2024-05-15 17:27:48 +04:00
[ ${#Configs[*]} -gt 0 ] || Configs=("$PROJDIR/$BUILDENV")
for config in "${Configs[@]}"; do
source "$config"
done
2024-05-12 22:09:50 +04:00
after_source_buildenv
2024-04-19 11:46:31 +04:00
read -a HOST_MAPPINGS <<<"${HOST_MAPPINGS//
/ }"
sourced=1
fi
_build "$image"
;;
esac
done
}
action=build
2024-05-15 17:27:48 +04:00
Configs=()
2024-04-19 11:46:31 +04:00
ForceBuild=
Pull=
NoCache=
PlainOutput=
Push=
args=(
"Construire les images pour DRE"
#"usage"
--check-only action=none "++Ne faire que la vérification de l'environnement"
2024-05-15 17:27:48 +04:00
-c:,--config:BUILDENV Configs "Spécifier un fichier d'environnement pour le build"
2024-04-19 11:46:31 +04:00
-r,--rebuild ForceBuild=1 "Forcer la (re)construction de l'image"
-U,--pull Pull=1 "++Forcer le re-téléchargement des images dépendantes"
-j,--no-cache NoCache=1 "++Construire l'image en invalidant le cache"
-D,--plain-output PlainOutput=1 "++Afficher le détail du build"
-p,--push Push=1 "Pousser les images vers le registry après construction"
)
parse_args "$@"; set -- "${args[@]}"
2024-05-15 17:27:48 +04:00
if [ ${#Configs[*]} -gt 0 ]; then
aconfigs=()
for config in "${Configs[@]}"; do
setx config=abspath "$config"
aconfigs+=("$config")
done
Configs=("${aconfigs[@]}")
# pas de vérification d'environnement si on spécifie Configs
# ne pas oublier d'implémenter un traitement spécifique si build_check_env()
# contient d'autres vérifications
else
build_check_env || die
fi
2024-04-19 11:46:31 +04:00
[ "$action" == none ] && exit 0
case "$action" in
build) build_images "$@";;
*) die "$action: action non implémentée";;
esac