Python is available as a custom script for StackStorm Action.
Here, let's take a look at the flow and usage for using Python scripts with StackStorm.
There are the following two.
If you want to use a static config file, use another config file.
It will be the flow.
From here, let's see the flow of actually running a python script as an Action.
It is assumed that the certification has been completed in advance.
The following is an example of authenticating with st2admin
/ st2admin
.
$ export ST2_AUTH_TOKEN=$(st2 auth st2admin -p st2admin -t)
As mentioned above, you need to create two files.
Both must be under / opt / stackstorm / packs / <pack name> / actions /
.
Here, create it with the pack name test
.
metadata
I think this can be seen fairly easily.
ʻEntry_pointwrites the relative path from
/ opt / stackstorm / packs /
/opt/stackstorm/packs/test/actions/python-test.yaml
---
name: "python-test"
runner_type: "python-script"
description: "this is a sample script of python"
enabled: true
entry_point: "python-test.py"
parameters:
param1:
type: "string"
description: "parameter 1"
required: true
position: 0
param2:
type: "string"
description: "parameter 2"
required: true
position: 1
default: "default parameter 2"
Since this script is called from StackStorm, there is a method itself, but it seems good to think that the run
method will be called in the end [^ 1].
/opt/stackstorm/packs/test/actions/python-test.py
import sys
from st2actions.runners.pythonrunner import Action
class PythonTestAction(Action):
def __init__(self, config):
self.config = config
def run(self, param1, param2):
print(param1, param2)
print(self.config["test_param1"])
return (True, param1 + "," + param2)
I'm passing config
in the constructor, which works with the config file I'll introduce next.
/opt/stackstorm/packs/test/config.yaml
test_param1: "static_param1"
If you put config.yaml
directly under<pack name>
, the setting value written in it will be read into the object called config
by the constructor when the python script is executed [^ 2 ].
Now, I would like to register with StackStorm and execute it ... but even if I register and execute it as it is, an error will occur. (Registration succeeds, but execution results in an error)
First, to register all Actions, run the st2ctl reload --register-actions
command.
This is a command that rereads all the settings under / opt / stackstorm / <pack name>
.
$ sudo st2ctl reload --register-actions
Registering content...[flags = --register-actions]
2016-08-27 14:23:15,107 INFO [-] Connecting to database "st2" @ "0.0.0.0:27017" as user "None".
2016-08-27 14:23:15,376 INFO [-] =========================================================
2016-08-27 14:23:15,376 INFO [-] ############## Registering actions ######################
2016-08-27 14:23:15,376 INFO [-] =========================================================
2016-08-27 14:23:17,147 INFO [-] Registered 63 actions.
##### st2 components status #####
st2actionrunner PID: 4291
st2actionrunner PID: 4293
st2actionrunner PID: 4295
st2actionrunner PID: 4297
st2actionrunner PID: 4299
st2actionrunner PID: 4301
st2actionrunner PID: 4303
st2actionrunner PID: 4305
st2actionrunner PID: 4307
st2actionrunner PID: 4309
st2api PID: 4316
st2api PID: 4810
st2stream PID: 4378
st2stream PID: 4756
st2auth PID: 4388
st2auth PID: 4748
st2garbagecollector PID: 4396
st2notifier PID: 4404
st2resultstracker PID: 4416
st2rulesengine PID: 4428
st2sensorcontainer PID: 4440
st2chatops is not running.
mistral-server PID: 4486
mistral-api PID: 4481
mistral-api PID: 4769
mistral-api PID: 4770
It is actually registered.
$ st2 action list -p test
+----------------------+------+-----------------------------------+
| ref | pack | description |
+----------------------+------+-----------------------------------+
| test.python-test | test | this is a sample script of python |
+----------------------+------+-----------------------------------+
Now, if you try to execute ..., an error will occur.
$ st2 run test.python_test param1=p1
..
id: 57c1dbf1e368b912caaf8908
status: failed
parameters:
param1: p1
result:
error: '
Virtual environment (/opt/stackstorm/virtualenvs/test) for pack "test" doesn''t exist. If you haven''t
installed a pack using "packs.install" command, you can create a new virtual environment using
"st2 run packs.setup_virtualenv packs=test" command''
'
traceback: " File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2actions/container/base.py", line 98, in _do_run
(status, result, context) = runner.run(action_params)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2actions/runners/pythonrunner.py", line 118, in run
raise Exception(msg)
"
There is a message in the middle, but it means that there is no execution environment for python with a pack called test
. It seems that the execution environment is divided for each pack by virtualenv
.
So, if you divide the pack properly, it seems that you can avoid cluttering the execution environment.
Now, let's create the environment according to the message. It says "hit the command like this" carefully, so execute it exactly as it is.
$ sudo st2 run packs.setup_virtualenv packs=test
....
id: 57c1dc6ce368b912caaf890b
status: succeeded
parameters:
packs:
- test
result:
exit_code: 0
result: 'Successfuly set up virtualenv for the following packs: test'
stderr: 'st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Setting up virtualenv for pack "test"
st2.actions.python.SetupVirtualEnvironmentAction: INFO Virtualenv path "/opt/stackstorm/virtualenvs/test" doesn''t exist
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Creating virtualenv for pack "test" in "/opt/stackstorm/virtualenvs/test"
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Creating virtualenv in "/opt/stackstorm/virtualenvs/test" using Python binary "/opt/stackstorm/st2/bin/python"
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Running command "/opt/stackstorm/st2/bin/virtualenv -p /opt/stackstorm/st2/bin/python /opt/stackstorm/virtualenvs/test" to create virtualenv.
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Installing base requirements
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG No pack specific requirements found
st2.actions.python.SetupVirtualEnvironmentAction: DEBUG Virtualenv for pack "test" successfully created in "/opt/stackstorm/virtualenvs/test"
'
stdout: ''
All are INFO and DEBUG, so there seems to be no problem. As you can see in the last message, you have a python environment in / opt / stackstorm / virtualenvs / test
.
Now that the registration itself has been completed, let's try again.
$ st2 run test.python-test param1=p1
..
id: 57c1ed13e368b912caaf8962
status: succeeded
parameters:
param1: p1
result:
exit_code: 0
result: p1,default parameter 2
stderr: ''
stdout: '(u''p1'', u''default parameter 2'')
static_param1
'
As mentioned above, what is output by print
is stored in stdout
, and what is returned by return
is stored in result
.
So, it's easy, but I've seen the flow of executing a python script. There may be a few components, but it looks like you can run python as a workflow very easily.
It didn't help. What is not interrupted or moved will continue to move. The changes will be reflected from the next execution. Therefore, it seems that there is no need to bother to stop the existing and working ones.
It is time to modify YAML (metadata). If you just modify the python script, you don't need to reload.
It didn't help.
You can use the st2ctl reload --register-actions
command to reflect something that has never existed before,
On the contrary, even if you delete the file and execute the above command, it will not be deleted. (Action remains registered)
Therefore, if you want to erase it, you have to explicitly erase it as follows.
st2 action delete test.python-test
When it comes to Python scripts, it seems that you can basically only explicitly add them to arguments or read them from environment variables [^ 3].
It seems that there are various things under the dictionary in the form of {{action_context}}
[^ 4].
For example, WebHook is the user, Workflow is the ID of the parent Action.
However, I haven't tried it.
Recommended Posts