-
Notifications
You must be signed in to change notification settings - Fork 29
/
workflow.py
1253 lines (1031 loc) · 42.2 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Copyright 2018-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.
"""Manage the set of CWL files created by ``renku`` commands.
Runs and Plans
~~~~~~~~~~~~~~
Renku records two different kinds of metadata when a workflow is executed,
``Run`` and ``Plan``.
Plans describe a recipe for a command, they function as a template that
can be used directly or combined with other workflow templates to create more
complex recipes.
These Plans can be run in various ways, on creation with ``renku run`,`
doing a ``renku rerun`` or ``renku update`` or manually using ``renku workflow
execute``.
Each time a ``Plan`` is run, we track that instance of it as a ``Run``.
Runs track workflow execution through time. They track which Plan was
run, at what time, with which specific values. This gives an insight into what
were the steps taken in a repository, how they were taken and what results they
produced.
The ``renku workflow`` group of commands contains most of the commands used
to interact with Plans and Runs
Working with Plans
~~~~~~~~~~~~~~~~~~
Listing Plans
*************
.. image:: ../_static/asciicasts/list_plans.delay.gif
:width: 850
:alt: List Plans
.. code-block:: console
$ renku workflow ls
ID NAME
--------------------------------------- ---------------
/plans/11a3702184394b93ac422df760e40999 cp-B-C-ca4da
/plans/96642cac86d9435e8abce2384f8618b9 cat-A-C-fa017
/plans/96c70626575c41c5a13853b070eaaaf5 my-other-run
/plans/9a0961844fcc46e1816fde00f57e24a8 my-run
Each entry corresponds to a recorded Plan/workflow template. You can also
show additional columns using the ``--columns`` parameter, which takes any
combination of values from ``id``, ``name``, ``keywords`` and ``description``.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow ls
:description: List Plans (workflow templates).
:extended:
Showing Plan Details
********************
.. image:: ../_static/asciicasts/show_plan.delay.gif
:width: 850
:alt: Show Plan
You can see the details of a plan by using ``renku workflow show``:
.. code-block:: console
$ renku workflow show my-run
Id: /plans/9a0961844fcc46e1816fde00f57e24a8
Name: run1
Command: cp A B
Success Codes:
Inputs:
- input-1:
Default Value: A
Position: 1
Outputs:
- output-2:
Default Value: B
Position: 2
This shows the unique Id of the Plan, its name, the full command of the Plan
if it was run without any modifications (more on that later), which exit codes
should be considered successful executions (defaults to ``0``) as well as its
inputs, outputs and parameters.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow show <name>
:description: Show details for Plan <name>.
:extended:
Executing Plans
***************
.. image:: ../_static/asciicasts/execute_plan.delay.gif
:width: 850
:alt: Execute Plans
Plans can be executed using ``renku workflow execute``. They can be run as-is
or their parameters can be modified as needed. Renku has a plugin architecture
to allow execution using various execution backends.
.. code-block:: console
$ renku workflow execute --provider cwltool --set input-1=file.txt my-run
Parameters can be set using the ``--set`` keyword or by specifying them in a
values YAML file and passing that using ``--values``. In case of passing a file,
the YAML should follow the this structure:
.. code-block:: yaml
learning_rate: 0.9
dataset_input: dataset.csv
chart_output: mychart.png
myworkflow:
lr: 0.8
lookuptable: lookup.xml
myotherworkflow:
language: en
In addition to being passed on the command line and being available to
``renku.api.*`` classes in Python scripts, parameters are also set as
environment variables when executing the command, in the form of
``RENKU_ENV_<parameter name>``.
Provider specific settings can be passed as file using the ``--config`` parameter.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow execute --provider <provider> [--set
<param-name>=<value>...] <name>
:description: Execute a Plan using <provider> as a backend, overriding
parameter <param-name>'s value.
:extended:
Iterate Plans
*************
.. image:: ../_static/asciicasts/iterate_plan.gif
:width: 850
:alt: Iterate Plans
For executing a Plan with different parametrization ``renku workflow iterate``
could be used. This sub-command is basically conducting a 'grid search'-like
execution of a Plan, with parameter-sets provided by the user.
.. code-block:: console
$ renku workflow iterate --map parameter-1=[1,2,3] \
--map parameter-2=[10,20] my-run
The set of possible values for a parameter can be given by ``--map`` command
line argument or by specifying them in a values YAML file and passing that
using ``--mapping``. Content of the mapping file for the above example
should be:
.. code-block:: yaml
parameter-1: [1,2,3]
parameter-2: [10,20]
.. cheatsheet::
:group: Workflows
:command: $ renku workflow iterate [--map <param-name>=[value,value,...]]
<name>
:description: Repeatedly execute a Plan, taking values from the list
specified with --map.
:extended:
By default ``renku workflow iterate`` will execute all the combination of the
given parameters' list of possible values. Sometimes it is desired that instead
of all the combination of possible values, a specific tuple of values are
executed. This could be done by marking the parameters that should be bound
together with the ``@tag`` suffix in their names.
.. code-block:: console
$ renku workflow iterate --map parameter-1@tag1=[1,2,3] \
--map parameter-2@tag1=[10,5,30] my-run
This will result in only three distinct execution of the ``my-run`` Plan,
with the following parameter combinations: ``[(1,10), (2,5), (3,30)]``. It is
important to note that parameters that have the same tag, should have the same
number of possible values, i.e. the values list should have the same length.
There's a special template variable for parameter values ``{iter_index}``, which
can be used to mark each iteration's index in a value of a parameter. The template
variable is going to be substituted with the iteration index (0, 1, 2, ...).
.. code-block:: console
$ renku workflow iterate --map parameter-1=[10,20,30] \
--map output=output_{iter_index}.txt my-run
This would execute ``my-run`` three times, where ``parameter-1`` values would be
``10``, `20`` and ``30`` and the producing output files ``output_0.txt``,
``output_1.txt`` and ``output_2.txt`` files in this order.
Exporting Plans
***************
You can export a Plan to a number of different workflow languages, such as CWL
(Common Workflow Language) by using ``renku workflow export``:
.. code-block:: console
$ renku workflow export --format cwl my-run
baseCommand:
- cp
class: CommandLineTool
cwlVersion: v1.0
id: 63e3a2a8-5b40-49b2-a2f4-eecc37bc76b0
inputs:
- default: B
id: _plans_9a0961844fcc46e1816fde00f57e24a8_outputs_2_arg
inputBinding:
position: 2
type: string
- default:
class: File
location: file:///home/user/my-project/A
id: _plans_9a0961844fcc46e1816fde00f57e24a8_inputs_1
inputBinding:
position: 1
type: File
- default:
class: Directory
location: file:///home/user/my-project/.renku
id: input_renku_metadata
type: Directory
- default:
class: Directory
location: file:///home/user/my-project/.git
id: input_git_directory
type: Directory
outputs:
- id: _plans_9a0961844fcc46e1816fde00f57e24a8_outputs_2
outputBinding:
glob: $(inputs._plans_9a0961844fcc46e1816fde00f57e24a8_outputs_2_arg)
type: File
requirements:
InitialWorkDirRequirement:
listing:
- entry: $(inputs._plans_9a0961844fcc46e1816fde00f57e24a8_inputs_1)
entryname: A
writable: false
- entry: $(inputs.input_renku_metadata)
entryname: .renku
writable: false
- entry: $(inputs.input_git_directory)
entryname: .git
writable: false
You can export into a file directly with ``-o <path>``.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow export --format <format> <plan>
:description: Export a Plan in a given format (e.g. 'cwl').
:extended:
Composing Plans into larger workflows
*************************************
.. image:: ../_static/asciicasts/compose_plan.delay.gif
:width: 850
:alt: Composing Plans
For more complex workflows consisting of several steps, you can use the
``renku workflow compose`` command. This creates a new workflow that has
substeps.
The basic usage is:
.. code-block:: console
$ renku run --name step1-- cp input intermediate
$ renku run --name step2 -- cp intermediate output
$ renku workflow compose my-composed-workflow step1 step2
This would create a new workflow called ``my-composed-workflow`` that
consists of ``step1`` and ``step2`` as steps. This new workflow is just
like any other workflow in renku in that it can be executed, exported
or composed with other workflows.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow compose <composed-name> <plan> <plan>
:description: Create a new Plan composed of child Plans.
:extended:
Workflows can also be composed based on past Runs and their
inputs/outputs, using the ``--from`` and ``--to`` parameters. This finds
chains of Runs from inputs to outputs and then adds them to the
composed plan, applying mappings (see below) where appropriate to make
sure the correct values for execution are used in the composite. This
also means that all the parameters in the used plans are exposed on the
composed plan directly.
In the example above, this would be:
.. code-block:: console
$ renku workflow compose --from input --to output my-composed-workflow
You can expose parameters of child steps on the parent workflow using
``--map``/``-m`` arguments followed by a mapping expression. Mapping expressions
take the form of ``<name>=<expression>`` where ``name`` is the name of the
property to be created on the parent workflow and expression points to one
or more fields on the child steps that should be mapped to this property.
The expressions come in two flavors, absolute references using the names
of workflows and properties, and relative references specifying the
position within a workflow.
An absolute expression in the example above could be ``step1.my_dataset``
to refer to the input, output or argument named ``my_dataset`` on the step
``step1``. A relative expression could be ``@step2.@output1`` to refer
to the first output of the second step of the composed workflow.
Valid relative expressions are ``@input<n>``, ``@output<n>`` and ``@param<n>``
for the nth input, output or argument of a step, respectively. For referring
to steps inside a composed workflow, you can use ``@step<n>``. For referencing
a mapping on a composed workflow, you can use ``@mapping<n>``. Of course, the
names of the objects for all these cases also work.
The expressions can also be combined using ``,`` if a mapping should point
to more than one parameter of a child step.
You can mix absolute and relative reference in the same expression, as you see
fit.
A full example of this would be:
.. code-block:: console
$ renku workflow compose --map input_file=step1.@input2 \
--map output_file=@step1.my-output,@step2.step2s_output \
my-composed-workflow step1 step2
This would create a mapping called ``input_file`` on the parent workflow that
points to the second input of ``step1`` and a mapping called ``output_file``
that points to both the output ``my-output`` on ``step1`` and
``step2s_output`` on ``step2``.
You can also set default values for mappings, which override the default values
of the parameters they're pointing to by using the ``--set``/``-s`` parameter, for
instance:
.. code-block:: console
$ renku workflow compose --map input_file=step1.@input2 \
--set input_file=data.csv
my-composed-workflow step1 step2
This would lead to ``data.csv`` being used for the second input of
``step1`` when ``my-composed-workflow`` is executed (if it isn't overridden
at execution time).
You can add a description to the mappings to make them more human-readable
by using the ``--describe-param``/``-p`` parameter, as shown here:
.. code-block:: console
$ renku workflow compose --map input_file=step1.@input2 \
-p input_file="The dataset to process"
my-composed-workflow step1 step2
You can also expose all inputs, outputs or parameters of child steps by
using ``--map-inputs``, ``--map-outputs`` or ``--map-params``, respectively.
On execution, renku will automatically detect links between steps, if an input
of one step uses the same path as an output of another step, and execute
them in the correct order. Since this depends on what values are passed
at runtime, you might want to enforce a certain order of steps by explicitly
mapping outputs to inputs.
You can do that using the ``--link <source>=<sink>`` parameters, e.g.
``--link step1.@output1=step2.@input1``. This gets recorded on the
workflow template and forces ``step2.@input1`` to always be set to the same
path as ``step1.@output1``, irrespective of which values are passed at
execution time.
This way, you can ensure that the steps in your workflow are always executed
in the correct order and that the dependencies between steps are modeled
correctly.
Renku can also add links for you automatically based on the default values
of inputs and outputs, where inputs/outputs that have the same path get
linked in the composed run. To do this, pass the ``--link-all`` flag.
.. warning:: Due to workflows having to be directed acyclic graphs, cycles
in the dependencies are not allowed. E.g. step1 depending on step2
depending on step1 is not allowed. Additionally, the flow of information
has to be from outputs to inputs or parameters, so you cannot map an input
to an output, only the other way around.
Values on inputs/outputs/parameters get set according to the following
order of precedence (lower precedence first):
- Default value on a input/output/parameter
- Default value on a mapping to the input/output/parameter
- Value passed to a mapping to the input/output/parameter
- Value passed to the input/output/parameter
- Value propagated to an input from the source of a workflow link
Editing Plans
*************
.. image:: ../_static/asciicasts/edit_plan.delay.gif
:width: 850
:alt: Editing Plans
Plans can be edited in some limited fashion, but we do not allow structural
changes, as that might cause issues with the reproducibility and provenance of
the project. If you want to do structural changes (e.g. adding/removing
parameters), we recommend you record a new plan instead.
You can change the name and description of Plans and of their parameters, as
well as changing default values of the parameters using the ``renku workflow
edit`` command:
.. code-block:: console
$ renku workflow edit my-run --name new-run --description "my description"
--rename-param input-1=my-input --set my-input=other-file.txt
--describe-param my-input="My input parameter" my-run
This would rename the Plan ``my-run`` to ``new-run``, change its description,
rename its parameter ``input-1`` to ``my-input`` and set the default of this
parameter to ``other-file.txt`` and set its description.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow edit <plan>
:description: Create a new Plan composed of child Plans.
:extended:
Removing Plans
**************
Sometimes you might want to discard a recorded Plan or reuse its name with a
new Plan. In these cases, you can delete the old plan using ``renku workflow
remove <plan name>``. Once a Plan is removed, it doesn't show up in most renku
workflow commands.
``renku update`` ignores deleted Plans, but ``renku rerun`` will still rerun
them if needed, to ensure reproducibility.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow delete <plan>
:description: Remove a Plan.
:extended:
Working with Runs
~~~~~~~~~~~~~~~~~
Listing Runs
************
To get a view of what commands have been execute in the project, you can use
the ``renku log --workflows`` command:
.. code-block:: console
$ renku log --workflows
DATE TYPE DESCRIPTION
------------------- ---- -------------
2021-09-21 15:46:02 Run cp A C
2021-09-21 10:52:51 Run cp A B
Refer to the documentation of the :ref:`cli-log` command for more details.
Visualizing Executions
**********************
.. image:: ../_static/asciicasts/visualize_runs.delay.gif
:width: 850
:alt: Visualizing Runs
You can visualize past Runs made with renku using the ``renku workflow
visualize`` command.
This will show a directed graph of executions and how they are connected. This
way you can see exactly how a file was generated and what steps it involved.
It also supports an interactive mode that lets you explore the graph in a more
detailed way.
.. code-block:: console
$ renku run echo "input" > input
$ renku run cp input intermediate
$ renku run cp intermediate output
$ renku workflow visualize
╔════════════╗
║echo > input║
╚════════════╝
*
*
*
┌─────┐
│input│
└─────┘
*
*
*
╔═════════════════════╗
║cp input intermediate║
╚═════════════════════╝
*
*
*
┌────────────┐
│intermediate│
└────────────┘
*
*
*
╔══════════════════════╗
║cp intermediate output║
╚══════════════════════╝
*
*
*
┌──────┐
│output│
└──────┘
$ renku workflow visualize intermediate
╔════════════╗
║echo > input║
╚════════════╝
*
*
*
┌─────┐
│input│
└─────┘
*
*
*
╔═════════════════════╗
║cp input intermediate║
╚═════════════════════╝
*
*
*
┌────────────┐
│intermediate│
└────────────┘
$ renku workflow visualize --from intermediate
┌────────────┐
│intermediate│
└────────────┘
*
*
*
╔══════════════════════╗
║cp intermediate output║
╚══════════════════════╝
*
*
*
┌──────┐
│output│
└──────┘
You can also run in interactive mode using the ``--interactive`` flag.
.. code-block:: console
$ renku workflow visualize --interactive
This will allow you to navigate between workflow execution and see details
by pressing the <Enter> key.
Use ``renku workflow visualize -h`` to see all available options.
.. cheatsheet::
:group: Workflows
:command: $ renku workflow visualize [--interactive]
:description: Show linked workflows as a graph.
:extended:
Input and output files
~~~~~~~~~~~~~~~~~~~~~~
You can list input and output files generated in the repository by running
``renku workflow inputs`` and ``renku workflow outputs`` commands. Alternatively,
you can check if all paths specified as arguments are input or output files
respectively.
.. code-block:: console
$ renku run wc < source.txt > result.wc
$ renku workflow inputs
source.txt
$ renku workflow outputs
result.wc
$ renku workflow outputs source.txt
$ echo $? # last command finished with an error code
1
.. cheatsheet::
:group: Workflows
:command: $ renku workflow inputs|||$ renku workflow outputs
:description: Show input respectively output files used by workflows.
:extended:
"""
import os
import pydoc
import shutil
import sys
from pathlib import Path
from typing import TYPE_CHECKING
import click
from lazy_object_proxy import Proxy
from renku.cli.utils.callback import ClickCallback
from renku.cli.utils.plugins import available_workflow_providers, supported_formats
from renku.core import errors
from renku.core.commands.echo import ERROR
from renku.core.commands.format.workflow import WORKFLOW_COLUMNS, WORKFLOW_FORMATS
from renku.core.commands.view_model.activity_graph import ACTIVITY_GRAPH_COLUMNS
if TYPE_CHECKING:
from renku.core.commands.view_model.composite_plan import CompositePlanViewModel
from renku.core.commands.view_model.plan import PlanViewModel
def _complete_workflows(ctx, param, incomplete):
from renku.core.commands.workflow import search_workflows_command
try:
result = search_workflows_command().build().execute(name=incomplete)
return list(filter(lambda x: x.startswith(incomplete), result.output))
except Exception:
return []
def _print_plan(plan: "PlanViewModel"):
"""Print a plan to stdout."""
from renku.core.utils.os import print_markdown
click.echo(click.style("Id: ", bold=True, fg="magenta") + click.style(plan.id, bold=True))
click.echo(click.style("Name: ", bold=True, fg="magenta") + click.style(plan.name, bold=True))
if plan.description:
print_markdown(plan.description)
click.echo(click.style("Command: ", bold=True, fg="magenta") + click.style(plan.full_command, bold=True))
click.echo(click.style("Success Codes: ", bold=True, fg="magenta") + click.style(plan.success_codes, bold=True))
if plan.inputs:
click.echo(click.style("Inputs: ", bold=True, fg="magenta"))
for run_input in plan.inputs:
click.echo(click.style(f"\t- {run_input.name}:", bold=True))
if run_input.description:
click.echo(click.style(f"\t\t{run_input.description}"))
click.echo(
click.style("\t\tDefault Value: ", bold=True, fg="magenta")
+ click.style(run_input.default_value, bold=True)
)
if run_input.position:
click.echo(
click.style("\t\tPosition: ", bold=True, fg="magenta") + click.style(run_input.position, bold=True)
)
if run_input.prefix:
click.echo(
click.style("\t\tPrefix: ", bold=True, fg="magenta") + click.style(run_input.prefix, bold=True)
)
if plan.outputs:
click.echo(click.style("Outputs: ", bold=True, fg="magenta"))
for run_output in plan.outputs:
click.echo(click.style(f"\t- {run_output.name}:", bold=True))
if run_output.description:
click.echo(click.style(f"\t\t{run_output.description}"))
click.echo(
click.style("\t\tDefault Value: ", bold=True, fg="magenta")
+ click.style(run_output.default_value, bold=True)
)
if run_output.position:
click.echo(
click.style("\t\tPosition: ", bold=True, fg="magenta") + click.style(run_output.position, bold=True)
)
if run_output.prefix:
click.echo(
click.style("\t\tPrefix: ", bold=True, fg="magenta") + click.style(run_output.prefix, bold=True)
)
if plan.parameters:
click.echo(click.style("Parameters: ", bold=True, fg="magenta"))
for run_parameter in plan.parameters:
click.echo(click.style(f"\t- {run_parameter.name}:", bold=True))
if run_parameter.description:
click.echo(click.style(f"\t\t{run_parameter.description}"))
click.echo(
click.style("\t\tDefault Value: ", bold=True, fg="magenta")
+ click.style(run_parameter.default_value, bold=True)
)
if run_parameter.position:
click.echo(
click.style("\t\tPosition: ", bold=True, fg="magenta")
+ click.style(run_parameter.position, bold=True)
)
if run_parameter.prefix:
click.echo(
click.style("\t\tPrefix: ", bold=True, fg="magenta") + click.style(run_parameter.prefix, bold=True)
)
def _print_composite_plan(composite_plan: "CompositePlanViewModel"):
"""Print a CompositePlan to stdout."""
from renku.core.utils.os import print_markdown
click.echo(click.style("Id: ", bold=True, fg="magenta") + click.style(composite_plan.id, bold=True))
click.echo(click.style("Name: ", bold=True, fg="magenta") + click.style(composite_plan.name, bold=True))
if composite_plan.description:
print_markdown(composite_plan.description)
click.echo(click.style("Steps: ", bold=True, fg="magenta"))
for step in composite_plan.steps:
click.echo(click.style(f"\t- {step.name}:", bold=True))
click.echo(click.style("\t\tId: ", bold=True, fg="magenta") + click.style(f"{step.id}", bold=True))
if composite_plan.mappings:
click.echo(click.style("Mappings: ", bold=True, fg="magenta"))
for mapping in composite_plan.mappings:
click.echo(click.style(f"\t- {mapping.name}:", bold=True))
if mapping.description:
click.echo(click.style(f"\t\t{mapping.description}"))
click.echo(
click.style("\t\tDefault Value: ", bold=True, fg="magenta")
+ click.style(mapping.default_value, bold=True)
)
click.echo(click.style("\tMaps to: ", bold=True, fg="magenta"))
for maps_to in mapping.maps_to:
click.echo(click.style(f"\t\t{maps_to}", bold=True))
if composite_plan.links:
click.echo(click.style("Links: ", bold=True, fg="magenta"))
for link in composite_plan.links:
click.echo(click.style("\t- From: ", bold=True, fg="magenta") + click.style(link.source, bold=True))
click.echo(click.style("\t\t To: ", bold=True, fg="magenta"))
for sink in link.sinks:
click.echo(click.style(f"\t\t- {sink}", bold=True))
@click.group()
def workflow():
"""Workflow commands."""
pass
@workflow.command("ls")
@click.option("--format", type=click.Choice(WORKFLOW_FORMATS), default="tabular", help="Choose an output format.")
@click.option(
"-c",
"--columns",
type=click.STRING,
default="id,name,command",
metavar="<columns>",
help="Comma-separated list of column to display: {}.".format(", ".join(WORKFLOW_COLUMNS.keys())),
show_default=True,
)
def list_workflows(format, columns):
"""List or manage workflows with subcommands."""
from renku.core.commands.workflow import list_workflows_command
result = list_workflows_command().build().execute(format=format, columns=columns)
click.echo(result.output)
@workflow.command()
@click.argument("name_or_id", metavar="<name_or_id>", shell_complete=_complete_workflows)
def show(name_or_id):
"""Show details for workflow <name_or_id>."""
from renku.core.commands.view_model.plan import PlanViewModel
from renku.core.commands.workflow import show_workflow_command
plan = show_workflow_command().build().execute(name_or_id=name_or_id).output
if plan:
if isinstance(plan, PlanViewModel):
_print_plan(plan)
else:
_print_composite_plan(plan)
else:
click.secho(ERROR + f"Workflow '{name_or_id}' not found.")
@workflow.command()
@click.argument("name", metavar="<name>", shell_complete=_complete_workflows)
@click.option("--force", is_flag=True, help="Override the existence check.")
def remove(name, force):
"""Remove a workflow named <name>."""
from renku.core.commands.workflow import remove_workflow_command
remove_workflow_command().build().execute(name=name, force=force)
@workflow.command()
@click.option("-d", "--description", help="Workflow step's description.")
@click.option("mappings", "-m", "--map", multiple=True, help="Mapping for a workflow parameter.")
@click.option("defaults", "-s", "--set", multiple=True, help="Default value for a workflow parameter.")
@click.option("links", "-l", "--link", multiple=True, help="Link source and sink parameters to connect steps.")
@click.option("-p", "--describe-param", multiple=True, help="Add description for a workflow parameter.")
@click.option("--map-inputs", is_flag=True, help="Exposes all child inputs as inputs on the CompositePlan.")
@click.option("--map-outputs", is_flag=True, help="Exposes all child outputs as outputs on the CompositePlan.")
@click.option("--map-params", is_flag=True, help="Exposes all child parameters as parameters on the CompositePlan.")
@click.option("--map-all", is_flag=True, help="Combination of --map-inputs, --map-outputs, --map-params.")
@click.option("--link-all", is_flag=True, help="Automatically link steps based on default values.")
@click.option("--keyword", multiple=True, help="List of keywords for the workflow.")
@click.option(
"--from",
"sources",
type=click.Path(exists=True, dir_okay=False),
multiple=True,
help="Start a composite plan from this file as input.",
)
@click.option(
"--to",
"sinks",
type=click.Path(exists=True, dir_okay=True),
multiple=True,
help="End a composite plan at this file as output.",
)
@click.argument("name", required=True)
@click.argument("steps", nargs=-1, type=click.UNPROCESSED, shell_complete=_complete_workflows)
def compose(
description,
mappings,
defaults,
links,
describe_param,
map_inputs,
map_outputs,
map_params,
map_all,
link_all,
keyword,
sources,
sinks,
name,
steps,
):
"""Create a composite workflow consisting of multiple steps."""
from renku.core.commands.workflow import compose_workflow_command
if (sources or sinks) and steps:
click.secho(ERROR + "--from/--to cannot be used at the same time as passing run/step names.")
exit(1)
elif not (sources or sinks or steps):
click.secho(ERROR + "Either --from/--to passing run/step names is required.")
exit(1)
if map_all:
map_inputs = map_outputs = map_params = True
result = (
compose_workflow_command()
.build()
.execute(
name=name,
description=description,
mappings=mappings,
defaults=defaults,
links=links,
param_descriptions=describe_param,
map_inputs=map_inputs,
map_outputs=map_outputs,
map_params=map_params,
link_all=link_all,
keywords=keyword,
steps=steps,
sources=sources,
sinks=sinks,
)
)
if not result.error:
_print_composite_plan(result.output)
@workflow.command()
@click.argument("workflow_name", metavar="<name or uuid>", shell_complete=_complete_workflows)
@click.option("--name", metavar="<new name>", help="New name of the workflow")
@click.option("--description", metavar="<new desc>", help="New description of the workflow")
@click.option(
"--set",
"set_params",
multiple=True,
metavar="<parameter>=<value>",
help="Set default <value> for a <parameter>/add new parameter",
)
@click.option(
"--map",
"map_params",
multiple=True,
metavar="<parameter>=<parameter or expression>",
help="New mapping on the workflow",
)
@click.option(
"--rename-param",
"rename_params",
multiple=True,
metavar='<parameter>="name"',
help="New name for parameter",
)
@click.option(
"--describe-param",
"describe_params",
multiple=True,
metavar='<parameter>="description"',
help="New description of the workflow",
)
def edit(workflow_name, name, description, set_params, map_params, rename_params, describe_params):
"""Edit workflow details."""
from renku.core.commands.view_model.plan import PlanViewModel
from renku.core.commands.workflow import edit_workflow_command
result = (
edit_workflow_command()
.build()
.execute(
name=workflow_name,
new_name=name,
description=description,
set_params=set_params,
map_params=map_params,
rename_params=rename_params,
describe_params=describe_params,
)
)
if not result.error:
plan = result.output
if isinstance(plan, PlanViewModel):
_print_plan(plan)
else:
_print_composite_plan(plan)
@workflow.command()
@click.argument("workflow_name", metavar="<name or uuid>", shell_complete=_complete_workflows)
@click.option(
"--format",
default="cwl",
type=click.Choice(Proxy(supported_formats), case_sensitive=False),
show_default=True,
help="Workflow language format.",
)
@click.option(
"-o",
"--output",
metavar="<path>",
type=click.Path(exists=False),
default=None,
help="Save to <path> instead of printing to terminal",
)
@click.option(
"--values",
metavar="<file>",
type=click.Path(exists=True, dir_okay=False),
default=None,
help="YAML file containing parameter mappings to be used.",
)