Class MCollective::RPC::Helpers
In: lib/mcollective/rpc/helpers.rb
Parent: Object

Various utilities for the RPC system

Methods

Public Class methods

Add SimpleRPC common options

[Source]

     # File lib/mcollective/rpc/helpers.rb, line 239
239:             def self.add_simplerpc_options(parser, options)
240:                 parser.separator ""
241: 
242:                 # add SimpleRPC specific options to all clients that use our library
243:                 parser.on('--np', '--no-progress', 'Do not show the progress bar') do |v|
244:                     options[:progress_bar] = false
245:                 end
246: 
247:                 parser.on('--one', '-1', 'Send request to only one discovered nodes') do |v|
248:                     options[:mcollective_limit_targets] = "1"
249:                 end
250: 
251:                 parser.on('--limit-nodes [COUNT]', '--ln [COUNT]', 'Send request to only a subset of nodes, can be a percentage') do |v|
252:                     options[:mcollective_limit_targets] = v
253:                 end
254:             end

Return color codes, if the config color= option is false just return a empty string

[Source]

    # File lib/mcollective/rpc/helpers.rb, line 38
38:             def self.color(code)
39:                 colorize = Config.instance.color
40: 
41:                 colors = {:red => "",
42:                           :green => "",
43:                           :yellow => "",
44:                           :cyan => "",
45:                           :bold => "",
46:                           :reset => ""}
47: 
48:                 if colorize
49:                     return colors[code] || ""
50:                 else
51:                     return ""
52:                 end
53:             end

Helper to return a string in specific color

[Source]

    # File lib/mcollective/rpc/helpers.rb, line 56
56:             def self.colorize(code, msg)
57:                 "#{self.color(code)}#{msg}#{self.color(:reset)}"
58:             end

Checks in PATH returns true if the command is found

[Source]

    # File lib/mcollective/rpc/helpers.rb, line 6
 6:             def self.command_in_path?(command)
 7:                 found = ENV["PATH"].split(File::PATH_SEPARATOR).map do |p|
 8:                     File.exist?(File.join(p, command))
 9:                 end
10: 
11:                 found.include?(true)
12:             end

Backward compatible display block for results without a DDL

[Source]

     # File lib/mcollective/rpc/helpers.rb, line 187
187:             def self.old_rpcresults(result, flags = {})
188:                 result_text = ""
189: 
190:                 if flags[:flatten]
191:                     result.each do |r|
192:                         if r[:statuscode] <= 1
193:                             data = r[:data]
194: 
195:                             unless data.is_a?(String)
196:                                 result_text << data.pretty_inspect
197:                             else
198:                                 result_text << data
199:                             end
200:                         else
201:                             result_text << r.pretty_inspect
202:                         end
203:                     end
204: 
205:                     result_text << ""
206:                 else
207:                     [result].flatten.each do |r|
208: 
209:                         if flags[:verbose]
210:                             result_text << "%-40s: %s\n" % [r[:sender], r[:statusmsg]]
211: 
212:                             if r[:statuscode] <= 1
213:                                 r[:data].pretty_inspect.split("\n").each {|m| result_text += "    #{m}"}
214:                                 result_text << "\n\n"
215:                             elsif r[:statuscode] == 2
216:                                 # dont print anything, no useful data to display
217:                                 # past what was already shown
218:                             elsif r[:statuscode] == 3
219:                                 # dont print anything, no useful data to display
220:                                 # past what was already shown
221:                             elsif r[:statuscode] == 4
222:                                 # dont print anything, no useful data to display
223:                                 # past what was already shown
224:                             else
225:                                 result_text << "    #{r[:statusmsg]}"
226:                             end
227:                         else
228:                             unless r[:statuscode] == 0
229:                                 result_text << "%-40s %s\n" % [r[:sender], colorize(:red, r[:statusmsg])]
230:                             end
231:                         end
232:                     end
233:                 end
234: 
235:                 result_text << ""
236:             end

Returns a blob of text representing the results in a standard way

It tries hard to do sane things so you often should not need to write your own display functions

If the agent you are getting results for has a DDL it will use the hints in there to do the right thing specifically it will look at the values of display in the DDL to choose when to show results

If you do not have a DDL you can pass these flags:

   printrpc exim.mailq, :flatten => true
   printrpc exim.mailq, :verbose => true

If you‘ve asked it to flatten the result it will not print sender hostnames, it will just print the result as if it‘s one huge result, handy for things like showing a combined mailq.

