urldetect: Move treeinfo media checking into Treeinfo class

Rather than in the base Distro class. Simplifies things a bit
This commit is contained in:
Cole Robinson 2018-03-29 16:14:08 -04:00
parent 7dacc8502e
commit b9e72b8609
4 changed files with 49 additions and 53 deletions

View File

@ -90,7 +90,7 @@ def _fetch_distro(distro):
try: try:
fetcher.prepareLocation() fetcher.prepareLocation()
store = urldetect.getDistroStore(guest, fetcher) store = urldetect.getDistroStore(guest, fetcher)
kernel, initrd, ignore = store.acquireKernel(guest) kernel, initrd, ignore = store.acquireKernel()
cleanup.append(kernel) cleanup.append(kernel)
cleanup.append(initrd) cleanup.append(initrd)
distro.kernel = kernel distro.kernel = kernel

View File

@ -207,15 +207,15 @@ def _testURL(fetcher, testdata):
# Fetch boot iso # Fetch boot iso
if testdata.testbootiso: if testdata.testbootiso:
boot = hvmstore.acquireBootDisk(hvmguest) boot = hvmstore.acquireBootISO()
logging.debug("acquireBootDisk: %s", str(boot)) logging.debug("acquireBootISO: %s", str(boot))
if boot is not True: if boot is not True:
raise AssertionError("%s-%s: bootiso fetching failed" % raise AssertionError("%s-%s: bootiso fetching failed" %
(distname, arch)) (distname, arch))
# Fetch regular kernel # Fetch regular kernel
kern = hvmstore.acquireKernel(hvmguest) kern = hvmstore.acquireKernel()
logging.debug("acquireKernel (hvm): %s", str(kern)) logging.debug("acquireKernel (hvm): %s", str(kern))
if kern[0] is not True or kern[1] is not True: if kern[0] is not True or kern[1] is not True:
@ -224,7 +224,7 @@ def _testURL(fetcher, testdata):
# Fetch xen kernel # Fetch xen kernel
if xenstore: if xenstore:
kern = xenstore.acquireKernel(xenguest) kern = xenstore.acquireKernel()
logging.debug("acquireKernel (xen): %s", str(kern)) logging.debug("acquireKernel (xen): %s", str(kern))
if kern[0] is not True or kern[1] is not True: if kern[0] is not True or kern[1] is not True:

View File

@ -108,13 +108,13 @@ class DistroInstaller(Installer):
def _prepare_cdrom_url(self, guest, fetcher): def _prepare_cdrom_url(self, guest, fetcher):
store = self._get_store(guest, fetcher) store = self._get_store(guest, fetcher)
media = store.acquireBootDisk(guest) media = store.acquireBootISO()
self._tmpfiles.append(media) self._tmpfiles.append(media)
return media return media
def _prepare_kernel_url(self, guest, fetcher): def _prepare_kernel_url(self, guest, fetcher):
store = self._get_store(guest, fetcher) store = self._get_store(guest, fetcher)
kernel, initrd, args = store.acquireKernel(guest) kernel, initrd, args = store.acquireKernel()
self._tmpfiles.append(kernel) self._tmpfiles.append(kernel)
if initrd: if initrd:
self._tmpfiles.append(initrd) self._tmpfiles.append(initrd)

View File

