use service id instead of service name

This commit is contained in:
Chai Feng 2018-10-03 11:22:50 +08:00
parent 9767d9e5e1
commit 8eb83755b9
No known key found for this signature in database
GPG Key ID: 2DCD9A24E523FFD2
2 changed files with 9 additions and 4 deletions

View File

@ -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 "^.+\$" 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 case "$1" in
start) start)
run-ufw-docker update-ufw-rules run-ufw-docker update-ufw-rules
@ -45,12 +49,12 @@ case "$1" in
[[ "$status" = @(kill|start) ]] || continue [[ "$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 [[ -z "$env_name" ]] && continue
declare -a agent_opts=() declare -a agent_opts=()
[[ "status" = start ]] && agent_opts+=(allow "$name") [[ "$status" = start ]] && agent_opts+=(allow "$name")
[[ "status" = stop ]] && agent_opts+=(delete allow "$name") [[ "$status" = kill ]] && agent_opts+=(delete allow "$name")
run-ufw-docker "${agent_opts[@]}" >&2 run-ufw-docker "${agent_opts[@]}" >&2
done done

View File

@ -144,7 +144,8 @@ function ufw-docker--service-allow() {
return 1 return 1
fi 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 if ! docker service inspect "$ufw_docker_agent" &>/dev/null; then
err "Not found ufw-docker-agent service, creating ..." err "Not found ufw-docker-agent service, creating ..."