From 0ada34b30b2ce5bf3a2ad6d17a5ccfd8d3679802 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Wed, 17 Jan 2024 21:41:41 -0800 Subject: [PATCH] find self service automatically --- README.md | 4 ++-- src/lib-gantry.sh | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5a48fe2..a2e334c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Gantry -[*Gantry*](https://github.com/shizunge/gantry) is a tool to update docker swarm services, enhanced [Shepherd](https://github.com/containrrr/shepherd). +[*Gantry*](https://github.com/shizunge/gantry) is a tool to update docker swarm services, [enhanced Shepherd](docs/migration.md). ## Usage @@ -54,7 +54,7 @@ You can configure the most behaviors of *Gantry* via environment variables. | GANTRY_SERVICES_EXCLUDED | | A space separated list of services names that are excluded from updating. | | GANTRY_SERVICES_EXCLUDED_FILTERS | | A space separated list of [filters](https://docs.docker.com/engine/reference/commandline/service_ls/#filter). Exclude services which match the given filters from updating. | | GANTRY_SERVICES_FILTERS | | A space separated list of [filters](https://docs.docker.com/engine/reference/commandline/service_ls/#filter) that are accepted by `docker service ls --filter` to select services to update. | -| GANTRY_SERVICES_SELF | | A service name to indicate whether a service is *Gantry* itself. *Gantry* will be the first service being updated. The manifest inspection will be always performed on the *Gantry* service to avoid an infinity loop of updating itself. | +| GANTRY_SERVICES_SELF | | This is optional. *Gantry* will try to find the service name of itself automatically, and update itself firstly. The manifest inspection will be always performed on the *Gantry* service to avoid an infinity loop of updating itself. User can use this to ask *Gantry* to update another service firstly or in case *Gantry* fails to find the service name of itself. | ### To check if new images are available diff --git a/src/lib-gantry.sh b/src/lib-gantry.sh index aab2c5a..46bad91 100755 --- a/src/lib-gantry.sh +++ b/src/lib-gantry.sh @@ -1,5 +1,5 @@ #!/bin/sh -# Copyright (C) 2023 Shizun Ge +# Copyright (C) 2023-2024 Shizun Ge # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -246,8 +246,45 @@ in_list() { return 1 } +current_container_name() { + local ALL_NETWORKS GWBRIDGE_NETWORK IPS; + ALL_NETWORKS=$(docker network ls --format {{.ID}}) || return 1; + [ -z "${ALL_NETWORKS}" ] && return 0; + GWBRIDGE_NETWORK=$(docker network ls --format {{.ID}} --filter "name=docker_gwbridge") || return 1; + IPS=$(ip route | grep src | sed -n "s/.* src \(\S*\).*$/\1/p"); + [ -z "${IPS}" ] && return 0; + local NID; + for NID in ${ALL_NETWORKS}; do + [ "${NID}" = "${GWBRIDGE_NETWORK}" ] && continue; + local ALL_LOCAL_NAME_AND_IP; + ALL_LOCAL_NAME_AND_IP=$(docker network inspect ${NID} --format "{{range .Containers}}{{.Name}}={{println .IPv4Address}}{{end}}") || return 1; + for NAME_AND_IP in ${ALL_LOCAL_NAME_AND_IP}; do + [ -z "${NAME_AND_IP}" ] && continue; + for IP in ${IPS}; do + [ ! $(echo ${NAME_AND_IP} | grep ${IP}) ] && continue; + local NAME=$(echo ${NAME_AND_IP} | sed "s/\(.*\)=${IP}.*$/\1/"); + echo ${NAME}; + return 0; + done; + done; + done; + return 0; +} + +current_service_name() { + local CNAME + CNAME=$(current_container_name) || return 1 + [ -z "${CNAME}" ] && return 0 + SNAME=$(docker container inspect ${CNAME} --format '{{range $key,$value := .Config.Labels}}{{$key}}={{println $value}}{{end}}' | grep "com.docker.swarm.service.name" | sed "s/com.docker.swarm.service.name=\(.*\)$/\1/") || return 1 + echo ${SNAME} +} + service_is_self() { - local SELF="${GANTRY_SERVICES_SELF:-""}" + if [ -z "${GANTRY_SERVICES_SELF}" ]; then + export GANTRY_SERVICES_SELF=$(current_service_name) + [ -n "${GANTRY_SERVICES_SELF}" ] && log INFO "Set GANTRY_SERVICES_SELF to ${GANTRY_SERVICES_SELF}." + fi + local SELF="${GANTRY_SERVICES_SELF}" local SERVICE_NAME="${1}" [ "${SERVICE_NAME}" = "${SELF}" ] }