summaryrefslogtreecommitdiff
path: root/bin
diff options
authorJeff Lane <jeffrey.lane@canonical.com>2016-09-12 18:12:41 +0000
committerSylvain Pineau <>2016-09-12 18:12:41 +0000
commitc23f0b0a40f1b2b4c39cb352e9db96519627f05f (patch)
treee9fad2e8786ab911c9a810c7b3d1f807c8d2aa04 /bin
parent2916c166fe827941d8e37d355418a136cfb2897d (diff)
parentb09d83b416924f6723b93c0450346b79f8e08525 (diff)
"automatic merge of lp:~bladernr/checkbox/1621986-virt-accept-urls/ by tarmac [r=sylvain-pineau,bladernr][bug=1621986][author=bladernr]"
Diffstat (limited to 'bin')
-rwxr-xr-xbin/virtualization104
1 files changed, 57 insertions, 47 deletions
diff --git a/bin/virtualization b/bin/virtualization
index 65fee88..fe59656 100755
--- a/bin/virtualization
+++ b/bin/virtualization
@@ -44,13 +44,11 @@ import tempfile
import tarfile
import time
import urllib.request
+from urllib.parse import urlparse
DEFAULT_TIMEOUT = 500
-class XENTest(object):
- pass
-
# The "TAR" type is a tarball that contains both
# a disk image and a kernel binary. This is useful
# on architectures that don't (yet) have a bootloader
@@ -110,9 +108,9 @@ QEMU_ARCH_CONFIG = {
'qemu_bin': 'qemu-system-ppc64',
'qemu_disk_type': QEMU_DISK_TYPE_VIRTIO,
'qemu_extra_args': [
- '-enable-kvm',
+ '-enable-kvm',
'-machine', 'pseries,usb=off',
- '-cpu', 'POWER8',
+ '-cpu', 'POWER8',
],
},
's390x': {
@@ -121,7 +119,7 @@ QEMU_ARCH_CONFIG = {
'qemu_bin': 'qemu-system-s390x',
'qemu_disk_type': QEMU_DISK_TYPE_VIRTIO,
'qemu_extra_args': [
- '-enable-kvm',
+ '-enable-kvm',
'-machine', 's390-ccw-virtio-2.5',
],
},
@@ -200,35 +198,68 @@ class KVMTest(object):
self.arch = check_output(['dpkg', '--print-architecture'],
universal_newlines=True).strip()
self.qemu_config = QEMU_ARCH_CONFIG[self.arch]
+ self.release = lsb_release.get_lsb_information()["CODENAME"]
- def download_image(self):
+ def url_to_path(self, image_path):
"""
- Downloads Cloud image for same release as host machine
+ Test the provided image path to determine if it's a URL or or a simple
+ file path
+ """
+ url = urlparse(image_path)
+ if url.scheme == '' or url.scheme == 'file':
+ # Gives us path wheter we specify a filesystem path or a file URL
+ logging.debug("Cloud image exists locally at %s" % url.path)
+ return url.path
+ elif url.scheme == 'http' or url.scheme == 'ftp':
+ # Gives us the stuff needed to build the URL to download the image
+ return self.download_image(image_path)
+
+ def construct_cloud_filename(self):
+ """
+ Build a URL for official Ubuntu images hosted either at
+ cloud-images.ubuntu.com or on a maas server hosting a mirror of
+ cloud-images.ubuntu.com
"""
-
- # Check Ubuntu release info. Example {quantal, precise}
- release = lsb_release.get_lsb_information()["CODENAME"]
-
- # Construct URL
- cloud_url = "http://cloud-images.ubuntu.com"
-
if self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_TAR:
cloud_iso = "%s-server-cloudimg-%s.tar.gz" % (
- release, self.qemu_config['cloudimg_arch'])
+ self.release, self.qemu_config['cloudimg_arch'])
elif self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_DISK:
cloud_iso = "%s-server-cloudimg-%s-disk1.img" % (
- release, self.qemu_config['cloudimg_arch'])
+ self.release, self.qemu_config['cloudimg_arch'])
else:
logging.error("Unknown cloud image type")
- return False
- image_url = "/".join((
- cloud_url, release, "current", cloud_iso))
+ sys.exit(1)
+ return cloud_iso
+ def download_image(self, image_url=None):
+ """
+ Downloads Cloud image for same release as host machine
+ """
+ if image_url is None:
+ # If we have not specified a URL to get our images from, default
+ # to ubuntu.com
+ cloud_url = "http://cloud-images.ubuntu.com"
+ cloud_iso = self.construct_cloud_filename()
+ full_url = "/".join((
+ cloud_url, self.release, "current", cloud_iso))
+ else:
+ url = urlparse(image_url)
+ if url.path.endswith('/') or url.path == '':
+ # If we have a relative URL (MAAS server mirror)
+ cloud_url = image_url
+ cloud_iso = self.construct_cloud_filename()
+ full_url = "/".join((
+ cloud_url, cloud_iso))
+ else:
+ # Assume anything else is an absolute URL to a remote server
+ cloud_iso = url.path.split('/')[-1]
+ cloud_url = "{}://{}".format(url.scheme, url.netloc)
+ full_url = image_url
logging.debug("Downloading {}, from {}".format(cloud_iso, cloud_url))
# Attempt download
try:
- resp = urllib.request.urlretrieve(image_url, cloud_iso)
+ resp = urllib.request.urlretrieve(full_url, cloud_iso)
except (IOError,
OSError,
urllib.error.HTTPError,
@@ -362,6 +393,10 @@ final_message: CERTIFICATION BOOT COMPLETE
logging.debug('No image specified, downloading one now.')
# Download cloud image
self.image = self.download_image()
+ else:
+ logging.debug('Cloud image location specified: %s.' %
+ self.image)
+ self.image = self.url_to_path(self.image)
if self.image and os.path.isfile(self.image):
@@ -410,35 +445,10 @@ final_message: CERTIFICATION BOOT COMPLETE
def test_kvm(args):
print("Executing KVM Test", file=sys.stderr)
- DEFAULT_CFG = "/etc/xdg/virtualization.cfg"
image = ""
timeout = ""
- # Configuration data can come from three sources.
- # Lowest priority is the config file.
- config_file = DEFAULT_CFG
- config = configparser.SafeConfigParser()
-
- try:
- config.readfp(open(config_file))
- except IOError:
- logging.warn("Config file %s was not found" % DEFAULT_CFG)
- logging.warn("Now looking for ENV variables and command line arguments instead")
- else:
- try:
- timeout = config.getfloat("KVM", "timeout")
- except ValueError:
- logging.warning('Invalid or Empty timeout in config file. '
- 'Falling back to default')
- except configparser.NoSectionError as e:
- logging.exception(e)
-
- try:
- image = config.get("KVM", "image")
- except configparser.NoSectionError:
- logging.exception('Invalid or Empty image in config file.')
-
- # Next in priority are environment variables.
+ # First in priority are environment variables.
if 'KVM_TIMEOUT' in os.environ:
try:
timeout = float(os.environ['KVM_TIMEOUT'])