treebuilder: Add branding package to template variables

The branding package name doesn't always match the product name.  This
saves the branding package names as discovered in the enabled repos and
exposes it to the templates as branding.release and branding.logos --
which could potentially be None so the template needs to take that into
account.

Related: rhbz#1956205
This commit is contained in:
Brian C. Lane 2021-05-03 11:18:12 -07:00
parent 6af2e23cc1
commit 980e628835
3 changed files with 53 additions and 41 deletions

View File

@ -218,7 +218,7 @@ def make_runtime(opts, mount_dir, work_dir, size=None):
product = DataHolder(name=opts.project, version=opts.releasever, release="", product = DataHolder(name=opts.project, version=opts.releasever, release="",
variant="", bugurl="", isfinal=False) variant="", bugurl="", isfinal=False)
rb = RuntimeBuilder(product, arch, fake_dbo) rb = RuntimeBuilder(product, arch, fake_dbo, skip_branding=True)
compression, compressargs = squashfs_args(opts) compression, compressargs = squashfs_args(opts)
if opts.squashfs_only: if opts.squashfs_only:

View File

@ -74,11 +74,6 @@ class RuntimeBuilder(object):
add_template_vars=None, add_template_vars=None,
skip_branding=False): skip_branding=False):
root = dbo.conf.installroot root = dbo.conf.installroot
# use a copy of product so we can modify it locally
product = product.copy()
product.name = product.name.lower()
self.vars = DataHolder(arch=arch, product=product, dbo=dbo, root=root,
basearch=arch.basearch, libdir=arch.libdir)
self.dbo = dbo self.dbo = dbo
self._runner = LoraxTemplateRunner(inroot=root, outroot=root, self._runner = LoraxTemplateRunner(inroot=root, outroot=root,
dbo=dbo, templatedir=templatedir) dbo=dbo, templatedir=templatedir)
@ -86,20 +81,29 @@ class RuntimeBuilder(object):
self.add_template_vars = add_template_vars or {} self.add_template_vars = add_template_vars or {}
self._installpkgs = installpkgs or [] self._installpkgs = installpkgs or []
self._excludepkgs = excludepkgs or [] self._excludepkgs = excludepkgs or []
self._runner.defaults = self.vars
self.dbo.reset() self.dbo.reset()
self._skip_branding = skip_branding
def _install_branding(self): # use a copy of product so we can modify it locally
product = product.copy()
product.name = product.name.lower()
self._branding = self.get_branding(skip_branding, product)
self.vars = DataHolder(arch=arch, product=product, dbo=dbo, root=root,
basearch=arch.basearch, libdir=arch.libdir,
branding=self._branding)
self._runner.defaults = self.vars
def get_branding(self, skip, product):
"""Select the branding from the available 'system-release' packages """Select the branding from the available 'system-release' packages
The *best* way to control this is to have a single package in the repo provide 'system-release' The *best* way to control this is to have a single package in the repo provide 'system-release'
When there are more than 1 package it will: When there are more than 1 package it will:
- Make a list of the available packages - Make a list of the available packages
- If variant is set look for a package ending with lower(variant) and use that - If variant is set look for a package ending with lower(variant) and use that
- If there are one or more non-generic packages, use the first one after sorting - If there are one or more non-generic packages, use the first one after sorting
Returns the package names of the system-release and release logos package
""" """
if self._skip_branding: if skip:
return return DataHolder(release=None, logos=None)
release = None release = None
q = self.dbo.sack.query() q = self.dbo.sack.query()
@ -108,11 +112,11 @@ class RuntimeBuilder(object):
if not p.name.startswith("generic")]) if not p.name.startswith("generic")])
if not pkgs: if not pkgs:
logger.error("No system-release packages found, could not get the release") logger.error("No system-release packages found, could not get the release")
return return DataHolder(release=None, logos=None)
logger.debug("system-release packages: %s", pkgs) logger.debug("system-release packages: %s", pkgs)
if self.vars.product.variant: if product.variant:
variant = [p for p in pkgs if p.endswith("-"+self.vars.product.variant.lower())] variant = [p for p in pkgs if p.endswith("-"+product.variant.lower())]
if variant: if variant:
release = variant[0] release = variant[0]
if not release: if not release:
@ -120,20 +124,25 @@ class RuntimeBuilder(object):
# release # release
logger.info('got release: %s', release) logger.info('got release: %s', release)
self._runner.installpkg(release)
# logos # logos uses the basename from release (fedora, redhat, centos, ...)
release, _suffix = release.split('-', 1) logos, _suffix = release.split('-', 1)
self._runner.installpkg('%s-logos' % release) return DataHolder(release=release, logos=logos+"-logos")
def install(self): def install(self):
'''Install packages and do initial setup with runtime-install.tmpl''' '''Install packages and do initial setup with runtime-install.tmpl'''
self._install_branding() if self._branding.release:
self._runner.installpkg(self._branding.release)
if self._branding.logos:
self._runner.installpkg(self._branding.logos)
if len(self._installpkgs) > 0: if len(self._installpkgs) > 0:
self._runner.installpkg(*self._installpkgs) self._runner.installpkg(*self._installpkgs)
if len(self._excludepkgs) > 0: if len(self._excludepkgs) > 0:
self._runner.removepkg(*self._excludepkgs) self._runner.removepkg(*self._excludepkgs)
self._runner.run("runtime-install.tmpl") self._runner.run("runtime-install.tmpl")
for tmpl in self.add_templates: for tmpl in self.add_templates:
self._runner.run(tmpl, **self.add_template_vars) self._runner.run(tmpl, **self.add_template_vars)

