Class: MCollective::RPC::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/rpc/helpers.rb

Overview

Various utilities for the RPC system

Class Method Summary collapse

Class Method Details

.add_simplerpc_options(parser, options) ⇒ Object

Add SimpleRPC common options



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/mcollective/rpc/helpers.rb', line 302

def self.add_simplerpc_options(parser, options)
  parser.separator ""

  # 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', Integer, 'Do requests in batches') do |v|
    options[:batch_size] = v
  end

  parser.on('--batch-sleep SECONDS', Float, 'Sleep time between batches') do |v|
    options[:batch_sleep_time] = v
  end

  parser.on('--limit-nodes COUNT', '--ln', '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
end

.color(code) ⇒ Object

Return color codes, if the config color= option is false just return a empty string



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mcollective/rpc/helpers.rb', line 71

def self.color(code)
  colorize = Config.instance.color

  colors = {:red => "",
    :green => "",
    :yellow => "",
    :cyan => "",
    :bold => "",
    :reset => ""}

  if colorize
    return colors[code] || ""
  else
    return ""
  end
end

.colorize(code, msg) ⇒ Object

Helper to return a string in specific color



89
90
91
# File 'lib/mcollective/rpc/helpers.rb', line 89

def self.colorize(code, msg)
  "#{self.color(code)}#{msg}#{self.color(:reset)}"
end

.command_in_path?(command) ⇒ Boolean

Checks in PATH returns true if the command is found

Returns:

  • (Boolean)


6
7
8
9
10
11
12
# File 'lib/mcollective/rpc/helpers.rb', line 6

def self.command_in_path?(command)
  found = ENV["PATH"].split(File::PATH_SEPARATOR).map do |p|
    File.exist?(File.join(p, command))
  end

  found.include?(true)
end

.extract_hosts_from_array(hosts) ⇒ Object

Given an array of something, make sure each is a string chomp off any new lines and return just the array of hosts



38
39
40
41
42
43
# File 'lib/mcollective/rpc/helpers.rb', line 38

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

.extract_hosts_from_json(json) ⇒ Object

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"}

]



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mcollective/rpc/helpers.rb', line 23

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

.old_rpcresults(result, flags = {}) ⇒ Object

Backward compatible display block for results without a DDL



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
# File 'lib/mcollective/rpc/helpers.rb', line 250

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], colorize(:red, r[:statusmsg])]
        end
      end
    end
  end

  result_text << ""
end

.rpcresults(result, flags = {}) ⇒ Object

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.



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
# File 'lib/mcollective/rpc/helpers.rb', line 111

def self.rpcresults(result, flags = {})
  flags = {:verbose => false, :flatten => false, :format => :console}.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]
          display = ddl[:display]
          result = r[:data]

          # 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
            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

.terminal_dimensionsObject

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mcollective/rpc/helpers.rb', line 49

def self.terminal_dimensions
  return [0, 0] unless STDOUT.tty?

  return [80, 40] if Util.windows?

  if ENV["COLUMNS"] && ENV["LINES"]
    return [ENV["COLUMNS"].to_i, ENV["LINES"].to_i]

  elsif ENV["TERM"] && command_in_path?("tput")
    return [`tput cols`.to_i, `tput lines`.to_i]

  elsif command_in_path?('stty')
    return `stty size`.scan(/\d+/).map {|s| s.to_i }
  else
    return [0, 0]
  end
rescue
  [0, 0]
end

.text_for_flattened_result(status, result) ⇒ Object

Returns text representing a flattened result of only good data



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/mcollective/rpc/helpers.rb', line 237

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

.text_for_result(sender, status, msg, result, ddl) ⇒ Object

Return text representing a result



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
# File 'lib/mcollective/rpc/helpers.rb', line 171

def self.text_for_result(sender, status, msg, result, ddl)
  statusses = ["",
               colorize(:red, "Request Aborted"),
               colorize(:yellow, "Unknown Action"),
               colorize(:yellow, "Missing Request Data"),
               colorize(:yellow, "Invalid Request Data"),
               colorize(:red, "Unknown Request Status")]

  result_text = "%-40s %s\n" % [sender, statusses[status]]
  result_text << "   %s\n" % [colorize(:yellow, msg)] unless msg == "OK"

  # only print good data, ignore data that results from failure
  if [0, 1].include?(status)
    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.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
    else
      result_text << "\n\t" + result.pretty_inspect.split("\n").join("\n\t")
    end
  end

  result_text << "\n"
  result_text
end