August 2014
Intermediate to advanced
284 pages
5h 24m
English
In Chapter 15, we documented how to build an agent and in Chapter 17 how to build an application to extend the built-in mco command. Although that is useful for sending requests interactively or in a small shell script, it may not meet your needs for programatic usage.
You can build stand-alone Ruby scripts that utilize the same client libraries. The structure for these scripts is very similar, and you can use every option shown in the previous sections. Let’s walk you through an example here:
#!/usr/bin/rubyrequire'mcollective'includeMCollective::RPCoptions=rpcoptionsdo|parser,options|parser.define_head"Script for the Thanks agent"parser.banner="Usage: thanks.rb [options] person"parser.on('-p','--person NAME','Person to say goodbye to.')do|name|options[:person]=nameendend# This is probably covered by the validation in the DDLunlessoptions.include?(:person)puts("You need to specify a person's name with --person")exit!1end# Create an MCollective client utilizing our agentclient=rpcclient("thanks",:options=>options)# Enable to see discovery results#client.discover :verbose => true# To disable the progress indicator#client.progress = false# Two different ways to get results# 1. Simple verbose outputprintrpcclient.say_goodbye(:person=>options[:person]),:verbose=>true# 2. Format the output as you like#client.say_goodbye(:person => options[:person]).each do |resp| ...