def update_vm_local(self): if self.vm.summary.guest != None: ip = self.vm.summary.guest.ipAddress else: ip = '' if self.vm.resourcePool: resource_pool_name = self.vm.resourcePool.name # db.instances.update_vm_rp_name_by_vm_mor_name(platform['id'], get_mor_name(vm), vm.resourcePool.name) else: resource_pool_name = None vm = db.instances.list_by_uuid(uuid=self.uuid, platform_id=self.platform_id) print('vm1') if vm: print('update_vm') db.instances.vcenter_update_vm_by_uuid( uuid=self.uuid, platform_id=self.platform_id, vm_name=self.vm.summary.config.name, vm_mor_name=get_mor_name(self.vm), template=self.vm.summary.config.template, vm_path_name=self.vm.summary.config.vmPathName, memory=self.vm.summary.config.memorySizeMB, cpu=self.vm.summary.config.numCpu, num_ethernet_cards=self.vm.summary.config.numEthernetCards, num_virtual_disks=self.vm.summary.config.numVirtualDisks, instance_uuid=self.vm.summary.config.instanceUuid, guest_id=self.vm.summary.config.guestId, guest_full_name=self.vm.summary.config.guestFullName, host=self.local_vm.host, ip=ip, status=self.vm.summary.runtime.powerState, resource_pool_name=resource_pool_name) print('update_vm2') else: db.instances.vcenter_vm_create( uuid=self.vm.summary.config.uuid, platform_id=self.platform_id, vm_name=self.vm.summary.config.name, vm_mor_name=get_mor_name(self.vm), template=self.vm.summary.config.template, vm_path_name=self.vm.summary.config.vmPathName, memory=self.vm.summary.config.memorySizeMB, cpu=self.vm.summary.config.numCpu, num_ethernet_cards=self.vm.summary.config.numEthernetCards, num_virtual_disks=self.vm.summary.config.numVirtualDisks, instance_uuid=self.vm.summary.config.instanceUuid, guest_id=self.vm.summary.config.guestId, guest_full_name=self.vm.summary.config.guestFullName, host='192.168.12.203', ip=ip, status=self.vm.summary.runtime.powerState, resource_pool_name=resource_pool_name, created_at=self.vm.config.createDate)
def sync_resourcepool(platform, dc, cluster, si, content): print ('sync_rp_start:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time()))) obj = content.viewManager.CreateContainerView(dc, [vim.ResourcePool], True) resourcepools = obj.view for rp in resourcepools: rp_db = db.resource_pool.get_rp_by_mor_name(get_mor_name(rp)) if rp_db: # print('update 1') update_resource_pool(rp_id=rp_db.id, platform_id=platform['id'], dc_name=dc.name, dc_mor_name=get_mor_name(dc), cluster_name=cluster.name, cluster_mor_name=get_mor_name(cluster), name=rp.name, mor_name=get_mor_name(rp), parent_name=rp.parent.name, over_all_status=rp.overallStatus, cpu_expand_able_reservation=rp.summary.config.cpuAllocation.expandableReservation, cpu_reservation=rp.summary.config.cpuAllocation.reservation, cpu_limit=rp.summary.config.cpuAllocation.limit, cpu_shares=rp.summary.config.cpuAllocation.shares.shares, cpu_level=rp.summary.config.cpuAllocation.shares.shares, cpu_over_all_usage=rp.summary.runtime.cpu.overallUsage, cpu_max_usage=rp.summary.runtime.cpu.maxUsage, memory_expand_able_reservation=rp.summary.config.memoryAllocation.expandableReservation, memory_reservation=rp.summary.config.memoryAllocation.reservation, memory_limit=rp.summary.config.memoryAllocation.limit, memory_shares=rp.summary.config.memoryAllocation.shares.shares, memory_level=rp.summary.config.memoryAllocation.shares.level, memory_over_all_usage=rp.summary.runtime.memory.overallUsage, memory_max_usage=rp.summary.runtime.memory.maxUsage) # print('update 2') else: # print('create 1') create_resource_pool(platform_id=platform['id'], dc_name=dc.name, dc_mor_name=get_mor_name(dc), cluster_name=cluster.name, cluster_mor_name=get_mor_name(cluster), name=rp.name, mor_name=get_mor_name(rp), parent_name=rp.parent.name, over_all_status=rp.overallStatus, cpu_expand_able_reservation=rp.summary.config.cpuAllocation.expandableReservation, cpu_reservation=rp.summary.config.cpuAllocation.reservation, cpu_limit=rp.summary.config.cpuAllocation.limit, cpu_shares=rp.summary.config.cpuAllocation.shares.shares, cpu_level=rp.summary.config.cpuAllocation.shares.shares, cpu_over_all_usage=rp.summary.runtime.cpu.overallUsage, cpu_max_usage=rp.summary.runtime.cpu.maxUsage, memory_expand_able_reservation=rp.summary.config.memoryAllocation.expandableReservation, memory_reservation=rp.summary.config.memoryAllocation.reservation, memory_limit=rp.summary.config.memoryAllocation.limit, memory_shares=rp.summary.config.memoryAllocation.shares.shares, memory_level=rp.summary.config.memoryAllocation.shares.level, memory_over_all_usage=rp.summary.runtime.memory.overallUsage, memory_max_usage=rp.summary.runtime.memory.maxUsage) db.instances.clean_all_vm_rp_name_by_rp_name(platform['id'], rp.name) print ('sync_rp_end:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())))
def sync_snap_info(platform_id, snapshotlist, vm, parent_id=None, depth=1): snapshot_id_list = [] current_snapshot = vm.snapshot.currentSnapshot # print(current_snapshot) current_snapshot_mor_name = get_mor_name(current_snapshot) if current_snapshot_mor_name == get_mor_name(snapshotlist.snapshot): current_snapshot_status = True else: current_snapshot_status = False create_time = snapshotlist.createTime.strftime("%Y-%m-%d %H:%M:%S") # 远端存在的 snapshot_id snapshot_id_list.append(snapshotlist.id) # 判断本地是否已存在snapshot_id snapshot = db.snapshots.get_snapshot_by_snapshot_id( vm.summary.config.uuid, snapshotlist.id) if snapshot: db.snapshots.update_snapshot(name=snapshotlist.name, mor_name=get_mor_name( snapshotlist.snapshot), description=snapshotlist.description, state=snapshotlist.state, snapshot_id=snapshotlist.id, snapshot_parent_id=parent_id, create_time=create_time, vm_uuid=vm.summary.config.uuid, current=current_snapshot_status) else: db.snapshots.create_snapshot(name=snapshotlist.name, mor_name=get_mor_name( snapshotlist.snapshot), description=snapshotlist.description, state=snapshotlist.state, snapshot_id=snapshotlist.id, snapshot_parent_id=parent_id, create_time=create_time, vm_uuid=vm.summary.config.uuid, current=current_snapshot_status) maxdepth = 10 if depth < maxdepth: if len(snapshotlist.childSnapshotList) > 0: for snapshot in snapshotlist.childSnapshotList: if hasattr(snapshot, "childSnapshotList"): snapshot_id = sync_snap_info(platform_id, snapshot, vm, snapshotlist.id, depth + 1) snapshot_id_list = snapshot_id_list + snapshot_id return snapshot_id_list
def sync_image(platform, ds): image_list_match = match_files(ds, '*.iso') image_lists = db.images.get_image_name_by_platform_id( platform['id'], ds.name) # print(image_lists) image_list_db = [] for image in image_lists: image_list_db.append(image.image_name) for image in image_list_match: if image['image_name'] in image_list_db: image_list_db.remove(image['image_name']) # print(image_list_db) db.images.update_image(platform_id=platform['id'], image_name=image['image_name'], path=image['path'], ds_name=ds.name, ds_mor_name=get_mor_name(ds), size=image['size'], last_change_time=image['last_change_time']) else: db.images.create_image(platform_id=platform['id'], image_name=image['image_name'], path=image['path'], ds_name=ds.name, ds_mor_name=get_mor_name(ds), size=image['size'], last_change_time=image['last_change_time']) # 删除为未在数据库中的image if image_list_db: for image_name in image_list_db: db.images.delete_image_by_image_name(image_name)
def sync_network_port_group(netwroks, dc_name, dc_mor_name, platform_id): if netwroks: for network in netwroks: # pass # 判断是否存在网络 network_mor_name = get_mor_name(network) # network_info = db_network.network_list_by_mor_name(platform_id, network_mor_name) network_info = db.network_port_group.network_list_by_mor_name( platform_id, network_mor_name) if not network_info: # db_network.network_create(network.name, network_mor_name, dc_name, dc_mor_name, platform_id) db.network_port_group.network_create(network.name, network_mor_name, dc_name, dc_mor_name, platform_id) else: # print(network_info) # db_network.network_update(network_info.get('id'), network.name, network_mor_name, dc_name, dc_mor_name) db.network_port_group.network_update(network_info.id, network.name, network_mor_name, dc_name, dc_mor_name)
def sync_datastore(platform, dc, si, content=None): print('sync_dc_start:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time()))) # content = si.RetrieveContent() obj = content.viewManager.CreateContainerView(dc, [vim.Datastore], True) datastores = obj.view data_store_list = db.datastores.get_datastore_ds_name_by_platform_id( platform['id'], dc.name) # print(data_store_list) for ds in datastores: ds_capacity = ds.summary.capacity ds_freespace = ds.summary.freeSpace ds_uncommitted = ds.summary.uncommitted if ds.summary.uncommitted else 0 ds_provisioned = ds_capacity - ds_freespace + ds_uncommitted ds_overp = ds_provisioned - ds_capacity ds_overp_pct = (ds_overp * 100) / ds_capacity \ if ds_capacity else 0 # print dir(ds) ds_name = ds.name ds_mor_name = get_mor_name(ds) dc_name = dc.name dc_mor_name = get_mor_name(dc) type = ds.info.vmfs.type version = ds.info.vmfs.version uuid = ds.info.vmfs.uuid ssd = ds.info.vmfs.ssd # local = ds.info.vmfs.local if ds.info.vmfs.local: local = True else: local = False # host = platform['ip'] host = ds.host[0].key.name capacity = ds_capacity # 存储容量 free_capacity = ds_freespace # 可用 used_capacity = ds_capacity - ds_freespace if ds_name in data_store_list: data_store_list.remove(ds_name) db.datastores.update_datastore(platform['id'], ds_name, ds_mor_name, dc_name, dc_mor_name, capacity, used_capacity, free_capacity, type, version, uuid, ssd, local, host) else: db.datastores.create_datastore(platform['id'], ds_name, ds_mor_name, dc_name, dc_mor_name, capacity, used_capacity, free_capacity, type, version, uuid, ssd, local, host) sync_image(platform, ds) # print(11) if data_store_list: for ds_name in data_store_list: print(ds_name) db.datastores.delete_datastore_by_ds_name(ds_name) print('sync_dc_stop:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())))
def sync_vcenter_vm(si, content, host, platform): # @celery.task() # def sync_vcenter_vm(host, platform): print('sync_vm_start:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time()))) vms = host.vm # print time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())) # 查询平台内所有的云主机列表 # platform_vm_list = db_vm.vcenter_get_vm_by_platform_id(platform['id'], host.name) platform_vm_list = db.instances.vcenter_get_vm_by_platform_id( platform['id'], host.name) # instance = Instance(platform_id=platform['id'], si=si, content=content) vm_list = [] for vm in platform_vm_list: vm_list.append(vm.uuid) # print(vm_list) for vm in vms: # print(dir(vm.config)) # print(vm.summary.config.name) # print(vm.config.createDate) # return 'ccc' if vm.resourcePool: resource_pool_name = vm.resourcePool.name # db.instances.update_vm_rp_name_by_vm_mor_name(platform['id'], get_mor_name(vm), vm.resourcePool.name) else: resource_pool_name = None # 判断是否已存在云主机 # print time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())) if vm.summary.guest != None: ip = vm.summary.guest.ipAddress else: ip = '' # print(vm_list) if vm.summary.config.uuid in vm_list: vm_list.remove(vm.summary.config.uuid) db.instances.vcenter_update_vm_by_uuid( uuid=vm.summary.config.uuid, platform_id=platform['id'], vm_name=vm.summary.config.name, vm_mor_name=get_mor_name(vm), template=vm.summary.config.template, vm_path_name=vm.summary.config.vmPathName, memory=vm.summary.config.memorySizeMB, cpu=vm.summary.config.numCpu, num_ethernet_cards=vm.summary.config.numEthernetCards, num_virtual_disks=vm.summary.config.numVirtualDisks, instance_uuid=vm.summary.config.instanceUuid, guest_id=vm.summary.config.guestId, guest_full_name=vm.summary.config.guestFullName, host=host.name, ip=ip, status=vm.summary.runtime.powerState, resource_pool_name=resource_pool_name) else: db.instances.vcenter_vm_create( uuid=vm.summary.config.uuid, platform_id=platform['id'], vm_name=vm.summary.config.name, vm_mor_name=get_mor_name(vm), template=vm.summary.config.template, vm_path_name=vm.summary.config.vmPathName, memory=vm.summary.config.memorySizeMB, cpu=vm.summary.config.numCpu, num_ethernet_cards=vm.summary.config.numEthernetCards, num_virtual_disks=vm.summary.config.numVirtualDisks, instance_uuid=vm.summary.config.instanceUuid, guest_id=vm.summary.config.guestId, guest_full_name=vm.summary.config.guestFullName, host=host.name, ip=ip, status=vm.summary.runtime.powerState, resource_pool_name=resource_pool_name, created_at=vm.config.createDate) # 同步 vm network device netowrk_device_manage.sync_network_device(platform_id=platform['id'], vm=vm) disk_manage.sync_disk(platform_id=platform['id'], vm=vm) # 异步处理 同步vm信息 # sync_snapshot.apply_async(args=[platform["id"], vm.summary.config.uuid]) sync_snapshot(platform_id=platform['id'], vm=vm) # print time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())) # 删除不存在的云主机 if vm_list: for uuid in vm_list: # db_vm.vm_delete_by_uuid(platform['id'], host.name, uuid) db.instances.vm_delete_by_uuid(platform['id'], host.name, uuid) # 删除相关的 network disk db.network_devices.device_delete_by_vm_uuid(platform['id'], uuid) db.disks.device_delete_by_vm_uuid(platform['id'], uuid) print('sync_vm_end:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())))
def sync_vcenter_tree(si, content, platform): print('sync_start:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time()))) # 获取当前云平台的 tree_id # vcenter_ids = db_vcenter.vcenter_tree_get_all_id(platform['id']) vcenter_ids = db.vcenter.vcenter_tree_get_all_id(platform['id']) vcenter_list = [] for tree in vcenter_ids: vcenter_list.append(tree.id) # 获取 platfrom tree result = db.vcenter.vcenter_tree_get_by_platform(platform['id'], platform['platform_name'], 1) if result: vcenter_list.remove(result.id) vCenter_pid = result.id db.vcenter.vcenter_tree_update(tree_type=1, platform_id=platform['id'], mor_name=None, name=platform['platform_name']) else: vCenter_pid = db.vcenter.vcenter_tree_create( tree_type=1, platform_id=platform['id'], name=platform['platform_name']) datacenters = content.rootFolder.childEntity for dc in datacenters: # print('pid:', vCenter_pid) dc_mor = get_mor_name(dc) dc_host_moc = get_mor_name(dc.hostFolder) dc_vm_moc = get_mor_name(dc.vmFolder) # 同步vcenter port group netwroks = dc.network sync_network_port_group(netwroks, dc.name, dc_mor, platform['id']) # 同步datastore sync_datastore(platform, dc, si, content) # 异步处理 同步ds信息 # sync_datastore.apply_async(args=[platform, dc, si]) # 获取 dc tree # result = db_vcenter.vcenter_tree_get_by_dc(platform['id'], dc_mor, 2) result = db.vcenter.vcenter_tree_get_by_dc(platform['id'], dc_mor, 2) # print(22) if result: vcenter_list.remove(result.id) dc_pid = result.id db.vcenter.vcenter_tree_update(tree_type=2, platform_id=platform['id'], name=dc.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=dc_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, pid=vCenter_pid) else: dc_pid = db.vcenter.vcenter_tree_create( tree_type=2, platform_id=platform['id'], name=dc.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=dc_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, pid=vCenter_pid) # print(33) # print('dc_pid:', dc_pid) clusters = dc.hostFolder.childEntity # print(clusters.name) for cluster in clusters: # print(44) cluster_mor = get_mor_name(cluster) # 添加/更新 cluster 信息 # 获取 cluster tree result = db.vcenter.vcenter_tree_get_by_cluster( platform['id'], cluster_mor, 3) if result: vcenter_list.remove(result.id) cluster_pid = result.id db.vcenter.vcenter_tree_update( tree_type=3, platform_id=platform['id'], name=cluster.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=cluster_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=dc_pid) else: cluster_pid = db.vcenter.vcenter_tree_create( tree_type=3, platform_id=platform['id'], name=cluster.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=cluster_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=dc_pid) rp_obj = content.viewManager.CreateContainerView( cluster, [vim.ResourcePool], True) rps = rp_obj.view for rp in rps: rp_mor = get_mor_name(rp) rp_info = db.vcenter.vcenter_tree_get_by_mor_name( platform['id'], rp_mor, 5) if rp_info: vcenter_list.remove(rp_info.id) if rp.parent.name == cluster.name: db.vcenter.vcenter_tree_update( tree_type=5, platform_id=platform['id'], name=rp.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=rp_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=cluster_pid) else: parent_rp_info = db.vcenter.vcenter_tree_get_by_mor_name( platform['id'], get_mor_name(rp.parent), 5) db.vcenter.vcenter_tree_update( tree_type=5, platform_id=platform['id'], name=rp.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=rp_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=parent_rp_info.id) else: if rp.parent.name == cluster.name: db.vcenter.vcenter_tree_create( tree_type=5, platform_id=platform['id'], name=rp.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=rp_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=cluster_pid) else: parent_rp_info = db.vcenter.vcenter_tree_get_by_mor_name( platform['id'], get_mor_name(rp.parent), 5) db.vcenter.vcenter_tree_create( tree_type=5, platform_id=platform['id'], name=rp.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=rp_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=parent_rp_info.id) sync_resourcepool(platform, dc, cluster, si, content) # 异步处理 同步rp信息 # sync_resourcepool.apply_async(args=[platform, dc, cluster, si, content]) hosts = cluster.host for host in hosts: host_mor = get_mor_name(host) # 获取 host tree result = db.vcenter.vcenter_tree_get_by_mor_name( platform['id'], host_mor, 4) if result: vcenter_list.remove(result.id) db.vcenter.vcenter_tree_update( tree_type=4, platform_id=platform['id'], name=host.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=host_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=cluster_pid) else: db.vcenter.vcenter_tree_create( tree_type=4, platform_id=platform['id'], name=host.name, dc_mor_name=dc_mor, dc_oc_name=dc.name, mor_name=host_mor, dc_host_folder_mor_name=dc_host_moc, dc_vm_folder_mor_name=dc_vm_moc, cluster_mor_name=cluster_mor, cluster_oc_name=cluster.name, pid=cluster_pid) # 同步vm信息 # 异步处理 同步vm信息 sync_vcenter_vm(si, content, host, platform) # sync_vcenter_vm.apply_async(args=[si, content, host, platform]) # sync_vcenter_vm.apply_async(args=[host, platform]) # 删除未操作的 tree if vcenter_list: for id in vcenter_list: # db_vcenter.vcenter_tree_delete_by_id(id) db.vcenter.vcenter_tree_delete_by_id(id) print('sync_end:', time.strftime('%Y.%m.%d:%H:%M:%S', time.localtime(time.time())))