4 # Toolset to create a standard interface of client and agent using
5 # an RPC metaphor, standard compliant agents will make it easier
6 # to create generic clients like web interfaces etc
8 autoload :ActionRunner, "mcollective/rpc/actionrunner"
9 autoload :Agent, "mcollective/rpc/agent"
10 autoload :Audit, "mcollective/rpc/audit"
11 autoload :Client, "mcollective/rpc/client"
12 autoload :Helpers, "mcollective/rpc/helpers"
13 autoload :Progress, "mcollective/rpc/progress"
14 autoload :Reply, "mcollective/rpc/reply"
15 autoload :Request, "mcollective/rpc/request"
16 autoload :Result, "mcollective/rpc/result"
17 autoload :Stats, "mcollective/rpc/stats"
19 # Creates a standard options hash, pass in a block to add extra headings etc
22 oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true}, "filter")
24 options = oparser.parse do |parser, options|
26 yield(parser, options)
29 Helpers.add_simplerpc_options(parser, options)
35 # Wrapper to create clients, supposed to be used as
38 # include MCollective::RPC
40 # exim = rpcclient("exim")
45 # rpcclient("exim") do |exim|
49 # It will take a few flags:
50 # :configfile => "etc/client.cfg"
52 # :exit_on_failure => true
54 # Options would be a build up options hash from the Optionparser
55 # you can use the rpcoptions helper to create this
57 # :exit_on_failure is true by default, and causes the application to
58 # exit if there is a failure constructing the RPC client. Set this flag
59 # to false to cause an Exception to be raised instead.
60 def rpcclient(agent, flags = {})
61 configfile = flags[:configfile] || "/etc/mcollective/client.cfg"
62 options = flags[:options] || nil
64 if flags.key?(:exit_on_failure)
65 exit_on_failure = flags[:exit_on_failure]
67 # We exit on failure by default for CLI-friendliness
68 exit_on_failure = true
73 rpc = Client.new(agent, :configfile => options[:config], :options => options)
74 @options = rpc.options
76 rpc = Client.new(agent, :configfile => configfile)
77 @options = rpc.options
81 puts("Could not create RPC client: #{e}")
95 # means for other classes to drop stats into this module
96 # its a bit hacky but needed so that the mixin methods like
97 # printrpcstats can easily get access to it without
98 # users having to pass it around in params.
103 # means for other classes to drop discovered hosts into this module
104 # its a bit hacky but needed so that the mixin methods like
105 # printrpcstats can easily get access to it without
106 # users having to pass it around in params.
107 def self.discovered(discovered)
108 @@discovered = discovered
111 # Prints stats, requires stats to be saved from elsewhere
112 # using the MCollective::RPC.stats method.
114 # If you've passed -v on the command line a detailed stat block
115 # will be printed, else just a one liner.
117 # You can pass flags into it:
119 # printrpcstats :caption => "Foo", :summarize => true
121 # This will use "Foo" as the caption to the stats in verbose
122 # mode and print out any aggregate summary information if present
123 def printrpcstats(flags={})
124 return unless @options[:output_format] == :console
126 flags = {:summarize => false, :caption => "rpc stats"}.merge(flags)
128 verbose = @options[:verbose] rescue verbose = false
133 puts("no stats to display")
137 puts stats.report(flags[:caption], flags[:summarize], verbose)
140 # Prints the result of an RPC call.
142 # In the default quiet mode - no flattening or verbose - only results
143 # that produce an error will be printed
145 # To get details of each result run with the -v command line option.
146 def printrpc(result, flags = {})
147 verbose = @options[:verbose] rescue verbose = false
148 verbose = flags[:verbose] || verbose
149 flatten = flags[:flatten] || false
150 format = @options[:output_format]
151 forced_mode = @options[:force_display_mode] || false
153 result_text = Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten, :format => format, :force_display_mode => forced_mode})
155 if result.is_a?(Array) && format == :console
156 puts "\n%s\n" % [ result_text ]
158 # when we get just one result to print dont pad them all with
159 # blank spaces etc, just print the individual result with no
161 puts result_text unless result_text == ""
165 # Wrapper for MCollective::Util.empty_filter? to make clients less fugly
166 # to write - ticket #18
167 def empty_filter?(options)
168 if options.include?(:filter)
169 Util.empty_filter?(options[:filter])
171 Util.empty_filter?(options)
175 def self.const_missing(const_name)
176 super unless const_name == :DDL
178 Log.warn("MCollective::RPC::DDL is deprecatd, please use MCollective::DDL instead")