Content is available under Creative Commons Attribution.


Embedding Scripts in Leroy Workflows

Leroy’s <execute> tag is a really flexible thing.  The obvious application is for it to run scripts that you put in the “commands” directory of your Leroy controller; these commands are uploaded to the agent and then run.  Commands are great for scripts you want to run again and again; for instance, a stop-service.bat or a that you want to reuse across several workflows using different parameters passed to the script.

However, sometimes you want to do something simple that exists in just one place.  A one-off script that you don’t need to have as its own file.  That’s where Leroy’s script embedding feature comes into play.

Leroy can embed two kinds of scripts; it can embed Python 3 which is run using Leroy’s built-in Python engine, or it can embed a script that will be piped through standard input to whatever interpreter you want.  The latter can be used to run Windows “CMD” or bat scripts, or UNIX shell scripts.

Here’s a sample of a workflow that would dump a Leroy agent’s log file to the controller’s output:


(UNIX / bash)

Pretty easy, right? Whatever is in the ‘shell’ attribute of the ‘execute’ step will be used as the interpreter, and whatever is in the ‘script’ tag will be piped in through Standard Input. You can use anything that takes input from standard input as a shell and this will work, so if you have a configuration that interactively asks questions, your ‘script’ can be the responses to the questions asked and it’ll work!

Note the type attribute on the script tag; in the examples above, our type was ‘shell’.  That type can be ‘python’ instead, if you’d like to run some Python.  Remember, Leroy uses Python 3.x so some of the stuff from 2.x won’t work or has changed.  Here’s a very simple example of a Python script:

Something that’s a little easy to mess up with Python is the tabbing. Note that in the shell examples above, I tabbed out the scripts to match the tabbing of the Workflow tabs to make it look “neat”. With Python, you can’t do that; your script must be “left justified”, because tabbing is significant to the language.

You can use features such as postProcess, precondition, and the like alongside scripts. Whatever return code your script generates will determine the success or failure of the execute step, 0 of course being success, unless you use a postProcess or ignoreError. Standard output and standard error will be redirected out to the controller’s output stream.

These inline scripts should be used sparingly, because they are difficult to re-use — scripts in the commands folder are a lot easier to re-use and re-purpose — but sometimes they are the best way to do something, and they add a lot of power to Leroy.

Leave a Reply

by: Epic Force