/
workflow.py
362 lines (278 loc) · 11.9 KB
/
workflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-
#
# Copyright 2017-2021 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku workflow commands."""
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
from renku.core import errors
from renku.core.commands.format.workflow import WORKFLOW_FORMATS
from renku.core.commands.view_model import plan_view
from renku.core.commands.view_model.composite_plan import CompositePlanViewModel
from renku.core.management.command_builder import inject
from renku.core.management.command_builder.command import Command
from renku.core.management.interface.client_dispatcher import IClientDispatcher
from renku.core.management.interface.plan_gateway import IPlanGateway
from renku.core.management.interface.project_gateway import IProjectGateway
from renku.core.management.workflow.concrete_execution_graph import ExecutionGraph
from renku.core.management.workflow.value_resolution import CompositePlanValueResolver, ValueResolver
from renku.core.models.workflow.composite_plan import CompositePlan
from renku.core.models.workflow.plan import AbstractPlan, Plan
from renku.core.utils import communication
def _ref(name):
"""Return workflow reference name."""
return "workflows/{0}".format(name)
def _deref(ref):
"""Remove workflows prefix."""
assert ref.startswith("workflows/")
return ref[len("workflows/") :]
def _safe_read_yaml(file: str) -> Dict[str, Any]:
try:
from renku.core.models import jsonld as jsonld
return jsonld.read_yaml(file)
except Exception as e:
raise errors.ParameterError(e)
@inject.autoparams()
def _find_workflow(name_or_id: str, plan_gateway: IPlanGateway) -> AbstractPlan:
workflow = plan_gateway.get_by_id(name_or_id) or plan_gateway.get_by_name(name_or_id)
if not workflow:
raise errors.ParameterError(f'The specified workflow "{name_or_id}" cannot be found.')
return workflow
@inject.autoparams()
def _list_workflows(plan_gateway: IPlanGateway, format: str, columns: List[str]):
"""List or manage workflows with subcommands."""
workflows = plan_gateway.get_newest_plans_by_names()
if format not in WORKFLOW_FORMATS:
raise errors.UsageError(f'Provided format "{format}" is not supported ({", ".join(WORKFLOW_FORMATS.keys())})"')
return WORKFLOW_FORMATS[format](list(map(lambda x: plan_view(x), workflows.values())), columns=columns)
def list_workflows_command():
"""Command to list or manage workflows with subcommands."""
return Command().command(_list_workflows).require_migration().with_database(write=False)
@inject.autoparams()
def _remove_workflow(name, force, plan_gateway: IPlanGateway):
"""Remove the remote named <name>."""
workflows = plan_gateway.get_newest_plans_by_names()
plan = None
if name.startswith("/plans/"):
plan = next(filter(lambda x: x.id == name, workflows.values()), None)
if not plan and name not in workflows:
raise errors.ParameterError(f'The specified workflow is "{name}" is not an active workflow.')
if not force:
prompt_text = f'You are about to remove the following workflow "{name}".' + "\n" + "\nDo you wish to continue?"
communication.confirm(prompt_text, abort=True, warning=True)
plan = plan or workflows[name]
plan._v_immutable = False
plan.invalidated_at = datetime.utcnow()
plan.freeze()
def remove_workflow_command():
"""Command that removes the workflow named <name>."""
return Command().command(_remove_workflow).require_clean().with_database(write=True).with_commit()
def _show_workflow(name_or_id: str):
"""Show the details of a workflow."""
workflow = _find_workflow(name_or_id)
return plan_view(workflow)
def show_workflow_command():
"""Command that the details of a workflow."""
return Command().command(_show_workflow).require_migration().with_database(write=False)
@inject.autoparams()
def _group_workflow(
name: str,
description: str,
mappings: List[str],
defaults: List[str],
links: List[str],
param_descriptions: List[str],
map_inputs: bool,
map_outputs: bool,
map_params: bool,
link_all: bool,
keywords: List[str],
steps: List[str],
plan_gateway: IPlanGateway,
project_gateway: IProjectGateway,
) -> CompositePlan:
"""Group workflows into a CompositePlan."""
if plan_gateway.get_by_name(name):
raise errors.ParameterError(f"Duplicate workflow name: workflow '{name}' already exists.")
child_workflows = []
for workflow_name_or_id in steps:
child_workflow = plan_gateway.get_by_id(workflow_name_or_id)
if not child_workflow:
child_workflow = plan_gateway.get_by_name(workflow_name_or_id)
if not child_workflow:
raise errors.ObjectNotFoundError(workflow_name_or_id)
child_workflows.append(child_workflow)
plan = CompositePlan(
description=description,
id=CompositePlan.generate_id(),
keywords=keywords,
name=name,
plans=child_workflows,
project_id=project_gateway.get_project().id,
)
if mappings:
plan.set_mappings_from_strings(mappings)
if defaults:
plan.set_mapping_defaults(defaults)
if links:
plan.set_links_from_strings(links)
graph = ExecutionGraph(plan)
cycles = graph.cycles
if cycles:
cycles = [map(lambda x: x.name, cycle) for cycle in cycles]
raise errors.GraphCycleError(cycles)
if param_descriptions:
plan.set_mapping_descriptions(param_descriptions)
if map_inputs:
plan.map_all_inputs()
if map_outputs:
plan.map_all_outputs()
if map_params:
plan.map_all_parameters()
if link_all:
# NOTE: propagate values to for linking to use
rv = CompositePlanValueResolver(plan, None)
plan = rv.apply()
graph = ExecutionGraph(plan, virtual_links=True)
cycles = graph.cycles
if cycles:
cycles = [map(lambda x: x.name, cycle) for cycle in cycles]
raise errors.GraphCycleError(cycles)
for virtual_link in graph.virtual_links:
plan.add_link(virtual_link[0], [virtual_link[1]])
plan_gateway.add(plan)
return CompositePlanViewModel.from_composite_plan(plan)
def compose_workflow_command():
"""Command that creates a group of several workflows."""
return (
Command().command(_group_workflow).require_migration().require_clean().with_database(write=True).with_commit()
)
@inject.autoparams()
def _edit_workflow(
name,
new_name: Optional[str],
description: Optional[str],
set_params: List[str],
map_params: List[str],
rename_params: List[str],
describe_params: List[str],
plan_gateway: IPlanGateway,
):
"""Edits a workflow details."""
derived_from = _find_workflow(name)
workflow = derived_from.derive()
if new_name:
workflow.name = new_name
if description:
workflow.description = description
if isinstance(workflow, Plan):
workflow.set_parameters_from_strings(set_params)
def _kv_extract(kv_string):
k, v = kv_string.split("=", maxsplit=1)
v = v.strip(' "')
return k, v
for param_string in rename_params:
name, new_name = _kv_extract(param_string)
for param in workflow.inputs + workflow.outputs + workflow.parameters:
if param.name == name:
param.name = new_name
break
else:
raise errors.ParameterNotFoundError(parameter=name, workflow=workflow.name)
for description_string in describe_params:
name, description = _kv_extract(description_string)
for param in workflow.inputs + workflow.outputs + workflow.parameters:
if param.name == name:
param.description = description
break
else:
raise errors.ParameterNotFoundError(parameter=name, workflow=workflow.name)
elif isinstance(workflow, CompositePlan) and len(map_params):
workflow.set_mappings_from_strings(map_params)
plan_gateway.add(workflow)
return plan_view(workflow)
def edit_workflow_command():
"""Command that edits the properties of a given workflow."""
return Command().command(_edit_workflow).require_clean().with_database(write=True).with_commit()
@inject.autoparams()
def _export_workflow(
name_or_id, client_dispatcher: IClientDispatcher, format: str, output: Optional[str], values: Optional[str]
):
"""Export a workflow to a given format."""
client = client_dispatcher.current_client
workflow = _find_workflow(name_or_id)
if output:
output = Path(output)
if values:
values = _safe_read_yaml(values)
rv = ValueResolver.get(workflow, values)
workflow = rv.apply()
if rv.missing_parameters:
communication.warn(
f'Could not resolve the following parameters in "{workflow.name}" workflow: '
f'{",".join(rv.missing_parameters)}'
)
from renku.core.plugins.workflow import workflow_converter
converter = workflow_converter(format)
return converter(workflow=workflow, basedir=client.path, output=output, output_format=format)
def export_workflow_command():
"""Command that exports a workflow into a given format."""
return Command().command(_export_workflow).require_clean().with_database(write=False)
@inject.autoparams()
def _execute_workflow(
name_or_id: str,
set_params: List[str],
provider: str,
config: Optional[str],
values: Optional[str],
client_dispatcher: IClientDispatcher,
):
workflow = _find_workflow(name_or_id)
from renku.core.plugins.pluginmanager import get_plugin_manager
pm = get_plugin_manager()
providers = pm.hook.workflow_provider()
provider = next(filter(lambda x: provider == x[1], providers), None)
if not provider:
raise errors.ParameterError(f"The specified workflow executor '{provider}' is not available.")
providers.remove(provider)
executor = pm.subset_hook_caller("workflow_execute", list(map(lambda x: x[0], providers)))
# apply the provided parameter settings provided by user
override_params = dict()
if values:
override_params.update(_safe_read_yaml(values))
if set_params:
for param in set_params:
name, value = param.split("=", maxsplit=1)
override_params[name] = value
if override_params:
rv = ValueResolver.get(workflow, override_params)
workflow = rv.apply()
if rv.missing_parameters:
communication.warn(
f'Could not resolve the following parameters in "{workflow.name}" workflow: '
f'{",".join(rv.missing_parameters)}'
)
if config:
config = _safe_read_yaml(config)
client = client_dispatcher.current_client
output_paths = executor(workflow=workflow, basedir=client.path, config=config)
return client.remove_unmodified(output_paths)
def execute_workflow_command():
"""Command that executes a workflow."""
return (
Command().command(_execute_workflow).require_migration().require_clean().with_database(write=True).with_commit()
)