Unable to run an action implemented by python3

python

(Chnic888) #1

Hello all,

I am trying to run my own pack which contains a python3 action. The python3 runtime was installed on server and the packs could be installed by restful API as well.

{
“packs”: [“http://github.com/xxx”],
“python3”: true,
“force”: true
}

But the question is when I run my action I always get the following error

Traceback (most recent call last):
File “/opt/stackstorm/runners/python_runner/python_runner/python_action_wrapper.py”, line 21, in
import traceback
File “/usr/local/python3/lib/python3.6/traceback.py”, line 5, in
import linecache
File “/opt/stackstorm/virtualenvs/st2_test/lib/python3.6/linecache.py”, line 11, in
import tokenize
File “/opt/stackstorm/virtualenvs/st2_test/lib/python3.6/tokenize.py”, line 33, in
import re
File “/opt/stackstorm/virtualenvs/st2_test/lib/python3.6/re.py”, line 142, in
class RegexFlag(enum.IntFlag):
AttributeError: module ‘enum’ has no attribute ‘IntFlag’

I searched this issue from the stackoverflow, somebody says it is an incompatible issue between python2 and python3.

I tried to fixed my issue according to above URL but not work. My st2 version is 2.8.0

st2 --version
st2 2.8.0, on Python 2.7.5

Is there any way to fixed my problem? Thanks.


(Tomaz Muraus) #2

It looks like that the issue is related to enum34 package being installed and used by some StackStorm component dependency and this is some how making it into python3 pack virtual environment (which shouldn’t happen).

We add python3 site-packages to the beginning of PYTHONPATH for Python runner actions so this shouldn’t happen since Python 3 enum module should have precedence over python 2 one (enum34).

Did you try adding from __future__ import absolute_import to your python action?

In any case, it would be easier if you can gist / paste the code (action) with which we can try to replicate the problem.


(Chnic888) #3

thx, tried to fix my issue by adding from future import absolute_import, still not works.

I uploaded my code to GitHub - chnic888/st2_test

BTW: where can I find the official code snippet or sample implemented by python3?


(Tomaz Muraus) #4

I tried to replicate the problem using the content you provided.

I needed to make two small fixes (see error below), but besides that, everything worked out of the box on StackStorm v2.8.0.

(virtualenv) vagrant@ubuntu-xenial:/data/stanley$ python st2client/st2client/shell.py pack install --python3 https://github.com/chnic888/st2_test.git
2018-07-13 09:29:23,424  WARNING - Auth API server is not available, skipping authentication.

	[ succeeded ] download pack
	[ succeeded ] make a prerun
	[ succeeded ] install pack dependencies
	[ succeeded ] register pack

+-------------+------------------------+
| Property    | Value                  |
+-------------+------------------------+
| name        | st2 Packs Test         |
| description | Stack Storm Packs Test |
| version     | 0.0.1                  |
| author      | chnic888               |
+-------------+------------------------+

(virtualenv) vagrant@ubuntu-xenial:/data/stanley$ python st2client/st2client/shell.py run st2_test.keys host=localhost
2018-07-13 09:30:13,922  WARNING - Auth API server is not available, skipping authentication.
..
id: 5b487126962d743a62441d05
status: failed
parameters: 
  host: localhost
result: 
  exit_code: 1
  result: None
  stderr: "Traceback (most recent call last):
  File "/data/stanley/contrib/runners/python_runner/python_runner/python_action_wrapper.py", line 224, in _get_action_instance
    actions_cls = action_loader.register_plugin(Action, self._file_path)
  File "/data/stanley/st2common/st2common/util/loader.py", line 173, in register_plugin
    module = imp.load_source(module_name, plugin_abs_file_path)
  File "/opt/stackstorm/virtualenvs/st2_test/lib/python3.5/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/opt/stackstorm/packs/st2_test/actions/keys.py", line 2
    from __future__ import absolute_import
    ^
SyntaxError: from __future__ imports must occur at the beginning of the file

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/stanley/contrib/runners/python_runner/python_runner/python_action_wrapper.py", line 320, in <module>
    obj.run()
  File "/data/stanley/contrib/runners/python_runner/python_runner/python_action_wrapper.py", line 178, in run
    action = self._get_action_instance()
  File "/data/stanley/contrib/runners/python_runner/python_runner/python_action_wrapper.py", line 231, in _get_action_instance
    raise exc_cls(msg)
SyntaxError: Failed to load action class from file "/opt/stackstorm/packs/st2_test/actions/keys.py" (action file most likely doesn't exist or contains invalid syntax): from __future__ imports must occur at the beginning of the file (keys.py, line 2)

(virtualenv) vagrant@ubuntu-xenial:/data/stanley$ python st2client/st2client/shell.py run st2_test.keys host=localhost
2018-07-13 09:31:12,206  WARNING - Auth API server is not available, skipping authentication.
.
id: 5b487160962d743a62441d0e
status: succeeded
parameters: 
  host: localhost
result: 
  exit_code: 0
  result: []
  stderr: ''
  stdout: ''

(virtualenv) vagrant@ubuntu-xenial:/data/stanley$ /opt/stackstorm/virtualenvs/st2_test/bin/python --version
Python 3.5.2

(virtualenv) vagrant@ubuntu-xenial:/data/stanley$ python st2client/st2client/shell.py --version
shell.py 2.8.0, on Python 2.7.12

As you can see, after I moved from __future__ import absolute_importto the top inside both of the files (as suggested by the error message), action worked just fine.


(Chnic888) #5

really really thx for your reply. It seems that you used the st2-client to call action but my host does not install this dependency since my usage is to call this action by restful api. (our project needs to integrate with st2 with restful apis)

I removed the two unnecessary lines from my code base and recall it, still failed. Could you please try to call this action by restful api instead of using st2-client at your best convenience? It seems the api use “/opt/stackstorm/runners/python_runner/python_runner/python_action_wrapper.py” to run the action.

Thanks. :slight_smile:


(Tomaz Muraus) #6

It should make no difference how you run the action - underneath CLI uses StackStorm API as well.

Please provide action runner service logs so we can see what is going on. Just saying “doesn’t work” without any corresponding log messages and details doesn’t help us much.

Please also include the output of the following commands (run them on the server where StackStorm is running):

st2 --version
python --version
/opt/stackstorm/virtualenvs/st2_test/bin/python --version

(AutoJunjie) #7

Hello Tomaz,

I am having the exact same error. and one thing i found out from your output is, your environment seems like running python 3.5, and myself and chnic888 is running 3.6, does that matter? system info and logs:

System info:

st2 --version
st2 2.8.1, on Python 2.7.5
/opt/stackstorm/virtualenvs/nornir_scirpts/bin/python --version
Python 3.6.3
CentOS Linux 7 

Python code:

#!/usr/bin/env python

from __future__ import absolute_import
import sys
print(sys.path)

Traceback:

Traceback (most recent call last):
  File "/opt/stackstorm/runners/python_runner/python_runner/python_action_wrapper.py", line 21, in <module>
    import traceback
  File "/usr/local/lib/python3.6/traceback.py", line 5, in <module>
    import linecache
  File "/opt/stackstorm/virtualenvs/nornir_network_automation/lib/python3.6/linecache.py", line 11, in <module>
    import tokenize
  File "/opt/stackstorm/virtualenvs/nornir_network_automation/lib/python3.6/tokenize.py", line 33, in <module>
    import re
  File "/opt/stackstorm/virtualenvs/nornir_network_automation/lib/python3.6/re.py", line 142, in <module>
    class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'

Action runner service logs:

[jhuang@chcgilperfops03 st2]$ cat st2actionrunner.24688.log
2018-07-31 03:15:17,573 140698490962320 INFO __init__ [-] Connecting to database "st2" @ "127.0.0.1:27017" as user "stackstorm".
2018-07-31 03:15:17,635 140698490962320 INFO __init__ [-] Successfully connected to database "st2" @ "127.0.0.1:27017" as user "stackstorm".
2018-07-31 03:15:18,005 140698490962320 INFO actionrunner [-] (PID=24688) Worker started.
2018-07-31 03:15:18,006 140698490962320 INFO consumers [-] Starting ActionExecutionScheduler...
2018-07-31 03:15:18,006 140698490962320 INFO consumers [-] Starting ActionExecutionDispatcher...
2018-07-31 03:15:18,045 140698312063248 INFO mixins [-] Connected to amqp://guest:**@127.0.0.1:5672//
2018-07-31 03:15:18,045 140698312062608 INFO mixins [-] Connected to amqp://guest:**@127.0.0.1:5672//
2018-07-31 03:16:44,121 140698312062768 AUDIT action [-] The status of action execution is changed from requested to scheduled. <LiveAction.id=5b5fd49ca4499d613f0d38e6, ActionExecution.id=5b5fd49ca4499d613f0d38e7> (liveaction_db={'status': 'scheduled', 'runner_info': {}, 'workflow_execution': None, 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'action_is_workflow': False, 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'callback': {}, 'task_execution': None, 'notify': None, 'result': {}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'action': u'nornir_scirpts.print_syspath', 'id': '5b5fd49ca4499d613f0d38e6', 'end_timestamp': None},action_execution_db={'status': u'scheduled', 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'web_url': u'https://chcgilperfops03.gogoeng.prod/#/history/5b5fd49ca4499d613f0d38e7/general', 'log': [{u'status': u'requested', u'timestamp': datetime.datetime(2018, 7, 31, 3, 16, 44, 42000, tzinfo=<bson.tz_util.FixedOffset object at 0x7ff6e0b92fd0>)}, {u'status': u'scheduled', u'timestamp': datetime.datetime(2018, 7, 31, 3, 16, 44, 109000, tzinfo=<bson.tz_util.FixedOffset object at 0x7ff6e0b92fd0>)}], 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'trigger_type': {}, 'runner': {u'runner_module': u'python_runner', u'uid': u'runner_type:python-script', u'runner_package': u'python_runner', u'description': u'A runner for launching python actions.', u'enabled': True, u'runner_parameters': {u'debug': {u'default': False, u'required': False, u'type': u'boolean', u'description': u'Enable runner debug mode.'}, u'content_version': {u'required': False, u'type': u'string', u'description': u'Git revision of the pack content to use for this action execution (git commit sha / tag / branch). Only applies to packs which are git repositories.'}, u'log_level': {u'default': u'DEBUG', u'enum': [u'AUDIT', u'CRITICAL', u'ERROR', u'WARNING', u'INFO', u'DEBUG'], u'type': u'string', u'description': u'Default log level for Python runner actions.'}, u'env': {u'type': u'object', u'description': u'Environment variables which will be available to the script.'}, u'timeout': {u'default': 600, u'type': u'integer', u'description': u"Action timeout in seconds. Action will get killed if it doesn't finish in timeout seconds."}}, u'id': u'5b5bbc79a4499d5c24c3553d', u'name': u'python-script'}, 'trigger_instance': {}, 'rule': {}, 'children': [], 'task_execution': None, 'trigger': {}, 'parent': None, 'result': {}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'workflow_execution': None, 'action': {u'description': u'python_script', u'parameters': {}, u'tags': [], u'enabled': True, u'name': u'print_syspath', u'entry_point': u'syspath.py', u'notify': {}, u'uid': u'action:nornir_scirpts:print_syspath', u'pack': u'nornir_scirpts', u'ref': u'nornir_scirpts.print_syspath', u'id': u'5b5f90d7a4499d0dc6523a3e', u'runner_type': u'python-script'}, 'liveaction': {u'runner_info': {}, u'parameters': {u'debug': True, u'log_level': u'DEBUG'}, u'action_is_workflow': False, u'callback': {}, u'action': u'nornir_scirpts.print_syspath', u'id': u'5b5fd49ca4499d613f0d38e6'}, 'id': '5b5fd49ca4499d613f0d38e7', 'end_timestamp': None})
2018-07-31 03:16:44,150 140698312062768 INFO action [-] The status of action execution is changed from requested to scheduled. <LiveAction.id=5b5fd49ca4499d613f0d38e6, ActionExecution.id=5b5fd49ca4499d613f0d38e7>
2018-07-31 03:16:44,183 140698312062768 AUDIT worker [-] Launching action execution. (liveaction_db={'status': 'running', 'runner_info': {'hostname': 'chcgilperfops03.gogoeng.prod', 'pid': 24688}, 'workflow_execution': None, 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'action_is_workflow': False, 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'callback': {}, 'task_execution': None, 'notify': None, 'result': {}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'action': u'nornir_scirpts.print_syspath', 'id': '5b5fd49ca4499d613f0d38e6', 'end_timestamp': None},action_execution_db={'status': u'running', 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'web_url': u'https://chcgilperfops03.gogoeng.prod/#/history/5b5fd49ca4499d613f0d38e7/general', 'log': [{u'status': u'requested', u'timestamp': datetime.datetime(2018, 7, 31, 3, 16, 44, 42000, tzinfo=<bson.tz_util.FixedOffset object at 0x7ff6e0bb3410>)}, {u'status': u'scheduled', u'timestamp': datetime.datetime(2018, 7, 31, 3, 16, 44, 109000, tzinfo=<bson.tz_util.FixedOffset object at 0x7ff6e0bb3410>)}, {u'status': u'running', u'timestamp': datetime.datetime(2018, 7, 31, 3, 16, 44, 173000, tzinfo=<bson.tz_util.FixedOffset object at 0x7ff6e0bb3410>)}], 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'trigger_type': {}, 'runner': {u'runner_module': u'python_runner', u'uid': u'runner_type:python-script', u'runner_package': u'python_runner', u'description': u'A runner for launching python actions.', u'enabled': True, u'runner_parameters': {u'debug': {u'default': False, u'required': False, u'type': u'boolean', u'description': u'Enable runner debug mode.'}, u'content_version': {u'required': False, u'type': u'string', u'description': u'Git revision of the pack content to use for this action execution (git commit sha / tag / branch). Only applies to packs which are git repositories.'}, u'log_level': {u'default': u'DEBUG', u'enum': [u'AUDIT', u'CRITICAL', u'ERROR', u'WARNING', u'INFO', u'DEBUG'], u'type': u'string', u'description': u'Default log level for Python runner actions.'}, u'env': {u'type': u'object', u'description': u'Environment variables which will be available to the script.'}, u'timeout': {u'default': 600, u'type': u'integer', u'description': u"Action timeout in seconds. Action will get killed if it doesn't finish in timeout seconds."}}, u'id': u'5b5bbc79a4499d5c24c3553d', u'name': u'python-script'}, 'trigger_instance': {}, 'rule': {}, 'children': [], 'task_execution': None, 'trigger': {}, 'parent': None, 'result': {}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'workflow_execution': None, 'action': {u'description': u'python_script', u'parameters': {}, u'tags': [], u'enabled': True, u'name': u'print_syspath', u'entry_point': u'syspath.py', u'notify': {}, u'uid': u'action:nornir_scirpts:print_syspath', u'pack': u'nornir_scirpts', u'ref': u'nornir_scirpts.print_syspath', u'id': u'5b5f90d7a4499d0dc6523a3e', u'runner_type': u'python-script'}, 'liveaction': {u'runner_info': {u'hostname': u'chcgilperfops03.gogoeng.prod', u'pid': 24688}, u'parameters': {u'debug': True, u'log_level': u'DEBUG'}, u'action_is_workflow': False, u'callback': {}, u'action': u'nornir_scirpts.print_syspath', u'id': u'5b5fd49ca4499d613f0d38e6'}, 'id': '5b5fd49ca4499d613f0d38e7', 'end_timestamp': None})
2018-07-31 03:16:44,218 140698312062768 INFO worker [-] Dispatched {~}action_execution: 5b5fd49ca4499d613f0d38e7 / {~}live_action: 5b5fd49ca4499d613f0d38e6 with "running" status.
2018-07-31 03:16:44,224 140698312062768 INFO base [-] Dispatching Action to a runner (runner_type_db={'runner_module': u'python_runner', 'uid': u'runner_type:python-script', 'name': u'python-script', 'runner_package': u'python_runner', 'enabled': True, 'query_module': None, 'runner_parameters': {u'debug': {u'default': False, u'required': False, u'type': u'boolean', u'description': u'Enable runner debug mode.'}, u'content_version': {u'required': False, u'type': u'string', u'description': u'Git revision of the pack content to use for this action execution (git commit sha / tag / branch). Only applies to packs which are git repositories.'}, u'log_level': {u'default': u'DEBUG', u'enum': [u'AUDIT', u'CRITICAL', u'ERROR', u'WARNING', u'INFO', u'DEBUG'], u'type': u'string', u'description': u'Default log level for Python runner actions.'}, u'env': {u'type': u'object', u'description': u'Environment variables which will be available to the script.'}, u'timeout': {u'default': 600, u'type': u'integer', u'description': u"Action timeout in seconds. Action will get killed if it doesn't finish in timeout seconds."}}, 'id': '5b5bbc79a4499d5c24c3553d', 'description': u'A runner for launching python actions.'},liveaction_db={'status': 'running', 'runner_info': {'hostname': 'chcgilperfops03.gogoeng.prod', 'pid': 24688}, 'workflow_execution': None, 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'action_is_workflow': False, 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'callback': {}, 'task_execution': None, 'notify': None, 'result': {}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'action': u'nornir_scirpts.print_syspath', 'id': '5b5fd49ca4499d613f0d38e6', 'end_timestamp': None})
2018-07-31 03:16:44,243 140698312062768 INFO loader [-] Loading runner module from "/opt/stackstorm/runners/python_runner/python_runner/python_runner.py".
2018-07-31 03:16:44,254 140698312062768 AUDIT access [-] Access granted to "st2admin" with the token set to expire at "2018-08-01T03:16:44.253477Z". (username=u'st2admin',token_expiration='2018-08-01T03:16:44.253477Z')
2018-07-31 03:16:44,258 140698312062768 WARNING api [-] "auth.api_url" configuration option is not configured
2018-07-31 03:16:44,258 140698312062768 WARNING api [-] "auth.api_url" configuration option is not configured
2018-07-31 03:16:44,358 140698312062768 WARNING python_runner [-] Failed to de-serialize result "None": expected string or buffer
2018-07-31 03:16:44,389 140698312062768 AUDIT base [-] Liveaction completed (liveaction_db={'status': 'failed', 'runner_info': {u'hostname': u'chcgilperfops03.gogoeng.prod', u'pid': 24688}, 'workflow_execution': None, 'parameters': {u'debug': True, u'log_level': u'DEBUG'}, 'action_is_workflow': False, 'start_timestamp': '2018-07-31 03:16:44.031922+00:00', 'callback': {}, 'task_execution': None, 'notify': None, 'result': {'stdout': '', 'exit_code': 1, 'stderr': u'Traceback (most recent call last):\n  File "/opt/stackstorm/runners/python_runner/python_runner/python_action_wrapper.py", line 21, in <module>\n    import traceback\n  File "/usr/local/lib/python3.6/traceback.py", line 5, in <module>\n    import linecache\n  File "/opt/stackstorm/virtualenvs/nornir_scirpts/lib/python3.6/linecache.py", line 11, in <module>\n    import tokenize\n  File "/opt/stackstorm/virtualenvs/nornir_scirpts/lib/python3.6/tokenize.py", line 33, in <module>\n    import re\n  File "/opt/stackstorm/virtualenvs/nornir_scirpts/lib/python3.6/re.py", line 142, in <module>\n    class RegexFlag(enum.IntFlag):\nAttributeError: module \'enum\' has no attribute \'IntFlag\'\n', 'result': 'None'}, 'context': {u'trace_context': {}, u'user': u'st2admin', u'pack': u'nornir_scirpts'}, 'action': u'nornir_scirpts.print_syspath', 'id': '5b5fd49ca4499d613f0d38e6', 'end_timestamp': '2018-07-31 03:16:44.361734+00:00'})

(AutoJunjie) #8

Also I tried to switch to the virtual env, and i can see enum actually has intFlag as an attribute

Python 3.6.3 (default, May 15 2018, 19:56:00) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> dir(enum)     
['DynamicClassAttribute', 'Enum', 'EnumMeta', 'Flag', 'IntEnum', 'IntFlag', 'MappingProxyType', 'OrderedDict', '_EnumDict', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_auto_null', '_decompose', '_high_bit', '_is_descriptor', '_is_dunder', '_is_sunder', '_make_class_unpicklable', '_or_', '_power_of_two', '_reduce_ex_by_name', 'auto', 'reduce', 'sys', 'unique']
>>> ```

(Tomaz Muraus) #9

I’m still unable to reproduce this issue locally on Ubuntu. I wonder if it’s related to RHEL and PYTHONPATH for Python runner actions not being set correctly or similar.

Can you please run python runner action with the following content and gist the output:

import os
import sys
import platform

from st2common.runners.base_action import Action


class PrintPythonEnvironmentAction(Action):

    def run(self):
        print('Using Python executable: %s' % (sys.executable))
        print('Using Python version: %s' % (sys.version))
        print('Platform: %s' % (platform.platform()))
        print('PYTHONPATH: %s' % (os.environ.get('PYTHONPATH')))

This should provide more information about the environment under which Python runner action is executed from and hopefully help us track down the root cause.


(Tomaz Muraus) #10

Just a heads up - I was able to replicate the issue on RHEL 7 today.

It looks like it’s related to RHEL 7 setup. I will look into a fix this week.


(Chnic888) #11

Nice to hear that :grinning:
btw: I also encountered this issue on CentOS 7.

cat /etc/os-release

NAME=“CentOS Linux”

VERSION=“7 (Core)”

ID=“centos”

ID_LIKE=“rhel fedora”

VERSION_ID=“7”

PRETTY_NAME=“CentOS Linux 7 (Core)”

ANSI_COLOR=“0;31”

CPE_NAME=“cpe:/o:centos:centos:7”

HOME_URL=“https://www.centos.org/

BUG_REPORT_URL=“https://bugs.centos.org/

CENTOS_MANTISBT_PROJECT=“CentOS-7”

CENTOS_MANTISBT_PROJECT_VERSION=“7”

REDHAT_SUPPORT_PRODUCT=“centos”

REDHAT_SUPPORT_PRODUCT_VERSION=“7”


(Tomaz Muraus) #12

The fix for this issue is now available here - Fix for "module 'enum' has no attribute 'IntFlag'" error when using python 3 for pack virtual environments under RHEL / CentOS by Kami · Pull Request #4297 · StackStorm/st2 · GitHub.

It would also be great if you could also verify / test that change on your StackStorm setup (the simplest way is to directly edit that file on your installation - /opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/sandboxing.py).