def get_cserver_uuid_maps_info(self): """获取openstack与cserver的虚拟机映射信息""" cserver_uuid_maps = {} cserver_db = PlatformDatabase(CServerDBConfig) sql = "select * from cserver_uuid_maps;" cserver_db.query(sql) vms_info = cserver_db.fetchAllRows() cserver_db.close() for vm in vms_info: item_info = {} item_info["ip"] = vm[3] item_info["flavor_id"] = vm[4] cserver_uuid_maps[vm[1]] = item_info return cserver_uuid_maps
def get_vcenter_uuid_maps_info(self): """获取openstack与vcenter的虚拟机映射信息""" vcenter_uuid_maps = {} vcenter_db = PlatformDatabase(VCenterDBConfig) sql = "select * from vcenter_uuid_maps;" vcenter_db.query(sql) vms_info = vcenter_db.fetchAllRows() vcenter_db.close() for vm in vms_info: item_info = {} item_info["ip"] = vm[3] item_info["flavor_id"] = vm[4] vcenter_uuid_maps[vm[1]] = item_info return vcenter_uuid_maps
def synchronism(self, ptype, platfrom_vms_list, hostname, zone, network_id, platform_name, specify_sync_vms_uuid=[]): ''' 与异构平台上的虚拟机进行同步 :param ptype: 异构平台的类型 :param platfrom_vms_list: 异构平台的虚拟机列表 :param hostname: 接管异构平台的主机名 :param zone: 接管异构平台的所属域 :param network_id: 异构平台所属的网络ID :param specify_sync_vms_uuid: 同步指定的虚拟机id列表 ''' #获取所属主机名是hostname的本的虚拟机列表信息 db_util = NovaDatabaseUtils() host_vms_list = db_util.get_vm_info_list(hostname) #删除列表中状态是'error'的虚拟机 local_vm_list = [] for host_vm in host_vms_list: if host_vm["vm_state"] == "error": self._delete_local_instance(ptype, host_vm) self._delete_instance_db(ptype, host_vm["id"]) else: local_vm_list.append(host_vm) #如果平台类型为cserver,则进行ID的影射转换 if ptype == "cserver": cserver_db = PlatformDatabase(CServerDBConfig) for plat_vm in platfrom_vms_list: sql = db.common.SELECT_CSERVER_UUID_MAP_TABLE_SQL % plat_vm["id"] cserver_db.query(sql) opestack_uuid = cserver_db.fetchOneRow() if opestack_uuid: plat_vm["id"] = opestack_uuid[0] cserver_db.close() local_vm_uuid_list = [vm["id"] for vm in local_vm_list] platform_vm_uuid_list = [vm["id"] for vm in platfrom_vms_list] common_vm_map = {} #得到本地虚拟机列表和平台虚拟机列表中具有共同uuid值的虚拟机列表 for vm_info in platfrom_vms_list: if vm_info["id"] in local_vm_uuid_list: common_vm_map[vm_info["id"]] = vm_info new_instances_list = [] deleted_instances_list = [] #获取平台中新添加的虚拟机列表 for pvm in platfrom_vms_list: if pvm["id"] not in local_vm_uuid_list: new_instances_list.append(pvm) #在本的创建异构平台上新添加的虚拟机 if new_instances_list: log.logger.info("synchronism new vm") print "add new instance................" self.take_over_new_vms(ptype, new_instances_list, hostname, zone, network_id, platform_name) for vm_info in local_vm_list: if vm_info["id"] not in platform_vm_uuid_list: #删除本地虚拟机(在vcenter或cserver那边已经删除的虚拟机) deleted_instances_list.append(vm_info["id"]) self._delete_local_instance(ptype, vm_info) self._delete_instance_db(ptype, vm_info["id"]) else: #同步两边具有相同uuid的虚拟机的状态 if vm_info["id"] in common_vm_map.keys(): if specify_sync_vms_uuid: if vm_info["id"] in specify_sync_vms_uuid: self._sync_vm_state(ptype, common_vm_map[vm_info["id"]]) else: self._sync_vm_state(ptype, common_vm_map[vm_info["id"]]) if ptype == "cserver": cserver_db = PlatformDatabase(CServerDBConfig) openstack_uuid = vm_info["id"] vm_ip = common_vm_map[vm_info["id"]]["ip"] print vm_ip, openstack_uuid sql = db.common.UPDATE_CSERVER_UUID_MAP_TABLE_SQL % (vm_ip, openstack_uuid) cserver_db.update(sql) cserver_db.close()
def check_is_exist_platform(self, platform_name): platformDB = PlatformDatabase(PlatformDBConfig) querysql = "select * from managercenterinfo where managercentername='%s';" % platform_name platformDB.query(querysql) return db_con.fetchAllRows()