1
|
|
|
import os |
2
|
|
|
|
3
|
|
|
from st2common.runners.base_action import Action |
4
|
|
|
from st2client.models.action_alias import ActionAliasMatch |
5
|
|
|
from st2client.models.aliasexecution import ActionAliasExecution |
6
|
|
|
|
7
|
|
|
from st2client.client import Client |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class ExecuteActionAliasAction(Action): |
11
|
|
|
def __init__(self, config=None): |
12
|
|
|
super(ExecuteActionAliasAction, self).__init__(config=config) |
13
|
|
|
api_url = os.environ.get('ST2_ACTION_API_URL', None) |
14
|
|
|
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None) |
15
|
|
|
self.client = Client(api_url=api_url, token=token) |
16
|
|
|
|
17
|
|
|
def run(self, text, source_channel=None, user=None): |
18
|
|
|
alias_match = ActionAliasMatch() |
19
|
|
|
alias_match.command = text |
20
|
|
|
alias, representation = self.client.managers['ActionAlias'].match( |
21
|
|
|
alias_match) |
22
|
|
|
|
23
|
|
|
execution = ActionAliasExecution() |
24
|
|
|
execution.name = alias.name |
25
|
|
|
execution.format = representation |
26
|
|
|
execution.command = text |
27
|
|
|
execution.source_channel = source_channel # ? |
28
|
|
|
execution.notification_channel = None |
29
|
|
|
execution.notification_route = None |
30
|
|
|
execution.user = user |
31
|
|
|
|
32
|
|
|
action_exec_mgr = self.app.client.managers['ActionAliasExecution'] |
33
|
|
|
|
34
|
|
|
execution = action_exec_mgr.create(execution) |
35
|
|
|
return execution.execution['id'] |
36
|
|
|
|