-
Notifications
You must be signed in to change notification settings - Fork 0
/
greenroom.rb
91 lines (77 loc) · 1.89 KB
/
greenroom.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env ruby
require 'sinatra'
require 'mcollective'
require 'json'
include MCollective::RPC
if !ENV['MCO_CONFIG'].nil?
if !ENV['MCO_CONFIG'].empty?
MCO_CONFIG = ENV['MCO_CONFIG']
end
else
MCO_CONFIG = '/etc/mcollective/client.cfg'
end
post '/:agent/:action' do
client = rpcclient(params[:agent], :configfile => MCO_CONFIG, :options => {
:progress_bar => false,
:verbose => false,
:config => MCO_CONFIG
})
client.discover
content = request.body.read
data = (content.nil? or content.empty?) ? {} : JSON.parse(content, :symbolize_names => true)
parse_timeouts(client, data)
parse_filters(client, data)
parse_limits(client, data)
arguments = {}
if data[:arguments]
data[:arguments].each do |arg|
arguments[$1.to_sym] = $2 if arg =~ /^(.+?)=(.+?)$/
end
end
JSON.dump(client.send(params[:action], arguments).map{|r| r.results})
end
def parse_filters(mco, args)
if args[:filters]
args[:filters].each do |type, values|
case type
when :class
values.each do |value|
mco.class_filter "/#{value}/"
end
when :fact
values.each do |value|
mco.fact_filter "#{value}"
end
when :agent
values.each do |value|
mco.agent_filter "#{value}"
end
when :identity
values.each do |value|
mco.identity_filter "#{value}"
end
when :compound
mco.compound_filter "#{values}"
end
end
end
end
def parse_limits(mco, args)
if args[:limit]
limits = args[:limit]
if limits[:targets]
mco.limit_targets = "#{limits[:targets]}"
end
if limits[:method]
mco.limit_method = "#{limits[:method]}"
end
end
end
def parse_timeouts(mco, args)
if args[:timeout]
mco.timeout = args[:timeout]
end
if args[:discovery_timeout]
mco.discovery_timeout = args[:discovery_timeout]
end
end