From e92bdff8c313c716ab29c54bab5811df0cf0f1c1 Mon Sep 17 00:00:00 2001 From: Chai Feng Date: Fri, 5 Oct 2018 13:39:57 +0800 Subject: [PATCH] extract main function --- docker-entrypoint.sh | 68 +++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index f8bab27..643f056 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -55,39 +55,43 @@ 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) - update-ufw-rules - docker events --format '{{.Time}} {{.Status}} {{.Actor.Attributes.name}}' --filter 'scope=local' --filter 'type=container' | - while read time status name; do - echo "$time $status $name" >&2 - [[ -z "$name" ]] && continue +function main() { + case "$1" in + start) + update-ufw-rules + docker events --format '{{.Time}} {{.Status}} {{.Actor.Attributes.name}}' --filter 'scope=local' --filter 'type=container' | + while read time status name; do + echo "$time $status $name" >&2 + [[ -z "$name" ]] && continue - [[ "$status" = @(kill|start) ]] || continue + [[ "$status" = @(kill|start) ]] || continue - declare -n env_name="ufw_public_$(get-service-id-of "$name")" - [[ -z "${env_name:-}" ]] && continue + declare -n env_name="ufw_public_$(get-service-id-of "$name")" + [[ -z "${env_name:-}" ]] && continue - port="${env_name:-deny}" - if [[ "$status" = kill ]]; then - port=deny - fi + declare port="${env_name:-deny}" + if [[ "$status" = kill ]]; then + port=deny + fi - ufw-update-rule-for-instance "$name" "$port" - done - sleep 60; exit 1 - ;; - delete|allow) - ufw-docker "$@" - ;; - update-ufw-rules) - update-ufw-rules - ;; - *) - if [[ -f "$1" ]]; then - exec "$@" - else - echo "Unknown parameters: $@" >&2 - exit 1 - fi -esac + echo ufw-update-rule-for-instance "$name" "$port" + done + sleep 60; exit 1 + ;; + delete|allow) + ufw-docker "$@" + ;; + update-ufw-rules) + update-ufw-rules + ;; + *) + if [[ -f "$1" ]]; then + exec "$@" + else + echo "Unknown parameters: $@" >&2 + exit 1 + fi + esac +} + +main "$@"