コード例 #1
0
def cleanup_cluster(context):
    """Delete cluster after vcenter sample run"""
    cluster1_name = context.testbed.config['CLUSTER1_NAME']
    names = set([cluster1_name])

    cluster_svc = Cluster(context.stub_config)
    cluster_summaries = cluster_svc.list(Cluster.FilterSpec(names=names))
    print("Found '{}' Clusters matching names {}".format(
        len(cluster_summaries), ", ".join(["'{}'".format(n) for n in names])))

    if len(cluster_summaries) < 1:
        return

    # Delete the cluster using the managed object
    cluster = cluster_summaries[0].cluster
    cluster_mo = vim.ClusterComputeResource(cluster, context.soap_stub)

    print("Deleting Cluster '{}' ({})".format(cluster, cluster1_name))
    task = cluster_mo.Destroy()
    pyVim.task.WaitForTask(task)
コード例 #2
0
 def get_cluster_by_name(self, datacenter_name, cluster_name):
     """
     Returns the identifier of a cluster
     with the mentioned names.
     """
     datacenter = self.get_datacenter_by_name(datacenter_name)
     if not datacenter:
         return None
     names = set([cluster_name]) if cluster_name else None
     filter_spec = Cluster.FilterSpec(datacenters=set([datacenter]),
                                      names=names)
     cluster_summaries = self.api_client.vcenter.Cluster.list(filter_spec)
     cluster = cluster_summaries[0].cluster if len(cluster_summaries) > 0 else None
     return cluster
コード例 #3
0
def get_cluster(stub_config, datacenter_name, cluster_name):
    """
    Returns the identifier of a cluster
    Note: The method assumes only one cluster and datacenter
    with the mentioned name.
    """

    datacenter = datacenter_helper.get_datacenter(stub_config, datacenter_name)
    if not datacenter:
        print("Datacenter '{}' not found".format(datacenter_name))
        return None

    filter_spec = Cluster.FilterSpec(names=set([cluster_name]),
                                     datacenters=set([datacenter]))

    cluster_svc = Cluster(stub_config)
    cluster_summaries = cluster_svc.list(filter_spec)
    if len(cluster_summaries) > 0:
        cluster = cluster_summaries[0].cluster
        print("Detected cluster '{}' as {}".format(cluster_name, cluster))
        return cluster
    else:
        print("Cluster '{}' not found".format(cluster_name))
        return None
コード例 #4
0
def get_placement_spec_for_cluster(context):
    """
    Cluster names are not guaranteed to be unique within a vCenter instance,
    so we qualify our search using the Datacenter.  If Folders are used, the
    search must be qualified using Folders since a Cluster name is not
    guaranteed to be unique across different Folders within a Datacenter.
    """
    # Select a Cluster meeting our requirements
    datacenter_name = context.testbed.config['DATACENTER2_NAME']
    datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name]
    cluster_name = context.testbed.config['CLUSTER1_NAME']

    filter = Cluster.FilterSpec(names=set([cluster_name]),
                                datacenters=set([datacenter]))
    cluster_summaries = context.client.vcenter.Cluster.list(filter=filter)

    if len(cluster_summaries) > 0:
        cluster = cluster_summaries[0].cluster
        print("Selecting Cluster '{}' ({})".format(cluster_name, cluster))
    else:
        print("Cluster '{}' not found".format(cluster_name))
        return None

    # Select a Folder meeting our requirements.
    #
    # Must be in the same Datacenter as the Cluster that was chosen.
    datacenter_name = context.testbed.config['DATACENTER2_NAME']
    datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name]

    folder_name = context.testbed.config['VM_FOLDER2_NAME']

    filter = Folder.FilterSpec(names=set([folder_name]),
                               datacenters=set([datacenter]))
    folder_summaries = context.client.vcenter.Folder.list(filter=filter)

    if len(folder_summaries) > 0:
        folder = folder_summaries[0].folder
        print("Selecting Folder '{}' ({})".format(folder_name, folder))
    else:
        print("Folder '{}' not found".format(folder_name))
        return None

    # Select a Datastore meeting our requirements.
    #
    # Must be in the same Datacenter as the Cluster that was chosen.
    # TODO No way to validate that Cluster is connected to Datastore
    datacenter_name = context.testbed.config['DATACENTER2_NAME']
    datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name]

    # TODO Parameterize based on NFS or VMFS
    datastore_name = context.testbed.config['NFS_DATASTORE_NAME']

    filter = Datastore.FilterSpec(names=set([datastore_name]),
                                  datacenters=set([datacenter]))
    datastore_summaries = context.client.vcenter.Datastore.list(filter=filter)

    if len(datastore_summaries) > 0:
        datastore = datastore_summaries[0].datastore
        print("Selecting Datastore '{}' ({})".format(datastore_name,
                                                     datastore))
    else:
        print("Datastore '{}' not found".format(datastore_name))
        return None

    placement_spec = VM.PlacementSpec(folder=folder,
                                      cluster=cluster,
                                      datastore=datastore)
    print("get_placement_spec_for_cluster: Result is '{}'".format(
        placement_spec))
    return placement_spec
