Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 124 lines (97 sloc) 4.229 kb
b783e38 @ripienaar Update issue 54:
ripienaar authored
1 #!/usr/bin/env ruby
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
2
3 # Generic client for MCollective Simple RPC
4 #
72a8cde @ripienaar Issue 116 - Update urls in code for new home url
ripienaar authored
5 # http://marionette-collective.org/simplerpc/
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
6
7 require 'mcollective'
8
9 include MCollective::RPC
10
db11af4 @ripienaar Issue 138 - DDL should support :type => :boolean
ripienaar authored
11 # As we're taking arguments on the command line we need a
12 # way to input booleans, true on the cli is a string so this
13 # method will take the ddl, find all arguments that are supposed
14 # to be boolean and if they are the strings "true"/"yes" or "false"/"no"
15 # turn them into the matching boolean
16 def booleanish_to_boolean(arguments, ddl)
17 arguments.keys.each do |key|
18 if ddl[:input].keys.include?(key)
19 if ddl[:input][key][:type] == :boolean
20 arguments[key] = true if arguments[key] == "true"
21 arguments[key] = true if arguments[key] == "yes"
22 arguments[key] = false if arguments[key] == "false"
23 arguments[key] = false if arguments[key] == "no"
24 end
25 end
26 end
27 end
28
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
29 begin
30 options = rpcoptions do |parser, options|
7256495 @ripienaar Fix mc-rpc banner text and do cosmetic changes to SimpleRPC common optio...
ripienaar authored
31 parser.banner = ""
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
32 parser.define_head "Generic Simple RPC client"
7a4a854 @ripienaar Update issue 16
ripienaar authored
33 parser.separator ""
34 parser.separator "Usage: mc-rpc [options] [filters] --agent <agent> --action <action> [--argument <key=val> --argument ...]"
35 parser.separator "Alternate Usage: mc-rpc [options] [filters] <agent> <action> [<key=val> <key=val> ...]"
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
36 parser.separator "Alternate Usage: mc-rpc --agent-help agent"
7a4a854 @ripienaar Update issue 16
ripienaar authored
37 parser.separator ""
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
38
39 options[:arguments] = {}
40
2db4864 @ripienaar - add some help text to mc-rpc
ripienaar authored
41 parser.on('--no-results', '--nr', "Do not process results, just send request") do |v|
58be24d @ripienaar Update issue 71:
ripienaar authored
42 options[:process_results] = false
43 end
44
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
45 parser.on('-a', '--agent AGENT', 'Agent to call') do |v|
46 options[:agent] = v
47 end
48
49 parser.on('--action ACTION', 'Action to call') do |v|
50 options[:action] = v
51 end
52
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
53 parser.on("--ah", "--agent-help AGENT", "Get help for an agent") do |v|
54 options[:agent_help] = v
55 end
56
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
57 parser.on('--arg', '--argument ARGUMENT', 'Arguments to pass to agent') do |v|
58 if v =~ /^(.+?)=(.+)$/
59 options[:arguments][$1.to_sym] = $2
60 else
61 STDERR.puts("Could not parse --arg #{v}")
62 end
63 end
64 end
65
7a4a854 @ripienaar Update issue 16
ripienaar authored
66 # Parse the alternative command line
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
67 unless (options.include?(:agent) && options.include?(:action)) || options.include?(:agent_help)
0ad36e7 @ripienaar Update issue 38
ripienaar authored
68 if ARGV.length >= 2
7a4a854 @ripienaar Update issue 16
ripienaar authored
69 options[:agent] = ARGV[0]
70 ARGV.delete_at(0)
71
72 options[:action] = ARGV[0]
73 ARGV.delete_at(0)
74
75 ARGV.each do |v|
76 if v =~ /^(.+?)=(.+)$/
77 options[:arguments][$1.to_sym] = $2
78 else
79 STDERR.puts("Could not parse --arg #{v}")
80 end
81 end
82 else
83 STDERR.puts("No agent, action and arguments specified")
84 exit!
85 end
86 end
87
58be24d @ripienaar Update issue 71:
ripienaar authored
88 # handle fire and forget mode
89 options[:process_results] = true unless options.include?(:process_results)
90 options[:arguments][:process_results] = options[:process_results]
91
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
92 if options[:agent_help]
93 config = MCollective::Config.instance
94 config.loadconfig(options[:config])
95
96 ddl = MCollective::RPC::DDL.new(options[:agent_help])
e2bc9a1 @ripienaar fix logic error with short form cli
ripienaar authored
97
62cd0e5 @ripienaar Issue 130 - Per user configuration broke the location detection of rpc-h...
ripienaar authored
98 puts ddl.help(config.rpchelptemplate)
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
99 elsif options[:process_results]
100 mc = rpcclient(options[:agent], {:options => options})
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
101
db11af4 @ripienaar Issue 138 - DDL should support :type => :boolean
ripienaar authored
102 booleanish_to_boolean(options[:arguments], mc.ddl.action_interface(options[:action])) unless mc.ddl.nil?
103
1d1475c @ripienaar Issue 50 - Data Description Language DSL
ripienaar authored
104 mc.agent_filter(options[:agent])
58be24d @ripienaar Update issue 71:
ripienaar authored
105 mc.discover :verbose => true
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
106
58be24d @ripienaar Update issue 71:
ripienaar authored
107 printrpc mc.send(options[:action], options[:arguments])
108
109 printrpcstats :caption => "#{options[:agent]}##{options[:action]} call stats"
110 else
f46fb9b @ripienaar Issue 89 - Fix fire and forget requests in mc-rpc
ripienaar authored
111 mc = rpcclient(options[:agent], {:options => options})
db11af4 @ripienaar Issue 138 - DDL should support :type => :boolean
ripienaar authored
112
113 booleanish_to_boolean(options[:arguments], mc.ddl.action_interface(options[:action])) unless mc.ddl.nil?
114
f46fb9b @ripienaar Issue 89 - Fix fire and forget requests in mc-rpc
ripienaar authored
115 mc.agent_filter(options[:agent])
116
58be24d @ripienaar Update issue 71:
ripienaar authored
117 puts "Request sent with id: " + mc.send(options[:action], options[:arguments])
118 end
58d4008 @ripienaar first working copy of a generic simple rpc frontend
ripienaar authored
119 rescue Exception => e
120 STDERR.puts("Could not call agent: #{e}")
121 end
122
123 # vi:tabstop=4:expandtab:ai
Something went wrong with that request. Please try again.