Chapter 16. Extending the Agent

Here we’ll introduce you to more complex things you can do in your agent. There isn’t a use for this functionality in our thanks plugin just yet, but it’s good to know what is possible.

We’ll give it the ability to execute external scripts or command lines; how to send custom log lines; and best of all, we’ll review how to blow up tragically, er, I mean error out gracefully. Of course.

Executing Scripts

You can call any external script, written in any programming language (including bash) that is capable of writing out the results to a file in JSON format:

action "python_script" do
  implemented_by "/lovely/little/python/script.py"
end

The script will be given two types of input:

  • A path to the file containing the request data in JSON format:

    • The first command-line parameter

    • Environment variable $MCOLLECTIVE_REQUEST_FILE

  • A path to the file where the response should be written in JSON format:

    • The second command-line parameter

    • Environment variable $MCOLLECTIVE_REPLY_FILE

The script should write the reply as a JSON hash into the reply file. The return code of your script should be one of the standard result codes from the table in “Results and Exceptions”.

If you do not specify a full path to the script, it will look for the script in the agent’s plugin directory. This makes it easy to bundle your scripts in with your agent plugins:

action "bundled_script" do
  implemented_by "bundled_script.py"
end

Location:
  $libdir/agent/agentname ...

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.