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 207 207: def self.add_simplerpc_options(parser, options) 208: parser.separator "" 209: 210: # add SimpleRPC specific options to all clients that use our library 211: parser.on('--np', '--no-progress', 'Do not show the progress bar') do |v| 212: options[:progress_bar] = false 213: end 214: 215: parser.on('--one', '-1', 'Send request to only one discovered nodes') do |v| 216: options[:mcollective_limit_targets] = "1" 217: end 218: 219: parser.on('--limit-nodes [COUNT]', '--ln [COUNT]', 'Send request to only a subset of nodes, can be a percentage') do |v| 220: options[:mcollective_limit_targets] = v 221: end 222: end
Return color codes, if the config color= option is false just return a empty string
# File lib/mcollective/rpc/helpers.rb, line 7 7: def self.color(code) 8: colorize = Config.instance.color 9: 10: colors = {:red => "[31m", 11: :green => "[32m", 12: :yellow => "[33m", 13: :cyan => "[36m", 14: :bold => "[1m", 15: :reset => "[0m"} 16: 17: if colorize 18: return colors[code] || "" 19: else 20: return "" 21: end 22: end
Backward compatible display block for results without a DDL
# File lib/mcollective/rpc/helpers.rb, line 155 155: def self.old_rpcresults(result, flags = {}) 156: result_text = "" 157: 158: if flags[:flatten] 159: result.each do |r| 160: if r[:statuscode] <= 1 161: data = r[:data] 162: 163: unless data.is_a?(String) 164: result_text << data.pretty_inspect 165: else 166: result_text << data 167: end 168: else 169: result_text << r.pretty_inspect 170: end 171: end 172: 173: result_text << "" 174: else 175: result.each do |r| 176: 177: if flags[:verbose] 178: result_text << "%-40s: %s\n" % [r[:sender], r[:statusmsg]] 179: 180: if r[:statuscode] <= 1 181: r[:data].pretty_inspect.split("\n").each {|m| result_text += " #{m}"} 182: result_text += "\n" 183: elsif r[:statuscode] == 2 184: # dont print anything, no useful data to display 185: # past what was already shown 186: elsif r[:statuscode] == 3 187: # dont print anything, no useful data to display 188: # past what was already shown 189: elsif r[:statuscode] == 4 190: # dont print anything, no useful data to display 191: # past what was already shown 192: else 193: result_text << " #{r[:statusmsg]}" 194: end 195: else 196: unless r[:statuscode] == 0 197: result_text << "%-40s %s\n" % [r[:sender], colorize(:red, r[:statusmsg])] 198: end 199: end 200: end 201: end 202: 203: result_text << "" 204: 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 47 47: def self.rpcresults(result, flags = {}) 48: flags = {:verbose => false, :flatten => false}.merge(flags) 49: 50: result_text = "" 51: 52: # if running in verbose mode, just use the old style print 53: # no need for all the DDL helpers obfuscating the result 54: if flags[:verbose] 55: result_text = old_rpcresults(result, flags) 56: else 57: result.each do |r| 58: begin 59: ddl = DDL.new(r.agent).action_interface(r.action.to_s) 60: 61: sender = r[:sender] 62: status = r[:statuscode] 63: message = r[:statusmsg] 64: display = ddl[:display] 65: result = r[:data] 66: 67: # appand the results only according to what the DDL says 68: case display 69: when :ok 70: if status == 0 71: result_text << text_for_result(sender, status, message, result, ddl) 72: end 73: 74: when :failed 75: if status > 0 76: result_text << text_for_result(sender, status, message, result, ddl) 77: end 78: 79: when :always 80: result_text << text_for_result(sender, status, message, result, ddl) 81: 82: when :flatten 83: result_text << text_for_flattened_result(status, result) 84: 85: end 86: rescue Exception => e 87: # no DDL so just do the old style print unchanged for 88: # backward compat 89: result_text = old_rpcresults(result, flags) 90: end 91: end 92: end 93: 94: result_text 95: end
Returns text representing a flattened result of only good data
# File lib/mcollective/rpc/helpers.rb, line 142 142: def self.text_for_flattened_result(status, result) 143: result_text = "" 144: 145: if status <= 1 146: unless result.is_a?(String) 147: result_text << result.pretty_inspect 148: else 149: result_text << result 150: end 151: end 152: end
Return text representing a result
# File lib/mcollective/rpc/helpers.rb, line 98 98: def self.text_for_result(sender, status, msg, result, ddl) 99: statusses = ["", 100: colorize(:red, "Request Aborted"), 101: colorize(:yellow, "Unknown Action"), 102: colorize(:yellow, "Missing Request Data"), 103: colorize(:yellow, "Invalid Request Data"), 104: colorize(:red, "Unknown Request Status")] 105: 106: result_text = "%-40s %s\n" % [sender, statusses[status]] 107: result_text << " %s\n" % [colorize(:yellow, msg)] unless msg == "OK" 108: 109: # only print good data, ignore data that results from failure 110: if [0, 1].include?(status) 111: if result.is_a?(Hash) 112: # figure out the lengths of the display as strings, we'll use 113: # it later to correctly justify the output 114: lengths = result.keys.map{|k| ddl[:output][k][:display_as].size} 115: 116: result.keys.each do |k| 117: # get all the output fields nicely lined up with a 118: # 3 space front padding 119: display_as = ddl[:output][k][:display_as] 120: display_length = display_as.size 121: padding = lengths.max - display_length + 3 122: result_text << " " * padding 123: 124: result_text << "#{display_as}:" 125: 126: if result[k].is_a?(String) || result[k].is_a?(Numeric) 127: result_text << " #{result[k]}\n" 128: else 129: result_text << "\n\t" + result[k].pretty_inspect.split("\n").join("\n\t") + "\n" 130: end 131: end 132: else 133: result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t") 134: end 135: end 136: 137: result_text << "\n" 138: result_text 139: end