Skip to content

Instantly share code, notes, and snippets.

@lalyos
Last active September 27, 2020 17:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalyos/23a80c25aeefa068720a to your computer and use it in GitHub Desktop.
Save lalyos/23a80c25aeefa068720a to your computer and use it in GitHub Desktop.
consul event handler to call plugn

Consul can send events to each node in the membership. This gist shows how to bind event handlers with plugins.

sending events

consul event -http-addr=<HOST>:8500 -name=start "mycluster"

installing plugins

The nodes can have multiple plugins

event handler

On the consul node start a watch:

consul watch -type=event ./handler.sh

it will call

#!/bin/bash
get_field() {
declare json="$1"
declare field="$2"
echo "$json"|jq ".$field" -r
}
process_json() {
while read json; do
[[ "$DEBUG" ]] && echo $json
event=$(get_field $json Name)
id=$(get_field $json ID)
payload=$(get_field $json Payload)
ltime=$(get_field $json LTime)
version=$(get_field $json Version)
echo $payload | base64 -d | \
EVENT_ID=$id \
EVENT_LTIME=$ltime \
EVENT_VERSION=$version \
plugn trigger $event
done
}
main() {
while read array ;do
echo $array | jq .[] -c | process_json
done
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment