Exemplo n.º 1
0
    def _do_command(self):
        self._validate_options()
        # Abort if not registered
        self.assert_should_be_registered()

        supported_resources = get_supported_resources()
        if "content_overrides" not in supported_resources:
            system_exit(
                os.EX_UNAVAILABLE,
                _("Error: The 'repo-override' command is not supported by the server."
                  ))

        # update entitlement certificates if necessary. If we do have new entitlements
        # CertLib.update() will call RepoActionInvoker.update().
        self.entcertlib.update()
        # make sure the EntitlementDirectory singleton is refreshed
        self._request_validity_check()

        overrides = Overrides()

        if not manage_repos_enabled():
            print(_("Repositories disabled by configuration."))

        if self.options.list:
            results = overrides.get_overrides(self.identity.uuid)
            if results:
                self._list(results, self.options.repos)
            else:
                print(
                    _("This system does not have any content overrides applied to it."
                      ))
            return

        if self.options.additions:
            repo_ids = [
                repo.id
                for repo in overrides.repo_lib.get_repos(apply_overrides=False)
            ]
            to_add = [
                Override(repo, name, value) for repo in self.options.repos
                for name, value in list(self.options.additions.items())
            ]
            try:
                results = overrides.add_overrides(self.identity.uuid, to_add)
            except connection.RestlibException as ex:
                if ex.code == 400:
                    # blocklisted overrides specified.
                    # Print message and return a less severe code.
                    mapped_message: str = ExceptionMapper().get_message(ex)
                    system_exit(1, mapped_message)
                else:
                    raise ex

            # Print out warning messages if the specified repo does not exist in the repo file.
            for repo in self.options.repos:
                if repo not in repo_ids:
                    print(
                        _("Repository '{repo}' does not currently exist, but the override has been added."
                          ).format(repo=repo))

        if self.options.removals:
            to_remove = [
                Override(repo, item) for repo in self.options.repos
                for item in self.options.removals
            ]
            results = overrides.remove_overrides(self.identity.uuid, to_remove)
        if self.options.remove_all:
            results = overrides.remove_all_overrides(self.identity.uuid,
                                                     self.options.repos)

        # Update the cache and refresh the repo file.
        overrides.update(results)