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 251 251: def self.add_simplerpc_options(parser, options) 252: parser.separator "" 253: 254: # add SimpleRPC specific options to all clients that use our library 255: parser.on('--np', '--no-progress', 'Do not show the progress bar') do |v| 256: options[:progress_bar] = false 257: end 258: 259: parser.on('--one', '-1', 'Send request to only one discovered nodes') do |v| 260: options[:mcollective_limit_targets] = "1" 261: end 262: 263: parser.on('--limit-nodes [COUNT]', '--ln [COUNT]', 'Send request to only a subset of nodes, can be a percentage') do |v| 264: options[:mcollective_limit_targets] = v 265: end 266: 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 199 199: def self.old_rpcresults(result, flags = {}) 200: result_text = "" 201: 202: if flags[:flatten] 203: result.each do |r| 204: if r[:statuscode] <= 1 205: data = r[:data] 206: 207: unless data.is_a?(String) 208: result_text << data.pretty_inspect 209: else 210: result_text << data 211: end 212: else 213: result_text << r.pretty_inspect 214: end 215: end 216: 217: result_text << "" 218: else 219: [result].flatten.each do |r| 220: 221: if flags[:verbose] 222: result_text << "%-40s: %s\n" % [r[:sender], r[:statusmsg]] 223: 224: if r[:statuscode] <= 1 225: r[:data].pretty_inspect.split("\n").each {|m| result_text += " #{m}"} 226: result_text << "\n\n" 227: elsif r[:statuscode] == 2 228: # dont print anything, no useful data to display 229: # past what was already shown 230: elsif r[:statuscode] == 3 231: # dont print anything, no useful data to display 232: # past what was already shown 233: elsif r[:statuscode] == 4 234: # dont print anything, no useful data to display 235: # past what was already shown 236: else 237: result_text << " #{r[:statusmsg]}" 238: end 239: else 240: unless r[:statuscode] == 0 241: result_text << "%-40s %s\n" % [r[:sender], colorize(:red, r[:statusmsg])] 242: end 243: end 244: end 245: end 246: 247: result_text << "" 248: 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: 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
# 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 186 186: def self.text_for_flattened_result(status, result) 187: result_text = "" 188: 189: if status <= 1 190: unless result.is_a?(String) 191: result_text << result.pretty_inspect 192: else 193: result_text << result 194: end 195: end 196: end
Return text representing a result
# 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 do |k| 147: begin 148: ddl[:output][k][:display_as].size 149: rescue 150: k.to_s.size 151: end 152: end 153: 154: result.keys.each do |k| 155: # get all the output fields nicely lined up with a 156: # 3 space front padding 157: begin 158: display_as = ddl[:output][k][:display_as] 159: rescue 160: display_as = k.to_s 161: end 162: 163: display_length = display_as.size 164: padding = lengths.max - display_length + 3 165: result_text << " " * padding 166: 167: result_text << "#{display_as}:" 168: 169: if result[k].is_a?(String) || result[k].is_a?(Numeric) 170: result_text << " #{result[k]}\n" 171: else 172: padding = " " * (lengths.max + 5) 173: result_text << " " << result[k].pretty_inspect.split("\n").join("\n" << padding) << "\n" 174: end 175: end 176: else 177: result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t") 178: end 179: end 180: 181: result_text << "\n" 182: result_text 183: end