diff --git a/CHANGES/1172.feature b/CHANGES/1172.feature new file mode 100644 index 000000000..59c07b430 --- /dev/null +++ b/CHANGES/1172.feature @@ -0,0 +1 @@ +Added `profile-artifact-urls` subcommand to `task` command. diff --git a/CHANGES/pulp-glue/1172.feature b/CHANGES/pulp-glue/1172.feature new file mode 100644 index 000000000..4b6b97bcc --- /dev/null +++ b/CHANGES/pulp-glue/1172.feature @@ -0,0 +1 @@ +Added `profile_artifact_urls` to `PulpTaskContext`. diff --git a/pulp-glue/pulp_glue/core/context.py b/pulp-glue/pulp_glue/core/context.py index 4658455dd..96851a092 100644 --- a/pulp-glue/pulp_glue/core/context.py +++ b/pulp-glue/pulp_glue/core/context.py @@ -447,6 +447,15 @@ def cancel(self, task_href: t.Optional[str] = None, background: bool = False) -> self.pulp_ctx.echo(_("Done."), err=True) return task + def profile_artifact_urls(self) -> t.Dict[str, str]: + self.pulp_ctx.needs_plugin(PluginRequirement("core", specifier=">=3.57.0")) + result = self.call( + "profile_artifacts", + parameters={self.HREF: self.pulp_href}, + )["urls"] + assert isinstance(result, dict) + return result + @property def scope(self) -> t.Dict[str, t.Any]: if self.resource_context: diff --git a/pulpcore/cli/core/task.py b/pulpcore/cli/core/task.py index ea110b0ed..0a9686d54 100644 --- a/pulpcore/cli/core/task.py +++ b/pulpcore/cli/core/task.py @@ -1,8 +1,11 @@ +import re import typing as t from contextlib import suppress from datetime import datetime +from pathlib import Path import click +import requests from pulp_glue.common.context import DATETIME_FORMATS, PluginRequirement, PulpEntityContext from pulp_glue.common.exceptions import PulpException from pulp_glue.common.i18n import get_translation @@ -167,6 +170,37 @@ def cancel( task_ctx.cancel(task_ctx.pulp_href) +@task.command() +@href_option +@uuid_option +@click.option("--download/--no-download") +@pass_task_context +@pass_pulp_context +def profile_artifact_urls( + pulp_ctx: PulpCLIContext, + task_ctx: PulpTaskContext, + /, + download: bool, +) -> None: + """Prints download links for profile urls of the task.""" + urls = task_ctx.profile_artifact_urls() + pulp_ctx.output_result(urls) + if download: + task_name: str = task_ctx.entity["name"] + uuid_match = re.match(r".*/api/v3/tasks/(?P.*)/", task_ctx.entity["pulp_href"]) + assert uuid_match is not None + uuid = uuid_match.group("uuid") + profile_artifact_dir = Path(".") / f"task_profile-{task_name}-{uuid}" + profile_artifact_dir.mkdir(exist_ok=True) + with requests.session() as session: + for name, url in urls.items(): + profile_artifact_path = profile_artifact_dir / name + click.echo(_("Downloading {path}").format(path=profile_artifact_path)) + response = session.get(url) + response.raise_for_status() + profile_artifact_path.write_bytes(response.content) + + @task.command() @click.option( "--finished-before",