module MCollective::RPC

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

Public Class Methods

discovered(discovered) click to toggle source

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.

# File lib/mcollective/rpc.rb, line 92
def self.discovered(discovered)
    @@discovered = discovered
end
reply() click to toggle source

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

# File lib/mcollective/rpc.rb, line 165
def self.reply
    RPC::Reply.new
end
request(msg) click to toggle source

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

# File lib/mcollective/rpc.rb, line 159
def self.request(msg)
    RPC::Request.new(msg)
end
stats(stats) click to toggle source

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.

# File lib/mcollective/rpc.rb, line 84
def self.stats(stats)
    @@stats = stats
end

Public Instance Methods

empty_filter?(options) click to toggle source

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

# File lib/mcollective/rpc.rb, line 149
def empty_filter?(options)
    if options.include?(:filter)
        Util.empty_filter?(options[:filter])
    else
        Util.empty_filter?(options)
    end
end
printrpc(result, flags = {}) click to toggle source

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.

# File lib/mcollective/rpc.rb, line 130
def printrpc(result, flags = {})
    verbose = @options[:verbose] rescue verbose = false
    verbose = flags[:verbose] || verbose
    flatten = flags[:flatten] || false

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

    if result.is_a?(Array)
        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={}) click to toggle source

Prints stats, requires stats to be saved from elsewhere using the ::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

# File lib/mcollective/rpc.rb, line 109
def printrpcstats(flags={})
    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 = {}) { |rpc| ... } click to toggle source

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

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

# File lib/mcollective/rpc.rb, line 56
def rpcclient(agent, flags = {})
    configfile = flags[:configfile] || "/etc/mcollective/client.cfg"
    options = flags[:options] || nil

    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
        puts("Could not create RPC client: #{e}")
        exit!
    end

    if block_given?
        yield(rpc)
    else
        return rpc
    end
end
rpcoptions() { |parser, options| ... } click to toggle source

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

# 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