diff --git a/.travis.yml b/.travis.yml index e615dea..7c3e1ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,6 @@ env: PYTHONPATH=$PWD:$PYTHONPATH PATH=$PWD/bin:$PATH # command to install dependencies install: "pip install -r requirements.txt" script: "ezmomi --help" +script: "python -m py_compile ezmomi/cli.py" +script: "python -m py_compile ezmomi/params.py" +script: "python -m py_compile ezmomi/ezmomi.py" diff --git a/ezmomi/cli.py b/ezmomi/cli.py index 1791a25..43e124e 100644 --- a/ezmomi/cli.py +++ b/ezmomi/cli.py @@ -5,7 +5,6 @@ from params import add_params from ezmomi import EZMomi - def cli(): # Set up command line arguments parser = argparse.ArgumentParser( diff --git a/ezmomi/ezmomi.py b/ezmomi/ezmomi.py index ff94c10..6d6cf99 100644 --- a/ezmomi/ezmomi.py +++ b/ezmomi/ezmomi.py @@ -182,9 +182,27 @@ def clone(self): cluster = self.get_obj([vim.ClusterComputeResource], ip_settings[0]['cluster'] ) - # use same root resource pool that my desired cluster uses - resource_pool = cluster.resourcePool + + resource_pool_str = self.config['resource_pool'] + + # resource_pool setting in config file takes priority over the + # default 'Resources' pool + if resource_pool_str == 'Resources' and ('resource_pool' in ip_settings[key]): + resource_pool_str = ip_settings[key]['resource_pool'] + + + resource_pool = self.get_resource_pool(cluster, resource_pool_str) + + if resource_pool is None: + print "Error: Unable to find Resource Pool '%s'" % resource_pool_str + sys.exit(1) + datastore = self.get_obj([vim.Datastore], ip_settings[0]['datastore']) + + if datastore is None: + print "Error: Unable to find Datastore '%s'" % ip_settings[0]['datastore'] + sys.exit(1) + template_vm = self.get_vm_failfast(self.config['template'], False, 'Template VM') # Relocation spec @@ -201,7 +219,7 @@ def clone(self): # don't clone nic devices from template for device in template_vm.config.hardware.device: if hasattr(device, 'addressType'): - # this is a VirtualEthernetCard, so we'll delete it, + # this is a VirtualEthernetCard, so we'll delete it nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove nic.device = device @@ -241,7 +259,7 @@ def clone(self): guest_map.adapter.ip.ipAddress = str(ip_settings[key]['ip']) guest_map.adapter.subnetMask = str(ip_settings[key]['subnet_mask']) - # these may not be set for certain IPs, e.g. storage IPs + # these may not be set for certain IPs try: guest_map.adapter.gateway = ip_settings[key]['gateway'] except: @@ -484,14 +502,56 @@ def send_email(self): s.quit() ''' - Get the vsphere object associated with a given text name + Find a resource pool given a pool name for desired cluster ''' - def get_obj(self, vimtype, name): - obj = None + def get_resource_pool(self, cluster, pool_name): + pool_obj = None + + # get a list of all resource pools in this cluster + cluster_pools_list = cluster.resourcePool.resourcePool + + # get list of all resource pools with a given text name + pool_selections = self.get_obj([vim.ResourcePool], pool_name, return_all=True) + + # get the first pool that exists in a given cluster + for p in pool_selections: + if p in cluster_pools_list: + pool_obj = p + break + + return pool_obj + + ''' + Get the vsphere object associated with a given text name + ''' + def get_obj(self, vimtype, name, return_all=False): + obj = list() container = self.content.viewManager.CreateContainerView( self.content.rootFolder, vimtype, True) + for c in container.view: if c.name == name: + if return_all is False: + return c + break + else: + obj.append(c) + + if len(obj) > 0: + return obj + else: + # for backwards-compat + return None + + ''' + Get the vsphere object associated with a given MoId + ''' + def get_obj_by_moid(self, vimtype, moid): + obj = None + container = self.content.viewManager.CreateContainerView( + self.content.rootFolder, vimtype, True) + for c in container.view: + if c._GetMoId() == moid: obj = c break return obj @@ -516,7 +576,7 @@ def get_host_system_failfast(self, name, verbose=False, host_system_term='HS'): sys.exit(1) if True == verbose: - print("Found HostSystem: {0} Name: {1}", hs, hs.name) + print "Found HostSystem: {0} Name: {1}" % (hs, hs.name) return hs @@ -540,7 +600,7 @@ def get_vm_failfast(self, name, verbose=False, vm_term='VM'): sys.exit(1) if True == verbose: - print("Found VirtualMachine: {0} Name: {1}", vm, vm.name) + print "Found VirtualMachine: %s Name: %s" % (vm, vm.name) return vm @@ -586,9 +646,6 @@ def WaitForTasks(self, tasks): else: continue - if not str(task) in taskList: - continue - if state == vim.TaskInfo.State.success: # Remove task from taskList taskList.remove(str(task)) diff --git a/ezmomi/params.py b/ezmomi/params.py index 0fa32e1..e6368bd 100644 --- a/ezmomi/params.py +++ b/ezmomi/params.py @@ -1,8 +1,6 @@ ''' Command line option definitions ''' - - def add_params(subparsers): # list list_parser = subparsers.add_parser( @@ -176,6 +174,12 @@ def add_params(subparsers): type=str, help='Domain, e.g. "example.com"' ) + clone_parser.add_argument( + '--resource-pool', + type=str, + default='Resources', + help='Resource Pool, e.g. "Linux Servers"' + ) # destroy destroy_parser = subparsers.add_parser(