def guest_creating_progress_report_engine(): """ Guest 创建进度上报引擎 """ list_creating_guest = list() template_size = dict() while True: if Utils.exit_flag: msg = 'Thread guest_creating_progress_report_engine say bye-bye' print msg logger.info(msg=msg) return try: try: payload = q_creating_guest.get( timeout=config['engine_cycle_interval']) list_creating_guest.append(payload) q_creating_guest.task_done() except Queue.Empty as e: pass # 当有 Guest 被创建时,略微等待一下,避免复制模板的动作还没开始,就开始计算进度。这样会产生找不到镜像路径的异常。 time.sleep(1) threads_status['guest_creating_progress_report_engine'] = { 'timestamp': ji.Common.ts() } for i, guest in enumerate(list_creating_guest): template_path = guest['template_path'] storage = Storage(storage_mode=guest['storage_mode'], dfs_volume=guest['dfs_volume']) if template_path not in template_size: template_size[template_path] = float( storage.getsize(path=template_path)) system_image_size = storage.getsize( path=guest['system_image_path']) progress = system_image_size / template_size[template_path] guest_event_emit.creating(uuid=guest['uuid'], progress=int(progress * 90)) if progress >= 1: del list_creating_guest[i] except: log_emit.warn(traceback.format_exc())
def guest_creating_progress_report_engine(): """ Guest 创建进度上报引擎 """ list_creating_guest = list() template_size = dict() while True: if Utils.exit_flag: msg = 'Thread guest_creating_progress_report_engine say bye-bye' print msg logger.info(msg=msg) return # noinspection PyBroadException try: try: payload = q_creating_guest.get(timeout=config['engine_cycle_interval']) list_creating_guest.append(payload) q_creating_guest.task_done() except Queue.Empty as e: pass threads_status['guest_creating_progress_report_engine'] = dict() threads_status['guest_creating_progress_report_engine']['timestamp'] = ji.Common.ts() # 当有 Guest 被创建时,略微等待一下,避免复制模板的动作还没开始,就开始计算进度。这样会产生找不到镜像路径的异常。 time.sleep(1) for i, guest in enumerate(list_creating_guest): template_path = guest['template_path'] progress = 0 if guest['storage_mode'] in [StorageMode.ceph.value, StorageMode.glusterfs.value]: if guest['storage_mode'] == StorageMode.glusterfs.value: if template_path not in template_size: Guest.dfs_volume = guest['dfs_volume'] Guest.init_gfapi() template_size[template_path] = float(Guest.gf.getsize(template_path)) system_image_size = Guest.gf.getsize(guest['system_image_path']) progress = system_image_size / template_size[template_path] elif guest['storage_mode'] in [StorageMode.local.value, StorageMode.shared_mount.value]: if template_path not in template_size: template_size[template_path] = float(os.path.getsize(template_path)) system_image_size = os.path.getsize(guest['system_image_path']) progress = system_image_size / template_size[template_path] else: del list_creating_guest[i] log = u' '.join([u'UUID: ', guest['uuid'], u'未支持的存储模式: ', str(guest['storage_mode'])]) logger.error(log) log_emit.error(log) guest_event_emit.creating(uuid=guest['uuid'], progress=int(progress * 90)) if progress >= 1: del list_creating_guest[i] except: logger.error(traceback.format_exc()) log_emit.error(traceback.format_exc())
def create(conn, msg): try: guest = Guest(uuid=msg['uuid'], name=msg['name'], template_path=msg['template_path'], disk=msg['disks'][0], xml=msg['xml'], storage_mode=msg['storage_mode'], dfs_volume=msg['dfs_volume']) q_creating_guest.put({ 'storage_mode': guest.storage.storage_mode, 'dfs_volume': guest.storage.dfs_volume, 'uuid': guest.uuid, 'template_path': guest.template_path, 'system_image_path': guest.system_image_path }) guest.generate_system_image() dom = guest.define_by_xml(conn=conn) assert isinstance(dom, libvirt.virDomain) log = u' '.join( [u'域', guest.name, u', UUID', guest.uuid, u'定义成功.']) log_emit.info(msg=log) guest_event_emit.creating(uuid=guest.uuid, progress=92) disk_info = guest.storage.image_info(path=guest.system_image_path) # 由该线程最顶层的异常捕获机制,处理其抛出的异常 guest.execute_os_template_initialize_operates( dom=conn.lookupByUUIDString(uuidstr=guest.uuid), os_template_initialize_operates=msg[ 'os_template_initialize_operates'], os_type=msg['os_type']) extend_data = dict() extend_data.update({'disk_info': disk_info}) guest_event_emit.creating(uuid=guest.uuid, progress=97) dom.create() log = u' '.join( [u'域', guest.name, u', UUID', guest.uuid, u'启动成功.']) log_emit.info(msg=log) Guest.quota(dom=dom, msg=msg) response_emit.success( _object=msg['_object'], action=msg['action'], uuid=msg['uuid'], data=extend_data, passback_parameters=msg.get('passback_parameters')) except: log_emit.error(traceback.format_exc()) response_emit.failure( _object=msg['_object'], action=msg.get('action'), uuid=msg.get('uuid'), passback_parameters=msg.get('passback_parameters'))
def create(cls, conn, msg): try: Guest.storage_mode = msg['storage_mode'] guest = Guest(uuid=msg['uuid'], name=msg['name'], template_path=msg['template_path'], disk=msg['disks'][0], xml=msg['xml']) if Guest.storage_mode == StorageMode.glusterfs.value: Guest.dfs_volume = msg['dfs_volume'] Guest.init_gfapi() guest.system_image_path = guest.disk['path'] q_creating_guest.put({ 'storage_mode': Guest.storage_mode, 'dfs_volume': Guest.dfs_volume, 'uuid': guest.uuid, 'template_path': guest.template_path, 'system_image_path': guest.system_image_path }) if not guest.generate_system_image(): raise RuntimeError('System image generate failure.') if not guest.define_by_xml(conn=conn): raise RuntimeError( 'Define the instance of virtual machine by xml failure.') guest_event_emit.creating(uuid=guest.uuid, progress=92) disk_info = dict() if Guest.storage_mode == StorageMode.glusterfs.value: disk_info = Disk.disk_info_by_glusterfs( dfs_volume=guest.dfs_volume, image_path=guest.system_image_path) elif Guest.storage_mode in [ StorageMode.local.value, StorageMode.shared_mount.value ]: disk_info = Disk.disk_info_by_local( image_path=guest.system_image_path) # 由该线程最顶层的异常捕获机制,处理其抛出的异常 guest.execute_os_template_initialize_operates( guest=conn.lookupByUUIDString(uuidstr=guest.uuid), os_template_initialize_operates=msg[ 'os_template_initialize_operates'], os_type=msg['os_type']) extend_data = dict() extend_data.update({'disk_info': disk_info}) guest_event_emit.creating(uuid=guest.uuid, progress=97) if not guest.start_by_uuid(conn=conn): raise RuntimeError( 'Start the instance of virtual machine by uuid failure.') cls.quota(guest=conn.lookupByUUIDString(uuidstr=guest.uuid), msg=msg) response_emit.success( _object=msg['_object'], action=msg['action'], uuid=msg['uuid'], data=extend_data, passback_parameters=msg.get('passback_parameters')) except: logger.error(traceback.format_exc()) log_emit.error(traceback.format_exc()) response_emit.failure( _object=msg['_object'], action=msg.get('action'), uuid=msg.get('uuid'), passback_parameters=msg.get('passback_parameters'))