1 class MCollective::Application::Rpc<MCollective::Application
2 description "Generic RPC agent client application"
4 usage "mco rpc [options] [filters] --agent <agent> --action <action> [--argument <key=val> --argument ...]"
5 usage "mco rpc [options] [filters] <agent> <action> [<key=val> <key=val> ...]"
8 :description => "Do not process results, just send request",
9 :arguments => ["--no-results", "--nr"],
14 :description => "Agent to call",
15 :arguments => ["-a", "--agent AGENT"]
18 :description => "Action to call",
19 :arguments => ["--action ACTION"]
22 :description => "Arguments to pass to agent",
23 :arguments => ["--arg", "--argument ARGUMENT"],
26 :validate => Proc.new {|val| val.match(/^(.+?)=(.+)$/) ? true : "Could not parse --arg #{val} should be of the form key=val" }
28 def post_option_parser(configuration)
29 # handle the alternative format that optparse cant parse
30 unless (configuration.include?(:agent) && configuration.include?(:action))
32 configuration[:agent] = ARGV[0]
35 configuration[:action] = ARGV[0]
39 if v =~ /^(.+?)=(.+)$/
40 configuration[:arguments] = [] unless configuration.include?(:arguments)
41 configuration[:arguments] << v
43 STDERR.puts("Could not parse --arg #{v}")
48 STDERR.puts("No agent, action and arguments specified")
53 # convert arguments to symbols for keys to comply with simplerpc conventions
54 args = configuration[:arguments].clone
55 configuration[:arguments] = {}
58 if v =~ /^(.+?)=(.+)$/
59 configuration[:arguments][$1.to_sym] = $2
64 def string_to_ddl_type(arguments, ddl)
67 arguments.keys.each do |key|
68 if ddl[:input].keys.include?(key)
69 case ddl[:input][key][:type]
71 arguments[key] = MCollective::DDL.string_to_boolean(arguments[key])
73 when :number, :integer, :float
74 arguments[key] = MCollective::DDL.string_to_number(arguments[key])
81 mc = rpcclient(configuration[:agent])
83 mc.agent_filter(configuration[:agent])
85 string_to_ddl_type(configuration[:arguments], mc.ddl.action_interface(configuration[:action])) if mc.ddl
88 configuration[:arguments][:process_results] = true
90 puts "Request sent with id: " + mc.send(configuration[:action], configuration[:arguments]) + " replies to #{mc.reply_to}"
91 elsif !configuration[:show_results]
92 configuration[:arguments][:process_results] = false
94 puts "Request sent with id: " + mc.send(configuration[:action], configuration[:arguments])
96 # if there's stuff on STDIN assume its JSON that came from another
97 # rpc or printrpc, we feed that in as discovery data
98 discover_args = {:verbose => true}
101 discovery_data = STDIN.read.chomp
102 discover_args = {:json => discovery_data} unless discovery_data == ""
105 mc.discover discover_args
107 printrpc mc.send(configuration[:action], configuration[:arguments])
109 printrpcstats :summarize => true, :caption => "#{configuration[:agent]}##{configuration[:action]} call stats" if mc.discover.size > 0