2018-10-03 01:14:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
[[ 0 -eq "$#" ]] && set -- start
|
|
|
|
|
2018-10-03 02:36:21 +00:00
|
|
|
ufw_docker_agent=ufw-docker-agent
|
2018-10-03 04:03:34 +00:00
|
|
|
ufw_docker_agent_image="${ufw_docker_agent_image:-chaifeng/${ufw_docker_agent}:181003}"
|
2018-10-03 02:36:21 +00:00
|
|
|
|
2018-10-03 03:09:54 +00:00
|
|
|
function ufw-update-service-instances() {
|
2018-10-03 02:36:21 +00:00
|
|
|
name="$1"
|
|
|
|
port="$2"
|
|
|
|
|
|
|
|
declare -a opts=("$name")
|
|
|
|
[[ "$port" = all ]] || opts+=("$port")
|
|
|
|
|
|
|
|
docker ps -qf "label=com.docker.swarm.service.name=${name}" |
|
|
|
|
while read name; do
|
2018-10-03 03:09:54 +00:00
|
|
|
ufw-docker allow "${opts[@]}"
|
2018-10-03 02:36:21 +00:00
|
|
|
done
|
|
|
|
}
|
2018-10-03 01:14:21 +00:00
|
|
|
|
2018-10-03 03:09:54 +00:00
|
|
|
function update-ufw-rules() {
|
|
|
|
declare -p | sed -e '/^declare -x ufw_public_/!d' \
|
|
|
|
-e 's/^declare -x ufw_public_//' \
|
|
|
|
-e 's/="/ /' \
|
|
|
|
-e 's/"$//' |
|
|
|
|
while read name port; do
|
|
|
|
echo "${name}=$port"
|
|
|
|
ufw-update-service-instances "${name}" "${port}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function run-ufw-docker() {
|
|
|
|
echo docker run --rm --cap-add NET_ADMIN --network host -v /etc/ufw:/etc/ufw "${ufw_docker_agent}" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
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 "^.+\$"
|
|
|
|
}
|
|
|
|
|
2018-10-03 03:22:50 +00:00
|
|
|
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 "^.+\$"
|
|
|
|
}
|
|
|
|
|
2018-10-03 01:14:21 +00:00
|
|
|
case "$1" in
|
|
|
|
start)
|
2018-10-03 03:09:54 +00:00
|
|
|
run-ufw-docker update-ufw-rules
|
2018-10-03 01:18:28 +00:00
|
|
|
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
|
|
|
|
|
2018-10-03 03:09:54 +00:00
|
|
|
[[ "$status" = @(kill|start) ]] || continue
|
|
|
|
|
2018-10-03 03:22:50 +00:00
|
|
|
declare -n env_name="ufw_public_$(get-service-id-of "$name")"
|
2018-10-03 03:09:54 +00:00
|
|
|
[[ -z "$env_name" ]] && continue
|
|
|
|
|
|
|
|
declare -a agent_opts=()
|
2018-10-03 03:22:50 +00:00
|
|
|
[[ "$status" = start ]] && agent_opts+=(allow "$name")
|
|
|
|
[[ "$status" = kill ]] && agent_opts+=(delete allow "$name")
|
2018-10-03 01:18:28 +00:00
|
|
|
|
2018-10-03 03:09:54 +00:00
|
|
|
run-ufw-docker "${agent_opts[@]}" >&2
|
2018-10-03 01:18:28 +00:00
|
|
|
done
|
2018-10-03 01:14:21 +00:00
|
|
|
sleep 60; exit 1
|
|
|
|
;;
|
|
|
|
delete|allow)
|
|
|
|
ufw-docker "$@"
|
|
|
|
;;
|
2018-10-03 03:09:54 +00:00
|
|
|
update-ufw-rules)
|
|
|
|
update-ufw-rules
|
|
|
|
;;
|
2018-10-03 01:14:21 +00:00
|
|
|
*)
|
|
|
|
if [[ -f "$1" ]]; then
|
|
|
|
exec "$@"
|
|
|
|
else
|
|
|
|
echo "Unknown parameters: $@" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
esac
|