lorax uses pyanaconda's SimpleConfigParser in three different places (twice with a copy that's been dumped into pylorax, once by importing it), just to do a fairly simple job: read some values out of /etc/os-release. The only value SimpleConfigParser is adding over Python's own ConfigParser here is to read a file with no section headers, and to unquote the values. The cost is either a dependency on pyanaconda, or needing to copy the whole of simpleparser plus some other utility bits from pyanaconda into lorax. This seems like a bad trade-off. This changes the approach: we copy one very simple utility function from pyanaconda (`unquote`), and do some very simple wrapping of ConfigParser to handle reading a file without any section headers, and returning unquoted values. This way we can read what we need out of os-release without needing a dep on pyanaconda or to copy lots of things from it into pylorax. Resolves: #449 Resolves: #450 Signed-off-by: Adam Williamson <awilliam@redhat.com>
105 lines
3.7 KiB
Python
105 lines
3.7 KiB
Python
#
|
|
# 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
|
|
|
|
from pylorax import DEFAULT_PLATFORM_ID
|
|
from pylorax.sysutils import flatconfig
|
|
|
|
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)
|
|
if not os.path.isdir(repodir):
|
|
os.makedirs(repodir)
|
|
|
|
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
|
|
|
|
# DNF 3.2 needs to have module_platform_id set, otherwise depsolve won't work correctly
|
|
if not os.path.exists("/etc/os-release"):
|
|
log.warning("/etc/os-release is missing, cannot determine platform id, falling back to %s", DEFAULT_PLATFORM_ID)
|
|
platform_id = DEFAULT_PLATFORM_ID
|
|
else:
|
|
os_release = flatconfig("/etc/os-release")
|
|
platform_id = os_release.get("PLATFORM_ID", DEFAULT_PLATFORM_ID)
|
|
log.info("Using %s for module_platform_id", platform_id)
|
|
dbc.module_platform_id = platform_id
|
|
|
|
# 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")
|
|
try:
|
|
dbo.fill_sack(load_system_repo=False)
|
|
dbo.read_comps()
|
|
except dnf.exceptions.Error as e:
|
|
log.error("Failed to update metadata: %s", str(e))
|
|
raise RuntimeError("Fetching metadata failed: %s" % str(e))
|
|
|
|
return dbo
|