Last active
August 29, 2015 14:05
-
-
Save gerardorochin/0a9c8c92a6e014a173ba to your computer and use it in GitHub Desktop.
Serf members HTTP API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# encoding: UTF-8 | |
# | |
# Get all members: | |
# curl "localhost:3737/api/members" | |
# | |
# Get members alive: | |
# curl "localhost:3737/api/members?status=alive" | |
# | |
# Get members status is alive and tag foo is bar: | |
# curl "localhost:3737/api/members?status=alive&foo=bar" | |
# | |
# Get member by name: | |
# curl "localhost:3737/api/members/<node-name>" | |
# | |
require 'sinatra' | |
require 'json' | |
set :port, 3737 | |
set :protection, :except => :json_csrf | |
if `which serf` == "" | |
raise "Serf exec not found." | |
end | |
def serf(args) | |
if defined?(args) | |
begin | |
return JSON.parse(`serf #{args.to_s} -format json`) | |
rescue Exception => error | |
return { 'error' => error } | |
end | |
else | |
return { 'error' => 'args not received' } | |
end | |
end | |
before do | |
content_type :json | |
headers 'Access-Control-Allow-Origin' => '*', | |
'Access-Control-Allow-Methods' => ['GET'] | |
end | |
get '/api' do | |
200 | |
end | |
get '/api/members' do | |
query = "" | |
if defined?(params) | |
# core attribs | |
["name", "status"].each do |atrib| | |
query += (params[atrib]) ? "-#{atrib}=#{params[atrib]}\s" : "" | |
end | |
# tags available on nodes | |
["foo", "another", "etc"].each do |tag| | |
query += (params[tag]) ? "-tag=#{tag}=#{params[tag]}\s" : "" | |
end | |
end | |
serf("members #{query}").to_json | |
end | |
get '/api/members/:node' do | |
serf("members -name=#{params[:node]}").to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment