Support image upload from a file location

The current VMTP supports uploading an image from a specified
URL. This commit will add the support to upload and create a
glance image from a specified file location.

Change-Id: I85698934d1eb4bf2231a4f0364c61f03fbd1a5fd
This commit is contained in:
pradeepcsekar 2016-02-19 15:22:29 -08:00
parent 70d915212a
commit 5da965319d
3 changed files with 24 additions and 6 deletions

View File

@ -45,6 +45,9 @@ dns_nameservers: [ '8.8.8.8' ]
# #
# A link to a Ubuntu Server 14.04 qcow2 image can be used here: # A link to a Ubuntu Server 14.04 qcow2 image can be used here:
# https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img # https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
#
# To upload the image as a file, prepend it with file:// like below
# file://<location of the image>
vm_image_url: '' vm_image_url: ''
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

View File

@ -42,10 +42,20 @@ class Compute(object):
''' '''
retry = 0 retry = 0
try: try:
# Upload the image # check image is file/url based.
img = glance_client.images.create( file_prefix = "file://"
name=str(final_image_name), disk_format="qcow2", container_format="bare", if image_url.startswith(file_prefix):
is_public=True, copy_from=image_url) image_location = image_url.split(file_prefix)[1]
with open(image_location) as f_image:
img = glance_client.images.create(
name=str(final_image_name), disk_format="qcow2",
container_format="bare", is_public=True, data=f_image)
else:
# Upload the image
img = glance_client.images.create(
name=str(final_image_name), disk_format="qcow2",
container_format="bare", is_public=True,
copy_from=image_url)
# Check for the image in glance # Check for the image in glance
while img.status in ['queued', 'saving'] and retry < retry_count: while img.status in ['queued', 'saving'] and retry < retry_count:
@ -61,11 +71,15 @@ class Compute(object):
print "Cannot upload image without admin access. Please make sure the "\ print "Cannot upload image without admin access. Please make sure the "\
"image is uploaded and is either public or owned by you." "image is uploaded and is either public or owned by you."
return False return False
except IOError:
# catch the exception for file based errors.
print "Failed while uploading the image. Please make sure the " \
"image at the specified location %s is correct." % image_url
return False
except Exception: except Exception:
print "Failed while uploading the image, please make sure the cloud "\ print "Failed while uploading the image, please make sure the cloud "\
"under test has the access to URL: %s." % image_url "under test has the access to URL: %s." % image_url
return False return False
return True return True
def delete_image(self, glance_client, img_name): def delete_image(self, glance_client, img_name):

View File

@ -822,7 +822,8 @@ def parse_opts_from_cli():
parser.add_argument('--vm-image-url', dest='vm_image_url', parser.add_argument('--vm-image-url', dest='vm_image_url',
action='store', action='store',
help='URL to a Linux image in qcow2 format that can be downloaded from', help='URL to a Linux image in qcow2 format that can be downloaded from'
'or location of the image file with prefix file://',
metavar='<url_to_image>') metavar='<url_to_image>')
parser.add_argument('--test-description', dest='test_description', parser.add_argument('--test-description', dest='test_description',