def _volume_recover(snapshot_info, name): snapshot_id = snapshot_info["id"] volume_info = yield get_volume(name=name) volume_id = volume_info.get('id') volume_metadata = volume_info.get("metadata") volume_metadata["status"] = "recovering" volume_metadata["recover_status"] = "" snapshot_metadata = snapshot_info.get("metadata") snapshot_metadata["status"] = "recovering" yield volume_request.update_metadata(volume_id, volume_metadata) yield volume_request.update_metadata(snapshot_id, snapshot_metadata) yield _gen_snapshot_recover_volume(snapshot_info, volume_info)
def execute(self): try: volume_id = self._message.get('volume_id') volume_name = self._message.get("display_name") source_volume_name = volume_name[len("ecloud-recover-"):] source_volume = yield get_volume(name=source_volume_name) source_id = source_volume["id"] source_volume_metadata = source_volume["metadata"] source_volume_metadata["status"] = "deleting" if source_volume["attachments"]: vm_id = source_volume["attachments"][0]["vm_id"] volume_request.update_metadata(source_id, source_volume_metadata) yield volume_request.volume_update( volume_id, name=volume_name[len('ecloud-recover-'):], description=0) yield volume_request.volume_update(source_id, description=5) yield detach_server_volume(vm_id, source_id) except Exception, e: LOG.error("Volume Recover When Attach Volume End Error %s" % e)
def _gen_snapshot_recover_volume(snapshot_info, volume_info): size = snapshot_info["size_gb"] tenant_id = snapshot_info["tenant"]["id"] source_volid = snapshot_info["id"] metadata = volume_info["metadata"] metadata["snapshot_id"] = source_volid location = snapshot_info["location"] volume_type = location[location.rindex("#") + 1:] recover_name = "ecloud-recover-%s" % snapshot_info["name"][ len("ecloud-snapshot-"):] availability_zone = location[:location. index("@")] if "lvm" in location else None try: yield volume_request.volume_create(size, tenant_id, consistencygroup_id=None, snapshot_id=None, source_volid=source_volid, name=recover_name, description=4, volume_type=volume_type, user_id=None, project_id=tenant_id, availability_zone=availability_zone, metadata=metadata, image_ref=None, scheduler_hints=None, source_replica=None) except Exception as e: LOG.error("Snapshot recover error when create volume clone. msg:%s", e) volume_id = volume_info.get('id') volume_metadata = volume_info.get("metadata") volume_metadata["status"] = "" volume_metadata["recover_status"] = "recover-error" snapshot_metadata = snapshot_info.get("metadata") snapshot_metadata["status"] = "" yield volume_request.update_metadata(volume_id, volume_metadata) yield volume_request.update_metadata(snapshot_info["id"], snapshot_metadata) raise SnapshotRecoverCreateError()
def _vm_recover(snapshot_info, name): vm_info = yield compute.get_server(name=name) vm_status = vm_info.get("state") if vm_status not in ["active", "stopped", "error"]: raise VmMustActiveOrStop() snapshot_id = snapshot_info["id"] snapshot_metadata = snapshot_info.get("metadata") snapshot_metadata["status"] = "recovering" vm_id = vm_info.get('id') yield set_or_update_vm_meta(vm_id, "status", "recovering") yield set_or_update_vm_meta(vm_id, "recover_status", "") yield volume_request.update_metadata(snapshot_id, snapshot_metadata) yield _gen_snapshot_recover_vm(vm_id, snapshot_info)
def execute(self): try: success = False if self._message.get("status") == "available": success = True volume_name = self._message.get("display_name") volume_id = self._message.get("volume_id") v_metadata = yield get_metadata(volume_id) source_name = volume_name[len("ecloud-recover-"):] snapshot_id = v_metadata.get("snapshot_id") LOG.debug("recover snapshot source name is %s snapshot is %s ", source_name, snapshot_id) if source_name.startswith("vd-"): source_volume = yield get_volume(name=source_name) source_id = source_volume["id"] if success: if source_volume.get("attachments"): vm_id = source_volume["attachments"][0]["vm_id"] yield attach_server_volume(vm_id, volume_id) else: yield volume_request.volume_update( volume_id, name=volume_name[len('ecloud-recover-'):], description=0) yield volume_request.volume_update(source_id, description=5) yield volume_request.volume_delete(source_id) LOG.debug( "recover snapshot delete source_id [%s] success ! ", source_id) else: v_metadata["recover_status"] = "recover-error" v_metadata["status"] = "" yield volume_request.update_metadata(volume_id, v_metadata) yield delete_metadata(snapshot_id, ["status"]) else: vm = yield compute.get_server(name=source_name, detailed=False) vm_id = vm["id"] if success: yield attach_server_volume(vm_id, volume_id) else: yield set_or_update_vm_meta(vm_id, "recover_status", "recover-error") if not success: yield volume_request.volume_delete(volume_id) except Exception, e: LOG.error("Create Recover Volume Error %s" % e)
def _gen_snapshot_volume(snapshots): for snapshot_item in snapshots: volume_id = snapshot_item["volume_id"] size = snapshot_item["size"] tenant_id = snapshot_item["tenant_id"] source_volid = volume_id metadata = snapshot_item["metadata"] status = snapshot_item["status"] availability_zone = metadata["availability_zone"] back_description = snapshot_item["display_discription"] volume_type = snapshot_item["volume_type"] snapshot_name = snapshot_item["name"] if snapshot_name.startswith("ecloud-snapshot-vd-"): volume_metadata = snapshot_item["volume_matedata"] volume_metadata["status"] = "snapshoting" yield volume_request.update_metadata(source_volid, volume_metadata) yield volume_request.reset_state(volume_id=volume_id, volume_state="available") try: volume = yield volume_request.volume_create( size, tenant_id, consistencygroup_id=None, snapshot_id=None, source_volid=source_volid, name=snapshot_name, description=back_description, volume_type=volume_type, user_id=None, project_id=tenant_id, availability_zone=availability_zone, metadata=metadata, image_ref=None, scheduler_hints=None, source_replica=None) except Exception as e: LOG.error("Snapshot error when create volume clone. msg:%s", e) yield volume_request.reset_state(volume_id=volume_id, volume_state=status) vm_id = metadata.get("vm_id", None) if vm_id: yield del_meta(vm_id, ["status"]) raise SnapshotCreateError()
def __set_volume_metadata(volume_id, metadata): try: metadata = {key: str(value) for key, value in metadata} yield volume_request.update_metadata(volume_id, metadata) except Exception as e: LOG.error("Snapshot set volume metadata error: %s" % e)