From 88683854ab62b9baf60cb7a125a601f6cf6e5c45 Mon Sep 17 00:00:00 2001 From: Chai Feng Date: Sat, 6 Oct 2018 15:25:47 +0800 Subject: [PATCH] Delete service environment variables with the same service name --- ufw-docker | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ufw-docker b/ufw-docker index b8a4d44..655ed92 100755 --- a/ufw-docker +++ b/ufw-docker @@ -202,23 +202,37 @@ function ufw-docker--service-allow() { --env "${service_env}" \ "${ufw_docker_agent_image}" else + declare -a service_env_list + service_env_list+=(--env-add "${service_env}") + + exec 8< <(ufw-docker--get-env-list) + while read -u 8 -r id value; do + [[ "$id" = "$service_id" ]] && continue + [[ "$value" = "${service_name}"/* ]] && service_env_list+=(--env-rm "ufw_public_${id}") + done + exec 8<&- + docker service update --update-parallelism=0 \ --env-add ufw_docker_agent_image="${ufw_docker_agent_image}" \ --env-add DEBUG="${DEBUG:-}" \ - --env-add "${service_env}" \ + "${service_env_list[@]}" \ --image "${ufw_docker_agent_image}" \ "${ufw_docker_agent}" fi } +function ufw-docker--get-env-list() { + docker service inspect "${ufw_docker_agent}" \ + --format '{{range $k,$v := .Spec.TaskTemplate.ContainerSpec.Env}}{{ $v }}{{"\n"}}{{end}}' | + sed -e '/^ufw_public_/!d' \ + -e 's/^ufw_public_//' \ + -e 's/=/ /' +} + function ufw-docker--service-delete() { declare service_name="$1" - exec 8< <(docker service inspect "${ufw_docker_agent}" \ - --format '{{range $k,$v := .Spec.TaskTemplate.ContainerSpec.Env}}{{ $v }}{{"\n"}}{{end}}' | - sed -e '/^ufw_public_/!d' \ - -e 's/^ufw_public_//' \ - -e 's/=/ /') + exec 8< <(ufw-docker--get-env-list) while read -u 8 -r id value; do if [[ "$id" = "$service_name" ]] || [[ "$value" = "${service_name}"/* ]]; then declare service_id="$id"