Skip to content

Commit

Permalink
Add os product tag based on /etc/os-release
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbernhard committed Dec 20, 2022
1 parent 455f21d commit 9d9be80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/subscription_manager/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging

from rhsm.certificate2 import CONTENT_ACCESS_CERT_TYPE
from rhsmlib.facts.hwprobe import HardwareCollector

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,22 +90,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
Expand All @@ -119,6 +126,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.
Expand Down
20 changes: 19 additions & 1 deletion src/subscription_manager/repolib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9d9be80

Please sign in to comment.