1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import uuid |
17
|
|
|
|
18
|
|
|
from st2common import log as logging |
19
|
|
|
from st2common.constants.action import LIVEACTION_STATUS_PENDING |
20
|
|
|
from st2common.constants.triggers import INQUIRY_TRIGGER |
21
|
|
|
from st2common.models.system.common import ResourceReference |
22
|
|
|
from st2common.persistence.execution import ActionExecution |
23
|
|
|
from st2common.runners.base import ActionRunner |
24
|
|
|
from st2common.services import action as action_service |
25
|
|
|
from st2common.transport.reactor import TriggerDispatcher |
26
|
|
|
from st2common.util import action_db as action_utils |
27
|
|
|
|
28
|
|
|
LOG = logging.getLogger(__name__) |
29
|
|
|
|
30
|
|
|
__all__ = [ |
31
|
|
|
'get_runner', |
32
|
|
|
'Inquirer', |
33
|
|
|
] |
34
|
|
|
|
35
|
|
|
# constants to lookup in runner_parameters. |
36
|
|
|
RUNNER_SCHEMA = 'schema' |
37
|
|
|
RUNNER_ROLES = 'roles' |
38
|
|
|
RUNNER_USERS = 'users' |
39
|
|
|
RUNNER_ROUTE = 'route' |
40
|
|
|
RUNNER_TTL = 'ttl' |
41
|
|
|
|
42
|
|
|
DEFAULT_SCHEMA = { |
43
|
|
|
"title": "response_data", |
44
|
|
|
"type": "object", |
45
|
|
|
"properties": { |
46
|
|
|
"continue": { |
47
|
|
|
"type": "boolean", |
48
|
|
|
"description": "Would you like to continue the workflow?", |
49
|
|
|
"required": True |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def get_runner(): |
56
|
|
|
return Inquirer(str(uuid.uuid4())) |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
class Inquirer(ActionRunner): |
60
|
|
|
"""This runner implements the ability to ask for more input during a workflow |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
def __init__(self, runner_id): |
64
|
|
|
super(Inquirer, self).__init__(runner_id=runner_id) |
65
|
|
|
self.trigger_dispatcher = TriggerDispatcher(LOG) |
66
|
|
|
|
67
|
|
|
def pre_run(self): |
68
|
|
|
super(Inquirer, self).pre_run() |
69
|
|
|
|
70
|
|
|
# TODO :This is awful, but the way "runner_parameters" and other variables get |
71
|
|
|
# assigned on the runner instance is even worse. Those arguments should |
72
|
|
|
# be passed to the constructor. |
73
|
|
|
self.schema = self.runner_parameters.get(RUNNER_SCHEMA, DEFAULT_SCHEMA) |
74
|
|
|
self.roles_param = self.runner_parameters.get(RUNNER_ROLES, []) |
75
|
|
|
self.users_param = self.runner_parameters.get(RUNNER_USERS, []) |
76
|
|
|
self.route = self.runner_parameters.get(RUNNER_ROUTE, "") |
77
|
|
|
self.ttl = self.runner_parameters.get(RUNNER_TTL, 1440) |
78
|
|
|
|
79
|
|
|
def run(self, action_parameters): |
80
|
|
|
|
81
|
|
|
liveaction_db = action_utils.get_liveaction_by_id(self.liveaction_id) |
82
|
|
|
exc = ActionExecution.get(liveaction__id=str(liveaction_db.id)) |
83
|
|
|
|
84
|
|
|
# Assemble and dispatch trigger |
85
|
|
|
trigger_ref = ResourceReference.to_string_reference( |
86
|
|
|
pack=INQUIRY_TRIGGER['pack'], |
87
|
|
|
name=INQUIRY_TRIGGER['name'] |
88
|
|
|
) |
89
|
|
|
trigger_payload = { |
90
|
|
|
"id": str(exc.id), |
91
|
|
|
"route": self.route |
92
|
|
|
} |
93
|
|
|
self.trigger_dispatcher.dispatch(trigger_ref, trigger_payload) |
94
|
|
|
|
95
|
|
|
# We only want to request a pause if this has a parent |
96
|
|
|
if liveaction_db.context.get("parent"): |
97
|
|
|
|
98
|
|
|
# Get the root liveaction and request that it pauses |
99
|
|
|
root_liveaction = action_service.get_root_liveaction(liveaction_db) |
100
|
|
|
action_service.request_pause( |
101
|
|
|
root_liveaction, |
102
|
|
|
self.context.get('user', None) |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
result = { |
106
|
|
|
"schema": self.schema, |
107
|
|
|
"roles": self.roles_param, |
108
|
|
|
"users": self.users_param, |
109
|
|
|
"route": self.route, |
110
|
|
|
"ttl": self.ttl |
111
|
|
|
} |
112
|
|
|
return (LIVEACTION_STATUS_PENDING, result, None) |
113
|
|
|
|