| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import logging |
| 19 |
import os.path |
| 20 |
|
| 21 |
from oslo_serialization import jsonutils |
| 22 |
|
| 23 |
from osc_lib.command import command |
| 24 |
|
| 25 |
from mistralclient.commands.v2 import base |
| 26 |
from mistralclient.commands.v2 import executions |
| 27 |
from mistralclient import utils |
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
|
| 32 |
class TaskFormatter(base.MistralFormatter): |
| 33 |
COLUMNS = [ |
| 34 |
('id', 'ID'), |
| 35 |
('name', 'Name'), |
| 36 |
('workflow_name', 'Workflow name'), |
| 37 |
('workflow_namespace', 'Workflow namespace'), |
| 38 |
('workflow_execution_id', 'Workflow Execution ID'), |
| 39 |
('state', 'State'), |
| 40 |
('state_info', 'State info'), |
| 41 |
('created_at', 'Created at'), |
| 42 |
('started_at', 'Started at'), |
| 43 |
('finished_at', 'Finished at'), |
| 44 |
('duration', 'Duration', True), |
| 45 |
] |
| 46 |
|
| 47 |
@staticmethod |
| 48 |
def format(task=None, lister=False): |
| 49 |
if task: |
| 50 |
state_info = (task.state_info if not lister |
| 51 |
else base.cut(task.state_info)) |
| 52 |
|
| 53 |
duration = base.get_duration_str(task.started_at, task.finished_at) |
| 54 |
|
| 55 |
data = ( |
| 56 |
task.id, |
| 57 |
task.name, |
| 58 |
task.workflow_name, |
| 59 |
task.workflow_namespace, |
| 60 |
task.workflow_execution_id, |
| 61 |
task.state, |
| 62 |
state_info, |
| 63 |
task.created_at, |
| 64 |
task.started_at or '<none>', |
| 65 |
task.finished_at or '<none>', |
| 66 |
duration |
| 67 |
) |
| 68 |
else: |
| 69 |
data = (tuple('' for _ in range(len(TaskFormatter.COLUMNS))),) |
| 70 |
|
| 71 |
return TaskFormatter.headings(), data |
| 72 |
|
| 73 |
|
| 74 |
class List(base.MistralExecutionLister): |
| 75 |
"""List all tasks.""" |
| 76 |
|
| 77 |
def get_parser(self, prog_name): |
| 78 |
parser = super(List, self).get_parser(prog_name) |
| 79 |
|
| 80 |
parser.add_argument( |
| 81 |
'workflow_execution', |
| 82 |
nargs='?', |
| 83 |
help='Workflow execution ID associated with list of Tasks.' |
| 84 |
) |
| 85 |
return parser |
| 86 |
|
| 87 |
def _get_format_function(self): |
| 88 |
return TaskFormatter.format_list |
| 89 |
|
| 90 |
def _get_resources(self, parsed_args): |
| 91 |
mistral_client = self.app.client_manager.workflow_engine |
| 92 |
|
| 93 |
return mistral_client.tasks.list( |
| 94 |
parsed_args.workflow_execution, |
| 95 |
marker=parsed_args.marker, |
| 96 |
limit=parsed_args.limit, |
| 97 |
sort_keys=parsed_args.sort_keys, |
| 98 |
sort_dirs=parsed_args.sort_dirs, |
| 99 |
fields=TaskFormatter.fields(), |
| 100 |
**base.get_filters(parsed_args) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
class Get(command.ShowOne): |
| 105 |
"""Show specific task.""" |
| 106 |
|
| 107 |
def get_parser(self, prog_name): |
| 108 |
parser = super(Get, self).get_parser(prog_name) |
| 109 |
|
| 110 |
parser.add_argument('task', help='Task identifier') |
| 111 |
|
| 112 |
return parser |
| 113 |
|
| 114 |
def take_action(self, parsed_args): |
| 115 |
mistral_client = self.app.client_manager.workflow_engine |
| 116 |
execution = mistral_client.tasks.get(parsed_args.task) |
| 117 |
|
| 118 |
return TaskFormatter.format(execution) |
| 119 |
|
| 120 |
|
| 121 |
class GetResult(command.Command): |
| 122 |
"""Show task output data.""" |
| 123 |
|
| 124 |
def get_parser(self, prog_name): |
| 125 |
parser = super(GetResult, self).get_parser(prog_name) |
| 126 |
parser.add_argument( |
| 127 |
'id', |
| 128 |
help='Task ID') |
| 129 |
|
| 130 |
return parser |
| 131 |
|
| 132 |
def take_action(self, parsed_args): |
| 133 |
mistral_client = self.app.client_manager.workflow_engine |
| 134 |
result = mistral_client.tasks.get(parsed_args.id).result |
| 135 |
|
| 136 |
try: |
| 137 |
result = jsonutils.loads(result) |
| 138 |
result = jsonutils.dumps(result, indent=4) + "\n" |
| 139 |
except Exception: |
| 140 |
LOG.debug("Task result is not JSON.") |
| 141 |
|
| 142 |
self.app.stdout.write(result or "\n") |
| 143 |
|
| 144 |
|
| 145 |
class GetPublished(command.Command): |
| 146 |
"""Show task published variables.""" |
| 147 |
|
| 148 |
def get_parser(self, prog_name): |
| 149 |
parser = super(GetPublished, self).get_parser(prog_name) |
| 150 |
parser.add_argument( |
| 151 |
'id', |
| 152 |
help='Task ID') |
| 153 |
|
| 154 |
return parser |
| 155 |
|
| 156 |
def take_action(self, parsed_args): |
| 157 |
mistral_client = self.app.client_manager.workflow_engine |
| 158 |
res = mistral_client.tasks.get(parsed_args.id) |
| 159 |
published = res.published |
| 160 |
published_glob = res.published_global \ |
| 161 |
if hasattr(res, 'published_global') else None |
| 162 |
|
| 163 |
try: |
| 164 |
published = jsonutils.loads(published) |
| 165 |
published = jsonutils.dumps(published, indent=4) + "\n" |
| 166 |
|
| 167 |
if published_glob: |
| 168 |
published_glob = jsonutils.loads(published_glob) |
| 169 |
published += jsonutils.dumps(published_glob, indent=4) + "\n" |
| 170 |
except Exception: |
| 171 |
LOG.debug("Task result is not JSON.") |
| 172 |
|
| 173 |
self.app.stdout.write(published or "\n") |
| 174 |
|
| 175 |
|
| 176 |
class Rerun(command.ShowOne): |
| 177 |
"""Rerun an existing task.""" |
| 178 |
|
| 179 |
def get_parser(self, prog_name): |
| 180 |
parser = super(Rerun, self).get_parser(prog_name) |
| 181 |
|
| 182 |
parser.add_argument( |
| 183 |
'id', |
| 184 |
help='Task identifier' |
| 185 |
) |
| 186 |
|
| 187 |
parser.add_argument( |
| 188 |
'--resume', |
| 189 |
action='store_true', |
| 190 |
dest='resume', |
| 191 |
default=False, |
| 192 |
help=('rerun only failed or unstarted action ' |
| 193 |
'executions for with-items task') |
| 194 |
) |
| 195 |
|
| 196 |
parser.add_argument( |
| 197 |
'-e', |
| 198 |
'--env', |
| 199 |
dest='env', |
| 200 |
help='Environment variables' |
| 201 |
) |
| 202 |
|
| 203 |
return parser |
| 204 |
|
| 205 |
def take_action(self, parsed_args): |
| 206 |
mistral_client = self.app.client_manager.workflow_engine |
| 207 |
|
| 208 |
env = ( |
| 209 |
utils.load_file(parsed_args.env) |
| 210 |
if parsed_args.env and os.path.isfile(parsed_args.env) |
| 211 |
else utils.load_content(parsed_args.env) |
| 212 |
) |
| 213 |
|
| 214 |
execution = mistral_client.tasks.rerun( |
| 215 |
parsed_args.id, |
| 216 |
reset=(not parsed_args.resume), |
| 217 |
env=env |
| 218 |
) |
| 219 |
|
| 220 |
return TaskFormatter.format(execution) |
| 221 |
|
| 222 |
|
| 223 |
class SubExecutionsLister(executions.SubExecutionsBaseLister): |
| 224 |
|
| 225 |
def _get_format_function(self): |
| 226 |
return executions.ExecutionFormatter.format_list |
| 227 |
|
| 228 |
def _get_resources_function(self): |
| 229 |
mistral_client = self.app.client_manager.workflow_engine |
| 230 |
|
| 231 |
return mistral_client.tasks.get_task_sub_executions |