Updated mcollective.init according to OSCI-658
[packages/precise/mcollective.git] / ext / mc-rpc-restserver.rb
1 #!/usr/bin/env ruby
2
3 # A very simple demonstration of writing a REST server
4 # for Simple RPC clients that takes requests over HTTP
5 # and returns results as JSON structures.
6
7 require 'rubygems'
8 require 'sinatra'
9 require 'mcollective'
10 require 'json'
11
12 include MCollective::RPC
13
14 # http://<your box>/mcollective/rpctest/echo/msg=hello%20world
15 #
16 # Creates a new Simple RPC client for the 'rpctest' agent, calls
17 # the echo action with a message 'hello world'.
18 #
19 # Returns all the answers as a JSON data block
20 get '/mcollective/:agent/:action/*' do
21   mc = rpcclient(params[:agent])
22   mc.discover
23
24   arguments = {}
25
26   # split up the wildcard params into key=val pairs and
27   # build the arguments hash
28   params[:splat].each do |arg|
29     arguments[$1.to_sym] = $2 if arg =~ /^(.+?)=(.+)$/
30   end
31
32   JSON.dump(mc.send(params[:action], arguments).map{|r| r.results})
33 end
34