def r_detach_disk(disk_uuid): args_rules = [Rules.DISK_UUID.value] try: ji.Check.previewing(args_rules, {'disk_uuid': disk_uuid}) disk = Disk() disk.uuid = disk_uuid disk.get_by('uuid') ret = dict() ret['state'] = ji.Common.exchange_state(20000) if disk.state != DiskState.mounted.value or disk.sequence == 0: # 表示未被任何实例使用,已被分离 # 序列为 0 的表示实例系统盘,系统盘不可以被分离 # TODO: 系统盘单独范围其它状态 return ret guest = Guest() guest.uuid = disk.guest_uuid guest.get_by('uuid') # 判断 Guest 是否处于可用状态 if guest.status in (status.GuestState.no_state.value, status.GuestState.dirty.value): ret['state'] = ji.Common.exchange_state(41259) return ret config = Config() config.id = 1 config.get() guest_xml = GuestXML(guest=guest, disk=disk, config=config) message = { '_object': 'guest', 'action': 'detach_disk', 'uuid': disk.guest_uuid, 'node_id': guest.node_id, 'xml': guest_xml.get_disk(), 'passback_parameters': { 'disk_uuid': disk.uuid } } Utils.emit_instruction(message=json.dumps(message)) disk.state = DiskState.unloading.value disk.update() return ret except ji.PreviewingError, e: return json.loads(e.message)
def r_attach_disk(uuid, disk_uuid): args_rules = [Rules.UUID.value, Rules.DISK_UUID.value] try: ji.Check.previewing(args_rules, {'uuid': uuid, 'disk_uuid': disk_uuid}) guest = Guest() guest.uuid = uuid guest.get_by('uuid') disk = Disk() disk.uuid = disk_uuid disk.get_by('uuid') config = Config() config.id = 1 config.get() ret = dict() ret['state'] = ji.Common.exchange_state(20000) # 判断欲挂载的磁盘是否空闲 if disk.guest_uuid.__len__() > 0 or disk.state != DiskState.idle.value: ret['state'] = ji.Common.exchange_state(41258) return ret # 判断 Guest 是否处于可用状态 if guest.status in (status.GuestState.no_state.value, status.GuestState.dirty.value): ret['state'] = ji.Common.exchange_state(41259) return ret # 判断 Guest 与 磁盘是否在同一宿主机上 if config.storage_mode in [ status.StorageMode.local.value, status.StorageMode.shared_mount.value ]: if guest.node_id != disk.node_id: ret['state'] = ji.Common.exchange_state(41260) return ret # 通过检测未被使用的序列,来确定当前磁盘在目标 Guest 身上的序列 disk.guest_uuid = guest.uuid disks, count = disk.get_by_filter(filter_str='guest_uuid:in:' + guest.uuid) already_used_sequence = list() for _disk in disks: already_used_sequence.append(_disk['sequence']) for sequence in range(0, dev_table.__len__()): if sequence not in already_used_sequence: disk.sequence = sequence break disk.state = DiskState.mounting.value guest_xml = GuestXML(guest=guest, disk=disk, config=config) message = { '_object': 'guest', 'action': 'attach_disk', 'uuid': uuid, 'node_id': guest.node_id, 'xml': guest_xml.get_disk(), 'passback_parameters': { 'disk_uuid': disk.uuid, 'sequence': disk.sequence }, 'disks': [disk.__dict__] } Utils.emit_instruction(message=json.dumps(message)) disk.update() return ret except ji.PreviewingError, e: return json.loads(e.message)