From 234c4daf698cd82cfa4fe2fa79274e3a30e09c59 Mon Sep 17 00:00:00 2001 From: Bernhard Suttner Date: Tue, 20 Dec 2022 16:31:10 +0100 Subject: [PATCH] Add os product tag based on /etc/os-release Currently, content filter based on the product-tags only works for RHEL. The content filter method should also be usable for other OSses, like CentOS, SuSE or even Debian/Ubuntu. With this approach, a [os-name]-[os-version] tag is created based on /etc/os-release. The tag is only generated and added if the feature is activated by setting use_os_release_product=1 in /etc/rhsm/rhsm.conf --- src/subscription_manager/model/__init__.py | 23 ++++++++++++++++++++-- src/subscription_manager/repolib.py | 20 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/subscription_manager/model/__init__.py b/src/subscription_manager/model/__init__.py index b0ca06ed1d..6be1a9d6a4 100644 --- a/src/subscription_manager/model/__init__.py +++ b/src/subscription_manager/model/__init__.py @@ -89,22 +89,28 @@ def __getitem__(self, key): return self._entitlements[key] -def find_content(ent_source, content_type=None): +def find_content(ent_source, content_type=None, use_os_release_product=False): """ Scan all entitlements looking for content of the given type. (string) Type will be compared case insensitive. + If use_os_release_product is enabled, generate a os-product-version tag and + add it ot the list of product_tags Returns a list of model.Content. """ entitled_content = [] content_access_entitlement_content = {} content_labels = set() + if use_os_release_product: + all_product_tags = add_os_product_tags(ent_source.product_tags) + else: + all_product_tags = ent_source.product_tags log.debug("Searching for content of type: %s" % content_type) for entitlement in ent_source: for content in entitlement.contents: # this is basically matching_content from repolib if content.content_type.lower() == content_type.lower() and content_tag_match( - content.tags, ent_source.product_tags + content.tags, all_product_tags ): if entitlement.entitlement_type == CONTENT_ACCESS_CERT_TYPE: content_access_entitlement_content[content.label] = content @@ -119,6 +125,19 @@ def find_content(ent_source, content_type=None): return entitled_content +def add_os_product_tags(product_tags): + """Add [os-id]-[os-version] tag to product tag list based on /etc/os-release + + Returns the product_tags including the os-product tag + """ + all_tags = product_tags + + dist_info = HardwareCollector().get_distribution() + os_product = "{name}-{version}".format(name=dist_info[4], version=dist_info[1]) + all_tags.add(os_product) + return all_tags + + def content_tag_match(content_tags, product_tags): """See if content required tags are provided by installed products. diff --git a/src/subscription_manager/repolib.py b/src/subscription_manager/repolib.py index dc649ea65d..6bf4f04650 100644 --- a/src/subscription_manager/repolib.py +++ b/src/subscription_manager/repolib.py @@ -493,13 +493,31 @@ def get_unique_content(self): # assumes items in content_list are hashable return set(content_list) + def use_os_release_product_enabled(self): + try: + use_os_release_product = conf["rhsm"].get_int("use_os_release_product") + except ValueError as e: + log.exception(e) + return False + except configparser.Error as e: + log.exception(e) + return False + else: + if use_os_release_product is None: + return False + return bool(use_os_release_product) + # Expose as public API for RepoActionInvoker.is_managed, since that # is used by Openshift tooling. # See https://bugzilla.redhat.com/show_bug.cgi?id=1223038 def matching_content(self): content = [] for content_type in ALLOWED_CONTENT_TYPES: - content += model.find_content(self.ent_source, content_type=content_type) + content += model.find_content( + self.ent_source, + content_type=content_type, + use_os_release_product=self.use_os_release_product_enabled(), + ) return content def get_all_content(self, baseurl, ca_cert):