View on GitHub

StackStorm Tutorials

A collection of short videos showing different features of StackStorm

Advanced Actions

Commands

Examples

forloop_chain.yaml - Available on GitHub

Files

remote-echo.yaml

---
name: remote-echo
description: Action that executes the Linux echo command on the localhost.
pack: tutorials
runner_type: remote-shell-cmd
enabled: true
parameters:
  message:
    description: The message that the command will echo.
    type: string
    required: true
  cmd:
    description: Arbitrary Linux command to be executed on the local host.
    required: true
    type: string
    default: 'echo ""'
    immutable: true

check-remote-bash-version.yaml

---
name: check-remote-bash-version
description: Check Bash version on host
pack: tutorials
runner_type: remote-shell-script
entry_point: check-bash-version.sh
enabled: true
parameters:
  major:
    type: boolean
    description: Include the major part of the version string
  minor:
    type: boolean
    description: Include the minor part of the version string

check-python-version-script.yaml

---
name: check-python-version-script
description: Check Python version on host
pack: tutorials
runner_type: python-script
entry_point: check-python-version-script.py
enabled: true
parameters:
  major:
    type: boolean
    description: Include the major part of the version string
  minor:
    type: boolean
    description: Include the minor part of the version string

check-python-version-script.py

#!/usr/local/bin/python

from __future__ import print_function

import sys

from st2common.runners.base_action import Action


def get_version_string(major=False, minor=False, micro=False):
    """
    Return all or part of the Python version string.

    If all parameters are False-y, returns the entire, unmangled Python version
    string

    Parameters
    ----------
    major : boolean
        If True, includes the major version in the returned Python version
        string
    minor : boolean
        If True, includes the minor version in the returned Python version
        string
    micro : boolean
        If True, includes the micro/bugfix version in the returned Python
        version string

    Returns
    -------
    str
        The constructed version string
    """
    version_tuple = tuple()
    if major:
        version_tuple += (sys.version_info[0],)
    if minor:
        version_tuple += (sys.version_info[1],)
    if micro:
        version_tuple += (sys.version_info[2],)
    if not any([major, minor, micro]):
        version_tuple = tuple(version_part for version_part in sys.version_info)

    return '.'.join([str(el) for el in version_tuple])


class GetVersionStringAction(Action):
    """
    StackStorm Action for the python-script runner that wraps the
    get_version_string function.

    Only accepts and passes the major and minor parameters.
    """
    def run(self, major, minor):
        return {
            "version": get_version_string(major=major, minor=minor),
            "major": get_version_string(major=True),
            "minor": get_version_string(minor=True),
            "micro": get_version_string(micro=True),
        }

get-todo-json.yaml

---
name: get-todo-json
description: Get a JSON representation of a TODO list item
pack: tutorials
runner_type: http-request
entry_point: ''
enabled: true
parameters:
  subdomain:
    type: string
    default: jsonplaceholder
  domain:
    type: string
    default: typicode.com
  path:
    type: string
    default: todos
  todo_number:
    type: integer
    default: 1
  url:
    type: string
    default: https://.//
  method:
    type: string
    default: GET
    immutable: true

Questions, Comments, and Corrections

Open an issue or submit a pull request.

Transcript

Hi everybody and welcome to StackStorm tutorials!

This tutorial is on Advanced Actions.

In the last tutorial, we went over a few different actions. Each action called a different type of script, we called a Bash script, a Python script, and a Node.js script.

All of those scripts ran on the StackStorm server itself.

In this tutorial, we’ll learn how to call scripts on remote machines. We’ll also learn how to call Python scripts directly, and we’ll learn how to send out an HTTP request.

To get started, let’s take a look at the actions we defined last time.

For the echo action, we used the local-shell-cmd runner. For the other three actions, we used the local-shell-script runner, but the entry_point for each was a different script.

For the check-bash-version action we called the check-bash-version.sh script; the Python and Node actions also used the local-shell-script runner, with slightly different entry points.

All of the scripts ran on the StackStorm server itself.

However, StackStorm can also run scripts on remote systems. Let’s take a look at one.

This action uses a slightly different runner - it uses the remote-shell-cmd runner. Let’s take a look at it. The remote-echo action takes the same parameters as the echo action, but the remote-shell-cmd runner itself takes an extra hosts parameter. Let’s see that in action.

Here, the remote echo action takes the same message parameter as before, but the command was run on the st2tutorials host. We can also run the same command on two different hosts. Here I’m running the action on both the st2tutorials host and localhost. I’m specifying their hostnames as a comma-separated list.

The command is run twice - first, it’s run on localhost, and second, it’s run on st2tutorials. In both cases, the standard out was “Hello World!” as expected, the action succeeded, and standard error was empty.

Next let’s take a look at running a script on a remote system.

This action, the check-remote-bash-version action, checks the version of Bash on a remote host. It uses the remote-shell-script runner, and has the same entry_point as the check-bash-version-script. This runner also takes a hosts parameter. Let’s see it in action.

This action has run the same Bash script on two different hosts: the st2tutorials host and localhost. In both cases, it succeeded, and the standard out was the same.

StackStorm can also run Python scripts directly with the python-script runner. Let’s take a look at an action that uses the python-script runner.

This action is the exact same as the check-python-version action from the previous tutorial, with two differences: this action uses the python-script runner instead of the local-shell-script runner. The entry_point is also a slightly different Python script. Let’s take a look at that.

This Python script is specific to StackStorm, because it imports directly from st2common. It defines a get_version_string function that accepts major, minor, and micro parameters. Similar to the action from the previous tutorial, it creates a version string, but instead of printing it to standard out, this function simply returns it. The script then defines a subclass of Action. That class’s run function accepts major and minor parameters, and passes them to the get_version_string function that we defined before. The run function simply returns a dictionary of the version, major, minor, and micro keys. Let’s see how that’s different than the local-shell-script runner.

As you can see, the result that was returned from the run function contains version, major, minor, and micro keys. Let’s compare that to the result of the action from the previous tutorial.

The previous action printed the version string to its standard out. StackStorm can capture the standard out from any shell script, but for Python scripts, it can capture the entire Python return value.