def test_glance_client_image_id(self): fixtures = {'image1': {'id': '1', 'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': {'prop1': 'propvalue1'}}} self.client.images = fixtures client, same_id = glance.get_glance_client(self.context, 1) self.assertEquals(same_id, 1)
def upload_image(context, image, instance, **kwargs): """Upload the snapshotted vm disk file to Glance image server.""" LOG.debug(_("Uploading image %s to the Glance image server") % image, instance=instance) read_file_handle = read_write_util.VmWareHTTPReadFile( kwargs.get("host"), kwargs.get("data_center_name"), kwargs.get("datastore_name"), kwargs.get("cookies"), kwargs.get("file_path")) file_size = read_file_handle.get_size() (glance_client, image_id) = glance.get_glance_client(context, image) # The properties and other fields that we need to set for the image. image_metadata = {"is_public": True, "disk_format": "vmdk", "container_format": "bare", "type": "vmdk", "properties": {"vmware_adaptertype": kwargs.get("adapter_type"), "vmware_ostype": kwargs.get("os_type"), "vmware_image_version": kwargs.get("image_version")}} start_transfer(read_file_handle, file_size, glance_client=glance_client, image_id=image_id, image_meta=image_metadata) LOG.debug(_("Uploaded image %s to the Glance image server") % image, instance=instance)
def upload_image(context, image, instance, **kwargs): """Upload the snapshotted vm disk file to Glance image server.""" LOG.debug(_("Uploading image %s to the Glance image server") % image, instance=instance) read_file_handle = read_write_util.VmWareHTTPReadFile( kwargs.get("host"), kwargs.get("data_center_name"), kwargs.get("datastore_name"), kwargs.get("cookies"), kwargs.get("file_path")) file_size = read_file_handle.get_size() (glance_client, image_id) = glance.get_glance_client(context, image) # The properties and other fields that we need to set for the image. image_metadata = { "is_public": True, "disk_format": "vmdk", "container_format": "bare", "type": "vmdk", "properties": { "vmware_adaptertype": kwargs.get("adapter_type"), "vmware_ostype": kwargs.get("os_type"), "vmware_image_version": kwargs.get("image_version") } } start_transfer(read_file_handle, file_size, glance_client=glance_client, image_id=image_id, image_meta=image_metadata) LOG.debug(_("Uploaded image %s to the Glance image server") % image, instance=instance)
def test_glance_client_image_ref(self): fixture = self._make_fixture(name='test image') image_id = self.service.create(self.context, fixture)['id'] image_url = 'http://something-less-likely/%s' % image_id client, same_id = glance.get_glance_client(self.context, image_url) self.assertEquals(same_id, image_id) self.assertEquals(client.host, 'something-less-likely')
def test_glance_client_image_ref(self): fixtures = {'image1': {'id': '1', 'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': {'prop1': 'propvalue1'}}} self.client.images = fixtures image_url = 'http://something-less-likely/1' client, same_id = glance.get_glance_client(self.context, image_url) self.assertEquals(same_id, 1) self.assertEquals(client.host, 'something-less-likely')
def get_vmdk_size_and_properties(context, image, instance): """ Get size of the vmdk file that is to be downloaded for attach in spawn. Need this to create the dummy virtual disk for the meta-data file. The geometry of the disk created depends on the size. """ LOG.debug(_("Getting image size for the image %s") % image) (glance_client, image_id) = glance.get_glance_client(context, image) meta_data = glance_client.get_image_meta(image_id) size, properties = meta_data["size"], meta_data["properties"] LOG.debug(_("Got image size of %(size)s for the image %(image)s") % locals()) return size, properties
def fetch_image(context, image, instance, **kwargs): """Download image from the glance image server.""" LOG.debug(_("Downloading image %s from glance image server") % image) (glance_client, image_id) = glance.get_glance_client(context, image) metadata, read_iter = glance_client.get_image(image_id) read_file_handle = read_write_util.GlanceFileRead(read_iter) file_size = int(metadata['size']) write_file_handle = read_write_util.VMWareHTTPWriteFile( kwargs.get("host"), kwargs.get("data_center_name"), kwargs.get("datastore_name"), kwargs.get("cookies"), kwargs.get("file_path"), file_size) start_transfer(read_file_handle, file_size, write_file_handle=write_file_handle) LOG.debug(_("Downloaded image %s from glance image server") % image)
def determine_from_glance(): glance_disk_format2nova_type = { "ami": ImageType.DISK, "aki": ImageType.KERNEL, "ari": ImageType.RAMDISK, "raw": ImageType.DISK_RAW, "vhd": ImageType.DISK_VHD, "iso": ImageType.DISK_ISO, } image_ref = instance.image_ref glance_client, image_id = glance.get_glance_client(context, image_ref) meta = glance_client.get_image_meta(image_id) disk_format = meta["disk_format"] try: return glance_disk_format2nova_type[disk_format] except KeyError: raise exception.InvalidDiskFormat(disk_format=disk_format)
def determine_from_glance(): glance_disk_format2nova_type = { 'ami': ImageType.DISK, 'aki': ImageType.KERNEL, 'ari': ImageType.RAMDISK, 'raw': ImageType.DISK_RAW, 'vhd': ImageType.DISK_VHD, 'iso': ImageType.DISK_ISO} image_ref = instance.image_ref glance_client, image_id = glance.get_glance_client(context, image_ref) meta = glance_client.get_image_meta(image_id) disk_format = meta['disk_format'] try: return glance_disk_format2nova_type[disk_format] except KeyError: raise exception.InvalidDiskFormat(disk_format=disk_format)
def get_image_service(context, image_href): """Get the proper image_service and id for the given image_href. The image_href param can be an href of the form http://myglanceserver:9292/images/42, or just an int such as 42. If the image_href is an int, then the default image service is returned. :param image_href: image ref/id for an image :returns: a tuple of the form (image_service, image_id) """ image_href = image_href or 0 if str(image_href).isdigit(): return (get_default_image_service(), int(image_href)) (glance_client, image_id) = glance.get_glance_client(context, image_href) image_service = nova.image.glance.GlanceImageService(glance_client) return (image_service, image_id)
def determine_from_glance(): glance_disk_format2nova_type = { 'ami': ImageType.DISK, 'aki': ImageType.KERNEL, 'ari': ImageType.RAMDISK, 'raw': ImageType.DISK_RAW, 'vhd': ImageType.DISK_VHD, 'iso': ImageType.DISK_ISO } image_ref = instance.image_ref glance_client, image_id = glance.get_glance_client( context, image_ref) meta = glance_client.get_image_meta(image_id) disk_format = meta['disk_format'] try: return glance_disk_format2nova_type[disk_format] except KeyError: raise exception.InvalidDiskFormat(disk_format=disk_format)
def _fetch_image_glance_disk(cls, context, session, instance, image, image_type): """Fetch the image from Glance NOTE: Unlike _fetch_image_glance_vhd, this method does not use the Glance plugin; instead, it streams the disks through domU to the VDI directly. Returns: A single filename if image_type is KERNEL_RAMDISK A list of dictionaries that describe VDIs, otherwise """ instance_id = instance.id # FIXME(sirp): Since the Glance plugin seems to be required for the # VHD disk, it may be worth using the plugin for both VHD and RAW and # DISK restores LOG.debug(_("Fetching image %(image)s") % locals()) LOG.debug(_("Image Type: %s"), ImageType.to_string(image_type)) if image_type == ImageType.DISK_ISO: sr_ref = safe_find_iso_sr(session) LOG.debug(_("ISO: Found sr possibly containing the ISO image")) else: sr_ref = safe_find_sr(session) glance_client, image_id = glance.get_glance_client(context, image) glance_client.set_auth_token(getattr(context, 'auth_token', None)) meta, image_file = glance_client.get_image(image_id) virtual_size = int(meta['size']) vdi_size = virtual_size LOG.debug(_("Size for image %(image)s:" + "%(virtual_size)d") % locals()) if image_type == ImageType.DISK: # Make room for MBR. vdi_size += MBR_SIZE_BYTES elif image_type in (ImageType.KERNEL, ImageType.RAMDISK) and \ vdi_size > FLAGS.max_kernel_ramdisk_size: max_size = FLAGS.max_kernel_ramdisk_size raise exception.Error( _("Kernel/Ramdisk image is too large: %(vdi_size)d bytes, " "max %(max_size)d bytes") % locals()) name_label = instance.name vdi_ref = cls.create_vdi(session, sr_ref, name_label, vdi_size, False) # From this point we have a VDI on Xen host; # If anything goes wrong, we need to remember its uuid. try: filename = None vdi_uuid = session.get_xenapi().VDI.get_uuid(vdi_ref) with_vdi_attached_here(session, vdi_ref, False, lambda dev: _stream_disk(dev, image_type, virtual_size, image_file)) if image_type in (ImageType.KERNEL, ImageType.RAMDISK): # We need to invoke a plugin for copying the # content of the VDI into the proper path. LOG.debug(_("Copying VDI %s to /boot/guest on dom0"), vdi_ref) fn = "copy_kernel_vdi" args = {} args['vdi-ref'] = vdi_ref # Let the plugin copy the correct number of bytes. args['image-size'] = str(vdi_size) task = session.async_call_plugin('glance', fn, args) filename = session.wait_for_task(task, instance_id) # Remove the VDI as it is not needed anymore. session.get_xenapi().VDI.destroy(vdi_ref) LOG.debug(_("Kernel/Ramdisk VDI %s destroyed"), vdi_ref) return [dict(vdi_type=ImageType.to_string(image_type), vdi_uuid=None, file=filename)] else: return [dict(vdi_type=ImageType.to_string(image_type), vdi_uuid=vdi_uuid, file=None)] except (cls.XenAPI.Failure, IOError, OSError) as e: # We look for XenAPI and OS failures. LOG.exception(_("instance %s: Failed to fetch glance image"), instance_id, exc_info=sys.exc_info()) e.args = e.args + ([dict(vdi_type=ImageType. to_string(image_type), vdi_uuid=vdi_uuid, file=filename)],) raise e
def test_glance_client_image_ref(self): fixture = self._make_fixture(name='test image') image_id = self.service.create(self.context, fixture)['id'] image_url = 'http://foo/%s' % image_id client, same_id = glance.get_glance_client(self.context, image_url) self.assertEquals(same_id, image_id)
def test_glance_client_image_id(self): fixture = self._make_fixture(name='test image') image_id = self.service.create(self.context, fixture)['id'] client, same_id = glance.get_glance_client(self.context, image_id) self.assertEquals(same_id, image_id)
def _fetch_image_glance_disk(cls, context, session, instance, image, image_type): """Fetch the image from Glance NOTE: Unlike _fetch_image_glance_vhd, this method does not use the Glance plugin; instead, it streams the disks through domU to the VDI directly. Returns: A single filename if image_type is KERNEL_RAMDISK A list of dictionaries that describe VDIs, otherwise """ instance_id = instance.id # FIXME(sirp): Since the Glance plugin seems to be required for the # VHD disk, it may be worth using the plugin for both VHD and RAW and # DISK restores LOG.debug(_("Fetching image %(image)s") % locals()) LOG.debug(_("Image Type: %s"), ImageType.to_string(image_type)) if image_type == ImageType.DISK_ISO: sr_ref = safe_find_iso_sr(session) LOG.debug(_("ISO: Found sr possibly containing the ISO image")) else: sr_ref = safe_find_sr(session) glance_client, image_id = glance.get_glance_client(context, image) glance_client.set_auth_token(getattr(context, 'auth_token', None)) meta, image_file = glance_client.get_image(image_id) virtual_size = int(meta['size']) vdi_size = virtual_size LOG.debug( _("Size for image %(image)s:" + "%(virtual_size)d") % locals()) if image_type == ImageType.DISK: # Make room for MBR. vdi_size += MBR_SIZE_BYTES elif image_type in (ImageType.KERNEL, ImageType.RAMDISK) and \ vdi_size > FLAGS.max_kernel_ramdisk_size: max_size = FLAGS.max_kernel_ramdisk_size raise exception.Error( _("Kernel/Ramdisk image is too large: %(vdi_size)d bytes, " "max %(max_size)d bytes") % locals()) name_label = get_name_label_for_image(image) vdi_ref = cls.create_vdi(session, sr_ref, name_label, vdi_size, False) # From this point we have a VDI on Xen host; # If anything goes wrong, we need to remember its uuid. try: filename = None vdi_uuid = session.get_xenapi().VDI.get_uuid(vdi_ref) with_vdi_attached_here( session, vdi_ref, False, lambda dev: _stream_disk( dev, image_type, virtual_size, image_file)) if image_type in (ImageType.KERNEL, ImageType.RAMDISK): # We need to invoke a plugin for copying the # content of the VDI into the proper path. LOG.debug(_("Copying VDI %s to /boot/guest on dom0"), vdi_ref) fn = "copy_kernel_vdi" args = {} args['vdi-ref'] = vdi_ref # Let the plugin copy the correct number of bytes. args['image-size'] = str(vdi_size) task = session.async_call_plugin('glance', fn, args) filename = session.wait_for_task(task, instance_id) # Remove the VDI as it is not needed anymore. session.get_xenapi().VDI.destroy(vdi_ref) LOG.debug(_("Kernel/Ramdisk VDI %s destroyed"), vdi_ref) return [ dict(vdi_type=ImageType.to_string(image_type), vdi_uuid=None, file=filename) ] else: return [ dict(vdi_type=ImageType.to_string(image_type), vdi_uuid=vdi_uuid, file=None) ] except (cls.XenAPI.Failure, IOError, OSError) as e: # We look for XenAPI and OS failures. LOG.exception(_("instance %s: Failed to fetch glance image"), instance_id, exc_info=sys.exc_info()) e.args = e.args + ([ dict(vdi_type=ImageType.to_string(image_type), vdi_uuid=vdi_uuid, file=filename) ], ) raise e