|
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
|
|
|
from st2common.exceptions.actionalias import ActionAliasAmbiguityException |
|
17
|
|
|
from st2common.models.utils.action_alias_utils import extract_parameters |
|
18
|
|
|
from st2common.util.actionalias_matching import match_command_to_alias |
|
19
|
|
|
|
|
20
|
|
|
from st2client import models |
|
21
|
|
|
from st2client.models.action_alias import ActionAlias |
|
22
|
|
|
from st2client.commands.action import ActionRunCommandMixin |
|
23
|
|
|
from st2client.commands import resource |
|
24
|
|
|
from st2client.formatters import table |
|
25
|
|
|
|
|
26
|
|
|
__all__ = [ |
|
27
|
|
|
'ActionAliasBranch' |
|
28
|
|
|
] |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class ActionAliasBranch(resource.ResourceBranch): |
|
32
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
|
33
|
|
|
super(ActionAliasBranch, self).__init__( |
|
34
|
|
|
ActionAlias, description, app, subparsers, |
|
35
|
|
|
parent_parser=parent_parser, read_only=False, |
|
36
|
|
|
commands={ |
|
37
|
|
|
'list': ActionAliasListCommand, |
|
38
|
|
|
'get': ActionAliasGetCommand |
|
39
|
|
|
}) |
|
40
|
|
|
|
|
41
|
|
|
self.commands['match'] = ActionAliasMatchCommand( |
|
42
|
|
|
self.resource, self.app, self.subparsers, |
|
43
|
|
|
add_help=False) |
|
44
|
|
|
self.commands['execute'] = ActionAliasExecuteCommand( |
|
45
|
|
|
self.resource, self.app, self.subparsers, |
|
46
|
|
|
add_help=False) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
class ActionAliasListCommand(resource.ContentPackResourceListCommand): |
|
50
|
|
|
display_attributes = ['ref', 'pack', 'description', 'enabled'] |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
class ActionAliasGetCommand(resource.ContentPackResourceGetCommand): |
|
54
|
|
|
display_attributes = ['all'] |
|
55
|
|
|
attribute_display_order = ['id', 'ref', 'pack', 'name', 'description', |
|
56
|
|
|
'enabled', 'action_ref', 'formats'] |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class ActionAliasMatchCommand(resource.ResourceCommand): |
|
60
|
|
|
display_attributes = ['id', 'name', 'description'] |
|
61
|
|
|
|
|
62
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
63
|
|
|
super(ActionAliasMatchCommand, self).__init__( |
|
64
|
|
|
resource, 'match', |
|
65
|
|
|
'Get the list of %s that match the command text.' % |
|
66
|
|
|
resource.get_plural_display_name().lower(), |
|
67
|
|
|
*args, **kwargs) |
|
68
|
|
|
|
|
69
|
|
|
self.parser.add_argument('match_text', |
|
70
|
|
|
metavar='command', |
|
71
|
|
|
help=help) |
|
72
|
|
|
|
|
73
|
|
|
self.parser.add_argument('-a', '--attr', nargs='+', |
|
74
|
|
|
default=self.display_attributes, |
|
75
|
|
|
help=('List of attributes to include in the ' |
|
76
|
|
|
'output. "all" will return all ' |
|
77
|
|
|
'attributes.')) |
|
78
|
|
|
self.parser.add_argument('-w', '--width', nargs='+', type=int, |
|
79
|
|
|
default=None, |
|
80
|
|
|
help=('Set the width of columns in output.')) |
|
81
|
|
|
|
|
82
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
83
|
|
|
def run(self, args, **kwargs): |
|
84
|
|
|
aliases = self.manager.get_all(**kwargs) |
|
85
|
|
|
matches = match_command_to_alias(args.match_text, aliases) |
|
86
|
|
|
return [match[0] for match in matches] # show only alias objects |
|
87
|
|
|
|
|
88
|
|
|
def run_and_print(self, args, **kwargs): |
|
89
|
|
|
instances = self.run(args, **kwargs) |
|
90
|
|
|
self.print_output(instances, table.MultiColumnTable, |
|
91
|
|
|
attributes=args.attr, widths=args.width, |
|
92
|
|
|
json=args.json, yaml=args.yaml) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
class ActionAliasExecuteCommand(ActionRunCommandMixin, |
|
96
|
|
|
resource.ResourceCommand): |
|
97
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
98
|
|
|
super(ActionAliasExecuteCommand, self).__init__( |
|
99
|
|
|
resource, 'execute', |
|
100
|
|
|
'Execute the command text by finding a matching ActionAlias.', |
|
101
|
|
|
*args, **kwargs) |
|
102
|
|
|
|
|
103
|
|
|
self.parser.add_argument('command_text', |
|
104
|
|
|
metavar='command', |
|
105
|
|
|
help=help) |
|
106
|
|
|
self.parser.add_argument('-h', '--help', |
|
107
|
|
|
action='store_true', dest='help', |
|
108
|
|
|
help='Print usage for the given action.') |
|
109
|
|
|
self.parser.add_argument('--trace-tag', '--trace_tag', |
|
110
|
|
|
help='A trace tag string to track execution later.', |
|
111
|
|
|
dest='trace_tag', required=False) |
|
112
|
|
|
self.parser.add_argument('--trace-id', |
|
113
|
|
|
help='Existing trace id for this execution.', |
|
114
|
|
|
dest='trace_id', required=False) |
|
115
|
|
|
self.parser.add_argument('-a', '--async', |
|
116
|
|
|
action='store_true', dest='async', |
|
117
|
|
|
help='Do not wait for action to finish.') |
|
118
|
|
|
self.parser.add_argument('-u', '--user', type=str, default=None, |
|
119
|
|
|
help='User under which to run the action (admins only).') |
|
120
|
|
|
|
|
121
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
122
|
|
|
def run(self, args, **kwargs): |
|
123
|
|
|
aliases = self.manager.get_all(**kwargs) |
|
124
|
|
|
matches = match_command_to_alias(args.command_text, aliases) |
|
125
|
|
|
if len(matches) > 1: |
|
126
|
|
|
raise ActionAliasAmbiguityException("Too many matches for provided command", |
|
127
|
|
|
matches=matches) |
|
128
|
|
|
elif len(matches) == 0: |
|
129
|
|
|
raise ActionAliasAmbiguityException("Could not locate an ActionAlias with a " |
|
130
|
|
|
"matching format command", matches=matches) |
|
131
|
|
|
match = matches[0] |
|
132
|
|
|
action_alias_db = match[0] |
|
133
|
|
|
|
|
134
|
|
|
if not action_alias_db.enabled: |
|
135
|
|
|
raise ValueError('Action alias with name "%s" is disabled.' % |
|
136
|
|
|
(action_alias_db.ref)) |
|
137
|
|
|
|
|
138
|
|
|
execution_parameters = extract_parameters( |
|
139
|
|
|
format_str=match[2], |
|
140
|
|
|
param_stream=args.command_text) |
|
141
|
|
|
|
|
142
|
|
|
execution = models.LiveAction() |
|
143
|
|
|
execution.action = action_alias_db.action_ref |
|
144
|
|
|
execution.parameters = execution_parameters |
|
145
|
|
|
execution.user = args.user |
|
146
|
|
|
|
|
147
|
|
|
if not args.trace_id and args.trace_tag: |
|
148
|
|
|
execution.context = {'trace_context': {'trace_tag': args.trace_tag}} |
|
149
|
|
|
|
|
150
|
|
|
if args.trace_id: |
|
151
|
|
|
execution.context = {'trace_context': {'id_': args.trace_id}} |
|
152
|
|
|
|
|
153
|
|
|
action_exec_mgr = self.app.client.managers['LiveAction'] |
|
154
|
|
|
|
|
155
|
|
|
execution = action_exec_mgr.create(execution, **kwargs) |
|
156
|
|
|
execution = self._get_execution_result(execution=execution, |
|
157
|
|
|
action_exec_mgr=action_exec_mgr, |
|
158
|
|
|
args=args, **kwargs) |
|
159
|
|
|
return execution |
|
160
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: