def discovery(self, compute_resource): """An example that discovers hosts and VMs in the inventory.""" # Find the first ClusterComputeResource if compute_resource is None: cr_list = ComputeResource.all(self.client) print("ERROR: You must specify a ComputeResource.") print("Available ComputeResource's:") for cr in cr_list: print(cr.name) sys.exit(1) try: ccr = ComputeResource.get(self.client, name=compute_resource) except ObjectNotFoundError: print("ERROR: Could not find ComputeResource with name %s" % compute_resource) sys.exit(1) print('Cluster: %s (%s hosts)' % (ccr.name, len(ccr.host))) ccr.preload("host", properties=["name", "vm"]) for host in ccr.host: print(' Host: %s (%s VMs)' % (host.name, len(host.vm))) # Get the vm views in one fell swoop host.preload("vm", properties=["name"]) for vm in host.vm: print(' VM: %s' % vm.name)
def run(self): """An example that discovers hosts and VMs in the inventory.""" # Find the first ClusterComputeResource ccr = None options = self.get_options() if options.compute_resource is None: logging.info('No compute resource specified, defaulting to first') cr_list = ComputeResource.all(self.client) options.compute_resource = cr_list[0] logging.info('Attempt to find compute resource %s', options.compute_resource.name) ccr = ComputeResource.get(self.client, name=options.compute_resource.name) print('Cluster: %s (%s hosts)' % (ccr.name, len(ccr.host))) ccr.preload("host", properties=["name", "vm"]) print 'name\tcpu\tmemory\tguestId\tpowerState\tguestState' for host in ccr.host: logging.debug('preloading views for %s', host) # Get the vm views in one fell swoop host.preload("vm", properties=['name', 'config', 'runtime', 'guest']) for vm in host.vm: print '%s\t%d\t%d\t%s\t%s\t%s' % (vm.name, vm.config.hardware.numCPU, vm.config.hardware.memoryMB, vm.config.guestId, vm.runtime.powerState, vm.guest.guestState)
def identify_resource_pool(self): if self.hostname is not None: logging.info('finding resource pool by host name') self.target = self.client.find_entity_view('HostSystem', filter={'name': self.hostname}) self.resource_pool = target.parent.resourcePool self.datacenter = self.target.parent.parent.parent elif self.crname is not None: logging.info('finding resource pool by compute resource') self.target = self.client.find_entity_view('ComputeResource', filter={'name': self.crname}) self.resource_pool = self.target.resourcePool self.datacenter = self.target.parent.parent else: logging.info('using first compute resource for resource pool') crs = ComputeResource.all(self.client) self.target = crs[0] print 'Defaulting to first ComputeResource %s' % (self.target.name) self.resource_pool = self.target.resourcePool self.datacenter = self.target.parent.parent
#!/usr/bin/python """A script which generates DHCP configuration for hosts matching a regex. Usage: find_vms_by_regex.py <regex> <compute_resource> e.g. find_vms_by_regex.py 'ssi2+' 'Online Engineering' """ import re import sys from psphere.client import Client from psphere.managedobjects import ComputeResource client = Client() vm_regex = sys.argv[1] p = re.compile(vm_regex) compute_resource = sys.argv[2] cr = ComputeResource.get(client, name=compute_resource) cr.resourcePool.preload("vm", properties=["name"]) for vm in sorted(cr.resourcePool.vm): if p.match(vm.name) is None: continue print(vm.name) client.logout()