[Source]

     # File lib/mcollective/rpc/helpers.rb, line 78
 78:             def self.rpcresults(result, flags = {})
 79:                 flags = {:verbose => false, :flatten => false}.merge(flags)
 80: 
 81:                 result_text = ""
 82:                 ddl = nil
 83: 
 84:                 # if running in verbose mode, just use the old style print
 85:                 # no need for all the DDL helpers obfuscating the result
 86:                 if flags[:verbose]
 87:                     result_text = old_rpcresults(result, flags)
 88:                 else
 89:                     [result].flatten.each do |r|
 90:                         begin
 91:                             ddl ||= DDL.new(r.agent).action_interface(r.action.to_s)
 92: 
 93:                             sender = r[:sender]
 94:                             status = r[:statuscode]
 95:                             message = r[:statusmsg]
 96:                             display = ddl[:display]
 97:                             result = r[:data]
 98: 
 99:                             # appand the results only according to what the DDL says
100:                             case display
101:                                 when :ok
102:                                     if status == 0
103:                                         result_text << text_for_result(sender, status, message, result, ddl)
104:                                     end
105: 
106:                                 when :failed
107:                                     if status > 0
108:                                         result_text << text_for_result(sender, status, message, result, ddl)
109:                                     end
110: 
111:                                 when :always
112:                                     result_text << text_for_result(sender, status, message, result, ddl)
113: 
114:                                 when :flatten
115:                                     result_text << text_for_flattened_result(status, result)
116: 
117:                             end
118:                         rescue Exception => e
119:                             # no DDL so just do the old style print unchanged for
120:                             # backward compat
121:                             result_text = old_rpcresults(result, flags)
122:                         end
123:                     end
124:                 end
125: 
126:                 result_text
127:             end

Figures out the columns and liens of the current tty

Returns [0, 0] if it can‘t figure it out or if you‘re not running on a tty

[Source]

    # File lib/mcollective/rpc/helpers.rb, line 18
18:             def self.terminal_dimensions
19:                 return [0, 0] unless STDIN.tty?
20: 
21:                 if ENV["COLUMNS"] && ENV["LINES"]
22:                     return [ENV["COLUMNS"].to_i, ENV["LINES"].to_i]
23: 
24:                 elsif ENV["TERM"] && command_in_path?("tput")
25:                     return [`tput cols`.to_i, `tput lines`.to_i]
26: 
27:                 elsif command_in_path?('stty')
28:                     return `stty size`.scan(/\d+/).map {|s| s.to_i }
29:                 else
30:                     return [0, 0]
31:                 end
32:             rescue
33:                 [0, 0]
34:             end

Returns text representing a flattened result of only good data

[Source]

     # File lib/mcollective/rpc/helpers.rb, line 174
174:             def self.text_for_flattened_result(status, result)
175:                 result_text = ""
176: 
177:                 if status <= 1
178:                     unless result.is_a?(String)
179:                         result_text << result.pretty_inspect
180:                     else
181:                         result_text << result
182:                     end
183:                 end
184:             end

Return text representing a result

[Source]

     # File lib/mcollective/rpc/helpers.rb, line 130
130:             def self.text_for_result(sender, status, msg, result, ddl)
131:                 statusses = ["",
132:                              colorize(:red, "Request Aborted"),
133:                              colorize(:yellow, "Unknown Action"),
134:                              colorize(:yellow, "Missing Request Data"),
135:                              colorize(:yellow, "Invalid Request Data"),
136:                              colorize(:red, "Unknown Request Status")]
137: 
138:                 result_text = "%-40s %s\n" % [sender, statusses[status]]
139:                 result_text << "   %s\n" % [colorize(:yellow, msg)] unless msg == "OK"
140: 
141:                 # only print good data, ignore data that results from failure
142:                 if [0, 1].include?(status)
143:                     if result.is_a?(Hash)
144:                         # figure out the lengths of the display as strings, we'll use
145:                         # it later to correctly justify the output
146:                         lengths = result.keys.map{|k| ddl[:output][k][:display_as].size}
147: 
148:                         result.keys.each do |k|
149:                             # get all the output fields nicely lined up with a
150:                             # 3 space front padding
151:                             display_as = ddl[:output][k][:display_as]
152:                             display_length = display_as.size
153:                             padding = lengths.max - display_length + 3
154:                             result_text << " " * padding
155: 
156:                             result_text << "#{display_as}:"
157: 
158:                             if result[k].is_a?(String) || result[k].is_a?(Numeric)
159:                                 result_text << " #{result[k]}\n"
160:                             else
161:                                 result_text << "\n\t" + result[k].pretty_inspect.split("\n").join("\n\t") + "\n"
162:                             end
163:                         end
164:                     else
165:                         result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t")
166:                     end
167:                 end
168: 
169:                 result_text << "\n"
170:                 result_text
171:             end

[Validate]