installertreemedia: Don't carry around _Distro internally

Detect the distro, and pull out all the data we need into an internal
cache. This will simplify future improvements
This commit is contained in:
Cole Robinson 2019-01-31 13:42:02 -05:00
parent bcb13bfbfa
commit 41b276ffcd
3 changed files with 44 additions and 26 deletions

View File

@ -103,21 +103,28 @@ def _testGuest(testdata, guest):
if guest is xenguest: if guest is xenguest:
return return
# Do this only after the distro detection, since we actually need
# to fetch files for that part
treemedia = installer._treemedia # pylint: disable=protected-access
fetcher = treemedia._cached_fetcher # pylint: disable=protected-access
def fakeAcquireFile(filename):
logging.debug("Fake acquiring %s", filename)
return filename
fetcher.acquireFile = fakeAcquireFile
# Fetch regular kernel # Fetch regular kernel
store = installer._treemedia._cached_store kernel, initrd, kernelargs = treemedia.prepare(guest, meter)
kernel, initrd = store.check_kernel_paths()
dummy = initrd dummy = initrd
if testdata.kernelregex and not re.match(testdata.kernelregex, kernel): if testdata.kernelregex and not re.match(testdata.kernelregex, kernel):
raise AssertionError("kernel=%s but testdata.kernelregex='%s'" % raise AssertionError("kernel=%s but testdata.kernelregex='%s'" %
(kernel, testdata.kernelregex)) (kernel, testdata.kernelregex))
kernelargs = store.get_kernel_url_arg()
if testdata.kernelarg == "None": if testdata.kernelarg == "None":
if bool(kernelargs): if bool(kernelargs):
raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'" raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'"
% (kernelargs, testdata.kernelarg)) % (kernelargs, testdata.kernelarg))
elif testdata.kernelarg: elif testdata.kernelarg:
if not kernelargs == testdata.kernelarg: if testdata.kernelarg != str(kernelargs).split("=")[0]:
raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'" raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'"
% (kernelargs, testdata.kernelarg)) % (kernelargs, testdata.kernelarg))

View File

@ -27,6 +27,13 @@ def _is_url(url):
url.startswith("ftp://")) url.startswith("ftp://"))
class _LocationData(object):
def __init__(self, os_variant, kernel_pairs, kernel_url_arg):
self.os_variant = os_variant
self.kernel_pairs = kernel_pairs
self.kernel_url_arg = kernel_url_arg
class InstallerTreeMedia(object): class InstallerTreeMedia(object):
""" """
Class representing --location Tree media. Can be one of Class representing --location Tree media. Can be one of
@ -61,7 +68,7 @@ class InstallerTreeMedia(object):
self.initrd_injections = [] self.initrd_injections = []
self._cached_fetcher = None self._cached_fetcher = None
self._cached_store = None self._cached_data = None
self._tmpfiles = [] self._tmpfiles = []
self._tmpvols = [] self._tmpvols = []
@ -98,23 +105,33 @@ class InstallerTreeMedia(object):
self._cached_fetcher.meter = meter self._cached_fetcher.meter = meter
return self._cached_fetcher return self._cached_fetcher
def _get_store(self, guest, fetcher): def _get_cached_data(self, guest, fetcher):
if not self._cached_store: if not self._cached_data:
self._cached_store = urldetect.getDistroStore(guest, fetcher) store = urldetect.getDistroStore(guest, fetcher)
return self._cached_store self._cached_data = _LocationData(
store.get_osdict_info(),
store.get_kernel_paths(),
store.get_kernel_url_arg())
return self._cached_data
def _prepare_kernel_url(self, guest, fetcher): def _prepare_kernel_url(self, guest, fetcher):
store = self._get_store(guest, fetcher) cache = self._get_cached_data(guest, fetcher)
kernelpath, initrdpath = store.check_kernel_paths()
def _check_kernel_pairs():
for kpath, ipath in cache.kernel_pairs:
if fetcher.hasFile(kpath) and fetcher.hasFile(ipath):
return kpath, ipath
raise RuntimeError(_("Couldn't find kernel for install tree."))
kernelpath, initrdpath = _check_kernel_pairs()
kernel = fetcher.acquireFile(kernelpath) kernel = fetcher.acquireFile(kernelpath)
self._tmpfiles.append(kernel) self._tmpfiles.append(kernel)
initrd = fetcher.acquireFile(initrdpath) initrd = fetcher.acquireFile(initrdpath)
self._tmpfiles.append(initrd) self._tmpfiles.append(initrd)
args = "" args = ""
if not self.location.startswith("/") and store.get_kernel_url_arg(): if not self.location.startswith("/") and cache.kernel_url_arg:
args += "%s=%s" % (store.get_kernel_url_arg(), self.location) args += "%s=%s" % (cache.kernel_url_arg, self.location)
perform_initrd_injections(initrd, perform_initrd_injections(initrd,
self.initrd_injections, self.initrd_injections,
@ -156,5 +173,5 @@ class InstallerTreeMedia(object):
def detect_distro(self, guest): def detect_distro(self, guest):
fetcher = self._get_fetcher(guest, None) fetcher = self._get_fetcher(guest, None)
store = self._get_store(guest, fetcher) cache = self._get_cached_data(guest, fetcher)
return store.get_osdict_info() return cache.os_variant

View File

@ -292,7 +292,7 @@ def getDistroStore(guest, fetcher):
if not sclass.is_valid(cache): if not sclass.is_valid(cache):
continue continue
store = sclass(fetcher, arch, _type, cache) store = sclass(fetcher.location, arch, _type, cache)
logging.debug("Detected class=%s osvariant=%s", logging.debug("Detected class=%s osvariant=%s",
store.__class__.__name__, store.get_osdict_info()) store.__class__.__name__, store.get_osdict_info())
return store return store
@ -325,11 +325,10 @@ class _DistroTree(object):
PRETTY_NAME = None PRETTY_NAME = None
matching_distros = [] matching_distros = []
def __init__(self, fetcher, arch, vmtype, cache): def __init__(self, location, arch, vmtype, cache):
self.fetcher = fetcher
self.type = vmtype self.type = vmtype
self.arch = arch self.arch = arch
self.uri = fetcher.location self.uri = location
self.cache = cache self.cache = cache
if self.cache.libosinfo_os_variant: if self.cache.libosinfo_os_variant:
@ -374,13 +373,8 @@ class _DistroTree(object):
def is_valid(cls, cache): def is_valid(cls, cache):
raise NotImplementedError raise NotImplementedError
def check_kernel_paths(self): def get_kernel_paths(self):
for kpath, ipath in self._kernel_paths: return self._kernel_paths
if self.fetcher.hasFile(kpath) and self.fetcher.hasFile(ipath):
return kpath, ipath
raise RuntimeError(_("Couldn't find kernel for "
"%(distro)s tree.") %
{"distro": self.PRETTY_NAME})
def get_osdict_info(self): def get_osdict_info(self):
""" """