Class | MCollective::RPC::Helpers |
In: |
lib/mcollective/rpc/helpers.rb
|
Parent: | Object |
Various utilities for the RPC system
Add SimpleRPC common options
# File lib/mcollective/rpc/helpers.rb, line 250 250: def self.add_simplerpc_options(parser, options) 251: parser.separator "" 252: 253: # add SimpleRPC specific options to all clients that use our library 254: parser.on('--np', '--no-progress', 'Do not show the progress bar') do |v| 255: options[:progress_bar] = false 256: end 257: 258: parser.on('--one', '-1', 'Send request to only one discovered nodes') do |v| 259: options[:mcollective_limit_targets] = 1 260: end 261: 262: parser.on('--batch SIZE', Integer, 'Do requests in batches') do |v| 263: options[:batch_size] = v 264: end 265: 266: parser.on('--batch-sleep SECONDS', Float, 'Sleep time between batches') do |v| 267: options[:batch_sleep_time] = v 268: end 269: 270: parser.on('--limit-seed NUMBER', Integer, 'Seed value for deterministic random batching') do |v| 271: options[:limit_seed] = v 272: end 273: 274: parser.on('--limit-nodes COUNT', '--ln', '--limit', 'Send request to only a subset of nodes, can be a percentage') do |v| 275: raise "Invalid limit specified: #{v} valid limits are /^\d+%*$/" unless v =~ /^\d+%*$/ 276: 277: if v =~ /^\d+$/ 278: options[:mcollective_limit_targets] = v.to_i 279: else 280: options[:mcollective_limit_targets] = v 281: end 282: end 283: 284: parser.on('--json', '-j', 'Produce JSON output') do |v| 285: options[:progress_bar] = false 286: options[:output_format] = :json 287: end 288: 289: parser.on('--display MODE', 'Influence how results are displayed. One of ok, all or failed') do |v| 290: if v == "all" 291: options[:force_display_mode] = :always 292: else 293: options[:force_display_mode] = v.intern 294: end 295: 296: raise "--display has to be one of 'ok', 'all' or 'failed'" unless [:ok, :failed, :always].include?(options[:force_display_mode]) 297: end 298: end
Given an array of something, make sure each is a string chomp off any new lines and return just the array of hosts
# File lib/mcollective/rpc/helpers.rb, line 29 29: def self.extract_hosts_from_array(hosts) 30: [hosts].flatten.map do |host| 31: raise "#{host} should be a string" unless host.is_a?(String) 32: host.chomp 33: end 34: end
Parse JSON output as produced by printrpc and extract the "sender" of each rpc response
The simplist valid JSON based data would be:
[
{"sender" => "example.com"}, {"sender" => "another.com"}
]
# File lib/mcollective/rpc/helpers.rb, line 14 14: def self.extract_hosts_from_json(json) 15: hosts = JSON.parse(json) 16: 17: raise "JSON hosts list is not an array" unless hosts.is_a?(Array) 18: 19: hosts.map do |host| 20: raise "JSON host list is not an array of Hashes" unless host.is_a?(Hash) 21: raise "JSON host list does not have senders in it" unless host.include?("sender") 22: 23: host["sender"] 24: end.uniq 25: end
Backward compatible display block for results without a DDL
# File lib/mcollective/rpc/helpers.rb, line 198 198: def self.old_rpcresults(result, flags = {}) 199: result_text = "" 200: 201: if flags[:flatten] 202: result.each do |r| 203: if r[:statuscode] <= 1 204: data = r[:data] 205: 206: unless data.is_a?(String) 207: result_text << data.pretty_inspect 208: else 209: result_text << data 210: end 211: else 212: result_text << r.pretty_inspect 213: end 214: end 215: 216: result_text << "" 217: else 218: [result].flatten.each do |r| 219: 220: if flags[:verbose] 221: result_text << "%-40s: %s\n" % [r[:sender], r[:statusmsg]] 222: 223: if r[:statuscode] <= 1 224: r[:data].pretty_inspect.split("\n").each {|m| result_text += " #{m}"} 225: result_text << "\n\n" 226: elsif r[:statuscode] == 2 227: # dont print anything, no useful data to display 228: # past what was already shown 229: elsif r[:statuscode] == 3 230: # dont print anything, no useful data to display 231: # past what was already shown 232: elsif r[:statuscode] == 4 233: # dont print anything, no useful data to display 234: # past what was already shown 235: else 236: result_text << " #{r[:statusmsg]}" 237: end 238: else 239: unless r[:statuscode] == 0 240: result_text << "%-40s %s\n" % [r[:sender], Util.colorize(:red, r[:statusmsg])] 241: end 242: end 243: end 244: end 245: 246: result_text << "" 247: 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.
# File lib/mcollective/rpc/helpers.rb, line 54 54: def self.rpcresults(result, flags = {}) 55: flags = {:verbose => false, :flatten => false, :format => :console, :force_display_mode => false}.merge(flags) 56: 57: result_text = "" 58: ddl = nil 59: 60: # if running in verbose mode, just use the old style print 61: # no need for all the DDL helpers obfuscating the result 62: if flags[:format] == :json 63: if STDOUT.tty? 64: result_text = JSON.pretty_generate(result) 65: else 66: result_text = result.to_json 67: end 68: else 69: if flags[:verbose] 70: result_text = old_rpcresults(result, flags) 71: else 72: [result].flatten.each do |r| 73: begin 74: ddl ||= DDL.new(r.agent).action_interface(r.action.to_s) 75: 76: sender = r[:sender] 77: status = r[:statuscode] 78: message = r[:statusmsg] 79: result = r[:data] 80: 81: if flags[:force_display_mode] 82: display = flags[:force_display_mode] 83: else 84: display = ddl[:display] 85: end 86: 87: # appand the results only according to what the DDL says 88: case display 89: when :ok 90: if status == 0 91: result_text << text_for_result(sender, status, message, result, ddl) 92: end 93: 94: when :failed 95: if status > 0 96: result_text << text_for_result(sender, status, message, result, ddl) 97: end 98: 99: when :always 100: result_text << text_for_result(sender, status, message, result, ddl) 101: 102: when :flatten 103: result_text << text_for_flattened_result(status, result) 104: 105: end 106: rescue Exception => e 107: # no DDL so just do the old style print unchanged for 108: # backward compat 109: result_text = old_rpcresults(result, flags) 110: end 111: end 112: end 113: end 114: 115: result_text 116: end
Returns text representing a flattened result of only good data
# File lib/mcollective/rpc/helpers.rb, line 185 185: def self.text_for_flattened_result(status, result) 186: result_text = "" 187: 188: if status <= 1 189: unless result.is_a?(String) 190: result_text << result.pretty_inspect 191: else 192: result_text << result 193: end 194: end 195: end
Return text representing a result
# File lib/mcollective/rpc/helpers.rb, line 119 119: def self.text_for_result(sender, status, msg, result, ddl) 120: statusses = ["", 121: Util.colorize(:red, "Request Aborted"), 122: Util.colorize(:yellow, "Unknown Action"), 123: Util.colorize(:yellow, "Missing Request Data"), 124: Util.colorize(:yellow, "Invalid Request Data"), 125: Util.colorize(:red, "Unknown Request Status")] 126: 127: result_text = "%-40s %s\n" % [sender, statusses[status]] 128: result_text << " %s\n" % [Util.colorize(:yellow, msg)] unless msg == "OK" 129: 130: # only print good data, ignore data that results from failure 131: if [0, 1].include?(status) 132: if result.is_a?(Hash) 133: # figure out the lengths of the display as strings, we'll use 134: # it later to correctly justify the output 135: lengths = result.keys.map do |k| 136: begin 137: ddl[:output][k][:display_as].size 138: rescue 139: k.to_s.size 140: end 141: end 142: 143: result.keys.sort_by{|k| k}.each do |k| 144: # get all the output fields nicely lined up with a 145: # 3 space front padding 146: begin 147: display_as = ddl[:output][k][:display_as] 148: rescue 149: display_as = k.to_s 150: end 151: 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 [String, Numeric].include?(result[k].class) 159: lines = result[k].to_s.split("\n") 160: 161: if lines.empty? 162: result_text << "\n" 163: else 164: lines.each_with_index do |line, i| 165: i == 0 ? padtxt = " " : padtxt = " " * (padding + display_length + 2) 166: 167: result_text << "#{padtxt}#{line}\n" 168: end 169: end 170: else 171: padding = " " * (lengths.max + 5) 172: result_text << " " << result[k].pretty_inspect.split("\n").join("\n" << padding) << "\n" 173: end 174: end 175: else 176: result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t") 177: end 178: end 179: 180: result_text << "\n" 181: result_text 182: end