diff --git a/CHANGES/459.feature b/CHANGES/459.feature new file mode 100644 index 000000000..c3c0e3c31 --- /dev/null +++ b/CHANGES/459.feature @@ -0,0 +1,2 @@ +Introduced the option ``--wait`` for the task-group show command. +By using this option, details of the task-group will be shown only after waiting for all related tasks to finish. diff --git a/pulpcore/cli/common/context.py b/pulpcore/cli/common/context.py index d4bacb1fb..0cea81d60 100644 --- a/pulpcore/cli/common/context.py +++ b/pulpcore/cli/common/context.py @@ -296,7 +296,7 @@ def wait_for_task_group(self, task_group: EntityDefinition) -> Any: time.sleep(1) self.echo(".", nl=False, err=True) task_group = self.api.call( - "task_group_read", parameters={"task_group_href": task_group["pulp_href"]} + "task_groups_read", parameters={"task_group_href": task_group["pulp_href"]} ) except KeyboardInterrupt: raise PulpNoWait( diff --git a/pulpcore/cli/core/task_group.py b/pulpcore/cli/core/task_group.py index 7284f1013..581b62d9d 100644 --- a/pulpcore/cli/core/task_group.py +++ b/pulpcore/cli/core/task_group.py @@ -1,7 +1,16 @@ +from typing import Optional + import click -from pulpcore.cli.common.context import PulpContext, pass_pulp_context -from pulpcore.cli.common.generic import href_option, list_command, pulp_group, show_command +from pulpcore.cli.common.context import PulpContext, PulpEntityContext +from pulpcore.cli.common.generic import ( + href_option, + list_command, + pass_entity_context, + pass_pulp_context, + pulp_group, + pulp_option, +) from pulpcore.cli.common.i18n import get_translation from pulpcore.cli.core.context import PulpTaskGroupContext @@ -17,4 +26,49 @@ def task_group(ctx: click.Context, pulp_ctx: PulpContext) -> None: task_group.add_command(list_command()) -task_group.add_command(show_command(decorators=[href_option])) + + +def _uuid_callback( + ctx: click.Context, param: click.Parameter, value: Optional[str] +) -> Optional[str]: + if value is not None: + entity_ctx = ctx.find_object(PulpEntityContext) + assert entity_ctx is not None + entity_ctx.pulp_href = f"{entity_ctx.pulp_ctx.api_path}task-groups/{value}/" + return value + + +uuid_option = pulp_option( + "--uuid", + help=_("UUID of the {entity}"), + callback=_uuid_callback, + expose_value=False, +) + + +@task_group.command() +@href_option +@uuid_option +@click.option("-w", "--wait", is_flag=True, help=_("Wait for the group-task to finish")) +@pass_entity_context +@pass_pulp_context +def show(pulp_ctx: PulpContext, task_group_ctx: PulpTaskGroupContext, wait: bool) -> None: + """Shows details of a group-task.""" + entity = task_group_ctx.entity + if wait: + if not entity["all_tasks_dispatched"]: + click.echo(_("Waiting for all tasks to be dispatched"), err=True) + else: + unfinished_tasks = [] + for task in entity["tasks"]: + if task["state"] in ["waiting", "running", "canceling"]: + unfinished_tasks.append(task["pulp_href"]) + + if unfinished_tasks: + click.echo( + _("Waiting for the following tasks to finish: {}").format(unfinished_tasks), + err=True, + ) + + entity = pulp_ctx.wait_for_task_group(entity) + pulp_ctx.output_result(entity) diff --git a/tests/scripts/pulp_file/test_acs.sh b/tests/scripts/pulp_file/test_acs.sh index 989d09afa..f087ddfd4 100755 --- a/tests/scripts/pulp_file/test_acs.sh +++ b/tests/scripts/pulp_file/test_acs.sh @@ -35,9 +35,14 @@ expect_succ pulp file acs show --name $acs test "$(echo "$OUTPUT" | jq ".paths | length")" -eq 2 # test refresh -expect_succ pulp file acs refresh --name $acs +expect_succ pulp --background file acs refresh --name $acs task_group=$(echo "$ERROUTPUT" | grep -E -o "${PULP_API_ROOT}api/v3/task-groups/[-[:xdigit:]]*/") -expect_succ pulp task-group show --href "$task_group" +expect_succ pulp task-group show --href "$task_group" --wait + +group_task_uuid="${task_group%/}" +group_task_uuid="${group_task_uuid##*/}" +expect_succ pulp task-group show --uuid "$group_task_uuid" + test "$(echo "$OUTPUT" | jq ".tasks | length")" -eq 2 # create a remote with manifest only and sync it