A small python framework for writing Serf handlers
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
Serf is a very nice service discovery and orchestration framework which allows you to write scripts to react to different events across your infrastructure. However most of the examples are simple shell scripts with lots of logic embedded in them. Combine that with per host configuration around registering event handlers and it's easy to build a fiddly, hard to reason about enviroment. It doesn't have to be that way.
Serf is the framework, what you built on top of it matters. I wanted something with the following properties:
SerfHandlerand package up your own handlers, say
Serf Master tries to do this, presented as a very small Python framework with no dependencies. Here's an example:
Imagine a cluster with a number of database servers and web servers. The database servers have the Serf role of
dband the web servers the Serf role of
web. We want the web servers to react whenever a new server is added to the cluster (maybe to tell a load balancer to reload?) and we want to be able to trigger a deploy. For the database servers we want to be able to trigger a backup custom event.
#!/usr/bin/env python from serf_master import SerfHandler, SerfHandlerProxy
class WebHandler(SerfHandler): def deploy(self): # run commands here to do with deployment
def member_join(self): # maybe rebalance the load balancer
class DatabaseHandler(SerfHandler): def backup(self): # run commands here to do with backups
if name == 'main': handler = SerfHandlerProxy() handler.register('web', WebHandler()) handler.register('db', DatabaseHandler()) handler.run()
The important parts are:
This says if the Serf role is
webthen use the
WebHandlerclass for any events.
This says for the
member-joinserf event we should execute the code we write here.
See the unit tests for examples of now this can be tested.
Using this with Serf is simple, just wire up all the event handlers to your script like so:
serf agent -event-handler /opt/your/script.py
Although you could restrict the events which are managed by this handler the whole point of serf-master is to move the handler definition into code and away from command line flags.
Serf Master is available on PyPi and can be installed with:
pip install serf_master