Giter VIP home page Giter VIP logo

serf-master's Introduction

Serf Master

Build Status Coverage Status Code Health

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:

  • Testable. I should be able to unit test the entire configuration.
  • Single package. All hosts should get the same code, with the code deciding what runs where.
  • Single event handler. I'd rather deal with logic about user events or roles within my code, rather than parameters to serf.
  • Make handlers sharable. You can simply extend SerfHandler and package up your own handlers, say serf_master_haproxy.

Serf Master tries to do this, presented as a very small Python framework with no dependencies. Here's an example:

An example

Imagine a cluster with a number of database servers and web servers. The database servers have the Serf role of db and 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:

handler.register('web', WebHandler())

This says if the Serf role is web then use the WebHandler class for any events.

def member_join(self):

This says for the member-join serf event we should execute the code we write here.

See the unit tests for examples of now this can be tested.

Configuration

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.

Installation

Serf Master is available on PyPi and can be installed with:

pip install serf_master

serf-master's People

Contributors

fmd avatar garethr avatar tarsisazevedo avatar thedrow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.