@ -246,37 +246,33 @@ class Distro(object):
"""Determine if uri points to a tree of the store's distro""" """Determine if uri points to a tree of the store's distro"""
raise NotImplementedError raise NotImplementedError
def acquireKernel(self, guest): def acquireKernel(self):
kernelpath = None kernelpath = None
initrdpath = None initrdpath = None
if self.treeinfo: for kpath, ipath in self._kernel_paths:
try: if self.fetcher.hasFile(kpath) and self.fetcher.hasFile(ipath):
kernelpath = self._getTreeinfoMedia("kernel") kernelpath = kpath
initrdpath = self._getTreeinfoMedia("initrd") initrdpath = ipath
except configparser.NoSectionError: break
pass
if not kernelpath or not initrdpath:
paths = self._kernel_paths
for kpath, ipath in paths:
if self.fetcher.hasFile(kpath) and self.fetcher.hasFile(ipath):
kernelpath = kpath
initrdpath = ipath
if not kernelpath or not initrdpath: if not kernelpath or not initrdpath:
raise RuntimeError(_("Couldn't find kernel for " raise RuntimeError(_("Couldn't find kernel for "
"%(distro)s tree.") % "%(distro)s tree.") %
{"distro": self.PRETTY_NAME}) {"distro": self.PRETTY_NAME})
return self._kernelFetchHelper(guest, kernelpath, initrdpath) args = ""
if not self.fetcher.location.startswith("/"):
args += "%s=%s" % (self._get_method_arg(), self.fetcher.location)
def acquireBootDisk(self, guest): kernel = self.fetcher.acquireFile(kernelpath)
ignore = guest try:
initrd = self.fetcher.acquireFile(initrdpath)
if self.treeinfo: return kernel, initrd, args
return self.fetcher.acquireFile(self._getTreeinfoMedia("boot.iso")) except Exception:
os.unlink(kernel)
raise
def acquireBootISO(self):
for path in self._boot_iso_paths: for path in self._boot_iso_paths:
if self.fetcher.hasFile(path): if self.fetcher.hasFile(path):
return self.fetcher.acquireFile(path) return self.fetcher.acquireFile(path)
@ -304,14 +300,6 @@ class Distro(object):
def _get_method_arg(self): def _get_method_arg(self):
return "method" return "method"
def _getTreeinfoMedia(self, mediaName):
if self.type == "xen":
t = "xen"
else:
t = self.treeinfo.get("general", "arch")
return self.treeinfo.get("images-%s" % t, mediaName)
def _fetchAndMatchRegex(self, filename, regex): def _fetchAndMatchRegex(self, filename, regex):
# Fetch 'filename' and return True/False if it matches the regex # Fetch 'filename' and return True/False if it matches the regex
try: try:
@ -327,23 +315,6 @@ class Distro(object):
self.__class__.__name__, filename) self.__class__.__name__, filename)
return False return False
def _kernelFetchHelper(self, guest, kernelpath, initrdpath):
# Simple helper for fetching kernel + initrd and performing
# cleanup if necessary
ignore = guest
kernel = self.fetcher.acquireFile(kernelpath)
args = ''
if not self.fetcher.location.startswith("/"):
args += "%s=%s" % (self._get_method_arg(), self.fetcher.location)
try:
initrd = self.fetcher.acquireFile(initrdpath)
return kernel, initrd, args
except Exception:
os.unlink(kernel)
raise
class GenericTreeinfoDistro(Distro): class GenericTreeinfoDistro(Distro):
PRETTY_NAME = "Generic Treeinfo" PRETTY_NAME = "Generic Treeinfo"
@ -363,6 +334,31 @@ class GenericTreeinfoDistro(Distro):
self._detect_version() self._detect_version()
if not self.treeinfo:
return
self._kernel_paths = []
self._boot_iso_paths = []
try:
self._kernel_paths.append(
(self._getTreeinfoMedia("kernel"),
self._getTreeinfoMedia("initrd")))
except Exception:
logging.debug("Failed to parse treeinfo kernel/initrd",
exc_info=True)
try:
self._boot_iso_paths.append(self._getTreeinfoMedia("boot.iso"))
except Exception:
logging.debug("Failed to parse treeinfo boot.iso", exc_info=True)
def _getTreeinfoMedia(self, mediaName):
image_type = self.treeinfo.get("general", "arch")
if self.type == "xen":
image_type = "xen"
return self.treeinfo.get("images-%s" % image_type, mediaName)
def _detect_version(self): def _detect_version(self):
pass pass