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