示例#1
0
    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
示例#2
0
    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)
示例#3
0
    def get_resource_pool_spec(self, config):
        spec = vim.ResourceConfigSpec()

        if config is None:
            return spec

        spec.cpuAllocation = self._resource_allocation(config.get("cpuAllocation"))
        spec.memoryAllocation = self._resource_allocation(config.get("memoryAllocation"))
        spec.changeVersion = config.get("changeVersion")

        return spec
示例#4
0
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
示例#5
0
 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)
示例#6
0
    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)
示例#7
0
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
示例#8
0
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
示例#9
0
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