コード例 #5
0
    def _get_vm_filter_spec(self):
        vm_filter_spec = VM.FilterSpec()
        datacenters = self.get_option('datacenters')
        if datacenters:
            temp_dcs = []
            for datacenter_name in datacenters:
                dc_filter_spec = Datacenter.FilterSpec(
                    names=set([datacenter_name]))
                datacenter_summaries = self.pyv.rest_content.vcenter.Datacenter.list(
                    dc_filter_spec)
                if len(datacenter_summaries) > 0:
                    temp_dcs.append(datacenter_summaries[0].datacenter)
                else:
                    self._handle_error(message="Unable to find datacenter %s" %
                                       datacenter_name)
            vm_filter_spec.datacenters = set(temp_dcs)

        clusters = self.get_option('clusters')
        if clusters:
            temp_clusters = []
            for cluster_name in clusters:
                ccr_filter_spec = Cluster.FilterSpec(names=set([cluster_name]))
                cluster_summaries = self.pyv.rest_content.vcenter.Cluster.list(
                    ccr_filter_spec)
                if len(cluster_summaries) > 0:
                    temp_clusters.append(cluster_summaries[0].cluster)
                else:
                    self._handle_error(message="Unable to find cluster %s" %
                                       cluster_name)
            vm_filter_spec.clusters = set(temp_clusters)

        folders = self.get_option('folders')
        if folders:
            temp_folders = []
            for folder_name in folders:
                folder_filter_spec = Folder.FilterSpec(
                    names=set([folder_name]))
                folder_summaries = self.pyv.rest_content.vcenter.Folder.list(
                    folder_filter_spec)
                if len(folder_summaries) > 0:
                    temp_folders.append(folder_summaries[0].folder)
                else:
                    self._handle_error(message="Unable to find folder %s" %
                                       folder_name)
            vm_filter_spec.folders = set(temp_folders)

        esxi_hosts = self.get_option('esxi_hostsystems')
        if esxi_hosts:
            temp_hosts = []
            for esxi_name in esxi_hosts:
                esxi_filter_spec = Host.FilterSpec(names=set([esxi_name]))
                esxi_summaries = self.pyv.rest_content.vcenter.Host.list(
                    esxi_filter_spec)
                if len(esxi_summaries) > 0:
                    temp_hosts.append(esxi_summaries[0].host)
                else:
                    self._handle_error(
                        message="Unable to find esxi hostsystem %s" %
                        esxi_name)
            vm_filter_spec.folders = set(temp_hosts)

        resource_pools = self.get_option('resource_pools')
        if resource_pools:
            temp_rps = []
            for rp_name in resource_pools:
                rp_filter_spec = ResourcePool.FilterSpec(names=set([rp_name]))
                rp_summaries = self.pyv.rest_content.vcenter.ResourcePool.list(
                    rp_filter_spec)
                if len(rp_summaries) > 0:
                    temp_rps.append(rp_summaries[0].resourcepool)
                else:
                    self._handle_error(
                        message="Unable to find resource pool %s" % rp_name)
            vm_filter_spec.folders = set(temp_rps)

        return vm_filter_spec