dk: génération d'un service systemd

This commit is contained in:
Jephté Clain 2018-11-22 15:34:07 +04:00
parent f72dd86584
commit ba29496150
1 changed files with 161 additions and 14 deletions

175
dk
View File

@ -12,6 +12,7 @@ COMMANDES
build
push
start, run
service
stop
up
logs
@ -27,8 +28,9 @@ OPTIONS
-p, --profile PROFILE
-P, --prod
-T, --test
-n, --fake
-j, --no-cache
-n, --fake"
-h, --host HOST"
}
function get_version() {
@ -79,8 +81,10 @@ function docker_set_run_args() {
}
function docker_check_name() {
[ -n "$NAME" ] || die "Vous devez définir NAME dans .build.env"
if [ "$1" == set_name ]; then
name="${NAME//[^a-zA-Z0-9_.-]/_}"
if [ "$1" == set_container_name ]; then
project_name="$NAME"
container_name="${project_name//[^a-zA-Z0-9_-]/}"
[ -n "$PROFILE" ] && container_name="${container_name}_$PROFILE"
fi
}
@ -103,6 +107,62 @@ function compose_set_env_args() {
export COMPOSE_PROJECT_NAME
fi
fi
if [ "$1" == set_container_name ]; then
if [ "$PROJECT_NAME" == --none-- ]; then
project_name="$(basename -- "$(pwd)")"
else
project_name="$PROJECT_NAME"
fi
container_name="${project_name//[^a-zA-Z0-9_-]/}"
[ -n "$PROFILE" ] && container_name="${container_name}_$PROFILE"
fi
}
function host_run() {
# lancer le script $2..@ sur l'hôte $1
# si $1 n'est pas défini ou est le nom l'hôte local, lancer le script en
# local avec les droits root
# sinon, si docker-machine existe, l'hôte doit correspondre à la machine active
# sinon, lancer inconditionnellement le script sur l'hôte distant
local host="$1" script="$2"; shift; shift
if [ -n "$host" ]; then
if check_hostname "$host"; then
estep "Lancement de $script localement"
runscript_as_root "$script" "$@"
elif progexists docker-machine; then
local dm; setx dm=docker-machine active 2>/dev/null
if [ "${host%%.*}" == "$dm" ]; then
estep "Copie du script vers root@$host"
scp "$script" "root@$host:/tmp/tmp-dk-service-script" || die
estep "Lancement du script à distance"
local -a args; args=(/tmp/tmp-dk-service-script "$@")
ssh -qt "root@$host" "$(qvals "${args[@]}"); rm -f /tmp/tmp-dk-service-script"
else
ewarn "La machine active ($dm) n'est pas la destination ($host)"
fi
else
estep "Copie du script vers root@$host"
scp "$script" "root@$host:/tmp/tmp-dk-service-script" || die
estep "Lancement du script à distance"
local -a args; args=(/tmp/tmp-dk-service-script "$@")
ssh -qt "root@$host" "$(qvals "${args[@]}"); rm -f /tmp/tmp-dk-service-script"
fi
else
estep "Lancement de $script localement"
runscript_as_root "$script" "$@"
fi
}
function local_run() {
# lancer le script $2..@ sur l'hôte $1 uniquement si c'est l'hôte courant
local host="$1" script="$2"; shift; shift
if [ -n "$host" ]; then
if ! check_hostname "$host"; then
eerror "Cette commande doit obligatoirement être lancée depuis l'hôte $host"
return 1
fi
fi
estep "Lancement de $script localement"
runscript_as_root "$script" "$@"
}
function initialize_build_env() {
@ -192,18 +252,92 @@ function auto_up() {
local -a replace_env_args env_args
local -a replace_run_args run_args
local -a replace_user_args user_args
local project_name container_name
if [ -f docker-compose.yml ]; then
compose_set_env_args
replace_run_args=(-d)
compose_up "$@"
else
docker_set_env_args
docker_check_name set_name
replace_run_args=(-d --name "$name")
docker_check_name set_container_name
replace_run_args=(-d --name "$container_name")
docker_up "$@"
fi
}
function default_compose_service() {
local docker_compose="$(which docker-compose)"
setx startcmd=qvals "$docker_compose" \
"${replace_env_args[@]}" "${env_args[@]}" \
up "${replace_run_args[@]}" "${run_args[@]}" \
"${replace_user_args[@]}" "${user_args[@]}" "$@"
setx stopcmd=qvals "$docker_compose" down
}
function default_docker_service() {
local docker="$(which docker)"
setx startcmd=qvals "$docker" run \
"${replace_env_args[@]}" "${env_args[@]}" \
"${replace_run_args[@]}" "${run_args[@]}" \
"$NAME" \
"${replace_user_args[@]}" "${user_args[@]}" "$@"
setx stopcmd=qvals "$docker" stop "$container_name"
}
function compose_service() { default_compose_service "$@"; }
function docker_service() { default_docker_service "$@"; }
function auto_service() {
local -a replace_env_args env_args
local -a replace_run_args run_args
local -a replace_user_args user_args
local project_name container_name startcmd stopcmd
local tmpscript; ac_set_tmpfile tmpscript
estep "Génération du service"
export COMPOSE_PROJECT_NAME=
if [ -f docker-compose.yml ]; then
compose_set_env_args set_container_name
replace_run_args=(-d --no-color)
compose_service "$@"
if [ -z "$HOST" -a -f .env ]; then
source ./.env
if [ -n "$PROFILE" ]; then
HOST="${PROFILE^^}_HOST"; HOST="${!HOST}"
fi
fi
else
docker_set_env_args
docker_check_name set_container_name
replace_run_args=(-d --name "$container_name")
docker_service "$@"
fi
[ -n "$COMPOSE_PROJECT_NAME" ] || COMPOSE_PROJECT_NAME="$project_name"
chmod 755 "$tmpscript"
cat >"$tmpscript" <<EOF
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
cat >/etc/systemd/system/$container_name.service <<EOD
[Unit]
Description=$project_name stack ($PROFILE)
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=$(pwd)
Environment=$(qval "COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME")
ExecStart=$startcmd
ExecStop=$stopcmd
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
EOD
systemctl daemon-reload
systemctl enable $container_name.service
EOF
estep "Installation du service"
local_run "$HOST" "$tmpscript"
}
function default_compose_stop() {
${FAKE:+qvals} docker-compose \
"${replace_env_args[@]}" "${env_args[@]}" \
@ -213,19 +347,20 @@ function default_compose_stop() {
function default_docker_stop() {
${FAKE:+qvals} docker container stop \
"${replace_stop_args[@]}" "${stop_args[@]}" \
"$name" "$@"
"$container_name" "$@"
}
function compose_stop() { default_compose_stop "$@"; }
function docker_stop() { default_docker_stop "$@"; }
function auto_stop() {
local -a replace_env_args env_args
local -a replace_stop_args stop_args
local project_name container_name
if [ -f docker-compose.yml ]; then
compose_set_env_args
compose_stop "$@"
else
docker_set_env_args
docker_check_name set_name
docker_check_name set_container_name
docker_stop "$@"
fi
}
@ -239,20 +374,21 @@ function default_compose_logs() {
function default_docker_logs() {
${FAKE:+qvals} docker logs \
"${replace_logs_args[@]}" "${logs_args[@]}" \
"$name" "$@"
"$container_name" "$@"
}
function compose_logs() { default_compose_logs "$@"; }
function docker_logs() { default_docker_logs "$@"; }
function auto_logs() {
local -a replace_env_args env_args
local -a replace_logs_args logs_args
local project_name container_name
if [ -f docker-compose.yml ]; then
compose_set_env_args
replace_logs_args=(-f)
compose_logs "$@"
else
docker_set_env_args
docker_check_name set_name
docker_check_name set_container_name
replace_logs_args=(-f)
docker_logs "$@"
fi
@ -268,11 +404,11 @@ function default_docker_down() {
estep "stop"
${FAKE:+qvals} docker container stop \
"${replace_down_args[@]}" "${down_args[@]}" \
"$name" "$@"
"$container_name" "$@"
estep "rm"
${FAKE:+qvals} docker container rm \
"${replace_rm_args[@]}" "${rm_args[@]}" \
"$name"
"$container_name"
}
function compose_down() { default_compose_down "$@"; }
function docker_down() { default_docker_down "$@"; }
@ -280,12 +416,13 @@ function auto_down() {
local -a replace_env_args env_args
local -a replace_down_args down_args
local -a replace_rm_args rm_args
local project_name container_name
if [ -f docker-compose.yml ]; then
compose_set_env_args
compose_down "$@"
else
docker_set_env_args
docker_check_name set_name
docker_check_name set_container_name
docker_down "$@"
fi
}
@ -308,16 +445,18 @@ if progexists docker-machine; then
fi
chdir=
NO_CACHE=
FAKE=
NO_CACHE=
HOST=
args=(
--help '$exit_with display_help'
-d:,--chdir: chdir=
-p:,--profile: PROFILE=
-P,--prod PROFILE=prod
-T,--test PROFILE=test
-j,--no-cache NO_CACHE=1
-n,--fake FAKE=1
-j,--no-cache NO_CACHE=1
-h:,--host: HOST=
)
parse_args "$@"; set -- "${args[@]}"
@ -359,6 +498,14 @@ while [ $# -gt 0 ]; do
enote "Profil $PROFILE"
auto_up "${args[@]}" || die
;;
service)
args=()
while [ $# -gt 0 -a "$1" != -- ]; do
args+=("$1"); shift
done
enote "Profil $PROFILE"
auto_service "${args[@]}" || die
;;
k|stop)
enote "Profil $PROFILE"
auto_stop || die