Module: MCollective::RPC

Included in:
Application
Defined in:
lib/mcollective/rpc.rb,
lib/mcollective/rpc/ddl.rb,
lib/mcollective/rpc/agent.rb,
lib/mcollective/rpc/audit.rb,
lib/mcollective/rpc/reply.rb,
lib/mcollective/rpc/stats.rb,
lib/mcollective/rpc/client.rb,
lib/mcollective/rpc/result.rb,
lib/mcollective/rpc/helpers.rb,
lib/mcollective/rpc/request.rb,
lib/mcollective/rpc/progress.rb,
lib/mcollective/rpc/actionrunner.rb

Overview

Toolset to create a standard interface of client and agent using an RPC metaphor, standard compliant agents will make it easier to create generic clients like web interfaces etc

Defined Under Namespace

Classes: ActionRunner, Agent, Audit, Client, DDL, Helpers, Progress, Reply, Request, Result, Stats

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.discovered(discovered) ⇒ Object

means for other classes to drop discovered hosts into this module its a bit hacky but needed so that the mixin methods like printrpcstats can easily get access to it without users having to pass it around in params.



108
109
110
# File 'lib/mcollective/rpc.rb', line 108

def self.discovered(discovered)
  @@discovered = discovered
end

.replyObject

Factory for RPC::Reply messages, only really here to make agents a bit easier to understand



184
185
186
# File 'lib/mcollective/rpc.rb', line 184

def self.reply
  RPC::Reply.new
end

.request(msg) ⇒ Object

Factory for RPC::Request messages, only really here to make agents a bit easier to understand



178
179
180
# File 'lib/mcollective/rpc.rb', line 178

def self.request(msg)
  RPC::Request.new(msg)
end

.stats(stats) ⇒ Object

means for other classes to drop stats into this module its a bit hacky but needed so that the mixin methods like printrpcstats can easily get access to it without users having to pass it around in params.



100
101
102
# File 'lib/mcollective/rpc.rb', line 100

def self.stats(stats)
  @@stats = stats
end

Instance Method Details

#empty_filter?(options) ⇒ Boolean

Wrapper for MCollective::Util.empty_filter? to make clients less fugly to write - ticket #18

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/mcollective/rpc.rb', line 168

def empty_filter?(options)
  if options.include?(:filter)
    Util.empty_filter?(options[:filter])
  else
    Util.empty_filter?(options)
  end
end

#printrpc(result, flags = {}) ⇒ Object

Prints the result of an RPC call.

In the default quiet mode - no flattening or verbose - only results that produce an error will be printed

To get details of each result run with the -v command line option.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mcollective/rpc.rb', line 148

def printrpc(result, flags = {})
  verbose = @options[:verbose] rescue verbose = false
  verbose = flags[:verbose] || verbose
  flatten = flags[:flatten] || false
  format = @options[:output_format]

  result_text =  Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten, :format => format})

  if result.is_a?(Array) && format == :console
    puts "\n%s\n" % [ result_text ]
  else
    # when we get just one result to print dont pad them all with
    # blank spaces etc, just print the individual result with no
    # padding
    puts result_text unless result_text == ""
  end
end

#printrpcstats(flags = {}) ⇒ Object

Prints stats, requires stats to be saved from elsewhere using the MCollective::RPC.stats method.

If you’ve passed -v on the command line a detailed stat block will be printed, else just a one liner.

You can pass flags into it, at the moment only one flag is supported:

printrpcstats :caption => “Foo”

This will use “Foo” as the caption to the stats in verbose mode



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mcollective/rpc.rb', line 125

def printrpcstats(flags={})
  return unless @options[:output_format] == :console

  verbose = @options[:verbose] rescue verbose = false
  caption = flags[:caption] || "rpc stats"

  begin
    stats = @@stats
  rescue
    puts("no stats to display")
    return
  end

  puts
  puts stats.report(caption, verbose)
end

#rpcclient(agent, flags = {}) ⇒ Object

Wrapper to create clients, supposed to be used as a mixin:

include MCollective::RPC

exim = rpcclient(“exim”) printrpc exim.mailq

or

rpcclient(“exim”) do |exim|

printrpc exim.mailq

end

It will take a few flags:

:configfile => "etc/client.cfg"
:options => options
:exit_on_failure => true

Options would be a build up options hash from the Optionparser you can use the rpcoptions helper to create this

:exit_on_failure is true by default, and causes the application to exit if there is a failure constructing the RPC client. Set this flag to false to cause an Exception to be raised instead.



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

def rpcclient(agent, flags = {})
  configfile = flags[:configfile] || "/etc/mcollective/client.cfg"
  options = flags[:options] || nil

  if flags.key?(:exit_on_failure)
    exit_on_failure = flags[:exit_on_failure]
  else
    # We exit on failure by default for CLI-friendliness
    exit_on_failure = true
  end

  begin
    if options
      rpc = Client.new(agent, :configfile => options[:config], :options => options)
      @options = rpc.options
    else
      rpc = Client.new(agent, :configfile => configfile)
      @options = rpc.options
    end
  rescue Exception => e
    if exit_on_failure
      puts("Could not create RPC client: #{e}")
      exit!
    else
      raise e
    end
  end

  if block_given?
    yield(rpc)
  else
    return rpc
  end
end

#rpcoptionsObject

Creates a standard options hash, pass in a block to add extra headings etc see Optionparser



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

def rpcoptions
  oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true}, "filter")

  options = oparser.parse do |parser, options|
    if block_given?
      yield(parser, options)
    end

    Helpers.add_simplerpc_options(parser, options)
  end

  return options
end