Skip to content

Commit

Permalink
find self service automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jan 18, 2024
1 parent f8cac1f commit 0ada34b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down
41 changes: 39 additions & 2 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}" ]
}
Expand Down

0 comments on commit 0ada34b

Please sign in to comment.