Allow usage images already in glance
--image argument can now contain an image name already present in glance. Change-Id: I95195fb6ab968ec0e45a996eb87c8716baf3e07f
This commit is contained in:
parent
ebbb91571e
commit
37b5e60948
@ -251,10 +251,10 @@ def get_arg_parser():
|
|||||||
help="""a format of an image to be uploaded to glance.
|
help="""a format of an image to be uploaded to glance.
|
||||||
Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT)
|
Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT)
|
||||||
parser.add_argument('--image', default=C.DEFAULT_IMAGE,
|
parser.add_argument('--image', default=C.DEFAULT_IMAGE,
|
||||||
help="""an image to be uploaded to glance. The name of
|
help="""An image name/path/url to be uploaded to
|
||||||
the image is the leaf name of the path which
|
glance if it's not already there. The name of
|
||||||
can be either a filename or url. Default is
|
the image is the leaf name of the path. Default
|
||||||
'%s'""" % C.DEFAULT_IMAGE)
|
is '%s'""" % C.DEFAULT_IMAGE)
|
||||||
parser.add_argument('--network-id',
|
parser.add_argument('--network-id',
|
||||||
help="""The ID of an existing network in our openstack
|
help="""The ID of an existing network in our openstack
|
||||||
instance with external connectivity""")
|
instance with external connectivity""")
|
||||||
|
@ -19,7 +19,7 @@ import shutil
|
|||||||
from six.moves import urllib
|
from six.moves import urllib
|
||||||
from tempest.lib import exceptions
|
from tempest.lib import exceptions
|
||||||
|
|
||||||
from config_tempest.constants import LOG
|
from config_tempest import constants as C
|
||||||
from config_tempest.services.base import VersionedService
|
from config_tempest.services.base import VersionedService
|
||||||
|
|
||||||
|
|
||||||
@ -52,9 +52,15 @@ class ImageService(VersionedService):
|
|||||||
# for an image which will be uploaded to the glance.
|
# for an image which will be uploaded to the glance.
|
||||||
# image.http_image and image.image_path can be 2 different images.
|
# image.http_image and image.image_path can be 2 different images.
|
||||||
# If image.http_image wasn't set as an override, it will be set to
|
# If image.http_image wasn't set as an override, it will be set to
|
||||||
# image.image_path
|
# image.image_path or to DEFAULT_IMAGE
|
||||||
conf.set('image', 'http_image', conf.get_defaulted('image',
|
image_path = conf.get_defaulted('image', 'image_path')
|
||||||
'image_path'))
|
if self._find_image_by_name(image_path) is None:
|
||||||
|
conf.set('image', 'http_image', image_path)
|
||||||
|
else:
|
||||||
|
# image.image_path is name of the image already present in glance,
|
||||||
|
# this value can't be set to image.http_image, therefor set the
|
||||||
|
# default value
|
||||||
|
conf.set('image', 'http_image', C.DEFAULT_IMAGE)
|
||||||
|
|
||||||
def set_versions(self):
|
def set_versions(self):
|
||||||
super(ImageService, self).set_versions(top_level=False)
|
super(ImageService, self).set_versions(top_level=False)
|
||||||
@ -107,17 +113,29 @@ class ImageService(VersionedService):
|
|||||||
image = self._find_image(image_id, image_name)
|
image = self._find_image(image_id, image_name)
|
||||||
|
|
||||||
if image:
|
if image:
|
||||||
LOG.info("(no change) Found image '%s'", image['name'])
|
C.LOG.info("(no change) Found image '%s'", image['name'])
|
||||||
path = os.path.abspath(image_dest)
|
path = os.path.abspath(image_dest)
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
self._download_image(image['id'], path)
|
self._download_image(image['id'], path)
|
||||||
else:
|
else:
|
||||||
LOG.info("Creating image '%s'", image_name)
|
C.LOG.info("Creating image '%s'", image_name)
|
||||||
if image_source.startswith("http:") or \
|
if image_source.startswith("http:") or \
|
||||||
image_source.startswith("https:"):
|
image_source.startswith("https:"):
|
||||||
self._download_file(image_source, image_dest)
|
self._download_file(image_source, image_dest)
|
||||||
else:
|
else:
|
||||||
shutil.copyfile(image_source, image_dest)
|
try:
|
||||||
|
shutil.copyfile(image_source, image_dest)
|
||||||
|
except IOError:
|
||||||
|
# let's try if this is the case when a user uses already
|
||||||
|
# existing image in glance which is not uploaded as *_alt
|
||||||
|
if image_name[-4:] == "_alt":
|
||||||
|
image = self._find_image(None, image_name[:-4])
|
||||||
|
if image:
|
||||||
|
path = os.path.abspath(image_dest)
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
self._download_image(image['id'], path)
|
||||||
|
else:
|
||||||
|
raise IOError
|
||||||
image = self._upload_image(image_name, image_dest)
|
image = self._upload_image(image_name, image_dest)
|
||||||
return image['id']
|
return image['id']
|
||||||
|
|
||||||
@ -132,12 +150,19 @@ class ImageService(VersionedService):
|
|||||||
return self.client.show_image(image_id)
|
return self.client.show_image(image_id)
|
||||||
except exceptions.NotFound:
|
except exceptions.NotFound:
|
||||||
pass
|
pass
|
||||||
found = [x for x in self.client.list_images()['images']
|
return self._find_image_by_name(image_name)
|
||||||
if x['name'] == image_name]
|
|
||||||
if found:
|
def _find_image_by_name(self, image_name):
|
||||||
return found[0]
|
"""Find image by name.
|
||||||
else:
|
|
||||||
return None
|
:type image_name: string
|
||||||
|
:return: Information in a dict about the found image
|
||||||
|
:rtype: dict or None if image was not found
|
||||||
|
"""
|
||||||
|
for x in self.client.list_images()['images']:
|
||||||
|
if x['name'] == image_name:
|
||||||
|
return x
|
||||||
|
return None
|
||||||
|
|
||||||
def _upload_image(self, name, path):
|
def _upload_image(self, name, path):
|
||||||
"""Upload image file from `path` into Glance with `name`.
|
"""Upload image file from `path` into Glance with `name`.
|
||||||
@ -145,8 +170,8 @@ class ImageService(VersionedService):
|
|||||||
:type name: string
|
:type name: string
|
||||||
:type path: string
|
:type path: string
|
||||||
"""
|
"""
|
||||||
LOG.info("Uploading image '%s' from '%s'",
|
C.LOG.info("Uploading image '%s' from '%s'",
|
||||||
name, os.path.abspath(path))
|
name, os.path.abspath(path))
|
||||||
if self.non_admin:
|
if self.non_admin:
|
||||||
visibility = 'community'
|
visibility = 'community'
|
||||||
else:
|
else:
|
||||||
@ -166,9 +191,9 @@ class ImageService(VersionedService):
|
|||||||
:type id: string
|
:type id: string
|
||||||
:type path: string
|
:type path: string
|
||||||
"""
|
"""
|
||||||
LOG.info("Downloading image %s to %s", id, path)
|
C.LOG.info("Downloading image %s to %s", id, path)
|
||||||
body = self.client.show_image_file(id)
|
body = self.client.show_image_file(id)
|
||||||
LOG.debug(type(body.data))
|
C.LOG.debug(type(body.data))
|
||||||
with open(path, 'wb') as out:
|
with open(path, 'wb') as out:
|
||||||
out.write(body.data)
|
out.write(body.data)
|
||||||
|
|
||||||
@ -179,9 +204,9 @@ class ImageService(VersionedService):
|
|||||||
:type destination: string
|
:type destination: string
|
||||||
"""
|
"""
|
||||||
if os.path.exists(destination):
|
if os.path.exists(destination):
|
||||||
LOG.info("Image '%s' already fetched to '%s'.", url, destination)
|
C.LOG.info("Image '%s' already fetched to '%s'.", url, destination)
|
||||||
return
|
return
|
||||||
LOG.info("Downloading '%s' and saving as '%s'", url, destination)
|
C.LOG.info("Downloading '%s' and saving as '%s'", url, destination)
|
||||||
f = urllib.request.urlopen(url)
|
f = urllib.request.urlopen(url)
|
||||||
data = f.read()
|
data = f.read()
|
||||||
with open(destination, "wb") as dest:
|
with open(destination, "wb") as dest:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user