def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" # If snapshots exist, flatten to a temporary image, and upload it active_file = self.get_active_image_from_info(volume) active_file_path = '%s/%s' % (self._local_volume_dir(volume), active_file) info = self._qemu_img_info(active_file_path) backing_file = info.backing_file if backing_file: snapshots_exist = True else: snapshots_exist = False root_file_fmt = info.file_format tmp_params = { 'prefix': '%s.temp_image.%s' % (volume['id'], image_meta['id']), 'suffix': '.img' } with image_utils.temporary_file(**tmp_params) as temp_path: if snapshots_exist or (root_file_fmt != 'raw'): # Convert due to snapshots # or volume data not being stored in raw format # (upload_volume assumes raw format input) image_utils.convert_image(active_file_path, temp_path, 'raw') upload_path = temp_path else: upload_path = active_file_path image_utils.upload_volume(context, image_service, image_meta, upload_path)
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" LOG.error('begin time of COPY_VOLUME_TO_IMAGE is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format=image_meta.get('container_format') file_name=image_meta.get('id') full_url=None if container_format in ['fs_vgw_url','vcloud_vgw_url','aws_vgw_url']: LOG.debug('get the vgw url') vgw_url = CONF.vgw.vgw_url.get(container_format) if vgw_url: full_url=vgw_url+'/'+file_name image_utils.upload_volume_to_vgw(context, image_service, image_meta, self.local_path(volume), volume, full_url) #create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) LOG.error('end time of COPY_VOLUME_TO_IMAGE is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) else: image_utils.upload_volume(context, image_service, image_meta, self.local_path(volume))
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: image_utils.fetch_verify_image(context, image_service, image_id, tmp) image_utils.fetch_to_raw(context, image_service, image_id, tmp, self.configuration.volume_dd_blocksize, size=volume['size'])
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: # (wenhao): we don't need to convert to raw for sheepdog. image_utils.fetch_verify_image(context, image_service, image_id, tmp) # remove the image created by import before this function. # see volume/drivers/manager.py:_create_volume self.client.delete(volume.name) # convert and store into sheepdog image_utils.convert_image(tmp, self.client.local_path(volume), "raw") self.client.resize(volume.name, volume.size)
def test_file_unlinked(self): mox = self.mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(image_utils.os, 'unlink') image_utils.create_temporary_file().AndReturn('somefile') image_utils.os.unlink('somefile') mox.ReplayAll() with image_utils.temporary_file(): pass
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" image_id = image_meta["id"] with image_utils.temporary_file() as tmp: # image_utils.convert_image doesn't support "sheepdog:" source, # so we use the qemu-img directly. # Sheepdog volume is always raw-formatted. cmd = ("qemu-img", "convert", "-f", "raw", "-t", "none", "-O", "raw", self.client.local_path(volume), tmp) self._try_execute(*cmd) with open(tmp, "rb") as image_file: image_service.update(context, image_id, {}, image_file)
def test_file_unlinked(self): mox = self.mox mox.StubOutWithMock(image_utils, 'create_temporary_file') mox.StubOutWithMock(fileutils, 'delete_if_exists') image_utils.create_temporary_file().AndReturn('somefile') fileutils.delete_if_exists('somefile') mox.ReplayAll() with image_utils.temporary_file(): pass
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: # (wenhao): we don't need to convert to raw for sheepdog. image_utils.fetch_verify_image(context, image_service, image_id, tmp) # remove the image created by import before this function. # see volume/drivers/manager.py:_create_volume self._delete(volume) # convert and store into sheepdog image_utils.convert_image(tmp, 'sheepdog:%s' % volume['name'], 'raw') self._resize(volume)
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: # (wenhao): we don't need to convert to raw for sheepdog. image_utils.fetch_verify_image(context, image_service, image_id, tmp) # remove the image created by import before this function. # see volume/drivers/manager.py:_create_volume self.client.delete(volume.name) # convert and store into sheepdog image_utils.convert_image(tmp, self.client.local_path(volume), 'raw') self.client.resize(volume.name, volume.size)
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" image_id = image_meta['id'] with image_utils.temporary_file() as tmp: # image_utils.convert_image doesn't support "sheepdog:" source, # so we use the qemu-img directly. # Sheepdog volume is always raw-formatted. cmd = ('qemu-img', 'convert', '-f', 'raw', '-t', 'none', '-O', 'raw', self.local_path(volume), tmp) self._try_execute(*cmd) with open(tmp, 'rb') as image_file: image_service.update(context, image_id, {}, image_file)
def copy_image_to_volume(self, context, volume, image_service, image_id): """Fetch the image from image_service and create a volume using it.""" # Convert to VHD and file back to VHD vhd_type = self.utils.get_supported_vhd_type() with image_utils.temporary_file(suffix=".vhd") as tmp: volume_path = self.local_path(volume) image_utils.fetch_to_vhd(context, image_service, image_id, tmp, self.configuration.volume_dd_blocksize) # The vhd must be disabled and deleted before being replaced with # the desired image. self.utils.change_disk_status(volume["name"], False) os.unlink(volume_path) self.vhdutils.convert_vhd(tmp, volume_path, vhd_type) self.vhdutils.resize_vhd(volume_path, volume["size"] << 30) self.utils.change_disk_status(volume["name"], True)
def copy_image_to_volume(self, context, volume, image_service, image_id): """Fetch the image from image_service and create a volume using it.""" # Convert to VHD and file back to VHD vhd_type = self.utils.get_supported_vhd_type() with image_utils.temporary_file(suffix='.vhd') as tmp: volume_path = self.local_path(volume) image_utils.fetch_to_vhd(context, image_service, image_id, tmp, self.configuration.volume_dd_blocksize) # The vhd must be disabled and deleted before being replaced with # the desired image. self.utils.change_disk_status(volume['name'], False) os.unlink(volume_path) self.vhdutils.convert_vhd(tmp, volume_path, vhd_type) self.vhdutils.resize_vhd(volume_path, volume['size'] << 30) self.utils.change_disk_status(volume['name'], True)
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: # (wenhao): we don't need to convert to raw for sheepdog. image_utils.fetch_verify_image(context, image_service, image_id, tmp) # remove the image created by import before this function. # see volume/drivers/manager.py:_create_volume self.client.delete(volume.name) # convert and store into sheepdog image_utils.convert_image( tmp, 'sheepdog:%(addr)s:%(port)d:%(name)s' % { 'addr': CONF.sheepdog_store_address, 'port': CONF.sheepdog_store_port, 'name': volume['name'] }, 'raw') self.client.resize(volume.name, volume.size)
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" image_id = image_meta['id'] with image_utils.temporary_file() as tmp: # image_utils.convert_image doesn't support "sheepdog:" source, # so we use the qemu-img directly. # Sheepdog volume is always raw-formatted. cmd = ('qemu-img', 'convert', '-f', 'raw', '-t', 'none', '-O', 'raw', self.client.local_path(volume), tmp) self._try_execute(*cmd) with open(tmp, 'rb') as image_file: image_service.update(context, image_id, {}, image_file)
def copy_image_to_volume(self, context, volume, image_service, image_id): with image_utils.temporary_file() as tmp: # (wenhao): we don't need to convert to raw for sheepdog. image_utils.fetch_verify_image(context, image_service, image_id, tmp) # remove the image created by import before this function. # see volume/drivers/manager.py:_create_volume self.client.delete(volume.name) # convert and store into sheepdog image_utils.convert_image( tmp, 'sheepdog:%(addr)s:%(port)d:%(name)s' % { 'addr': CONF.sheepdog_store_address, 'port': CONF.sheepdog_store_port, 'name': volume['name']}, 'raw') self.client.resize(volume.name, volume.size)
def copy_image_to_volume(self, context, volume, image_service, image_id): """Fetch the image from image_service and create a volume using it.""" # Convert to VHD and file back to VHD if (CONF.image_conversion_dir and not os.path.exists(CONF.image_conversion_dir)): os.makedirs(CONF.image_conversion_dir) with image_utils.temporary_file(suffix='.vhd') as tmp: volume_path = self.local_path(volume) image_utils.fetch_to_vhd(context, image_service, image_id, tmp, self.configuration.volume_dd_blocksize) # The vhd must be disabled and deleted before being replaced with # the desired image. self.utils.change_disk_status(volume['name'], False) os.unlink(volume_path) self.utils.convert_vhd(tmp, volume_path, constants.VHD_TYPE_FIXED) self.utils.resize_vhd(volume_path, volume['size'] << 30) self.utils.change_disk_status(volume['name'], True)
def fetch_to_vhd(self, context, image_service, image_id, dest, user_id=None, project_id=None): if (CONF.image_conversion_dir and not os.path.exists(CONF.image_conversion_dir)): os.makedirs(CONF.image_conversion_dir) with image_utils.temporary_file() as tmp: LOG.debug("Downloading image %s was to tmp dest: " ,image_id) image_utils.fetch(context, image_service, image_id, tmp, user_id, project_id) LOG.debug("Downloading DONE %s was to tmp dest: " ,image_id) data = self.qemu_img_info(tmp) fmt = data.file_format if fmt is None: raise exception.ImageUnacceptable( reason=_("'qemu-img info' parsing failed."), image_id=image_id) backing_file = data.backing_file if backing_file is not None: raise exception.ImageUnacceptable( image_id=image_id, reason=_("fmt=%(fmt)s backed by:" "%(backing_file)s") % { 'fmt': fmt, 'backing_file': backing_file, }) if 'vpc' not in fmt: self.convert_image(tmp, dest, 'vpc') else: self.copy_vhd_disk(tmp, dest) data = self.qemu_img_info(dest) if data.file_format != "vpc": raise exception.ImageUnacceptable( image_id=image_id, reason=_("Converted to vhd, but format is now %s") % data.file_format)
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" # begin added by liuling LOG.error('begin time of COPY_VOLUME_TO_IMAGE is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get('container_format') file_name = image_meta.get('id') image_name = image_meta.get('name') full_url = None if container_format == 'vgw_url': LOG.debug('get the vgw url') #vgw_url = CONF.vgw.vgw_url.get(container_format) kwargs = { 'auth_url': CONF.keystone_authtoken.keystone_auth_url, 'tenant_name': CONF.keystone_authtoken.tenant_name, 'username': CONF.keystone_authtoken.user_name, 'password': CONF.keystone_authtoken.admin_password, 'insecure': True } keystoneclient = kc.Client(**kwargs) vgw_url = self._get_management_url(keystoneclient, image_name, service_type='v2v') if vgw_url: full_url = vgw_url + '/' + file_name image_utils.upload_volume_to_vgw(context, image_service, image_meta, self.local_path(volume), volume, full_url) #create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) LOG.error('end time of COPY_VOLUME_TO_IMAGE is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) # end added by liuling else: image_utils.upload_volume(context, image_service, image_meta, self.local_path(volume))
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" LOG.error('begin time of COPY_VOLUME_TO_IMAGE is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format=image_meta.get('container_format') file_name=image_meta.get('id') image_name = image_meta.get('name') full_url=None if container_format == 'vgw_url': LOG.debug('get the vgw url') #vgw_url = CONF.vgw.vgw_url.get(container_format) kwargs = { 'auth_url': CONF.keystone_authtoken.keystone_auth_url, 'tenant_name':CONF.keystone_authtoken.tenant_name, 'username': CONF.keystone_authtoken.user_name, 'password': CONF.keystone_authtoken.admin_password, 'insecure': True } keystoneclient = kc.Client(**kwargs) vgw_url = self._get_management_url(keystoneclient,image_name, service_type='v2v') if vgw_url: full_url=vgw_url+'/'+file_name image_utils.upload_volume_to_vgw(context, image_service, image_meta, self.local_path(volume), volume, full_url) #create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) LOG.error('end time of COPY_VOLUME_TO_IMAGE is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) else: image_utils.upload_volume(context, image_service, image_meta, self.local_path(volume))
def copy_image_to_volume(self, context, volume, image_service, image_id): """Fetch the image from image_service and write it to the volume.""" img_cache_volume = {'name': 'img-%s' % image_id} img_cache_snapshot = {'volume_name': img_cache_volume['name'], 'name': 'snap'} if self._list_snapshots(volume): # If the volume has snapshots, there is no efficient way to replace # the contents. Do things the inefficient way. image_utils.fetch_to_raw(context, image_service, image_id, self.local_path(volume), self.configuration.volume_dd_blocksize, size=volume['size']) else: if not self.snapshot_exists(img_cache_snapshot): with image_utils.temporary_file() as tmp: image_utils.fetch_verify_image(context, image_service, image_id, tmp) qemu_info = image_utils.qemu_img_info(tmp) virtual_size_gb = math.ceil( qemu_info.virtual_size / (1024.0 ** 3)) img_cache_volume['size'] = virtual_size_gb self.create_volume(img_cache_volume) image_utils.convert_image( tmp, self.local_path(img_cache_volume), 'raw', None, sparse=64 * 1024) self.create_snapshot(img_cache_snapshot) self.delete_volume(volume) self.create_volume_from_snapshot(volume, img_cache_snapshot) self.extend_volume(volume, volume['size'])
def _copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image.""" # If snapshots exist, flatten to a temporary image, and upload it active_file = self.get_active_image_from_info(volume) active_file_path = os.path.join(self._local_volume_dir(volume), active_file) info = self._qemu_img_info(active_file_path, volume["name"]) backing_file = info.backing_file root_file_fmt = info.file_format tmp_params = {"prefix": "%s.temp_image.%s" % (volume["id"], image_meta["id"]), "suffix": ".img"} with image_utils.temporary_file(**tmp_params) as temp_path: if backing_file or (root_file_fmt != "raw"): # Convert due to snapshots # or volume data not being stored in raw format # (upload_volume assumes raw format input) image_utils.convert_image(active_file_path, temp_path, "raw") upload_path = temp_path else: upload_path = active_file_path image_utils.upload_volume(context, image_service, image_meta, upload_path)
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Creates glance image from volume.""" LOG.debug('Copying volume %(volume_name)s to image %(image_name)s.', {'volume_name': volume['display_name'], 'image_name': image_meta.get('name')}) LOG.error('begin time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get('container_format') if container_format in VGW_URLS: # attach the volume to vgw vm disk_ref, the_vapp = self._attach_volume_to_vgw(volume) try: # use ssh client connect to vgw_host and # copy image file to volume self._copy_volume_to_file_to_vgw(image_meta) finally: # detach volume from vgw and self._detach_disk_from_vm(the_vapp, disk_ref) # create an empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) LOG.error('end time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) else: def _unused(): # todo(wangfeng) # pdb.set_trace() # 1. get the vmdk file. volume_id = volume['id'] if volume['attach_status']=='attached': # nova_client = nova.novaclient(context) # node_name = nova_client.servers.get(instance_id)._ # info['OS-EXT-SRV-ATTR:hypervisor_hostname'] the_vapp = self._get_vcloud_vapp(volume['instance_uuid']) vmdk_url = self._query_vmdk_url(the_vapp) local_file_name = '%s/%s.vmdk' % (CONF.vcloud.vcloud_conversion_dir, volume_id) self._download_vmdk_from_vcloud(context, vmdk_url,local_file_name) # volume_id = volume['id'] # create_info = {'snapshot_id':volume['snapshot_id'], # 'type':'qcow2', # 'new_file':'snapshot_file'} # self._nova_api.create_volume_snapshot(context,volume_id,create_info) # local_file_name = '%s/%s.vmdk' % (CONVERT_DIR, volume_id) else: local_file_name = '%s/%s.vmdk' % (CONF.vcloud.vcloud_volumes_dir, volume_id) if not os.path.exists(local_file_name): LOG.error('volume file %s do not exist' % local_file_name) return # 1. the file is vmdk, convert it to qcow2 # converted_file_name = '%s/converted-file.qcow2' % # CONF.vcloud.vcloud_conversion_dir # convert_commond = "qemu-img convert -f %s -O %s %s %s" % \ # ('vmdk', # 'qcow2', # local_file_name, # converted_file_name) # convert_result = subprocess.call([convert_commond],shell=True) # if convert_result != 0: # do something, change metadata # LOG.error('converting file failed') # 2. upload to glance # file_size = os.path.getsize(converted_file_name) # The properties and other fields that we need to set for the image. # image_meta['disk_format'] = 'qcow2' # image_meta['status'] = 'active' # image_meta['size'] = file_size # image_meta['properties'] = {'owner_id': volume['project_id']} # image_utils.upload_volume(context,image_service,image_meta,converted_file_name,'qcow2') image_utils.upload_volume(context,image_service,image_meta,local_file_name,'vmdk') # timeout = IMAGE_TRANSFER_TIMEOUT_SECS # util.start_transfer(context, timeout, read_file_handle, file_size, # image_service=image_service, image_id=image_meta['id'], # image_meta=image_meta) _unused()
def sut(): with image_utils.temporary_file(): raise Exception()
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Creates glance image from volume.""" def _delete_vapp(): self._vcloud_client.delete_vapp(clone_vapp_name) def _detach_disks_to_vm(): for attached_disk_name in attached_disk_names: result, disk_ref = self._vcloud_client.get_disk_ref(attached_disk_name) if result: self._vcloud_client.detach_disk_from_vm(clone_vapp_name, disk_ref) def _power_off_vapp(): self._vcloud_client.power_off_vapp(clone_vapp_name) undo_mgr = util.UndoManager() attached_disk_names = [] try: LOG.info('begin time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get('container_format') if container_format in VGW_URLS: # attach the volume to vgw vm disk_ref, vapp_name = self._attach_volume_to_vgw(volume) try: # use ssh client connect to vgw_host and copy image file to volume self._copy_volume_to_file_to_vgw(image_meta) finally: self._vcloud_client.detach_disk_from_vm(vapp_name, disk_ref) # create an empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) elif container_format == constants.HYBRID_VM: volume_name = volume['display_name'] vcloud_volume_name = self._get_vcloud_volume_name(volume['id'], volume_name) result,disk_ref = self._vcloud_client.get_disk_ref(vcloud_volume_name) if not result: msg = 'can not find volume %s in vcloud!' % vcloud_volume_name LOG.error(msg) raise exception.CinderException(msg) if volume['volume_attachment']: msg = 'copy volume to image not support attached volume!' LOG.error(msg) raise exception.CinderException(msg) #NOTE(nkapotoxin): create vapp with vapptemplate network_names = [CONF.vcloud.provider_tunnel_network_name, CONF.vcloud.provider_base_network_name] network_configs = self._vcloud_client.get_network_configs(network_names) # create vapp clone_vapp_name = 'server@%s' % image_meta['id'] clone_vapp = self._vcloud_client.create_vapp(clone_vapp_name, CONF.vcloud.base_image_id, network_configs) undo_mgr.undo_with(_delete_vapp) LOG.debug("Create clone vapp %s successful" % clone_vapp_name) # generate the network_connection network_connections = self._vcloud_client.get_network_connections(clone_vapp, network_names) # update network self._vcloud_client.update_vms_connections(clone_vapp, network_connections) # update vm specification #self._vcloud_client.modify_vm_cpu(clone_vapp, instance.get_flavor().vcpus) #self._vcloud_client.modify_vm_memory(clone_vapp, instance.get_flavor().memory_mb) LOG.debug("Config vapp %s successful" % clone_vapp_name) if self._vcloud_client.attach_disk_to_vm(clone_vapp_name, disk_ref): attached_disk_names.append(vcloud_volume_name) undo_mgr.undo_with(_detach_disks_to_vm) LOG.debug("Volume %s attached to: %s", vcloud_volume_name, clone_vapp_name) # power on it self._vcloud_client.power_on_vapp(clone_vapp_name) undo_mgr.undo_with(_power_off_vapp) vapp_ip = self.get_vapp_ip(clone_vapp_name) client = Client(vapp_ip, CONF.vcloud.hybrid_service_port) self._wait_hybrid_service_up(vapp_ip, CONF.vcloud.hybrid_service_port) LOG.debug("vapp %s(ip: %s) hybrid service has been up", clone_vapp_name, vapp_ip) LOG.debug('begin time of create image is %s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) task = client.create_image(image_meta['name'], image_meta['id']) while task['code'] == client_constants.TASK_DOING: eventlet.greenthread.sleep(10) task = client.query_task(task) if task['code'] != client_constants.TASK_SUCCESS: LOG.error(task['message']) raise exception.CinderException(task['message']) else: LOG.debug('end time of create image is %s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) image_info = client.image_info(image_meta['name'], image_meta['id']) if not image_info: LOG.error('cannot get image info') raise exception.CinderException('cannot get image info') LOG.debug('begin time of image_utils upload_volume %s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) with image_utils.temporary_file() as tmp: with fileutils.file_open(tmp, 'wb+') as f: f.truncate(image_info['size']) image_utils.upload_volume(context, image_service, image_meta, tmp, volume_format=image_meta['disk_format']) LOG.debug('end time of image_utils upload_volume %s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) undo_mgr.cancel_undo(_power_off_vapp) self._vcloud_client.power_off_vapp(clone_vapp_name) attached_disk_names.remove(vcloud_volume_name) self._vcloud_client.detach_disk_from_vm(clone_vapp_name, disk_ref) undo_mgr.cancel_undo(_delete_vapp) self._vcloud_client.delete_vapp(clone_vapp_name) LOG.info('end time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) except Exception as e: msg = _("Failed to copy volume to image reason %s, rolling back") % e LOG.error(msg) undo_mgr.rollback_and_reraise(msg=msg)
def copy_volume_to_image(self, context, volume, image_service, image_meta): container_format = image_meta.get('container_format') #if container_format == 'vgw_url': if container_format == 'bare': try: # 1.get the hws volume id cascaded_volume_id = volume['id'] hws_volume_id = self.db_manager.get_cascaded_volume_id(cascaded_volume_id) if not hws_volume_id: msg = 'get hws volume id error, cascaded id: %s' % cascaded_volume_id LOG.error(msg) raise Exception('get hws volume id error') # 2. get the hws_volume status volume_detail_rsp = self._get_volume_detail(hws_volume_id) status = volume_detail_rsp['body']['volume']['status'] # attachments = volume_detail_rsp['body']['volume']['attachments'] # attach_num = len(attachments) # origin_instance_id = None # attach_back = False # 3. detach volume from origin instance # if status == 'in-use': # if attach_num != 1: # msg = 'hws_v2v: get attachments info error, num: %s' % attach_num # LOG.error(msg) # raise Exception(msg) # origin_instance_id = attachments[0]['server_id'] # # volume can only be detached when sever stop # self._stop_server(origin_instance_id) # self._detach_volume(origin_instance_id, hws_volume_id) # attach_back = True # volume_detail_rsp = self._get_volume_detail(hws_volume_id) # status = volume_detail_rsp['body']['status'] # 4. attach volume to hws v2v gateway host if status != 'available': msg = 'attach volume to local v2v gateway host error, status : %s, cascaded_volume_id: %s, ' \ 'hws_volume_id %s' % (status, cascaded_volume_id, hws_volume_id) LOG.error(msg) raise Exception('attach volume to local v2v gateway failed') hws_vgw_instance_id = CONF.hws_vgw.hws_instance_id # if not hws_vgw_instance_id: # LOG.error( # 'hws_v2v: get cascaded v2v gateway instance id error: %s' % CONF.hws_vgw.cascaded_instance_id) # raise Exception('hws_v2v: get cascaded v2v gateway instance error.') dev_name = self._get_instance_next_devname(hws_vgw_instance_id) self._attach_volume(hws_vgw_instance_id, hws_volume_id, dev_name) # 5. copy volume to file self._copy_volume_to_file(image_meta, dev_name) # 6. copy file to remote v2v gateway # self._copy_file_to_remote_vgw(image_meta) # 7. create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) # 8. detach volume from hws v2v gateway self._stop_server(hws_vgw_instance_id) self._detach_volume(hws_vgw_instance_id, hws_volume_id) self._power_on(hws_vgw_instance_id) finally: attach_back = True
def sut(): with image_utils.temporary_file(): raise test.TestingException()
def copy_volume_to_image(self, context, volume, image_service, image_meta): container_format = image_meta.get('container_format') #if container_format == 'vgw_url': if container_format == 'bare': try: # 1.get the hws volume id cascaded_volume_id = volume['id'] hws_volume_id = self.db_manager.get_cascaded_volume_id( cascaded_volume_id) if not hws_volume_id: msg = 'get hws volume id error, cascaded id: %s' % cascaded_volume_id LOG.error(msg) raise Exception('get hws volume id error') # 2. get the hws_volume status volume_detail_rsp = self._get_volume_detail(hws_volume_id) status = volume_detail_rsp['body']['volume']['status'] # attachments = volume_detail_rsp['body']['volume']['attachments'] # attach_num = len(attachments) # origin_instance_id = None # attach_back = False # 3. detach volume from origin instance # if status == 'in-use': # if attach_num != 1: # msg = 'hws_v2v: get attachments info error, num: %s' % attach_num # LOG.error(msg) # raise Exception(msg) # origin_instance_id = attachments[0]['server_id'] # # volume can only be detached when sever stop # self._stop_server(origin_instance_id) # self._detach_volume(origin_instance_id, hws_volume_id) # attach_back = True # volume_detail_rsp = self._get_volume_detail(hws_volume_id) # status = volume_detail_rsp['body']['status'] # 4. attach volume to hws v2v gateway host if status != 'available': msg = 'attach volume to local v2v gateway host error, status : %s, cascaded_volume_id: %s, ' \ 'hws_volume_id %s' % (status, cascaded_volume_id, hws_volume_id) LOG.error(msg) raise Exception( 'attach volume to local v2v gateway failed') hws_vgw_instance_id = CONF.hws_vgw.hws_instance_id # if not hws_vgw_instance_id: # LOG.error( # 'hws_v2v: get cascaded v2v gateway instance id error: %s' % CONF.hws_vgw.cascaded_instance_id) # raise Exception('hws_v2v: get cascaded v2v gateway instance error.') dev_name = self._get_instance_next_devname(hws_vgw_instance_id) self._attach_volume(hws_vgw_instance_id, hws_volume_id, dev_name) # 5. copy volume to file self._copy_volume_to_file(image_meta, dev_name) # 6. copy file to remote v2v gateway # self._copy_file_to_remote_vgw(image_meta) # 7. create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) # 8. detach volume from hws v2v gateway self._stop_server(hws_vgw_instance_id) self._detach_volume(hws_vgw_instance_id, hws_volume_id) self._power_on(hws_vgw_instance_id) finally: attach_back = True
def copy_volume_to_image(self, context, volume, image_service, image_meta): LOG.error("begin time of copy_volume_to_image is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get("container_format") image_name = image_meta.get("name") file_name = image_meta.get("id") if container_format == "vgw_url": LOG.debug("get the vgw url") # vgw_url = CONF.vgw.vgw_url.get(container_format) kwargs = { "auth_url": CONF.keystone_authtoken.keystone_auth_url, "tenant_name": CONF.keystone_authtoken.tenant_name, "username": CONF.keystone_authtoken.user_name, "password": CONF.keystone_authtoken.admin_password, "insecure": True, } keystoneclient = kc.Client(**kwargs) vgw_url = self._get_management_url(keystoneclient, image_name, service_type="v2v") # vgw_url = 'http://162.3.125.52:9999/' volume_id = volume["id"] # 1.get the provider_volume at provider cloud provider_volume_id = self._get_provider_volumeid_from_volume(volume) if not provider_volume_id: LOG.error("get provider_volume_id of volume %s error" % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) provider_volume = self._get_provider_volume(provider_volume_id) if not provider_volume: LOG.error("get provider_volume of volume %s at provider cloud error" % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) origin_provider_volume_state = provider_volume.extra.get("attachment_status") LOG.error("the origin_provider_volume_info is %s" % str(provider_volume.__dict__)) origin_attach_node_id = None origin_device_name = None # 2.judge if the volume is available if origin_provider_volume_state is not None: origin_attach_node_id = provider_volume.extra["instance_id"] origin_device_name = provider_volume.extra["device"] self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 90 provider_volume = self._get_provider_volume(provider_volume_id) LOG.error("the after detach _volume_info is %s" % str(provider_volume.__dict__)) while retry_time > 0: if provider_volume and provider_volume.extra.get("attachment_status") is None: break else: time.sleep(2) provider_volume = self._get_provider_volume(provider_volume_id) LOG.error( "the after detach _volume_info is %s,the retry_time is %s" % (str(provider_volume.__dict__), str(retry_time)) ) retry_time = retry_time - 1 # 3.attach the volume to vgw host try: # 3.1 get the vgw host vgw_host = self._get_provider_node(self.configuration.cgw_host_id) if not vgw_host: raise exception_ex.VgwHostNotFound(Vgw_id=self.configuration.cgw_host_id) device_name = self._get_next_device_name(vgw_host) LOG.error("**********************************************") LOG.error("the volume status %s" % provider_volume.state) self.adpter.attach_volume(vgw_host, provider_volume, device_name) # query volume status time.sleep(1) retry_time = 120 provider_volume = self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get("attachment_status") == "attached": break else: time.sleep(2) provider_volume = self._get_provider_volume(provider_volume_id) retry_time = retry_time - 1 except Exception as e: raise e time.sleep(5) conn = rpyc.connect(self.configuration.cgw_host_ip, int(CONF.vgw.rpc_service_port)) LOG.error( "begin time of copy_volume_to_file is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) ) full_file_path = conn.root.copy_volume_to_file(device_name, file_name, CONF.vgw.store_file_dir) LOG.error("end time of copy_volume_to_image is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) # todo exception occured clean env if not full_file_path: self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError(volume_id=volume_id) LOG.error("begin time of push_file_to_vgw is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) push_file_result = conn.root.exposed_push_file_to_vgw(full_file_path, vgw_url) LOG.error("end time of push_file_to_vgw is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) if not push_file_result: LOG.error("post file file %s to %s failed" % (push_file_result, vgw_url)) self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError(volume_id=volume_id) conn.close() # create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) # 4.detach form vgw self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 120 provider_volume = self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get("attachment_status") is None: break else: time.sleep(2) provider_volume = self._get_provider_volume(provider_volume_id) retry_time = retry_time - 1 LOG.error("**********************************************") LOG.error("the volume status %s" % provider_volume.state) # attach the volume back if origin_provider_volume_state is not None: origin_attach_node = self._get_provider_node(origin_attach_node_id) self.adpter.attach_volume(origin_attach_node, provider_volume, origin_device_name) else: if not os.path.exists(self.configuration.provider_image_conversion_dir): fileutils.ensure_tree(self.configuration.provider_image_conversion_dir) provider_volume_id = self._get_provider_volumeid_from_volume(volume) task_ret = self.adpter.export_volume( provider_volume_id, self.configuration.provider_image_conversion_dir, str(image_meta["id"]), cgw_host_id=self.configuration.cgw_host_id, cgw_host_ip=self.configuration.cgw_host_ip, cgw_username=self.configuration.cgw_username, cgw_certificate=self.configuration.cgw_certificate, transfer_station=self.configuration.storage_tmp_dir, ) if not task_ret: raise exception_ex.ProviderExportVolumeError temp_path = os.path.join(self.configuration.provider_image_conversion_dir, str(image_meta["id"])) upload_image = temp_path try: image_utils.upload_volume(context, image_service, image_meta, upload_image) finally: fileutils.delete_if_exists(upload_image) LOG.error("end time of copy_volume_to_image is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
def copy_volume_to_image(self, context, volume, image_service, image_meta): LOG.error('begin time of copy_volume_to_image is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format=image_meta.get('container_format') file_name=image_meta.get('id') if container_format in ['fs_vgw_url','vcloud_vgw_url','aws_vgw_url']: LOG.debug('get the vgw url') vgw_url = CONF.vgw.vgw_url.get(container_format) #vgw_url = 'http://162.3.125.52:9999/' volume_id = volume['id'] #1.get the provider_volume at provider cloud provider_volume_id = self._get_provider_volumeid_from_volume(volume) if not provider_volume_id: LOG.error('get provider_volume_id of volume %s error' % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) provider_volume=self._get_provider_volume(provider_volume_id) if not provider_volume: LOG.error('get provider_volume of volume %s at provider cloud error' % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) origin_provider_volume_state= provider_volume.extra.get('attachment_status') origin_attach_node_id = None origin_device_name=None #2.judge if the volume is available if origin_provider_volume_state is not None: origin_attach_node_id = provider_volume.extra['instance_id'] origin_device_name = provider_volume.extra['device'] self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 50 provider_volume=self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get('attachment_status') is None: break else: time.sleep(1) provider_volume=self._get_provider_volume(provider_volume_id) retry_time = retry_time-1 #3.attach the volume to vgw host try: #3.1 get the vgw host vgw_host= self._get_provider_node(self.configuration.cgw_host_id) if not vgw_host: raise exception_ex.VgwHostNotFound(Vgw_id=self.configuration.cgw_host_id) device_name=self._get_next_device_name(vgw_host) LOG.error('**********************************************') LOG.error('the volume status %s' %provider_volume.state) self.adpter.attach_volume(vgw_host, provider_volume, device_name) #query volume status time.sleep(1) retry_time = 120 provider_volume=self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get('attachment_status') =='attached': break else: time.sleep(1) provider_volume=self._get_provider_volume(provider_volume_id) retry_time = retry_time-1 except Exception as e: raise e time.sleep(5) conn=rpyc.connect(self.configuration.cgw_host_ip,int(CONF.vgw.rpc_service_port)) LOG.error('begin time of copy_volume_to_file is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) full_file_path = conn.root.copy_volume_to_file(device_name,file_name,CONF.vgw.store_file_dir) LOG.error('end time of copy_volume_to_image is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) #todo exception occured clean env if not full_file_path: self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError(volume_id=volume_id) LOG.error('begin time of push_file_to_vgw is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) push_file_result =conn.root.exposed_push_file_to_vgw(full_file_path,vgw_url) LOG.error('end time of push_file_to_vgw is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) if not push_file_result: LOG.error('post file file %s to %s failed' %(push_file_result,vgw_url)) self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError(volume_id=volume_id) conn.close() #create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) #4.detach form vgw self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 120 provider_volume=self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get('attachment_status') is None: break else: time.sleep(1) provider_volume=self._get_provider_volume(provider_volume_id) retry_time = retry_time-1 LOG.error('**********************************************') LOG.error('the volume status %s' %provider_volume.state) #attach the volume back if origin_provider_volume_state is not None: origin_attach_node = self._get_provider_node(origin_attach_node_id) self.adpter.attach_volume(origin_attach_node, provider_volume, origin_device_name) else: if not os.path.exists(self.configuration.provider_image_conversion_dir): fileutils.ensure_tree(self.configuration.provider_image_conversion_dir) provider_volume_id = self._get_provider_volumeid_from_volume(volume) task_ret = self.adpter.export_volume(provider_volume_id, self.configuration.provider_image_conversion_dir, str(image_meta['id']), cgw_host_id=self.configuration.cgw_host_id, cgw_host_ip=self.configuration.cgw_host_ip, cgw_username=self.configuration.cgw_username, cgw_certificate=self.configuration.cgw_certificate, transfer_station=self.configuration.storage_tmp_dir) if not task_ret: raise exception_ex.ProviderExportVolumeError temp_path = os.path.join(self.configuration.provider_image_conversion_dir, str(image_meta['id'])) upload_image = temp_path try: image_utils.upload_volume(context, image_service, image_meta, upload_image) finally: fileutils.delete_if_exists(upload_image) LOG.error('end time of copy_volume_to_image is %s' %(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Creates glance image from volume.""" LOG.debug( 'Copying volume %(volume_name)s to image %(image_name)s.', { 'volume_name': volume['display_name'], 'image_name': image_meta.get('name') }) LOG.error('begin time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get('container_format') if container_format in VGW_URLS: # attach the volume to vgw vm disk_ref, the_vapp = self._attach_volume_to_vgw(volume) try: # use ssh client connect to vgw_host and # copy image file to volume self._copy_volume_to_file_to_vgw(image_meta) finally: # detach volume from vgw and self._detach_disk_from_vm(the_vapp, disk_ref) # create an empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) LOG.error('end time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) else: def _unused(): # todo(wangfeng) # pdb.set_trace() # 1. get the vmdk file. volume_id = volume['id'] if volume['attach_status'] == 'attached': # nova_client = nova.novaclient(context) # node_name = nova_client.servers.get(instance_id)._ # info['OS-EXT-SRV-ATTR:hypervisor_hostname'] the_vapp = self._get_vcloud_vapp(volume['instance_uuid']) vmdk_url = self._query_vmdk_url(the_vapp) local_file_name = '%s/%s.vmdk' % ( CONF.vcloud.vcloud_conversion_dir, volume_id) self._download_vmdk_from_vcloud(context, vmdk_url, local_file_name) # volume_id = volume['id'] # create_info = {'snapshot_id':volume['snapshot_id'], # 'type':'qcow2', # 'new_file':'snapshot_file'} # self._nova_api.create_volume_snapshot(context,volume_id,create_info) # local_file_name = '%s/%s.vmdk' % (CONVERT_DIR, volume_id) else: local_file_name = '%s/%s.vmdk' % ( CONF.vcloud.vcloud_volumes_dir, volume_id) if not os.path.exists(local_file_name): LOG.error('volume file %s do not exist' % local_file_name) return # 1. the file is vmdk, convert it to qcow2 # converted_file_name = '%s/converted-file.qcow2' % # CONF.vcloud.vcloud_conversion_dir # convert_commond = "qemu-img convert -f %s -O %s %s %s" % \ # ('vmdk', # 'qcow2', # local_file_name, # converted_file_name) # convert_result = subprocess.call([convert_commond],shell=True) # if convert_result != 0: # do something, change metadata # LOG.error('converting file failed') # 2. upload to glance # file_size = os.path.getsize(converted_file_name) # The properties and other fields that we need to set for the image. # image_meta['disk_format'] = 'qcow2' # image_meta['status'] = 'active' # image_meta['size'] = file_size # image_meta['properties'] = {'owner_id': volume['project_id']} # image_utils.upload_volume(context,image_service,image_meta,converted_file_name,'qcow2') image_utils.upload_volume(context, image_service, image_meta, local_file_name, 'vmdk') # timeout = IMAGE_TRANSFER_TIMEOUT_SECS # util.start_transfer(context, timeout, read_file_handle, file_size, # image_service=image_service, image_id=image_meta['id'], # image_meta=image_meta) _unused()
def copy_volume_to_image(self, context, volume, image_service, image_meta): LOG.error('begin time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) container_format = image_meta.get('container_format') file_name = image_meta.get('id') if container_format in ['fs_vgw_url', 'vcloud_vgw_url', 'aws_vgw_url']: LOG.debug('get the vgw url') vgw_url = CONF.vgw.vgw_url.get(container_format) #vgw_url = 'http://162.3.125.52:9999/' volume_id = volume['id'] #1.get the provider_volume at provider cloud provider_volume_id = self._get_provider_volumeid_from_volume( volume) if not provider_volume_id: LOG.error('get provider_volume_id of volume %s error' % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) provider_volume = self._get_provider_volume(provider_volume_id) if not provider_volume: LOG.error( 'get provider_volume of volume %s at provider cloud error' % volume_id) raise exception_ex.ProviderVolumeNotFound(volume_id=volume_id) origin_provider_volume_state = provider_volume.extra.get( 'attachment_status') origin_attach_node_id = None origin_device_name = None #2.judge if the volume is available if origin_provider_volume_state is not None: origin_attach_node_id = provider_volume.extra['instance_id'] origin_device_name = provider_volume.extra['device'] self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 50 provider_volume = self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get( 'attachment_status') is None: break else: time.sleep(1) provider_volume = self._get_provider_volume( provider_volume_id) retry_time = retry_time - 1 #3.attach the volume to vgw host try: #3.1 get the vgw host vgw_host = self._get_provider_node( self.configuration.cgw_host_id) if not vgw_host: raise exception_ex.VgwHostNotFound( Vgw_id=self.configuration.cgw_host_id) device_name = self._get_next_device_name(vgw_host) LOG.error('**********************************************') LOG.error('the volume status %s' % provider_volume.state) self.adpter.attach_volume(vgw_host, provider_volume, device_name) #query volume status time.sleep(1) retry_time = 120 provider_volume = self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get( 'attachment_status') == 'attached': break else: time.sleep(1) provider_volume = self._get_provider_volume( provider_volume_id) retry_time = retry_time - 1 except Exception as e: raise e time.sleep(5) conn = rpyc.connect(self.configuration.cgw_host_ip, int(CONF.vgw.rpc_service_port)) LOG.error('begin time of copy_volume_to_file is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) full_file_path = conn.root.copy_volume_to_file( device_name, file_name, CONF.vgw.store_file_dir) LOG.error('end time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) #todo exception occured clean env if not full_file_path: self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError( volume_id=volume_id) LOG.error('begin time of push_file_to_vgw is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) push_file_result = conn.root.exposed_push_file_to_vgw( full_file_path, vgw_url) LOG.error('end time of push_file_to_vgw is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) if not push_file_result: LOG.error('post file file %s to %s failed' % (push_file_result, vgw_url)) self.adpter.detach_volume(provider_volume) conn.close() raise exception_ex.ProviderExportVolumeError( volume_id=volume_id) conn.close() #create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) fileutils.delete_if_exists(tmp) #4.detach form vgw self.adpter.detach_volume(provider_volume) time.sleep(1) retry_time = 120 provider_volume = self._get_provider_volume(provider_volume_id) while retry_time > 0: if provider_volume and provider_volume.extra.get( 'attachment_status') is None: break else: time.sleep(1) provider_volume = self._get_provider_volume( provider_volume_id) retry_time = retry_time - 1 LOG.error('**********************************************') LOG.error('the volume status %s' % provider_volume.state) #attach the volume back if origin_provider_volume_state is not None: origin_attach_node = self._get_provider_node( origin_attach_node_id) self.adpter.attach_volume(origin_attach_node, provider_volume, origin_device_name) else: if not os.path.exists( self.configuration.provider_image_conversion_dir): fileutils.ensure_tree( self.configuration.provider_image_conversion_dir) provider_volume_id = self._get_provider_volumeid_from_volume( volume) task_ret = self.adpter.export_volume( provider_volume_id, self.configuration.provider_image_conversion_dir, str(image_meta['id']), cgw_host_id=self.configuration.cgw_host_id, cgw_host_ip=self.configuration.cgw_host_ip, cgw_username=self.configuration.cgw_username, cgw_certificate=self.configuration.cgw_certificate, transfer_station=self.configuration.storage_tmp_dir) if not task_ret: raise exception_ex.ProviderExportVolumeError temp_path = os.path.join( self.configuration.provider_image_conversion_dir, str(image_meta['id'])) upload_image = temp_path try: image_utils.upload_volume(context, image_service, image_meta, upload_image) finally: fileutils.delete_if_exists(upload_image) LOG.error('end time of copy_volume_to_image is %s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
def copy_volume_to_image(self, context, volume, image_service, image_meta): """Copy the volume to the specified image. copy disk to image and disk for image. """ # TODO(haifeng)user delete iamge on openstack, image still in Azure. azure_type = StorageAccountTypes.premium_lrs \ if 'azure_ssd' == volume.volume_type.name \ else StorageAccountTypes.standard_lrs metadata = volume.get('volume_metadata', []) metadata_dict = {item['key']: item['value'] for item in metadata} os_type = metadata_dict.get('os_type') if not os_type: reason = 'Volume miss os_type can\'t copy to Image.' message = (_("Copy Volume to Image %(image_id)s in Azure" " failed. reason: %(reason)s") % dict(image_id=image_meta['id'], reason=reason)) LOG.exception(message) raise exception.VolumeBackendAPIException(data=message) disk_name = self._get_name_from_id(VOLUME_PREFIX, volume['id']) image_name = self._get_name_from_id(IMAGE_PREFIX, image_meta['id']) try: disk_obj = self.disks.get(CONF.azure.resource_group, disk_name) except Exception as e: message = (_("Create Image from Volume %(volume)s in Azure" " failed. reason: %(reason)s") % dict(volume=volume['id'], reason=six.text_type(e))) LOG.exception(message) raise exception.VolumeBackendAPIException(data=message) self._copy_disk(image_name, disk_obj.id, azure_type) try: image_dict = { 'location': CONF.azure.location, 'storage_profile': { 'os_disk': { 'os_type': os_type, 'os_state': "Generalized", 'managed_disk': { 'id': disk_obj.id } } } } async_action = self.images.create_or_update( CONF.azure.resource_group, image_name, image_dict) async_action.result() except Exception as e: message = (_("Copy Volume to Image %(volume)s in Azure" " failed. reason: %(reason)s") % dict(volume=disk_name, reason=six.text_type(e))) LOG.exception(message) raise exception.VolumeBackendAPIException(data=message) # create a empty file to glance with image_utils.temporary_file() as tmp: image_utils.upload_volume(context, image_service, image_meta, tmp) image_meta['disk_format'] = 'vhd' image_meta['properties'] = { 'os_type': os_type, 'azure_image_size_gb': volume.size } image_service.update(context, image_meta['id'], image_meta)