2024-11-23 18:43:03 +01:00

197 lines
7.3 KiB
Bash
Executable File

#!/bin/bash
progName="./$(/bin/basename $0)"
vdgRepository="registry-docker.ville-geneve.ch"
# Fonction d'affichage d'utilisation
function print_usage {
# print
/bin/cat << EOF
Usage: $progName [options]
Options:
build Construction des images docker 'PHP-FPM' pour 'drupal'
build-no-cache Construction des images docker 'PHP-FPM' pour 'drupal' sans cache
pull Téléchargement de l'image docker depuis le dépôt '${vdgRepository}'
pull-tests Téléchargement de l'image docker de tests depuis le dépôt '${vdgRepository}'
-t,--tag Variable permettant de définir le <tag> de l'image 'PHP-FPM' de base
-n,--type Variable permettant de définir le <type> de l'image
bash Ouverture d'un shell dans le conteneur 'PHP-FPM' de base
bash-tests Ouverture d'un shell dans le conteneur 'PHP-FPM' de tests
bash-root Ouverture d'un shell 'root' dans le conteneur 'PHP-FPM' de base
bash-root-tests Ouverture d'un shell 'root' dans le conteneur 'PHP-FPM' de tests
help,-h,--help Affichage de cette aide
Exemples:
Pour construire une image 'PHP-FPM' pour 'drupal' (le type et le tag seront demandés)
$progName build
Pour construire une image 'PHP-FPM' pour 'drupal' avec le type '<type>' et le tag '<tag>' pour 'drupal'
$progName --type=<type> --tag=<tag> build
ou
$progName -n <type> -t <tag> build
Pour télécharger l'image 'PHP-FPM' pour 'drupal' avec le type '<type>' et le tag '<tag>' depuis le dépôt '${vdgRepository}'
$progName --type=<type> --tag=<tag> pull
Pour construire et analyser le contenu de l'image 'PHP-FPM' pour 'drupal' avec comme type '<type>' et comme tag '<tag>'
$progName -n <type> -t <tag> bash
EOF
}
set -e
_UID=$(id -u)
_GID=$(id -g)
_IMG_REPO="${vdgRepository}/devspe/docker-base-image/php"
_TYPE=""
_TAG=""
DOCKER_REGISTRY="$(grep -s 'DOCKER_REGISTRY=' .env | cut -f2 -d=)"
CYAN='\e[1;36m'
RED='\e[1;31m'
NC='\e[0m'
if [ $# -eq 0 ]; then
echo "${progName}: you must specify an option"
echo -e "Try '$progName help' for more information.\n"
exit 1
fi
while test $# -gt 0
do
case "$1" in
-n|--type*)
if [[ $1 =~ type= ]]; then
_TYPE="$(echo $1 | cut -f2 -d=)"
shift
else
_TYPE="$2"
shift 2
fi
;;
-t|--tag*)
if [[ $1 =~ tag= ]]; then
_TAG="$(echo $1 | cut -f2 -d=)"
shift
else
_TAG="$2"
shift 2
fi
;;
build-no-cache)
COMMAND="$1"
shift
;;
build)
COMMAND="$1"
shift
;;
pull|pull-tests|pull-test)
COMMAND="$1"
_SUFFIX="$(echo $1 | cut -f2 -d- -s | sed -e 's|^tests\?$|-tests|')"
shift
;;
bash|bash-tests|bash-test)
COMMAND="$1"
_SUFFIX="$(echo $1 | cut -f2 -d- -s | sed -e 's|^tests\?$|-tests|')"
shift
;;
bash-root|bash-root-tests|bash-root-test)
COMMAND="$1"
_SUFFIX="$(echo $1 | cut -f2 -d- -s | sed -e 's|^tests\?$|-tests|')"
shift
;;
-h|--help|help)
print_usage
exit 0
;;
*)
echo "${progName}: invalid option -- '$1'!"
echo -e "Try '$progName help' for more information.\n"
exit 1
;;
esac
done
# En l'absence du type de l'image de base le demander
if [ -z "${_TYPE}" ]; then
echo -n "Entrer le type de l'image de base : "
read _TYPE
fi
if [ -z "${_TYPE}" ]; then
echo -e "${RED}Le type est obligatoire !${NC}"
exit 2
fi
if ! [[ "${_TYPE}" =~ ^[a-z]+_[a-z]+[a-z_-]*$ ]] ; then
echo -e "${RED}Le type doit être de la forme 'vdg_php', 'drupal_php', 'codeigniter_php', 'ci_php' !${NC}"
exit 3
fi
# En l'absence du tag de l'image de base le demander
if [ -z "${_TAG}" ]; then
echo -n "Entrer le tag de l'image de base : "
read _TAG
fi
if [ -z "${_TAG}" ]; then
echo -e "${RED}Le tag est obligatoire !${NC}"
exit 2
fi
if [[ "${_TAG}" =~ -tests?$ ]]; then
_TAG=$(echo "${_TAG}" | sed -e 's|-tests\?$||')
fi
if ! [[ "${_TAG}" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?-fpm-alpine([0-9]+\.[0-9]+)?$ ]] ; then
echo -e "${RED}Le tag doit être de la forme '#.#-fpm-alpine#.#', '#.#.#-fpm-alpine#.#', '#.#-fpm-alpine' ou '#.#.#-fpm-alpine' !"
exit 3
fi
if [ ! -d "src/$_TYPE/$_TAG" ]; then
echo -e "${RED}Le répertoire pour l'image de base $_TYPE avec le tag $_TAG n'existe pas. Veuillez créer la structure.${NC}"
exit 4
fi
case "$COMMAND" in
build-no-cache)
echo -e "${CYAN}# Building base image $_TYPE with tag $_TAG:"
docker build --build-arg IMG_TAG="$_TAG" --build-arg DIR_ARG="src/${_TYPE}/$_TAG" --pull --no-cache -t $_IMG_REPO/$_TYPE:$_TAG -f ./src/${_TYPE}/$_TAG/Dockerfile .
echo -e "${CYAN}# Building tests image ${_TYPE} with tag ${_TAG}-tests:"
docker build --build-arg IMG_TAG="$_TAG" --build-arg DIR_ARG="src/${_TYPE}/$_TAG" --build-arg IMG_NAME="$_IMG_REPO/$_TYPE" --no-cache -t $_IMG_REPO/$_TYPE:${_TAG}-tests -f ./src/${_TYPE}/$_TAG/Dockerfile-tests .
;;
build)
echo -e "${CYAN}# Building base image $_TYPE with tag $_TAG:"
docker build --build-arg IMG_TAG="$_TAG" --build-arg DIR_ARG="src/${_TYPE}/$_TAG" --pull -t $_IMG_REPO/$_TYPE:$_TAG -f ./src/${_TYPE}/$_TAG/Dockerfile .
echo -e "${CYAN}# Building tests image $_TYPE with tag ${_TAG}-tests:"
docker build --build-arg IMG_TAG="$_TAG" --build-arg DIR_ARG="src/${_TYPE}/$_TAG" --build-arg IMG_NAME="$_IMG_REPO/$_TYPE" -t $_IMG_REPO/$_TYPE:${_TAG}-tests -f ./src/${_TYPE}/$_TAG/Dockerfile-tests .
;;
pull|pull-tests|pull-test)
DOCKER_REGISTRY_USER="$(grep -s 'DOCKER_REGISTRY_USER=' .env | cut -f2 -d=)"
grep -s 'DOCKER_REGISTRY_PASSWORD=' .env | cut -f2 -d= | docker login -u ${DOCKER_REGISTRY_USER} --password-stdin ${DOCKER_REGISTRY}
if [ $? -eq 0 ]; then
docker pull $_IMG_REPO/$_TYPE:$_TAG$_SUFFIX
fi
docker logout ${DOCKER_REGISTRY}
;;
bash|bash-tests|bash-test)
if [ -z "$(docker image ls -q ${_IMG_REPO}/${_TYPE}:${_TAG}${_SUFFIX} 2>/dev/null)" ]; then
echo -e "${RED}L'image 'PHP-FPM' ${_TYPE} avec le tag '${_TAG}${_SUFFIX}' n'existe pas localement !${NC}"
echo "Vous pouvez la construire avec la commande :"
echo -e " ${progName} -n ${_TYPE} -t ${_TAG} build"
echo "ou la télécharger depuis le dépôt VdG avec la commande :"
echo " ${progName} -n ${_TYPE} -t ${_TAG} pull${_SUFFIX} "
exit 4
fi
docker run --rm -it ${_IMG_REPO}/${_TYPE}:${_TAG}${_SUFFIX} /bin/sh
;;
bash-root|bash-root-tests|bash-root-test)
if [ -z "$(docker image ls -q ${_IMG_REPO}/${_TYPE}:${_TAG}${_SUFFIX} 2>/dev/null)" ]; then
echo -e "${RED}L'image 'PHP-FPM' ${_TYPE} avec le tag '${_TAG}${_SUFFIX}' n'existe pas localement !${NC}"
echo "Vous pouvez la construire avec la commande :"
echo -e " ${progName} -n ${_TYPE} -t ${_TAG} build"
echo "ou la télécharger depuis le dépôt VdG avec la commande :"
echo " ${progName} -n ${_TYPE} -t ${_TAG} pull${_SUFFIX} "
exit 4
fi
docker run --rm --user root -it ${_IMG_REPO}/${_TYPE}:${_TAG}${_SUFFIX} /bin/sh
;;
esac
exit 0