extract main function

This commit is contained in:
Chai Feng 2018-10-05 13:39:57 +08:00
parent b1aec55699
commit e92bdff8c3
No known key found for this signature in database
GPG Key ID: 2DCD9A24E523FFD2

View File

@ -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 "^.+\$" 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 function main() {
start) case "$1" in
update-ufw-rules start)
docker events --format '{{.Time}} {{.Status}} {{.Actor.Attributes.name}}' --filter 'scope=local' --filter 'type=container' | update-ufw-rules
while read time status name; do docker events --format '{{.Time}} {{.Status}} {{.Actor.Attributes.name}}' --filter 'scope=local' --filter 'type=container' |
echo "$time $status $name" >&2 while read time status name; do
[[ -z "$name" ]] && continue 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")" declare -n env_name="ufw_public_$(get-service-id-of "$name")"
[[ -z "${env_name:-}" ]] && continue [[ -z "${env_name:-}" ]] && continue
port="${env_name:-deny}" declare port="${env_name:-deny}"
if [[ "$status" = kill ]]; then if [[ "$status" = kill ]]; then
port=deny port=deny
fi fi
ufw-update-rule-for-instance "$name" "$port" echo ufw-update-rule-for-instance "$name" "$port"
done done
sleep 60; exit 1 sleep 60; exit 1
;; ;;
delete|allow) delete|allow)
ufw-docker "$@" ufw-docker "$@"
;; ;;
update-ufw-rules) update-ufw-rules)
update-ufw-rules update-ufw-rules
;; ;;
*) *)
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
exec "$@" exec "$@"
else else
echo "Unknown parameters: $@" >&2 echo "Unknown parameters: $@" >&2
exit 1 exit 1
fi fi
esac esac
}
main "$@"