def list_snapshots(self, name): """ :param name: :return: """ if name.startswith("vm-"): vm = yield get_server(name=name) status = vm["state"] r_status = vm["recover-status"] id = vm['id'] elif name.startswith("vd-"): source = yield get_volume(name=name) status = source["status"] r_status = source["recover-status"] id = source['id'] else: raise InvalidateParam() out_snapshots = yield snapshot_package.list_snapshot(name) result = { "snapshots": out_snapshots, "status": status, "id": id, "recover-status": enum_recover_status(status, r_status) } self.response(Response(result=result, total=len(out_snapshots)))
def attach_volume(self, attachment): volume_id = attachment['volume_id'] server_id = attachment['vm_id'] volume = yield get_volume(volume_id, detailed=True) server = yield compute.get_server(vm_id=server_id) if not volume or not server: raise InvalidateParam(args=['vm_id', 'volume_id']) volume_project = unicode(volume['tenant'].get("id", None)) server_project = unicode(server['tenant'].get("id", None)) if volume_project and server_project and server_project != volume_project: raise AttachBeyondDomainError volume_user = unicode(volume['user'].get("id", None)) server_user = unicode(server['user'].get("id", None)) if volume_user and server_user and volume_user != server_user: raise AttachBeyondDomainError yield compute.attach_server_volume(volume_id=volume_id, server_id=server_id) optLog.write( self.request, optLog.Type.VDISK, volume['name'], optLog.Operator.ATTACH, volume['metadata']['displayname'] + ">>" + server['name'] + " " + gen_network_comment(server['network'].values())) self.response(Response())
def execute(self): try: volume_id = self._message.get('volume_id') volume_name = self._message.get("display_name") vm_name = volume_name[len("ecloud-recover-"):] LOG.debug("vm recover message is %s", self._message) source_volume = yield get_volume(volume_id=volume_id, vd_type=4) metadata = source_volume["metadata"] vm_id = metadata.get("vm_id") snapshot_id = metadata.get("snapshot_id") volume_info = yield list_server_attach_volume(vm_id, vd_type=1) sys_volume_id = volume_info[0]["volume_id"] yield snapshotdao.update_block_device_mapping(vm_id, volume_id) new_sys_volume_name = "ecloud-sys-volume-%s" % vm_name yield volume_request.volume_update(volume_id, name=new_sys_volume_name, description=1) yield snapshotdao.update_volume_db(volume_id=sys_volume_id, status="available", attach_status="detached") yield volume_request.volume_update(sys_volume_id, description=5) yield set_or_update_vm_meta(vm_id, compute.NEED_DELETE_SYS_VOLUME, sys_volume_id) yield compute.server_action(vm_id, "reboot", info={"type": "HARD"}) except Exception, e: LOG.error("VM Recover When Attach Volume End Error %s" % e)
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 update_snapshot(snapshot_id, name, des): try: snapshot = yield get_volume(volume_id=snapshot_id, vd_type=2) if snapshot: volume_meta = snapshot['metadata'] \ if snapshot['metadata'] else {} volume_meta['des'] = str(des) volume_meta['displayname'] = str(name) yield set_metadata(volume_id=snapshot_id, metadata=volume_meta) except Exception, e: LOG.error("Update Snapshot Error: %s" % e) raise SnapshotOperationFailed()
def __force_detach_server_volume(volume_id): """ force detach specific volume from specific server :param volume_id: The :id: volume """ try: volume = yield _vm.get_volume(volume_id=volume_id, detailed=False) if volume['status'] != 'in-use': yield __force_del_detach_volume(volume_id) yield _vm.force_delete_volume_from_db(volume_id=volume_id) else: raise ServerDetachVolumeError() except Exception as e: LOG.error("force detach volume from server error: %s" % e)
def delete_snapshot(snapshot_id): try: snapshot_info = yield get_volume(volume_id=snapshot_id, vd_type=2) if snapshot_info: snapshot_id = snapshot_info.get("id") volume_meta = snapshot_info['metadata'] \ if snapshot_info['metadata'] else {} volume_meta['status'] = "deleting" yield set_metadata(volume_id=snapshot_id, metadata=volume_meta) yield volume_request.volume_delete(snapshot_id) except Exception as e: LOG.error("Del Snapshot Error: %s" % e) raise SnapshotDeleteError raise gen.Return(snapshot_info)
def get_snapshot(snapshot_id, detailed=False): snapshot = yield get_volume(volume_id=snapshot_id, vd_type=2) if detailed: raise gen.Return(snapshot) snapshot_info = { "id": snapshot["id"], "name": snapshot["name"], "displayname": snapshot["metadata"]["displayname"], "create_at": snapshot.get('created_at'), "des": snapshot["metadata"]["des"], "size": snapshot["size_gb"], "status": snapshot["status"] } raise gen.Return(snapshot_info)
def create_volume_snapshot(self, params): """ :param params: { 'type':'snapshot_type', 0:vm;1:vd 'id':"source_id", 'name':'name', 'description':'description', 'volume_ids':[id1,id2...] } :return: """ snapshot_type = params.get("type") source_id = params.get("id") name = params.get("name") description = params.get("des", "") volume_ids = params.get("volume_ids", []) if snapshot_type not in [0, 1]: raise SnapshotTypeNotStandard if snapshot_type == 0: info = yield get_server(vm_id=source_id) if not info: raise VmNotExist() else: info = yield get_volume(volume_id=source_id) if not info: raise VolumeNotExist() yield snapshot_create(source_id, snapshot_type, name, description, volume_ids) if snapshot_type == 0: for volume in volume_ids: volume_info = yield volume_package.get_volume(volume_id=volume) optLog.write(self.request, Type.SNAPSHOT, volume_info.get("name"), Operator.CREATE, name) optLog.write(self.request, Type.SNAPSHOT, info.get("name"), Operator.CREATE, name) self.response(Response())
def detach_volume(self, attachment): volume_id = attachment['volume_id'] server_id = attachment['vm_id'] volume = yield get_volume(volume_id, detailed=True) server = yield compute.get_server(vm_id=server_id, detailed=True) if not volume or not server: raise InvalidateParam(args=['vm_id', 'volume_id']) yield compute.detach_server_volume(server_id=server_id, volume_id=volume_id) optLog.write( self.request, optLog.Type.VDISK, volume['name'], optLog.Operator.DETACH, volume['metadata']['displayname'] + ">>" + server['name'] + " " + gen_network_comment(server['network'].values())) self.response(Response())
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 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)