def delete_platform_instances(self, ptype, hostname): """删除异构平台上的虚拟机在本地的记录 :param ptype: 异构平台的类型 :param vm_info: 虚拟机的信息 """ db_util = NovaDatabaseUtils() plaform_db = "" del_sql = "" if ptype == "cserver": plaform_db = PlatformDatabase(CServerDBConfig) del_sql = db.common.DELETE_CSERVER_UUID_MAP_TABLE_SQL elif ptype == "vcenter": plaform_db = PlatformDatabase(VCenterDBConfig) del_sql = db.common.DELETE_VCENTER_UUID_MAP_TABLE_SQL if not plaform_db: log.logger.info("the platform type is error: %s" % ptype) return local_vm_list = db_util.get_vm_info_list(hostname) for vm in local_vm_list: log.logger.info("Delete local vm: %s, uuid: %s" % (vm["name"], vm["id"])) self._delete_local_instance(ptype, vm) os.system("/usr/local/bin/deletevm.sh %s" % vm["id"]) openstack_uuid = vm["id"] sql = del_sql % openstack_uuid plaform_db.delete(sql) plaform_db.close()
def _get_db_conncet(self): """ 获取数据库的链接 """ if not self._dbconncet: self._dbconncet = PlatformDatabase(PlatformDBConfig) return self._dbconncet
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()
def _delete_instance_db(self, ptype, vm_id): """根据虚拟机的id从数据库中进行删除 :param ptype: 异构平台的类型 :param vm_id: 虚拟机的ID """ os.system("/usr/local/bin/deletevm.sh %s" % vm_id) openstack_uuid = vm_id if ptype == "cserver": cserver_db = PlatformDatabase(CServerDBConfig) sql = db.common.DELETE_CSERVER_UUID_MAP_TABLE_SQL % openstack_uuid cserver_db.delete(sql) cserver_db.close() elif ptype == "vcenter": vcenter_db = PlatformDatabase(VCenterDBConfig) sql = db.common.DELETE_VCENTER_UUID_MAP_TABLE_SQL % openstack_uuid vcenter_db.delete(sql) vcenter_db.close()
def _add_vm(self, ptype, vm_info, zone, network_id, image_id, hostname): ''' :param ptype: 异构平台的类型 :param platfrom_vms_list: 异构平台的虚拟机列表 :param hostname: 接管异构平台的主机名 :param zone: 接管异构平台的所属域 :param network_id: 异构平台所属的网络ID ''' flavor_id = self.get_vm_flavor_id(vm_info) #如果异构平台的类型为cserver则将虚拟机在cserver端和在openstack端的uuid影射关系写入数据库 if ptype == "cserver": cserver_flavor_id = self.get_cserver_flavor_id() cserver_db = PlatformDatabase(CServerDBConfig) openstack_uuid = cserver_uuid = vm_info["id"] vm_ip = vm_info["ip"] sql = db.common.INSERT_CSERVER_UUID_MAP_TABLE_SQL % (openstack_uuid, cserver_uuid, vm_ip, flavor_id) cserver_db.insert(sql) cserver_db.close() self._create_new_vm(ptype, vm_info, image_id, cserver_flavor_id, zone, network_id) elif ptype == "vcenter": vcenter_flavor_id = self.get_vcenter_flavor_id() vcenter_db = PlatformDatabase(VCenterDBConfig) openstack_uuid = vcenter_uuid = vm_info["id"] vm_ip = "" sql = db.common.INSERT_VCENTER_UUID_MAP_TABLE_SQL % (openstack_uuid, vcenter_uuid, vm_ip, flavor_id) vcenter_db.insert(sql) vcenter_db.close() self._create_new_vm(ptype, vm_info, image_id, vcenter_flavor_id, zone, network_id) else: #在本地创建新的虚拟机 self._create_new_vm(ptype, vm_info, image_id, flavor_id, zone, network_id) for i in range(60): time.sleep(1) db_util = NovaDatabaseUtils() vm_state = db_util.query_vm_state(vm_info["id"]) #如果虚拟机的状态为error,则同步数据库中的虚拟机的主机名 if vm_state and vm_state[0] == 'error': log.logger.info("create new vm failed, vm state is 'error'") self._sync_vm_hostname(vm_info, hostname) return #构建完成后,同步虚拟机的状态和去除元数据 if vm_state and vm_state[0] != "building": self._sync_vm_state(ptype, vm_info) self._reset_vm_metadata(vm_info["id"]) return