From 8eb83755b9c31810e327d3861a0fc24e41c05bbc Mon Sep 17 00:00:00 2001 From: Chai Feng Date: Wed, 3 Oct 2018 11:22:50 +0800 Subject: [PATCH] use service id instead of service name --- docker-entrypoint.sh | 10 +++++++--- ufw-docker | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 39f5252..b10a939 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -36,6 +36,10 @@ function get-service-name-of() { docker inspect "$1" --format '{{range $k,$v:=.Config.Labels}}{{ if eq $k "com.docker.swarm.service.name" }}{{$v}}{{end}}{{end}}' | grep -E "^.+\$" } +function get-service-id-of() { + docker inspect "$1" --format '{{range $k,$v:=.Config.Labels}}{{ if eq $k "com.docker.swarm.service.id" }}{{$v}}{{end}}{{end}}' | grep -E "^.+\$" +} + case "$1" in start) run-ufw-docker update-ufw-rules @@ -45,12 +49,12 @@ case "$1" in [[ "$status" = @(kill|start) ]] || continue - declare -n env_name="ufw_public_$(get-service-name-of "$name")" + declare -n env_name="ufw_public_$(get-service-id-of "$name")" [[ -z "$env_name" ]] && continue declare -a agent_opts=() - [[ "status" = start ]] && agent_opts+=(allow "$name") - [[ "status" = stop ]] && agent_opts+=(delete allow "$name") + [[ "$status" = start ]] && agent_opts+=(allow "$name") + [[ "$status" = kill ]] && agent_opts+=(delete allow "$name") run-ufw-docker "${agent_opts[@]}" >&2 done diff --git a/ufw-docker b/ufw-docker index 19ad0fb..dcfcb17 100755 --- a/ufw-docker +++ b/ufw-docker @@ -144,7 +144,8 @@ function ufw-docker--service-allow() { return 1 fi - service_env="ufw_public_${service_name}=${service_port:-all}" + service_id="$(docker service inspect "${service_name}" --format "{{.ID}}")" + service_env="ufw_public_${service_id}=${service_port:-all}" if ! docker service inspect "$ufw_docker_agent" &>/dev/null; then err "Not found ufw-docker-agent service, creating ..."