class PulpContainerRepositoryContext(PulpRepositoryContext): HREF = "container_container_repository_href" LIST_ID = "repositories_container_container_list" READ_ID = "repositories_container_container_read" CREATE_ID = "repositories_container_container_create" UPDATE_ID = "repositories_container_container_partial_update" DELETE_ID = "repositories_container_container_delete" SYNC_ID = "repositories_container_container_sync" VERSION_CONTEXT = PulpContainerRepositoryVersionContext CAPABILITIES = { "sync": [PluginRequirement("container")], "pulpexport": [PluginRequirement("container", "2.8.0.dev")], }
def preprocess_body(self, body: EntityDefinition) -> EntityDefinition: body = super().preprocess_body(body) if self.pulp_ctx.has_plugin( PluginRequirement("core", min="3.17.dev", inverted=True)): if "creation_hooks" in body: body["permissions_assignment"] = body.pop("creation_hooks") return body
def preprocess_body(self, body: EntityDefinition) -> EntityDefinition: body = super().preprocess_body(body) if self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.16.0.dev")): if "repository" in body and "publication" not in body: body["publication"] = None if "repository" not in body and "publication" in body: body["repository"] = None return body
def has_plugin(pulp_ctx: PulpContext, name: str, min_version: Optional[str], max_version: Optional[str]) -> None: """ Check whether a specific plugin is installed on the server. """ available = pulp_ctx.has_plugin( PluginRequirement(name, min_version, max_version)) pulp_ctx.output_result(available) sys.exit(0 if available else 1)
def purge( pulp_ctx: PulpContext, task_ctx: PulpTaskContext, finished: Optional[datetime], state: Optional[Tuple[str]], ) -> None: pulp_ctx.needs_plugin(PluginRequirement("core", "3.17.0.dev")) state_list = list(state) if state else None finished_str = finished.strftime(DATETIME_FORMATS[1]) if finished else None task_ctx.purge(finished_str, state_list)
def cleanup(pulp_ctx: PulpContext, **kwargs: Any) -> None: """ Cleanup orphaned content. """ body = {k: v for k, v in kwargs.items() if v is not None} if pulp_ctx.has_plugin(PluginRequirement("core", "3.14")): result = pulp_ctx.call("orphans_cleanup_cleanup", body=body) else: result = pulp_ctx.call("orphans_delete") pulp_ctx.output_result(result)
class PulpAnsibleRepositoryContext(PulpRepositoryContext): HREF = "ansible_ansible_repository_href" LIST_ID = "repositories_ansible_ansible_list" READ_ID = "repositories_ansible_ansible_read" CREATE_ID = "repositories_ansible_ansible_create" UPDATE_ID = "repositories_ansible_ansible_partial_update" DELETE_ID = "repositories_ansible_ansible_delete" SYNC_ID = "repositories_ansible_ansible_sync" MODIFY_ID = "repositories_ansible_ansible_modify" VERSION_CONTEXT = PulpAnsibleRepositoryVersionContext CAPABILITIES = {"pulpexport": [PluginRequirement("ansible")]}
def comps_upload( pulp_ctx: PulpContext, file: IO[bytes], repository: Optional[EntityFieldDefinition], replace: Optional[bool], ) -> None: """Create comps.xml content-units by uploading a comps.xml-formatted file""" pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.17.0dev")) entity_ctx = PulpRpmCompsXmlContext(pulp_ctx) href = None if isinstance(repository, PulpEntityContext): href = repository.pulp_href result = entity_ctx.upload_comps(file, href, replace) pulp_ctx.output_result(result)
def process_value(self, ctx: click.Context, value: Any) -> Any: if value is not None and self.needs_plugins: pulp_ctx = ctx.find_object(PulpContext) assert pulp_ctx is not None for plugin_requirement in self.needs_plugins: if not plugin_requirement.feature: plugin_requirement = PluginRequirement( plugin_requirement.name, plugin_requirement.min, plugin_requirement.max, feature=_("the {name} option").format(name=self.name), inverted=plugin_requirement.inverted, ) pulp_ctx.needs_plugin(plugin_requirement) return super().process_value(ctx, value)
def label_command(**kwargs: Any) -> click.Command: """A factory that creates a label command group.""" if "name" not in kwargs: kwargs["name"] = "label" decorators = kwargs.pop("decorators", [name_option, href_option]) need_plugins = kwargs.pop("need_plugins", [PluginRequirement("core", "3.10.0")]) @click.group(**kwargs) @pass_pulp_context def label_group(pulp_ctx: PulpContext) -> None: for item in need_plugins: pulp_ctx.needs_plugin(*item) @click.command(name="set", help=_("Add or update a label")) @click.option("--key", required=True, help=_("Key of the label")) @click.option("--value", required=True, help=_("Value of the label")) @pass_entity_context def label_set(entity_ctx: PulpEntityContext, key: str, value: str) -> None: """Add or update a label""" href = entity_ctx.entity["pulp_href"] entity_ctx.set_label(href, key, value) @click.command(name="unset", help=_("Remove a label with a given key")) @click.option("--key", required=True, help=_("Key of the label")) @pass_entity_context def label_unset(entity_ctx: PulpEntityContext, key: str) -> None: """Remove a label with a given key""" href = entity_ctx.entity["pulp_href"] entity_ctx.unset_label(href, key) @click.command(name="show", help=_("Show the value for a particular label key")) @click.option("--key", required=True, help=_("Key of the label")) @pass_entity_context def label_show(entity_ctx: PulpEntityContext, key: str) -> None: """Show the value for a particular label key""" href = entity_ctx.entity["pulp_href"] click.echo(entity_ctx.show_label(href, key)) for subcmd in [label_set, label_unset, label_show]: for decorator in decorators: subcmd = decorator(subcmd) label_group.add_command(subcmd) return label_group
def remove(self, href: str, users: Optional[List[str]], groups: Optional[List[str]]) -> Any: if self.pulp_ctx.has_plugin(PluginRequirement("core", min="3.17.0.dev")): body = self.preprocess_body({"users": users, "groups": groups}) body["role"] = self.DOWNLOAD_ROLE return self.pulp_ctx.call(self.REMOVE_ROLE_ID, parameters={self.HREF: href}, body=body) else: body = self.preprocess_body({ "usernames": users, "groupnames": groups }) return self.pulp_ctx.call(self.REMOVE_ID, parameters={self.HREF: href}, body=body)
def task_summary(pulp_ctx: PulpContext) -> None: """ List a summary of tasks by status. """ from pulpcore.cli.core.context import PulpTaskContext TASK_STATES = [ "waiting", "skipped", "running", "completed", "failed", "canceled" ] if pulp_ctx.has_plugin(PluginRequirement("core", min="3.14")): TASK_STATES.append("canceling") result = {} for state in TASK_STATES: payload = {"limit": 1, "state": state} answer = pulp_ctx.call(PulpTaskContext.LIST_ID, parameters=payload) result[state] = answer["count"] pulp_ctx.output_result(result)
def main( ctx: click.Context, base_url: str, username: Optional[str], password: Optional[str], cert: Optional[str], key: Optional[str], verify_ssl: bool, format: str, verbose: int, background: bool, refresh_api: bool, dry_run: bool, timeout: int, ) -> None: def _debug_callback(level: int, x: str) -> None: if verbose >= level: click.secho(x, err=True, bold=True) api_kwargs = dict( base_url=base_url, doc_path="/pulp/api/v3/docs/api.json", username=username, password=password, cert=cert, key=key, validate_certs=verify_ssl, refresh_cache=refresh_api, safe_calls_only=dry_run, debug_callback=_debug_callback, user_agent=f"Pulp-CLI/{__version__}", ) ctx.obj = PulpContext( api_kwargs=api_kwargs, format=format, background_tasks=background, timeout=timeout ) ctx.obj.needs_plugin(PluginRequirement("core", min="3.11"))
"--created-after", "pulp_created__gte", help=_("Search for {entities} created at or after this ISO 8601 date"), type=str, ) pulp_created_lte_option = pulp_option( "--created-before", "pulp_created__lte", help=_("Search for {entities} created at or before this ISO 8601 date"), type=str, ) retained_versions_option = pulp_option( "--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]) publication_filter_options = [ content_in_option, pulp_created_gte_option, pulp_created_lte_option, pulp_option("--repository-version", help=_("Search {entities} by repository version HREF")), ] common_remote_create_options = [ click.option("--name", required=True), click.option("--url", required=True), click.option("--ca-cert", help=_("a PEM encoded CA certificate")), click.option("--client-cert", help=_("a PEM encoded client certificate")), click.option("--client-key", help=_("a PEM encode private key")),
repo_type: str) -> None: if repo_type == "file": ctx.obj = PulpFileRepositoryContext(pulp_ctx) else: raise NotImplementedError() lookup_options = [href_option, name_option] nested_lookup_options = [repository_href_option, repository_option] update_options = [ click.option("--description"), remote_option, click.option("--manifest"), pulp_option( "--autopublish/--no-autopublish", needs_plugins=[PluginRequirement("file", "1.7.0")], default=None, ), retained_versions_option, ] create_options = update_options + [click.option("--name", required=True)] file_options = [ click.option("--sha256", cls=GroupOption, expose_value=False, group=["relative_path"]), click.option( "--relative-path", cls=GroupOption, expose_value=False, group=["sha256"],
def role(ctx: click.Context, pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("core", min="3.17.dev")) ctx.obj = PulpRoleContext(pulp_ctx)
def rpm(pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("rpm", min="3.9"))
@click.group() def orphan() -> None: """ Handle orphaned content. """ pass @orphan.command() @pulp_option( "--content-hrefs", help=_("List of specific Contents to delete if they are orphans"), callback=load_json_callback, needs_plugins=[PluginRequirement("core", "3.14")], ) @pulp_option( "--protection-time", "orphan_protection_time", type=int, help=_( "How long in minutes Pulp should hold orphan Content and Artifacts before becoming" " candidates for cleanup task"), needs_plugins=[PluginRequirement("core", "3.15")], ) @pass_pulp_context def cleanup(pulp_ctx: PulpContext, **kwargs: Any) -> None: """ Cleanup orphaned content. """
destroy_command, href_option, list_command, lookup_callback, null_callback, pulp_option, show_command, update_command, ) from pulpcore.cli.common.i18n import get_translation from pulpcore.cli.core.context import PulpUserContext, PulpUserRoleContext translation = get_translation(__name__) _ = translation.gettext req_core_3_17 = PluginRequirement("core", min="3.17.dev") username_option = pulp_option( "--username", help=_("Username of the {entity}"), expose_value=False, callback=lookup_callback("username", PulpUserContext), ) lookup_options = [ href_option, username_option, ] update_options = [ click.option( "--password", help=
def HREF(self) -> str: # type:ignore if not self.pulp_ctx.has_plugin( PluginRequirement("core", min="3.17.dev")): return "auth_groups_user_href" return "groups_user_href"
lookup_options = [href_option, name_option] python_remote_options = [ click.option("--policy", type=click.Choice(["immediate", "on_demand", "streamed"], case_sensitive=False)), click.option("--includes", callback=_package_list_callback, help=_("Package allowlist")), click.option("--excludes", callback=_package_list_callback, help=_("Package blocklist")), click.option("--prereleases", type=click.BOOL, default=True), pulp_option("--keep-latest-packages", type=int, needs_plugins=[PluginRequirement("python", "3.2.0")]), pulp_option( "--package-types", callback=load_json_callback, needs_plugins=[PluginRequirement("python", "3.2.0")], ), pulp_option( "--exclude-platforms", callback=load_json_callback, needs_plugins=[PluginRequirement("python", "3.2.0")], ), ] remote.add_command(list_command(decorators=[label_select_option])) remote.add_command(show_command(decorators=lookup_options)) remote.add_command(
required=True, callback=_repository_callback, help=_("repository with content to distribute"), ), click.option( "--version", type=int, help=_("a repository version number, leave blank for latest")), ] distribution.add_command(list_command(decorators=filter_options)) distribution.add_command(show_command(decorators=lookup_options)) distribution.add_command(destroy_command(decorators=lookup_options)) distribution.add_command(create_command(decorators=create_options)) distribution.add_command( label_command(need_plugins=[ PluginRequirement("core", "3.10.0"), PluginRequirement("ansible", "0.8.0.dev"), ])) # TODO Add content_guard option @distribution.command() @name_option @href_option @click.option("--base-path", help=_("new base_path")) @click.option("--repository", type=str, default=None, help=_("new repository to be served")) @click.option( "--version",
def ansible(pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("ansible", min="0.7"))
def python_group(pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("python", min="3.1"))
@pass_pulp_context @click.pass_context def group(ctx: click.Context, pulp_ctx: PulpContext) -> None: ctx.obj = PulpGroupContext(pulp_ctx) lookup_options = [name_option, href_option] create_options = [click.option("--name", required=True)] group.add_command(list_command()) group.add_command(show_command(decorators=lookup_options)) group.add_command(destroy_command(decorators=lookup_options)) group.add_command(create_command(decorators=create_options)) group.add_command( role_command(decorators=lookup_options, needs_plugins=[PluginRequirement("core", min="3.17")])) @group.group() @click.option( "-t", "--type", "perm_type", type=click.Choice(["model", "object"], case_sensitive=False), default="model", ) @pass_entity_context @pass_pulp_context @click.pass_context def permission( ctx: click.Context,
def rbac(ctx: click.Context, pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("core", "3.15.0.dev")) ctx.obj = PulpRbacContentGuardContext(pulp_ctx)
def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> None: if repo_type == "file": ctx.obj = PulpFileRepositoryContext(pulp_ctx) else: raise NotImplementedError() lookup_options = [href_option, name_option] nested_lookup_options = [repository_href_option, repository_option] update_options = [ click.option("--description"), click.option("--remote", callback=_remote_callback), click.option("--manifest"), pulp_option( "--autopublish/--no-autopublish", needs_plugins=[PluginRequirement("file", "1.7.0")], default=None, ), pulp_option("--retained-versions", needs_plugins=[PluginRequirement("core", "3.13.0.dev")]), ] create_options = update_options + [ click.option("--name", required=True), ] file_options = [ click.option("--sha256", cls=GroupOption, expose_value=False, group=["relative_path"]), click.option( "--relative-path", cls=GroupOption, expose_value=False, group=["sha256"], callback=_content_callback,
def container(pulp_ctx: PulpContext) -> None: pulp_ctx.needs_plugin(PluginRequirement("container", min="2.3"))
@click.pass_context def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> None: if repo_type == "python": ctx.obj = PulpPythonRepositoryContext(pulp_ctx) else: raise NotImplementedError() lookup_options = [href_option, name_option] nested_lookup_options = [repository_href_option, repository_option] update_options = [ click.option("--description"), remote_option, pulp_option( "--autopublish/--no-autopublish", needs_plugins=[PluginRequirement("python", "3.3.0.dev")], default=None, ), retained_versions_option, ] create_options = update_options + [click.option("--name", required=True)] package_option = click.option( "--filename", callback=_content_callback, expose_value=False, help=_("Filename of the python package"), ) content_json_callback = create_content_json_callback(PulpPythonContentContext) modify_options = [ click.option( "--add-content",
), ] update_options = [ click.option("--description"), click.option("--retain-package-versions", type=int), remote_option, click.option("--metadata-checksum-type", type=click.Choice(CHECKSUM_CHOICES, case_sensitive=False)), click.option("--package-checksum-type", type=click.Choice(CHECKSUM_CHOICES, case_sensitive=False)), click.option("--gpgcheck", type=click.Choice(("0", "1"))), click.option("--repo-gpgcheck", type=click.Choice(("0", "1"))), click.option("--sqlite-metadata/--no-sqlite-metadata", default=None), pulp_option( "--autopublish/--no-autopublish", needs_plugins=[PluginRequirement("rpm", "3.12.0")], default=None, ), retained_versions_option, ] create_options = update_options + [click.option("--name", required=True)] repository.add_command(list_command(decorators=[label_select_option])) repository.add_command(show_command(decorators=lookup_options)) repository.add_command(create_command(decorators=create_options)) repository.add_command( update_command(decorators=lookup_options + update_options)) repository.add_command(destroy_command(decorators=lookup_options)) repository.add_command(task_command(decorators=nested_lookup_options)) repository.add_command(version_command(decorators=nested_lookup_options)) repository.add_command(label_command(decorators=nested_lookup_options))