def r_create(): os_template_image = OSTemplateImage() args_rules = [ Rules.LABEL.value, Rules.DESCRIPTION.value, Rules.PATH.value, Rules.LOGO.value, Rules.OS_TEMPLATE_PROFILE_ID_EXT.value, Rules.ACTIVE.value, Rules.OS_TEMPLATE_IMAGE_KIND.value ] os_template_image.label = request.json.get('label') os_template_image.description = request.json.get('description') os_template_image.path = request.json.get('path') os_template_image.logo = request.json.get('logo') os_template_image.active = bool(int(request.json.get('active', 1))) os_template_image.os_template_profile_id = request.json.get( 'os_template_profile_id') os_template_image.kind = request.json.get('kind') os_template_image.progress = 100 try: ji.Check.previewing(args_rules, os_template_image.__dict__) ret = dict() ret['state'] = ji.Common.exchange_state(20000) if os_template_image.exist_by('path'): ret['state'] = ji.Common.exchange_state(40901) ret['state']['sub']['zh-cn'] = ''.join( [ret['state']['sub']['zh-cn'], ': ', os_template_image.path]) return ret os_template_profile = OSTemplateProfile() os_template_profile.id = os_template_image.os_template_profile_id if not os_template_profile.exist(): ret['state'] = ji.Common.exchange_state(40401) ret['state']['sub']['zh-cn'] = ''.join([ ret['state']['sub']['zh-cn'], u': 操作系统模板描述文件ID: ', os_template_image.os_template_profile_id.__str__() ]) return ret os_template_image.create() os_template_image.get_by('path') ret['data'] = os_template_image.__dict__ return ret except ji.PreviewingError, e: return json.loads(e.message)
def r_convert_to_os_template_image(snapshot_id, disk_uuid): args_rules = [ Rules.SNAPSHOT_ID.value, Rules.DISK_UUID.value, Rules.LABEL.value ] try: ret = dict() ret['state'] = ji.Common.exchange_state(20000) ji.Check.previewing(args_rules, {'snapshot_id': snapshot_id, 'disk_uuid': disk_uuid, 'label': request.json.get('label')}) rows, _ = SnapshotDiskMapping.get_by_filter(filter_str=':'.join(['snapshot_id', 'eq', snapshot_id])) disks_uuid = list() for row in rows: disks_uuid.append(row['disk_uuid']) if disk_uuid not in disks_uuid: ret['state'] = ji.Common.exchange_state(40401) ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], u': 未在快照: ', snapshot_id, u' 中找到磁盘:', disk_uuid]) return ret config = Config() config.id = 1 config.get() snapshot = Snapshot() os_template_image = OSTemplateImage() guest = Guest() disk = Disk() snapshot.snapshot_id = snapshot_id snapshot.get_by('snapshot_id') snapshot.progress = 252 guest.uuid = snapshot.guest_uuid guest.get_by('uuid') disk.uuid = disk_uuid disk.get_by('uuid') os_template_image.id = guest.os_template_image_id os_template_image.get() image_name = '_'.join([snapshot.snapshot_id, disk.uuid]) + '.' + disk.format os_template_image.id = 0 os_template_image.label = request.json.get('label') os_template_image.path = '/'.join([os.path.dirname(os_template_image.path), image_name]) os_template_image.kind = OSTemplateImageKind.custom.value os_template_image.progress = 0 os_template_image.create_time = ji.Common.tus() if os_template_image.exist_by('path'): ret['state'] = ji.Common.exchange_state(40901) ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template_image.path]) return ret os_template_image.create() os_template_image.get_by('path') message = { '_object': 'snapshot', 'action': 'convert', 'uuid': disk.guest_uuid, 'snapshot_id': snapshot.snapshot_id, 'storage_mode': config.storage_mode, 'dfs_volume': config.dfs_volume, 'node_id': disk.node_id, 'snapshot_path': disk.path, 'template_path': os_template_image.path, 'os_template_image_id': os_template_image.id, 'passback_parameters': {'id': snapshot.snapshot_id, 'os_template_image_id': os_template_image.id} } Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False)) snapshot.update() return ret except ji.PreviewingError, e: return json.loads(e.message)
def r_update(_id): os_template_image = OSTemplateImage() args_rules = [Rules.ID.value] if 'label' in request.json: args_rules.append(Rules.LABEL.value, ) if 'description' in request.json: args_rules.append(Rules.DESCRIPTION.value, ) if 'path' in request.json: args_rules.append(Rules.PATH.value, ) if 'active' in request.json: args_rules.append(Rules.ACTIVE.value, ) if 'logo' in request.json: args_rules.append(Rules.LOGO.value, ) if 'os_template_profile_id' in request.json: args_rules.append(Rules.OS_TEMPLATE_PROFILE_ID_EXT.value, ) if 'kind' in request.json: args_rules.append(Rules.OS_TEMPLATE_IMAGE_KIND.value, ) if args_rules.__len__() < 2: ret = dict() ret['state'] = ji.Common.exchange_state(20000) return ret request.json['id'] = _id try: ji.Check.previewing(args_rules, request.json) os_template_image.id = request.json.get('id') os_template_image.get() os_template_image.label = request.json.get('label', os_template_image.label) os_template_image.description = request.json.get( 'description', os_template_image.description) os_template_image.path = request.json.get('path', os_template_image.path) os_template_image.active = request.json.get('active', os_template_image.active) os_template_image.logo = request.json.get('logo', os_template_image.logo) os_template_image.os_template_profile_id = \ request.json.get('os_template_profile_id', os_template_image.os_template_profile_id) os_template_image.kind = request.json.get('kind', os_template_image.kind) os_template_image.update() os_template_image.get() ret = dict() ret['state'] = ji.Common.exchange_state(20000) ret['data'] = os_template_image.__dict__ return ret except ji.PreviewingError, e: return json.loads(e.message)