View File

@ -63,7 +63,7 @@ def makeFakeRPM(repo_dir, name, epoch, version, release, files=None, provides=No
class InstallBrandingTestCase(unittest.TestCase): class InstallBrandingTestCase(unittest.TestCase):
def install_branding(self, repo_dir, variant=None, skip_branding=False): def install_branding(self, repo_dir, variant=None, skip_branding=False):
"""Run the _install_branding and return the names of the installed packages""" """Run the get_branding function in a test repo"""
with tempfile.TemporaryDirectory(prefix="lorax.test.") as root_dir: with tempfile.TemporaryDirectory(prefix="lorax.test.") as root_dir:
dbo = get_dnf_base_object(root_dir, ["file://"+repo_dir], enablerepos=[], disablerepos=[]) dbo = get_dnf_base_object(root_dir, ["file://"+repo_dir], enablerepos=[], disablerepos=[])
self.assertTrue(dbo is not None) self.assertTrue(dbo is not None)
@ -72,11 +72,7 @@ class InstallBrandingTestCase(unittest.TestCase):
variant=variant, bugurl="http://none", isfinal=True) variant=variant, bugurl="http://none", isfinal=True)
arch = ArchData(os.uname().machine) arch = ArchData(os.uname().machine)
rb = RuntimeBuilder(product, arch, dbo, skip_branding=skip_branding) rb = RuntimeBuilder(product, arch, dbo, skip_branding=skip_branding)
rb._install_branding() return rb._branding
dbo.resolve()
self.assertTrue(dbo.transaction is not None)
return sorted(p.name for p in dbo.transaction.install_set)
def test_no_pkgs(self): def test_no_pkgs(self):
"""Test with a repo with no system-release packages""" """Test with a repo with no system-release packages"""
@ -85,8 +81,9 @@ class InstallBrandingTestCase(unittest.TestCase):
makeFakeRPM(repo_dir, "fake-milhouse", 0, "1.0.0", "1") makeFakeRPM(repo_dir, "fake-milhouse", 0, "1.0.0", "1")
os.system("createrepo_c " + repo_dir) os.system("createrepo_c " + repo_dir)
pkgs = self.install_branding(repo_dir) branding = self.install_branding(repo_dir)
self.assertEqual(pkgs, []) self.assertEqual(branding.release, None)
self.assertEqual(branding.logos, None)
def test_generic_pkg(self): def test_generic_pkg(self):
"""Test with a repo with only a generic-release package""" """Test with a repo with only a generic-release package"""
@ -95,8 +92,9 @@ class InstallBrandingTestCase(unittest.TestCase):
makeFakeRPM(repo_dir, "generic-release", 0, "33", "1", ["/etc/system-release"], ["system-release"]) makeFakeRPM(repo_dir, "generic-release", 0, "33", "1", ["/etc/system-release"], ["system-release"])
os.system("createrepo_c " + repo_dir) os.system("createrepo_c " + repo_dir)
pkgs = self.install_branding(repo_dir) branding = self.install_branding(repo_dir)
self.assertEqual(pkgs, []) self.assertEqual(branding.release, None)
self.assertEqual(branding.logos, None)
def test_two_pkgs(self): def test_two_pkgs(self):
"""Test with a repo with generic-release, and a fedora-release package""" """Test with a repo with generic-release, and a fedora-release package"""
@ -107,12 +105,14 @@ class InstallBrandingTestCase(unittest.TestCase):
makeFakeRPM(repo_dir, "fedora-logos", 0, "33", "1") makeFakeRPM(repo_dir, "fedora-logos", 0, "33", "1")
os.system("createrepo_c " + repo_dir) os.system("createrepo_c " + repo_dir)
pkgs = self.install_branding(repo_dir) branding = self.install_branding(repo_dir)
self.assertEqual(pkgs, ["fedora-logos", "fedora-release"]) self.assertEqual(branding.release, "fedora-release")
self.assertEqual(branding.logos, "fedora-logos")
# Test with a variant set, but not available # Test with a variant set, but not available
pkgs = self.install_branding(repo_dir, variant="workstation") branding = self.install_branding(repo_dir, variant="workstation")
self.assertEqual(pkgs, ["fedora-logos", "fedora-release"]) self.assertEqual(branding.release, "fedora-release")
self.assertEqual(branding.logos, "fedora-logos")
def test_three_pkgs(self): def test_three_pkgs(self):
"""Test with a repo with generic-release, fedora-release, fedora-release-workstation package""" """Test with a repo with generic-release, fedora-release, fedora-release-workstation package"""
@ -124,16 +124,18 @@ class InstallBrandingTestCase(unittest.TestCase):
makeFakeRPM(repo_dir, "fedora-release-workstation", 0, "33", "1", ["/etc/system-release"], ["system-release"]) makeFakeRPM(repo_dir, "fedora-release-workstation", 0, "33", "1", ["/etc/system-release"], ["system-release"])
os.system("createrepo_c " + repo_dir) os.system("createrepo_c " + repo_dir)
pkgs = self.install_branding(repo_dir) branding = self.install_branding(repo_dir)
self.assertEqual(pkgs, ["fedora-logos", "fedora-release"]) self.assertEqual(branding.release, "fedora-release")
self.assertEqual(branding.logos, "fedora-logos")
# Test with a variant set # Test with a variant set
pkgs = self.install_branding(repo_dir, variant="workstation") branding = self.install_branding(repo_dir, variant="workstation")
self.assertEqual(pkgs, ["fedora-logos", "fedora-release-workstation"]) self.assertEqual(branding.release, "fedora-release-workstation")
self.assertEqual(branding.logos, "fedora-logos")
# Test with a variant set, but not available # Test with a variant set, but not available
pkgs = self.install_branding(repo_dir, variant="server") branding = self.install_branding(repo_dir, variant="server")
self.assertEqual(pkgs, ["fedora-logos", "fedora-release"]) self.assertEqual(branding.release, "fedora-release")
def test_skip_branding(self): def test_skip_branding(self):
"""Test disabled branding""" """Test disabled branding"""
@ -142,5 +144,6 @@ class InstallBrandingTestCase(unittest.TestCase):
makeFakeRPM(repo_dir, "fedora-logos", 0, "33", "1") makeFakeRPM(repo_dir, "fedora-logos", 0, "33", "1")
os.system("createrepo_c " + repo_dir) os.system("createrepo_c " + repo_dir)
pkgs = self.install_branding(repo_dir, skip_branding=True) branding = self.install_branding(repo_dir, skip_branding=True)
self.assertEqual(pkgs, []) self.assertEqual(branding.release, None)
self.assertEqual(branding.logos, None)