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 238 238: def self.add_simplerpc_options(parser, options) 239: parser.separator "" 240: 241: # add SimpleRPC specific options to all clients that use our library 242: parser.on('--np', '--no-progress', 'Do not show the progress bar') do |v| 243: options[:progress_bar] = false 244: end 245: 246: parser.on('--one', '-1', 'Send request to only one discovered nodes') do |v| 247: options[:mcollective_limit_targets] = "1" 248: end 249: 250: parser.on('--limit-nodes [COUNT]', '--ln [COUNT]', 'Send request to only a subset of nodes, can be a percentage') do |v| 251: options[:mcollective_limit_targets] = v 252: end 253: end
Return color codes, if the config color= option is false just return a empty string
# File lib/mcollective/rpc/helpers.rb, line 38 38: def self.color(code) 39: colorize = Config.instance.color 40: 41: colors = {:red => "[31m", 42: :green => "[32m", 43: :yellow => "[33m", 44: :cyan => "[36m", 45: :bold => "[1m", 46: :reset => "[0m"} 47: 48: if colorize 49: return colors[code] || "" 50: else 51: return "" 52: end 53: end
Checks in PATH returns true if the command is found
# 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
# File lib/mcollective/rpc/helpers.rb, line 186 186: def self.old_rpcresults(result, flags = {}) 187: result_text = "" 188: 189: if flags[:flatten] 190: result.each do |r| 191: if r[:statuscode] <= 1 192: data = r[:data] 193: 194: unless data.is_a?(String) 195: result_text << data.pretty_inspect 196: else 197: result_text << data 198: end 199: else 200: result_text << r.pretty_inspect 201: end 202: end 203: 204: result_text << "" 205: else 206: result.each do |r| 207: 208: if flags[:verbose] 209: result_text << "%-40s: %s\n" % [r[:sender], r[:statusmsg]] 210: 211: if r[:statuscode] <= 1 212: r[:data].pretty_inspect.split("\n").each {|m| result_text += " #{m}"} 213: result_text += "\n" 214: elsif r[:statuscode] == 2 215: # dont print anything, no useful data to display 216: # past what was already shown 217: elsif r[:statuscode] == 3 218: # dont print anything, no useful data to display 219: # past what was already shown 220: elsif r[:statuscode] == 4 221: # dont print anything, no useful data to display 222: # past what was already shown 223: else 224: result_text << " #{r[:statusmsg]}" 225: end 226: else 227: unless r[:statuscode] == 0 228: result_text << "%-40s %s\n" % [r[:sender], colorize(:red, r[:statusmsg])] 229: end 230: end 231: end 232: end 233: 234: result_text << "" 235: 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 78 78: def self.rpcresults(result, flags = {}) 79: flags = {:verbose => false, :flatten => false}.merge(flags) 80: 81: result_text = "" 82: 83: # if running in verbose mode, just use the old style print 84: # no need for all the DDL helpers obfuscating the result 85: if flags[:verbose] 86: result_text = old_rpcresults(result, flags) 87: else 88: result.each do |r| 89: begin 90: ddl = DDL.new(r.agent).action_interface(r.action.to_s) 91: 92: sender = r[:sender] 93: status = r[:statuscode] 94: message = r[:statusmsg] 95: display = ddl[:display] 96: result = r[:data] 97: 98: # appand the results only according to what the DDL says 99: case display 100: when :ok 101: if status == 0 102: result_text << text_for_result(sender, status, message, result, ddl) 103: end 104: 105: when :failed 106: if status > 0 107: result_text << text_for_result(sender, status, message, result, ddl) 108: end 109: 110: when :always 111: result_text << text_for_result(sender, status, message, result, ddl) 112: 113: when :flatten 114: result_text << text_for_flattened_result(status, result) 115: 116: end 117: rescue Exception => e 118: # no DDL so just do the old style print unchanged for 119: # backward compat 120: result_text = old_rpcresults(result, flags) 121: end 122: end 123: end 124: 125: result_text 126: 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
# 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
# File lib/mcollective/rpc/helpers.rb, line 173 173: def self.text_for_flattened_result(status, result) 174: result_text = "" 175: 176: if status <= 1 177: unless result.is_a?(String) 178: result_text << result.pretty_inspect 179: else 180: result_text << result 181: end 182: end 183: end
Return text representing a result
# File lib/mcollective/rpc/helpers.rb, line 129 129: def self.text_for_result(sender, status, msg, result, ddl) 130: statusses = ["", 131: colorize(:red, "Request Aborted"), 132: colorize(:yellow, "Unknown Action"), 133: colorize(:yellow, "Missing Request Data"), 134: colorize(:yellow, "Invalid Request Data"), 135: colorize(:red, "Unknown Request Status")] 136: 137: result_text = "%-40s %s\n" % [sender, statusses[status]] 138: result_text << " %s\n" % [colorize(:yellow, msg)] unless msg == "OK" 139: 140: # only print good data, ignore data that results from failure 141: if [0, 1].include?(status) 142: if result.is_a?(Hash) 143: # figure out the lengths of the display as strings, we'll use 144: # it later to correctly justify the output 145: lengths = result.keys.map{|k| ddl[:output][k][:display_as].size} 146: 147: result.keys.each do |k| 148: # get all the output fields nicely lined up with a 149: # 3 space front padding 150: display_as = ddl[:output][k][:display_as] 151: display_length = display_as.size 152: padding = lengths.max - display_length + 3 153: result_text << " " * padding 154: 155: result_text << "#{display_as}:" 156: 157: if result[k].is_a?(String) || result[k].is_a?(Numeric) 158: result_text << " #{result[k]}\n" 159: else 160: result_text << "\n\t" + result[k].pretty_inspect.split("\n").join("\n\t") + "\n" 161: end 162: end 163: else 164: result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t") 165: end 166: end 167: 168: result_text << "\n" 169: result_text 170: end