What you have to do before running StackStorm commands

gkzz
5 min readMay 27, 2019

--

“I can’t install st2!”

If you have difficulty installing it, this article may help you.

I write it regarded as the official doument, which also tells us how to run actions.

The document says to us, you can run actions, only if you take three steps.

  • Create an action file as python file
  • Define a rule as yaml file
  • Register them

Before defining rules:

[root@ip-xxx-xxx-xxx-xxx ~]$ st2 rule list
+----------------+---------+----------------------------------------+---------+-----------+---------+ | enabled || ref | pack | description -----------+---------+ | enabled | sults of | True |+----------------+---------+-----------------------------for | |-----------+---------+ | |
| chatops.notify | chatops | Notification rule to send re-----------+---------+sults of | True |
| | | action executions to stream
for | |
| | | chatops
| |
+----------------+---------+----------------------------------------+---------+
[root@ip-xxx-xxx-xxx-xxx ~]$

By default, only “chatops.notify” is registerd. The other rules like “example” are Not.

[root@ip-xxx-xxx-xxx-xxx ~]$ st2 rule list --pack=examples
No matching items found
[root@ip-xxx-xxx-xxx-xxx ~]$ st2 rule get examples.sample_rule_with_webhook
Rule "examples.sample_rule_with_webhook" is not found.

So, I’ll define my first rule and the action.

Here is this reference.

Define it as yaml file:

[root@ip-xxx-xxx-xxx-xxx ~]$ cat /opt/stackstorm/packs/examples/actions/my_echo_action.yaml
---
name: "echo_action"
runner_type: "python-script"
description: "Print message to standard output."
enabled: true
entry_point: "my_echo_action.py"
parameters:
message:
type: "string"
description: "Message to print."
required: true
position: 0

Python file:

[root@ip-xxx-xxx-xxx-xxx ~]$ cat /opt/stackstorm/packs/examples/actions/my_echo_action.py
import sys
from st2common.runners.base_action import Actionclass MyEchoAction(Action):
def run(self, message):
print(message)
if message == 'working':
return (True, message)
return (False, message)

Note: “Action“ is set as an argument of a python classes with a run method.

Deploy it!

[root@ip-xxx-xxx-xxx-xxx ~]$ st2 action create /opt/stackstorm/packs/examples/actions/my_echo_action.yaml
+---------------+--------------------------------------------+
| Property | Value |
+---------------+--------------------------------------------+
| id | 5cec20cbf7094139ce60d4ee |
| name | echo_action |
| pack | default |
| description | Print message to standard output. |
| enabled | True |
| entry_point | my_echo_action.py |
| metadata_file | |
| notify | |
| output_schema | |
| parameters | { |
| | "message": { |
| | "position": 0, |
| | "required": true, |
| | "type": "string", |
| | "description": "Message to print." |
| | } |
| | } |
| ref | default.echo_action |
| runner_type | python-script |
| tags | |
| uid | action:default:echo_action |
+---------------+--------------------------------------------+
[root@ip-xxx-xxx-xxx-xxx ~]$ st2ctl reload --register-actions
Registering content...[flags = --config-file /etc/st2/st2.conf --register-actions]
2019-05-27 13:40:54,441 INFO [-] Connecting to database "st2" @ "127.0.0.1:27017" as user "stackstorm".
2019-05-27 13:40:54,445 INFO [-] Successfully connected to database "st2" @ "127.0.0.1:27017" as user "stackstorm".
2019-05-27 13:40:54,796 INFO [-] =========================================================
2019-05-27 13:40:54,796 INFO [-] ############## Registering runners ######################
2019-05-27 13:40:54,796 INFO [-] =========================================================
2019-05-27 13:40:55,719 INFO [-] Registered 15 runners.
2019-05-27 13:40:55,719 INFO [-] =========================================================
2019-05-27 13:40:55,719 INFO [-] ############## Registering actions ######################
2019-05-27 13:40:55,719 INFO [-] =========================================================
2019-05-27 13:41:02,197 INFO [-] Registered 252 actions.
##### st2 components status #####
st2actionrunner PID: 14909
st2actionrunner PID: 14911
st2actionrunner PID: 14913
st2actionrunner PID: 14915
st2actionrunner PID: 14917
st2actionrunner PID: 14919
st2actionrunner PID: 14921
st2actionrunner PID: 14923
st2actionrunner PID: 14925
st2actionrunner PID: 14928
st2api PID: 14780
st2api PID: 14798
st2stream PID: 14703
st2stream PID: 14768
st2auth PID: 14641
st2auth PID: 14661
st2garbagecollector PID: 13903
st2notifier PID: 13906
st2resultstracker PID: 13912
st2rulesengine PID: 13917
st2sensorcontainer PID: 14808
st2chatops is not running.
st2timersengine PID: 13937
st2workflowengine PID: 14830
st2scheduler PID: 13947
mistral-server PID: 15784
mistral.api PID: 15783
mistral.api PID: 15821
mistral.api PID: 15822

