Skip to content

Instantly share code, notes, and snippets.

@ryanuber
Last active August 29, 2015 13:57
Show Gist options
  • Save ryanuber/9476817 to your computer and use it in GitHub Desktop.
Save ryanuber/9476817 to your computer and use it in GitHub Desktop.
Example Serf router-style handler

Serf handler router

When using @hashicorp's excellent Serf orchestration tool, you typically configure a handler for each type of event you expect to encounter (more about events and handlers here). To change handler configuration, one must update the Serf configuration and reload the agent with a SIGHUP or a restart.

Thanks to the flexibility and open-endedness Serf offers for configuring and executing handlers, we can achieve similar functionality by configuring only a single "router" handler, which we will never need to update. This removes the orchestration work of reloading the agents by allowing you to simply drop new executables with predictable names into a directory.

You can name the handler executables by event type (e.g. member-join). User events get prefixed with user- (user-deploy, user-app-reload, etc.).

What you will end up with is a directory structure that looks like this:

$ tree /etc/serf
/etc/serf
└── handlers
    ├── member-failed
    ├── member-join
    ├── member-leave
    ├── member-update
    ├── user-deploy
    └── user-app-reload
#!/bin/bash
SERFDIR="/etc/serf"
[ "$SERF_EVENT" == "user" ] && EVENT="user-${SERF_USER_EVENT}" || EVENT="$SERF_EVENT"
HANDLER="${SERFDIR}/handlers/${EVENT}"
[ -x "$HANDLER" ] || HANDLER="${SERFDIR}/handlers/default"
exec "$HANDLER"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment