| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import logging |
| 19 |
|
| 20 |
from oslo_serialization import jsonutils |
| 21 |
|
| 22 |
from osc_lib.command import command |
| 23 |
|
| 24 |
from mistralclient.commands.v2 import base |
| 25 |
from mistralclient import utils |
| 26 |
|
| 27 |
LOG = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
class ActionExecutionFormatter(base.MistralFormatter): |
| 31 |
COLUMNS = [ |
| 32 |
('id', 'ID'), |
| 33 |
('name', 'Name'), |
| 34 |
('workflow_name', 'Workflow name'), |
| 35 |
('workflow_namespace', 'Workflow namespace'), |
| 36 |
('task_name', 'Task name'), |
| 37 |
('task_execution_id', 'Task ID'), |
| 38 |
('state', 'State'), |
| 39 |
('state_info', 'State info'), |
| 40 |
('accepted', 'Accepted'), |
| 41 |
('created_at', 'Created at'), |
| 42 |
('updated_at', 'Updated at'), |
| 43 |
] |
| 44 |
|
| 45 |
LIST_COLUMN_FIELD_NAMES = [c[0] for c in COLUMNS if c[0] != 'state_info'] |
| 46 |
LIST_COLUMN_HEADING_NAMES = [c[1] for c in COLUMNS if c[0] != 'state_info'] |
| 47 |
|
| 48 |
@staticmethod |
| 49 |
def format(action_ex=None, lister=False): |
| 50 |
if lister: |
| 51 |
columns = ActionExecutionFormatter.LIST_COLUMN_HEADING_NAMES |
| 52 |
else: |
| 53 |
columns = ActionExecutionFormatter.headings() |
| 54 |
|
| 55 |
if action_ex: |
| 56 |
if hasattr(action_ex, 'task_name'): |
| 57 |
task_name = action_ex.task_name |
| 58 |
else: |
| 59 |
task_name = None |
| 60 |
|
| 61 |
data = ( |
| 62 |
action_ex.id, |
| 63 |
action_ex.name, |
| 64 |
action_ex.workflow_name, |
| 65 |
action_ex.workflow_namespace, |
| 66 |
task_name, |
| 67 |
action_ex.task_execution_id, |
| 68 |
action_ex.state, |
| 69 |
) |
| 70 |
|
| 71 |
if not lister: |
| 72 |
data += (action_ex.state_info,) |
| 73 |
|
| 74 |
data += ( |
| 75 |
action_ex.accepted, |
| 76 |
action_ex.created_at, |
| 77 |
action_ex.updated_at or '<none>' |
| 78 |
) |
| 79 |
else: |
| 80 |
data = (('',) * len(columns),) |
| 81 |
|
| 82 |
return columns, data |
| 83 |
|
| 84 |
|
| 85 |
class Create(command.ShowOne): |
| 86 |
"""Create new Action execution or just run specific action.""" |
| 87 |
|
| 88 |
def produce_output(self, parsed_args, column_names, data): |
| 89 |
if not column_names: |
| 90 |
return 0 |
| 91 |
|
| 92 |
return super(Create, self).produce_output( |
| 93 |
parsed_args, |
| 94 |
column_names, |
| 95 |
data |
| 96 |
) |
| 97 |
|
| 98 |
def get_parser(self, prog_name): |
| 99 |
parser = super(Create, self).get_parser(prog_name) |
| 100 |
|
| 101 |
parser.add_argument( |
| 102 |
'name', |
| 103 |
help='Action name to execute.' |
| 104 |
) |
| 105 |
parser.add_argument( |
| 106 |
dest='input', |
| 107 |
nargs='?', |
| 108 |
help='Action input.' |
| 109 |
) |
| 110 |
parser.add_argument( |
| 111 |
'-s', |
| 112 |
'--save-result', |
| 113 |
dest='save_result', |
| 114 |
action='store_true', |
| 115 |
help='Save the result into DB.' |
| 116 |
) |
| 117 |
parser.add_argument( |
| 118 |
'--run-sync', |
| 119 |
dest='run_sync', |
| 120 |
action='store_true', |
| 121 |
help='Run the action synchronously.' |
| 122 |
) |
| 123 |
parser.add_argument( |
| 124 |
'-t', |
| 125 |
'--target', |
| 126 |
dest='target', |
| 127 |
help='Action will be executed on <target> executor.' |
| 128 |
) |
| 129 |
parser.add_argument( |
| 130 |
'--namespace', |
| 131 |
nargs='?', |
| 132 |
default='', |
| 133 |
help="Namespace of the action(s).", |
| 134 |
) |
| 135 |
|
| 136 |
return parser |
| 137 |
|
| 138 |
def take_action(self, parsed_args): |
| 139 |
params = {} |
| 140 |
|
| 141 |
if parsed_args.save_result: |
| 142 |
params['save_result'] = parsed_args.save_result |
| 143 |
|
| 144 |
if parsed_args.run_sync: |
| 145 |
params['run_sync'] = parsed_args.run_sync |
| 146 |
|
| 147 |
if parsed_args.target: |
| 148 |
params['target'] = parsed_args.target |
| 149 |
|
| 150 |
action_input = None |
| 151 |
|
| 152 |
if parsed_args.input: |
| 153 |
action_input = utils.load_json(parsed_args.input) |
| 154 |
|
| 155 |
mistral_client = self.app.client_manager.workflow_engine |
| 156 |
|
| 157 |
action_ex = mistral_client.action_executions.create( |
| 158 |
parsed_args.name, |
| 159 |
action_input, |
| 160 |
namespace=parsed_args.namespace, |
| 161 |
**params |
| 162 |
) |
| 163 |
|
| 164 |
if not parsed_args.run_sync and parsed_args.save_result: |
| 165 |
return ActionExecutionFormatter.format(action_ex) |
| 166 |
else: |
| 167 |
self.app.stdout.write("%s\n" % action_ex.output) |
| 168 |
|
| 169 |
return None, None |
| 170 |
|
| 171 |
|
| 172 |
class List(base.MistralExecutionLister): |
| 173 |
"""List all Action executions.""" |
| 174 |
|
| 175 |
def _get_format_function(self): |
| 176 |
return ActionExecutionFormatter.format_list |
| 177 |
|
| 178 |
def get_parser(self, prog_name): |
| 179 |
parser = super(List, self).get_parser(prog_name) |
| 180 |
|
| 181 |
parser.add_argument( |
| 182 |
'task_execution_id', |
| 183 |
nargs='?', |
| 184 |
help='Task execution ID.' |
| 185 |
) |
| 186 |
|
| 187 |
return parser |
| 188 |
|
| 189 |
def _get_resources(self, parsed_args): |
| 190 |
mistral_client = self.app.client_manager.workflow_engine |
| 191 |
|
| 192 |
return mistral_client.action_executions.list( |
| 193 |
parsed_args.task_execution_id, |
| 194 |
marker=parsed_args.marker, |
| 195 |
limit=parsed_args.limit, |
| 196 |
sort_keys=parsed_args.sort_keys, |
| 197 |
sort_dirs=parsed_args.sort_dirs, |
| 198 |
|
| 199 |
|
| 200 |
**base.get_filters(parsed_args) |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
class Get(command.ShowOne): |
| 205 |
"""Show specific Action execution.""" |
| 206 |
|
| 207 |
def get_parser(self, prog_name): |
| 208 |
parser = super(Get, self).get_parser(prog_name) |
| 209 |
|
| 210 |
parser.add_argument('action_execution', help='Action execution ID.') |
| 211 |
|
| 212 |
return parser |
| 213 |
|
| 214 |
def take_action(self, parsed_args): |
| 215 |
mistral_client = self.app.client_manager.workflow_engine |
| 216 |
|
| 217 |
execution = mistral_client.action_executions.get( |
| 218 |
parsed_args.action_execution |
| 219 |
) |
| 220 |
|
| 221 |
return ActionExecutionFormatter.format(execution) |
| 222 |
|
| 223 |
|
| 224 |
class Update(command.ShowOne): |
| 225 |
"""Update specific Action execution.""" |
| 226 |
|
| 227 |
def get_parser(self, prog_name): |
| 228 |
parser = super(Update, self).get_parser(prog_name) |
| 229 |
|
| 230 |
parser.add_argument( |
| 231 |
'id', |
| 232 |
help='Action execution ID.' |
| 233 |
) |
| 234 |
parser.add_argument( |
| 235 |
'--state', |
| 236 |
dest='state', |
| 237 |
choices=['PAUSED', 'RUNNING', 'SUCCESS', 'ERROR', 'CANCELLED'], |
| 238 |
help='Action execution state' |
| 239 |
) |
| 240 |
parser.add_argument( |
| 241 |
'--output', |
| 242 |
dest='output', |
| 243 |
help='Action execution output' |
| 244 |
) |
| 245 |
|
| 246 |
return parser |
| 247 |
|
| 248 |
def take_action(self, parsed_args): |
| 249 |
output = None |
| 250 |
|
| 251 |
if parsed_args.output: |
| 252 |
output = utils.load_json(parsed_args.output) |
| 253 |
|
| 254 |
mistral_client = self.app.client_manager.workflow_engine |
| 255 |
|
| 256 |
execution = mistral_client.action_executions.update( |
| 257 |
parsed_args.id, |
| 258 |
parsed_args.state, |
| 259 |
output |
| 260 |
) |
| 261 |
|
| 262 |
return ActionExecutionFormatter.format(execution) |
| 263 |
|
| 264 |
|
| 265 |
class GetOutput(command.Command): |
| 266 |
"""Show Action execution output data.""" |
| 267 |
|
| 268 |
def get_parser(self, prog_name): |
| 269 |
parser = super(GetOutput, self).get_parser(prog_name) |
| 270 |
|
| 271 |
parser.add_argument( |
| 272 |
'id', |
| 273 |
help='Action execution ID.' |
| 274 |
) |
| 275 |
|
| 276 |
return parser |
| 277 |
|
| 278 |
def take_action(self, parsed_args): |
| 279 |
mistral_client = self.app.client_manager.workflow_engine |
| 280 |
|
| 281 |
output = mistral_client.action_executions.get(parsed_args.id).output |
| 282 |
|
| 283 |
try: |
| 284 |
output = jsonutils.loads(output) |
| 285 |
output = jsonutils.dumps(output, indent=4) + "\n" |
| 286 |
except Exception: |
| 287 |
LOG.debug("Task result is not JSON.") |
| 288 |
|
| 289 |
self.app.stdout.write(output or "\n") |
| 290 |
|
| 291 |
|
| 292 |
class GetInput(command.Command): |
| 293 |
"""Show Action execution input data.""" |
| 294 |
|
| 295 |
def get_parser(self, prog_name): |
| 296 |
parser = super(GetInput, self).get_parser(prog_name) |
| 297 |
|
| 298 |
parser.add_argument( |
| 299 |
'id', |
| 300 |
help='Action execution ID.' |
| 301 |
) |
| 302 |
|
| 303 |
return parser |
| 304 |
|
| 305 |
def take_action(self, parsed_args): |
| 306 |
mistral_client = self.app.client_manager.workflow_engine |
| 307 |
|
| 308 |
result = mistral_client.action_executions.get(parsed_args.id).input |
| 309 |
|
| 310 |
try: |
| 311 |
result = jsonutils.loads(result) |
| 312 |
result = jsonutils.dumps(result, indent=4) + "\n" |
| 313 |
except Exception: |
| 314 |
LOG.debug("Task result is not JSON.") |
| 315 |
|
| 316 |
self.app.stdout.write(result or "\n") |
| 317 |
|
| 318 |
|
| 319 |
class Delete(command.Command): |
| 320 |
"""Delete action execution.""" |
| 321 |
|
| 322 |
def get_parser(self, prog_name): |
| 323 |
parser = super(Delete, self).get_parser(prog_name) |
| 324 |
|
| 325 |
parser.add_argument( |
| 326 |
'action_execution', |
| 327 |
nargs='+', |
| 328 |
help='Id of action execution identifier(s).' |
| 329 |
) |
| 330 |
|
| 331 |
return parser |
| 332 |
|
| 333 |
def take_action(self, parsed_args): |
| 334 |
mistral_client = self.app.client_manager.workflow_engine |
| 335 |
|
| 336 |
utils.do_action_on_many( |
| 337 |
lambda s: mistral_client.action_executions.delete(s), |
| 338 |
parsed_args.action_execution, |
| 339 |
"Request to delete action execution %s has been accepted.", |
| 340 |
"Unable to delete the specified action execution(s)." |
| 341 |
) |