Convert lorax-composer yum base object to DNF

This no longer uses the enabled configuration setting to select repos to
use. It uses everything in the repo_dir, and if system repos have not
been disabled it copies them into the repo_dir at startup, overwriting
the previous copy.
This commit is contained in:
Brian C. Lane 2018-04-30 15:23:48 -07:00
parent 9bf93d7154
commit 25394eefb7
3 changed files with 91 additions and 118 deletions

View File

@ -44,8 +44,8 @@ def configure(conf_file="/etc/lorax/composer.conf", root_dir="/", test_config=Fa
conf.add_section("composer")
conf.set("composer", "share_dir", os.path.realpath(joinpaths(root_dir, "/usr/share/lorax/")))
conf.set("composer", "lib_dir", os.path.realpath(joinpaths(root_dir, "/var/lib/lorax/composer/")))
conf.set("composer", "yum_conf", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/yum.conf")))
conf.set("composer", "yum_root", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/yum/root/")))
conf.set("composer", "dnf_conf", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/dnf.conf")))
conf.set("composer", "dnf_root", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/dnf/root/")))
conf.set("composer", "repo_dir", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/repos.d/")))
conf.set("composer", "cache_dir", os.path.realpath(joinpaths(root_dir, "/var/tmp/composer/cache/")))
@ -64,14 +64,14 @@ def configure(conf_file="/etc/lorax/composer.conf", root_dir="/", test_config=Fa
return conf
def make_yum_dirs(conf):
"""Make any missing yum directories
def make_dnf_dirs(conf):
"""Make any missing dnf directories
:param conf: The configuration to use
:type conf: ComposerConfig
:returns: None
"""
for p in ["yum_conf", "repo_dir", "cache_dir", "yum_root"]:
for p in ["dnf_conf", "repo_dir", "cache_dir", "dnf_root"]:
p_dir = os.path.dirname(conf.get("composer", p))
if not os.path.exists(p_dir):
os.makedirs(p_dir)

View File

@ -0,0 +1,86 @@
#
# Copyright (C) 2017-2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# pylint: disable=bad-preconf-access
import logging
log = logging.getLogger("lorax-composer")
import dnf
import dnf.logging
from glob import glob
import os
import shutil
def get_base_object(conf):
"""Get the DNF object with settings from the config file
:param conf: configuration object
:type conf: ComposerParser
:returns: A DNF Base object
:rtype: dnf.Base
"""
cachedir = os.path.abspath(conf.get("composer", "cache_dir"))
dnfconf = os.path.abspath(conf.get("composer", "dnf_conf"))
dnfroot = os.path.abspath(conf.get("composer", "dnf_root"))
repodir = os.path.abspath(conf.get("composer", "repo_dir"))
# Setup the config for the DNF Base object
dbo = dnf.Base()
dbc = dbo.conf
# TODO - Handle this
# dbc.logdir = logdir
dbc.installroot = dnfroot
if not os.path.isdir(dnfroot):
os.makedirs(dnfroot)
dbc.cachedir = cachedir
dbc.reposdir = [repodir]
dbc.install_weak_deps = False
dbc.prepend_installroot('persistdir')
dbc.tsflags.append('nodocs')
if conf.get_default("dnf", "proxy", None):
dbc.proxy = conf.get("dnf", "proxy")
if conf.has_option("dnf", "sslverify") and not conf.getboolean("dnf", "sslverify"):
dbc.sslverify = False
_releasever = conf.get_default("composer", "releasever", None)
if not _releasever:
# Use the releasever of the host system
_releasever = dnf.rpm.detect_releasever("/")
log.info("releasever = %s", _releasever)
dbc.releasever = _releasever
# write the dnf configuration file
with open(dnfconf, "w") as f:
f.write(dbc.dump())
# dnf needs the repos all in one directory, composer uses repodir for this
# if system repos are supposed to be used, copy them into repodir, overwriting any previous copies
if not conf.has_option("repos", "use_system_repos") or conf.getboolean("repos", "use_system_repos"):
for repo_file in glob("/etc/yum.repos.d/*.repo"):
shutil.copy2(repo_file, repodir)
dbo.read_all_repos()
# Update the metadata from the enabled repos to speed up later operations
log.info("Updating repository metadata")
dbo.fill_sack(load_system_repo=False)
dbo.read_comps()
return dbo

View File

@ -1,113 +0,0 @@
#
# Copyright (C) 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# pylint: disable=bad-preconf-access
import logging
log = logging.getLogger("lorax-composer")
import configparser
from fnmatch import fnmatchcase
from glob import glob
import os
import yum
# This is a hack to short circuit yum's internal logging
yum.logginglevels._added_handlers = True
from pylorax.sysutils import joinpaths
def get_base_object(conf):
"""Get the Yum object with settings from the config file
:param conf: configuration object
:type conf: ComposerParser
:returns: A Yum base object
:rtype: YumBase
"""
cachedir = os.path.abspath(conf.get("composer", "cache_dir"))
yumconf = os.path.abspath(conf.get("composer", "yum_conf"))
yumroot = os.path.abspath(conf.get("composer", "yum_root"))
repodir = os.path.abspath(conf.get("composer", "repo_dir"))
c = configparser.ConfigParser()
# add the main section
section = "main"
data = {"cachedir": cachedir,
"keepcache": 0,
"gpgcheck": 0,
"plugins": 0,
"assumeyes": 1,
"reposdir": "",
"tsflags": "nodocs"}
if conf.get_default("yum", "proxy", None):
data["proxy"] = conf.get("yum", "proxy")
if conf.has_option("yum", "sslverify") and not conf.getboolean("yum", "sslverify"):
data["sslverify"] = "0"
c.add_section(section)
list(map(lambda key_value: c.set(section, key_value[0], key_value[1]), list(data.items())))
# write the yum configuration file
with open(yumconf, "w") as f:
c.write(f)
# create the yum base object
yb = yum.YumBase()
yb.preconf.fn = yumconf
yb.preconf.root = yumroot
if not os.path.isdir(yb.preconf.root):
os.makedirs(yb.preconf.root)
_releasever = conf.get_default("composer", "releasever", None)
if not _releasever:
distroverpkg = ['system-release(releasever)', 'redhat-release']
# Use yum private function to guess the releasever
_releasever = yum.config._getsysver("/", distroverpkg)
log.info("releasever = %s", _releasever)
yb.preconf.releasever = _releasever
# Turn on as much yum logging as we can
yb.preconf.debuglevel = 6
yb.preconf.errorlevel = 6
yb.logger.setLevel(logging.DEBUG)
yb.verbose_logger.setLevel(logging.DEBUG)
# Gather up all the available repo files, add the ones matching "repos":"enabled" patterns
enabled_repos = conf.get("repos", "enabled").split(",")
repo_files = glob(joinpaths(repodir, "*.repo"))
if not conf.has_option("repos", "use_system_repos") or conf.getboolean("repos", "use_system_repos"):
repo_files.extend(glob("/etc/yum.repos.d/*.repo"))
for repo_file in repo_files:
name = os.path.basename(repo_file)[:-5]
if any([fnmatchcase(name, pattern) for pattern in enabled_repos]): # pylint: disable=cell-var-from-loop
yb.getReposFromConfigFile(repo_file)
# Update the metadata from the enabled repos to speed up later operations
log.info("Updating yum repository metadata")
for r in yb.repos.sort():
r.metadata_expire = 0
r.mdpolicy = "group:all"
yb.doRepoSetup()
yb.repos.doSetup()
yb.repos.populateSack(mdtype='all', cacheonly=1)
return yb