Chapter 17. Creating a Client Application

We’ve done a bit of testing with the agent, doing direct RPCUtil queries against it. That’s a bit long of a command isn’t it? And look at all that messy RPCUtil output.

Why don’t we build a proper client plugin to interface with our new agent?

Baseline Client

Unfortunately, there’s no easy command to generate a template for us, so we’ll just have to do this ourselves. This application will be thanks/application/thanks.rb. Assuming you are still inside the thanks/agent/ directory from earlier:

$ mkdir ../application
$ cd ../application
$ $EDITOR thanks.rb

Now let’s populate the file like so (you can find this file in the source code supplied with the book):

class MCollective::Application::Thanks<MCollective::Application
  description "Sends a thanks message."
  usage "mco thanks [OPTIONS]"

  # This options parser updates the help page
  option :person,
         :description => "The person the dolphins say Goodbye to.",
         :arguments   => ["-p NAME", "--person NAME"],
         :type        => String,
         :require     => true

  # another hook where we could throw exceptions if the input isn't valid
  def validate_configuration(configuration)
    # this shouldn't happen since the option is mandatory above
    raise "Need to supply a person to get a reply." \
      unless configuration.include?(:person)
  end

  # Now we enter main processing
  def main
   client = rpcclient("thanks")
    printrpc client.say_goodbye(
      :person => configuration[:person],
      :options => options
    )

    # Exit using halt and it will pass on the ...

Get Learning MCollective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.