monitor docker events

This commit is contained in:
Chai Feng 2018-10-03 15:02:50 +08:00
parent dc0c629107
commit 9f25de1d2f
No known key found for this signature in database
GPG Key ID: 2DCD9A24E523FFD2

View File

@ -6,8 +6,8 @@ set -euo pipefail
ufw_docker_agent=ufw-docker-agent ufw_docker_agent=ufw-docker-agent
ufw_docker_agent_image="${ufw_docker_agent_image:-chaifeng/${ufw_docker_agent}:181003}" ufw_docker_agent_image="${ufw_docker_agent_image:-chaifeng/${ufw_docker_agent}:181003}"
function ufw-update-service-instances() { function ufw-update-rule-for-instance() {
id="$1" name="$1"
port="$2" port="$2"
declare -a opts declare -a opts
@ -16,10 +16,16 @@ function ufw-update-service-instances() {
[[ "$port" = @(all|deny) ]] && port="" [[ "$port" = @(all|deny) ]] && port=""
run-ufw-docker "${opts[@]}" "${name}" "$port"
}
function ufw-update-service-instances() {
id="$1"
port="$2"
docker ps -qf "label=com.docker.swarm.service.id=${id}" | docker ps -qf "label=com.docker.swarm.service.id=${id}" |
while read name; do while read name; do
echo "$id $name $port" echo "$id $name $port"
run-ufw-docker "${opts[@]}" "${name}" "$port" ufw-update-rule-for-instance "${name}" "$port"
done done
} }
@ -64,12 +70,12 @@ case "$1" in
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
declare -a agent_opts=() port="${env_name:-deny}"
if [[ "$status" = kill ]]; then if [[ "$status" = kill ]]; then
agent_opts+=(delete allow "$name") port=deny
fi fi
run-ufw-docker "${agent_opts[@]}" >&2 ufw-update-rule-for-instance "$name" "$port"
done done
sleep 60; exit 1 sleep 60; exit 1
;; ;;