From ba29496150d12dadf64e401f86a74ac50c8418c6 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 22 Nov 2018 15:34:07 +0400 Subject: [PATCH] =?UTF-8?q?dk:=20g=C3=A9n=C3=A9ration=20d'un=20service=20s?= =?UTF-8?q?ystemd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dk | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 161 insertions(+), 14 deletions(-) diff --git a/dk b/dk index aec3197..b7a2330 100755 --- a/dk +++ b/dk @@ -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" </etc/systemd/system/$container_name.service <