From 26823e56b9219fca3d0b8e6e8dcd79bb272009f7 Mon Sep 17 00:00:00 2001 From: Jeff Lane Date: Wed, 8 Feb 2017 19:48:01 -0500 Subject: bin/virtualization: fixed bug that broke download of cloud image. LP: #1662580 --- bin/virtualization | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/virtualization b/bin/virtualization index 0ee74e9..5b70a54 100755 --- a/bin/virtualization +++ b/bin/virtualization @@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ - from argparse import ArgumentParser import configparser from glob import glob @@ -222,7 +221,7 @@ class KVMTest(object): cloud-images.ubuntu.com or on a maas server hosting a mirror of cloud-images.ubuntu.com """ - def _construct_filename(alt_pattern=None): + def _construct_filename(alt_pattern=None, initial_url=None): if self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_TAR: cloud_iso = "%s-server-cloudimg-%s.tar_gz" % ( self.release, self.qemu_config['cloudimg_arch']) @@ -233,6 +232,10 @@ class KVMTest(object): elif self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_DISK: cloud_iso = "%s-server-cloudimg-%s-disk1.img" % ( self.release, self.qemu_config['cloudimg_arch']) + elif initial_url is not none: + # LP 1662580 - if we pass a full URL, assume the last piece is + # the filname and return that. + cloud_iso = initial_url.split('/')[-1] else: logging.error("Unknown cloud image type") sys.exit(1) @@ -306,17 +309,18 @@ class KVMTest(object): sys.exit(1) else: full_url = image_url + cloud_iso = _construct_filename(initial_url=full_url) - return full_url + return full_url, cloud_iso def download_image(self, image_url=None): """ Downloads Cloud image for same release as host machine """ if image_url is None: - full_url = self.construct_cloud_url() + full_url, cloud_iso = self.construct_cloud_url() else: - full_url = self.construct_cloud_url(image_url) + full_url, cloud_iso = self.construct_cloud_url(image_url) logging.debug("Acquiring cloud image from: {}".format(full_url)) # Attempt download -- cgit v1.2.3 From f4731f869fc726dca4ee5ec39e4c03a06b63ad32 Mon Sep 17 00:00:00 2001 From: Jeff Lane Date: Mon, 13 Feb 2017 14:08:19 -0500 Subject: Fixes from review comments. Also fixed bug caught while testing other fixes when a bad URL is provided causing _test_url to traceback. Also a few PEP8 fixes. --- bin/virtualization | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/bin/virtualization b/bin/virtualization index 5b70a54..ff11061 100755 --- a/bin/virtualization +++ b/bin/virtualization @@ -31,6 +31,7 @@ import re import logging import lsb_release import requests +from requests.packages.urllib3.exceptions import NewConnectionError import shlex import signal from subprocess import ( @@ -215,7 +216,7 @@ class KVMTest(object): # Gives us the stuff needed to build the URL to download the image return self.download_image(image_path) - def construct_cloud_url(self, image_url=None): + def get_image_name_and_url(self, image_url=None): """ Build a URL for official Ubuntu images hosted either at cloud-images.ubuntu.com or on a maas server hosting a mirror of @@ -232,7 +233,7 @@ class KVMTest(object): elif self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_DISK: cloud_iso = "%s-server-cloudimg-%s-disk1.img" % ( self.release, self.qemu_config['cloudimg_arch']) - elif initial_url is not none: + elif initial_url: # LP 1662580 - if we pass a full URL, assume the last piece is # the filname and return that. cloud_iso = initial_url.split('/')[-1] @@ -247,7 +248,13 @@ class KVMTest(object): def _test_cloud_url(url): # test our URL to make sure it's reachable - ret = requests.head(url) + try: + ret = requests.head(url) + except (OSError, NewConnectionError) as e: + logging.error("Unable to connect to {}".format(url)) + logging.error(e) + return False + if ret.status_code is not 200: return False else: @@ -279,10 +286,10 @@ class KVMTest(object): else: url = urlparse(image_url) if ( - url.path.endswith('/') or - url.path == '' or + url.path.endswith('/') or + url.path == '' or not url.path.endswith(".img") - ): + ): # If we have a relative URL (local copies of official images) # http://192.168.0.1/ or http://192.168.0.1/images/ cloud_iso = _construct_filename() @@ -310,7 +317,7 @@ class KVMTest(object): else: full_url = image_url cloud_iso = _construct_filename(initial_url=full_url) - + return full_url, cloud_iso def download_image(self, image_url=None): @@ -318,9 +325,9 @@ class KVMTest(object): Downloads Cloud image for same release as host machine """ if image_url is None: - full_url, cloud_iso = self.construct_cloud_url() + full_url, cloud_iso = self.get_image_name_and_url() else: - full_url, cloud_iso = self.construct_cloud_url(image_url) + full_url, cloud_iso = self.get_image_name_and_url(image_url) logging.debug("Acquiring cloud image from: {}".format(full_url)) # Attempt download -- cgit v1.2.3