def generate_rp_config(self): rp_spec = vim.ResourceConfigSpec() cpu_alloc = vim.ResourceAllocationInfo() cpu_alloc.expandableReservation = self.cpu_expandable_reservations cpu_alloc.limit = self.cpu_limit cpu_alloc.reservation = self.cpu_reservation cpu_alloc_shares = vim.SharesInfo() if self.cpu_shares == 'custom': cpu_alloc_shares.shares = self.cpu_allocation_shares cpu_alloc_shares.level = self.cpu_shares cpu_alloc.shares = cpu_alloc_shares rp_spec.cpuAllocation = cpu_alloc mem_alloc = vim.ResourceAllocationInfo() mem_alloc.limit = self.mem_limit mem_alloc.expandableReservation = self.mem_expandable_reservations mem_alloc.reservation = self.mem_reservation mem_alloc_shares = vim.SharesInfo() if self.mem_shares == 'custom': mem_alloc_shares.shares = self.mem_allocation_shares mem_alloc_shares.level = self.mem_shares mem_alloc.shares = mem_alloc_shares rp_spec.memoryAllocation = mem_alloc return rp_spec
def state_add_rp(self): changed = True rp_spec = vim.ResourceConfigSpec() cpu_alloc = vim.ResourceAllocationInfo() cpu_alloc.expandableReservation = self.cpu_expandable_reservations cpu_alloc.limit = int(self.cpu_limit) cpu_alloc.reservation = int(self.cpu_reservation) cpu_alloc_shares = vim.SharesInfo() cpu_alloc_shares.level = self.cpu_shares cpu_alloc.shares = cpu_alloc_shares rp_spec.cpuAllocation = cpu_alloc mem_alloc = vim.ResourceAllocationInfo() mem_alloc.limit = int(self.mem_limit) mem_alloc.expandableReservation = self.mem_expandable_reservations mem_alloc.reservation = int(self.mem_reservation) mem_alloc_shares = vim.SharesInfo() mem_alloc_shares.level = self.mem_shares mem_alloc.shares = mem_alloc_shares rp_spec.memoryAllocation = mem_alloc self.dc_obj = find_datacenter_by_name( self.content, self.datacenter) self.cluster_obj = find_cluster_by_name_datacenter( self.dc_obj, self.cluster) rootResourcePool = self.cluster_obj.resourcePool rootResourcePool.CreateResourcePool(self.resource_pool, rp_spec) self.module.exit_json(changed=changed)
def _resource_allocation(self, config): spec = vim.ResourceAllocationInfo() if config is None: return spec shares_config = { "normal": vim.SharesInfo.Level.normal, "low": vim.SharesInfo.Level.low, "high": vim.SharesInfo.Level.high } spec.expandableReservation = config.get("expandableReservation", False) spec.limit = config.get("limit", -1) spec.reservation = config.get("reservation", 0) spec.shares = vim.SharesInfo() shares = config.get("shares", None) if shares: level = shares.get("level") if level == "custom": spec.shares.level = vim.SharesInfo.Level.custom spec.shares.shares = shares.get("shares") else: spec.shares.level = shares_config.get(level) return spec
def _update_version3_resources(self, resources): allocations = list() for resource in resources: allocation = vim.DistributedVirtualSwitch.HostInfrastructureTrafficResource( ) allocation.allocationInfo = vim.DistributedVirtualSwitch.HostInfrastructureTrafficResource.ResourceAllocation( ) allocation.key = resource['name'] if 'limit' in resource: allocation.allocationInfo.limit = resource['limit'] if 'reservation' in resource: allocation.allocationInfo.reservation = resource['reservation'] if 'shares_level' in resource: allocation.allocationInfo.shares = vim.SharesInfo() allocation.allocationInfo.shares.level = resource[ 'shares_level'] if 'shares' in resource and resource[ 'shares_level'] == 'custom': allocation.allocationInfo.shares.shares = resource[ 'shares'] elif resource['shares_level'] == 'custom': self.module.fail_json( msg= "Resource %s, shares_level set to custom but shares not specified" % resource['name']) allocations.append(allocation) spec = vim.DistributedVirtualSwitch.ConfigSpec() spec.configVersion = self.dvs.config.configVersion spec.infrastructureTrafficResourceConfig = allocations task = self.dvs.ReconfigureDvs_Task(spec) wait_for_task(task)
def _get_allocation_info(limits, allocation_type): allocation = allocation_type() if limits.limit: allocation.limit = limits.limit else: # Set as 'unlimited' allocation.limit = -1 if limits.reservation: allocation.reservation = limits.reservation else: allocation.reservation = 0 shares = vim.SharesInfo() if limits.shares_level: shares.level = limits.shares_level if (shares.level == 'custom' and limits.shares_share): shares.shares = limits.shares_share else: shares.shares = 0 else: shares.level = 'normal' shares.shares = 0 # The VirtualEthernetCardResourceAllocation has 'share' instead of # 'shares'. if hasattr(allocation, 'share'): allocation.share = shares else: allocation.shares = shares return allocation
def gather_disk_facts(vm_obj): """ Gather facts about VM's disks Args: vm_obj: Managed object of virtual machine Returns: A list of dict containing disks information """ disks_facts = dict() if vm_obj is None: return disks_facts disk_index = 0 for disk in vm_obj.config.hardware.device: if isinstance(disk, vim.vm.device.VirtualDisk): if disk.storageIOAllocation is None: disk.storageIOAllocation = vim.StorageResourceManager.IOAllocationInfo( ) disk.storageIOAllocation.shares = vim.SharesInfo() if disk.shares is None: disk.shares = vim.SharesInfo() disks_facts[disk_index] = dict( key=disk.key, label=disk.deviceInfo.label, summary=disk.deviceInfo.summary, backing_filename=disk.backing.fileName, backing_datastore=disk.backing.datastore.name, backing_disk_mode=disk.backing.diskMode, backing_sharing=disk.backing.sharing, backing_writethrough=disk.backing.writeThrough, backing_thinprovisioned=disk.backing.thinProvisioned, backing_eagerlyscrub=bool(disk.backing.eagerlyScrub), backing_uuid=disk.backing.uuid, controller_key=disk.controllerKey, unit_number=disk.unitNumber, iolimit_limit=disk.storageIOAllocation.limit, iolimit_shares_level=disk.storageIOAllocation.shares.level, iolimit_shares_limit=disk.storageIOAllocation.shares. shares, shares_level=disk.shares.level, shares_limit=disk.shares.shares, capacity_in_kb=disk.capacityInKB, capacity_in_bytes=disk.capacityInBytes, ) disk_index += 1 return disks_facts
def get_ioandshares_diskconfig(self, disk_spec, disk): io_disk_spec = vim.StorageResourceManager.IOAllocationInfo() if 'iolimit' in disk: io_disk_spec.limit = disk['iolimit']['limit'] if 'shares' in disk['iolimit']: shares_spec = vim.SharesInfo() shares_spec.level = disk['iolimit']['shares']['level'] if shares_spec.level == 'custom': shares_spec.shares = disk['iolimit']['shares']['level_value'] io_disk_spec.shares = shares_spec disk_spec.device.storageIOAllocation = io_disk_spec if 'shares' in disk: shares_spec = vim.SharesInfo() shares_spec.level = disk['shares']['level'] if shares_spec.level == 'custom': shares_spec.shares = disk['shares']['level_value'] io_disk_spec.shares = shares_spec disk_spec.device.storageIOAllocation = io_disk_spec return disk_spec
def get_ioandshares_diskconfig(self, disk_spec, disk): io_disk_spec = vim.StorageResourceManager.IOAllocationInfo() if "iolimit" in disk: io_disk_spec.limit = disk["iolimit"]["limit"] if "shares" in disk["iolimit"]: shares_spec = vim.SharesInfo() shares_spec.level = disk["iolimit"]["shares"]["level"] if shares_spec.level == "custom": shares_spec.shares = disk["iolimit"]["shares"][ "level_value" ] io_disk_spec.shares = shares_spec disk_spec.device.storageIOAllocation = io_disk_spec if "shares" in disk: shares_spec = vim.SharesInfo() shares_spec.level = disk["shares"]["level"] if shares_spec.level == "custom": shares_spec.shares = disk["shares"]["level_value"] io_disk_spec.shares = shares_spec disk_spec.device.storageIOAllocation = io_disk_spec return disk_spec
def create_rp(self, name, esx_name, parent="/"): if self.check_pool_existence(name, esx_name): raise ExistenceException("Resource pool %s already exists " "on esx %s" % (name, esx_name)) root_pool = self._get_pool_mor(parent, esx_name) cpu_alloc = vim.ResourceAllocationInfo( shares=vim.SharesInfo(level='normal'), limit=-1, expandableReservation=True, reservation=0) memory_alloc = vim.ResourceAllocationInfo( shares=vim.SharesInfo(level='normal'), limit=-1, expandableReservation=True, reservation=0) pool_spec = vim.ResourceConfigSpec(cpuAllocation=cpu_alloc, memoryAllocation=memory_alloc) try: root_pool.CreateResourcePool(name=name, spec=pool_spec) except vim.fault.DuplicateName as e: raise ExistenceException(e.msg)
def make_resourcepool(self, cluster, resourcepool_name): """ Create a new Resource Pool on a cluster. Arguments: :param cluster: the cluster to use (see `get_cluster`) :param resourcepool_name: the name for the new resource pool """ rp_spec = vim.ResourceConfigSpec() rp_spec.cpuAllocation = vim.ResourceAllocationInfo() rp_spec.cpuAllocation.limit = -1 # No limit rp_spec.cpuAllocation.expandableReservation = True rp_spec.cpuAllocation.reservation = 1000 # MHz rp_spec.cpuAllocation.shares = vim.SharesInfo() rp_spec.cpuAllocation.shares.level = vim.SharesInfo.Level.normal rp_spec.memoryAllocation = vim.ResourceAllocationInfo() rp_spec.memoryAllocation.limit = -1 # No limit rp_spec.memoryAllocation.expandableReservation = True rp_spec.memoryAllocation.reservation = 256 # MiB rp_spec.memoryAllocation.shares = vim.SharesInfo() rp_spec.memoryAllocation.shares.level = vim.SharesInfo.Level.normal cluster.resourcePool.CreateResourcePool( name=resourcepool_name, spec=rp_spec)
def ADD_Pool(content, esxi, pool_name, cpu_limit, ram_limit): i = 0 while True: try: hostname = content.rootFolder.childEntity[0].hostFolder.childEntity[i].name if hostname == esxi: if Search_Pool(content, esxi, pool_name) == True: print(bcolors.WARNING + "Pool already exist" + bcolors.ENDC) return host = content.rootFolder.childEntity[0].hostFolder.childEntity[i] configSpec = vim.ResourceConfigSpec() cpuAllocationInfo = vim.ResourceAllocationInfo() memAllocationInfo = vim.ResourceAllocationInfo() sharesInfo = vim.SharesInfo(level='normal') cpuAllocationInfo.reservation = int(cpu_limit / 2) cpuAllocationInfo.expandableReservation = False cpuAllocationInfo.shares = sharesInfo cpuAllocationInfo.limit = cpu_limit memAllocationInfo.reservation = int(ram_limit / 2) memAllocationInfo.expandableReservation = False memAllocationInfo.shares = sharesInfo memAllocationInfo.limit = ram_limit configSpec.cpuAllocation = cpuAllocationInfo configSpec.memoryAllocation = memAllocationInfo try: host.resourcePool.CreateResourcePool(pool_name, configSpec) print (bcolors.OKGREEN + "Pool successful created" + bcolors.ENDC) return except: print (bcolors.FAIL + "Pool NOT created" + bcolors.ENDC) return except: print(bcolors.FAIL + "Host not exist" + bcolors.ENDC) return i = i + 1
def fix_resource_allocation(self, vm_name, cpu_limit=2000, memory_limit=None): vm_mor = self.get_vm_mor(vm_name) memory_limit = memory_limit if memory_limit else \ vm_mor.config.hardware.memoryMB shares = vim.SharesInfo(level="normal") cpu_allocation = vim.ResourceAllocationInfo(limit=cpu_limit, shares=shares) memory_alloc = vim.ResourceAllocationInfo(limit=memory_limit, shares=shares) vm_mor.ReconfigVM_Task( vim.vm.ConfigSpec(cpuAllocation=cpu_allocation, memoryAllocation=memory_alloc)).wait()
def resourcepool_update(service_instance, parent, name, cpuexpandableReservation, cpulimit, cpureservation, cpushares, cpulevel, memoryexpandableReservation, memorylimit, memoryreservation, memoryshares, memorylevel): cpuAllocation = vim.ResourceAllocationInfo() cpuAllocation.expandableReservation = cpuexpandableReservation cpuAllocation.limit = cpulimit cpuAllocation.reservation = int(cpureservation) cpuShareInfo = vim.SharesInfo() cpuShareInfo.shares = int(cpushares) cpuSharesLevel = vim.SharesLevel(cpulevel) cpuShareInfo.level = cpuSharesLevel cpuAllocation.shares = cpuShareInfo memoryAllocation = vim.ResourceAllocationInfo() memoryAllocation.expandableReservation = memoryexpandableReservation memoryAllocation.limit = memorylimit memoryAllocation.reservation = int(memoryreservation) memoryShareInfo = vim.SharesInfo() memoryShareInfo.shares = int(memoryshares) memorySharesLevel = vim.SharesLevel(memorylevel) memoryShareInfo.level = memorySharesLevel memoryAllocation.shares = memoryShareInfo rpspec = vim.ResourceConfigSpec() rpspec.cpuAllocation = cpuAllocation rpspec.memoryAllocation = memoryAllocation content = service_instance.RetrieveContent() for respool in get_vim_objects(content, vim.ResourcePool): if respool.name == name: print "Found resourcepool " + name newresp = respool.UpdateConfig(name, rpspec) print "Updated resourcepool " + name
def cpu_shares(self, shares): """ Configure CPU shares for a VM Args: shares (int): CPU shares to be configured Returns: Task """ assert shares >= 0 config_spec = vim.vm.ConfigSpec() shares_alloc = vim.ResourceAllocationInfo() shares_alloc.shares = vim.SharesInfo(level="custom", shares=shares) config_spec.cpuAllocation = shares_alloc return self.vm_obj.ReconfigVM_Task(config_spec)
def _update_version2_resources(self, resources): allocations = list() for resource in resources: resource_cfg = self.find_netioc_by_key(resource['name']) allocation = vim.DVSNetworkResourcePoolConfigSpec() allocation.allocationInfo = vim.DVSNetworkResourcePoolAllocationInfo() allocation.key = resource['name'] allocation.configVersion = resource_cfg.configVersion if 'limit' in resource: allocation.allocationInfo.limit = resource['limit'] if 'shares_level' in resource: allocation.allocationInfo.shares = vim.SharesInfo() allocation.allocationInfo.shares.level = resource['shares_level'] if 'shares' in resource and resource['shares_level'] == 'custom': allocation.allocationInfo.shares.shares = resource['shares'] allocations.append(allocation) self.dvs.UpdateNetworkResourcePool(allocations)
def _update_version3_resources(self, resources): allocations = list() for resource in resources: allocation = (vim.DistributedVirtualSwitch. HostInfrastructureTrafficResource()) allocation.allocationInfo = ( vim.DistributedVirtualSwitch.HostInfrastructureTrafficResource. ResourceAllocation()) allocation.key = resource["name"] if "limit" in resource: allocation.allocationInfo.limit = resource["limit"] if "reservation" in resource: allocation.allocationInfo.reservation = resource["reservation"] if "shares_level" in resource: allocation.allocationInfo.shares = vim.SharesInfo() allocation.allocationInfo.shares.level = resource[ "shares_level"] if ("shares" in resource and resource["shares_level"] == "custom"): allocation.allocationInfo.shares.shares = resource[ "shares"] elif resource["shares_level"] == "custom": self.module.fail_json( msg= "Resource %s, shares_level set to custom but shares not specified" % resource["name"]) allocations.append(allocation) spec = vim.DistributedVirtualSwitch.ConfigSpec() spec.configVersion = self.dvs.config.configVersion spec.infrastructureTrafficResourceConfig = allocations task = self.dvs.ReconfigureDvs_Task(spec) wait_for_task(task)
def _update_version2_resources(self, resources): allocations = list() for resource in resources: resource_cfg = self.find_netioc_by_key(resource["name"]) allocation = vim.DVSNetworkResourcePoolConfigSpec() allocation.allocationInfo = ( vim.DVSNetworkResourcePoolAllocationInfo()) allocation.key = resource["name"] allocation.configVersion = resource_cfg.configVersion if "limit" in resource: allocation.allocationInfo.limit = resource["limit"] if "shares_level" in resource: allocation.allocationInfo.shares = vim.SharesInfo() allocation.allocationInfo.shares.level = resource[ "shares_level"] if ("shares" in resource and resource["shares_level"] == "custom"): allocation.allocationInfo.shares.shares = resource[ "shares"] allocations.append(allocation) self.dvs.UpdateNetworkResourcePool(allocations)
def create(ctx, server_client, name, use_external_resource): esxi_host_ip = ctx.node.properties['connection_config'].get('esxi_ip') esxi_host_username = ctx.node.properties['connection_config'].get( 'esxi_username') esxi_host_password = ctx.node.properties['connection_config'].get( 'esxi_password') vcenter_ip = ctx.node.properties['connection_config'].get('host') vcenter_username = ctx.node.properties['connection_config'].get('username') vcenter_password = ctx.node.properties['connection_config'].get('password') vmware_client = VMWareClient(vcenter_ip, vcenter_username, vcenter_password) ctx.logger.debug('Connect to vcenter (%s) successfully !' % str(vcenter_ip)) resource_pool_name = ctx.node.properties['connection_config'].get( 'resource_pool_name') datacenter_name = ctx.node.properties['connection_config'].get( 'datacenter_name') # cluster_name = ctx.node.properties['connection_config'].get('cluster_name') # ctx.logger.debug('++++++++++++++++++++++++++datacenter_name:%s'%str(datacenter_name)) # dc = server_client._get_obj_by_name(vim.Datacenter, datacenter_name).hostFolder.AddStandaloneHost(spec=host_connect_spec,addConnected=True) # ctx.logger.debug('++++++++++++++++++++++++++dc:%s'%str(dc)) # if not dc: # vmware_client.create_datacenter(datacenter_name) # ctx.logger.debug('datacenter:%s is created'%str(datacenter_name)) # # eh = server_client._get_obj_by_name(vim.HostSystem,esxi_host_ip) ctx.logger.debug('esxi_host_ip 55 = %s' % str(esxi_host_ip)) existing_id = server_client._get_obj_by_name( vim.Datacenter, datacenter_name, ) si = SmartConnectNoSSL(host=vcenter_ip, user=vcenter_username, pwd=vcenter_password, port=443) if existing_id is not None: existing_id = existing_id.id else: folder = si.RetrieveContent().rootFolder # ctx.logger.info('folder78=%s'%str(folder)) host_connect_spec = vim.host.ConnectSpec() host_connect_spec.hostName = esxi_host_ip host_connect_spec.userName = esxi_host_username host_connect_spec.password = esxi_host_password host_connect_spec.force = True host_connect_spec.sslThumbprint = get_ssl_thumbprint(esxi_host_ip) folder.CreateDatacenter( name=datacenter_name).hostFolder.AddStandaloneHost( spec=host_connect_spec, addConnected=True) #ctx.logger.debug('new_host.hostFolder 90 = %s' % str(new_host.hostFolder)) ctx.logger.debug('Add host to vcenter successfully') existing_id = server_client._get_obj_by_name(vim.Datacenter, datacenter_name, use_cache=False) runtime_properties = ctx.instance.runtime_properties runtime_properties['vsphere_datacenter_id'] = existing_id existing_id = server_client._get_obj_by_name( vim.ResourcePool, resource_pool_name, ) ctx.logger.info('existing_id 103= %s' % str(existing_id)) if existing_id is not None: existing_id = existing_id.id else: dc = si.content.rootFolder.childEntity for d in dc: for i in d.hostFolder.childEntity: ctx.logger.info('dc.hostFolder name = %s' % str(i.name)) #在指定esxi创建资源池 if i.name == esxi_host_ip: cr = d.hostFolder.childEntity[0] rootResourcePool = cr.resourcePool configSpec = vim.ResourceConfigSpec() cpuAllocationInfo = vim.ResourceAllocationInfo() memAllocationInfo = vim.ResourceAllocationInfo() sharesInfo = vim.SharesInfo(level='normal') cpuAllocationInfo.reservation = 0 cpuAllocationInfo.expandableReservation = True cpuAllocationInfo.shares = sharesInfo cpuAllocationInfo.limit = -1 memAllocationInfo.reservation = 0 memAllocationInfo.expandableReservation = True memAllocationInfo.shares = sharesInfo memAllocationInfo.limit = -1 configSpec.cpuAllocation = cpuAllocationInfo configSpec.memoryAllocation = memAllocationInfo rootResourcePool.CreateResourcePool( resource_pool_name, configSpec) while True: existing_p_id = server_client._get_obj_by_name( vim.ResourcePool, resource_pool_name, use_cache=False) if existing_p_id: ctx.logger.debug( "Resource_pool created successful!") existing_id = existing_p_id.id break runtime_properties = ctx.instance.runtime_properties runtime_properties['vsphere_resource_pool_id'] = existing_id
def resourcepool_create(service_instance, parent, name, cpuexpandableReservation, cpulimit, cpureservation, cpushares, cpulevel, memoryexpandableReservation, memorylimit, memoryreservation, memoryshares, memorylevel): #cpuAllocation = vim.ResourceAllocationInfo() #cpuAllocation.expandableReservation = False #cpuAllocation.limit = -1 #cpuAllocation.reservation = 1000 #cpuShareInfo = vim.SharesInfo() #cpuShareInfo.shares = 1000 #cpuSharesLevel = vim.SharesLevel('normal'); #cpuShareInfo.level = cpuSharesLevel #cpuAllocation.shares = cpuShareInfo #print cpuAllocation #memoryAllocation = vim.ResourceAllocationInfo() #memoryAllocation.expandableReservation = False #memoryAllocation.limit = -1 #memoryAllocation.reservation = 1000 #memoryShareInfo = vim.SharesInfo() #memoryShareInfo.shares = 1000 #memorySharesLevel = vim.SharesLevel('normal'); #memoryShareInfo.level = memorySharesLevel #memoryAllocation.shares = memoryShareInfo #print memoryAllocation cpuAllocation = vim.ResourceAllocationInfo() cpuAllocation.expandableReservation = cpuexpandableReservation cpuAllocation.limit = cpulimit cpuAllocation.reservation = int(cpureservation) cpuShareInfo = vim.SharesInfo() cpuShareInfo.shares = int(cpushares) cpuSharesLevel = vim.SharesLevel(cpulevel) cpuShareInfo.level = cpuSharesLevel cpuAllocation.shares = cpuShareInfo #print cpuAllocation memoryAllocation = vim.ResourceAllocationInfo() memoryAllocation.expandableReservation = memoryexpandableReservation memoryAllocation.limit = memorylimit memoryAllocation.reservation = int(memoryreservation) memoryShareInfo = vim.SharesInfo() memoryShareInfo.shares = int(memoryshares) memorySharesLevel = vim.SharesLevel(memorylevel) memoryShareInfo.level = memorySharesLevel memoryAllocation.shares = memoryShareInfo #print memoryAllocation rpspec = vim.ResourceConfigSpec() rpspec.cpuAllocation = cpuAllocation rpspec.memoryAllocation = memoryAllocation #print rpspec content = service_instance.RetrieveContent() for respool in get_vim_objects(content, vim.ResourcePool): if respool.name == parent: print "Found parent resourcepool" newresp = respool.CreateResourcePool(name, rpspec) print "Created resourcepool " + newresp.name