nulib/support/build

153 lines
4.5 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-04-19 12:19:14 +04:00
PROJDIR=; COMPOSERDIR=; COMPOSERPHAR=; VENDORDIR=; BUILDENV0=; BUILDENV=; DIST=; IMAGENAME=; BUILD_IMAGES=(web db)
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"
if [ -f "$PROJDIR/Dockerfile.$1" ]; then dockerfile="$PROJDIR/Dockerfile.$1"
elif [ -f "$MYDIR/Dockerfile.$1" ]; then dockerfile="$MYDIR/Dockerfile.$1"
else dockerfile="$PROJDIR/$VENDORDIR/nulib/php/support/Dockerfile.$1"
fi
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)
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
source "$PROJDIR/$BUILDENV"
read -a HOST_MAPPINGS <<<"${HOST_MAPPINGS//
/ }"
sourced=1
fi
_build "$image"
;;
esac
done
}
action=build
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"
-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[@]}"
build_check_env || die
[ "$action" == none ] && exit 0
case "$action" in
build) build_images "$@";;
*) die "$action: action non implémentée";;
esac