Note: According to the document, the file you have to execute register command is yaml file, not but python’s.

Register an individual action by calling st2 action create my_action_metadata.yaml. To reload all actions, use st2ctl reload --register-actions

sources;

Finally, run your first action!

[root@ip-xxx-xxx-xxx-xxx ~]$ st2 run examples.echo_action message="hello world"
.
id: 5cec2465f7094139ce60d4f6
status: failed
parameters:
message: hello world
result:
exit_code: 0
result: hello world
stderr: ''
stdout: 'hello world
'
[root@ip-xxx-xxx-xxx-xxx ~]$ st2 run examples.echo_action message="working"
.
id: 5cec2412f7094139ce60d4f3
status: succeeded
parameters:
message: working
result:
exit_code: 0
result: working
stderr: ''
stdout: 'working
'

Note: Packs are, in general, located at /opt/stackstorm/packs/, thefore, I made necessary configuration as root user. (It’s bothersome to change files’ ownerships.)

When using StackStorm and pack management actions, all packs are installed into the system packs directory, which defaults to /opt/stackstorm/packs.

sources;

cf. StackStorm ‘s file structure:

[root@ip-xxx-xxx-xxx-xxx ~]$ tree /opt/stackstorm/packs/ -d -L 3
/opt/stackstorm/packs/
├── chatops
│ ├── actions
│ │ ├── templates
│ │ └── workflows
│ ├── rules
│ └── tests
│ └── fixtures
├── core
│ ├── actions
│ │ └── send_mail
│ ├── sensors
│ └── tests
├── default
│ ├── actions
│ │ └── workflows
│ ├── rules
│ └── sensors
├── examples
├── actions
│ │ ├── bash_exit_code
│ │ ├── bash_ping
│ │ ├── bash_random
│ │ ├── chains
│ │ ├── pythonactions
│ │ ├── ubuntu_pkg_info
│ │ ├── windows
│ │ └── workflows
│ ├── aliases
│ ├── lib
│ ├── rules
│ ├── scripts
│ ├── sensors
│ ├── tests
│ └── triggers
├── linux
│ ├── actions
│ │ ├── checks
│ │ ├── lib
│ │ └── workflows
│ └── sensors
├── packs
│ ├── actions
│ │ ├── pack_mgmt
│ │ └── workflows
│ ├── aliases
│ └── tests
│ └── fixtures
└── st2
├── actions
│ ├── lib
│ └── workflows
├── aliases
├── sensors
└── tests
└── fixtures
55 directories

Thank you!

I’m happy to you to follow me on medium and Twitter!

cf. A useful Japanese article about StackStorm’s action:

--

--

gkzz
gkzz

Written by gkzz

🇯🇵 #SoftwareDeveloper #MeijiUniv @apc_tweet Opinions are my own. #Geek #ギークハウス大倉山 #gkz https://gkzz.github.io/

No responses yet