From 9f0bcc04aef9f283603f6b847d0e070eeed90531 Mon Sep 17 00:00:00 2001 From: "Ashby, Jason (IMS)" Date: Tue, 22 Sep 2015 11:19:06 -0400 Subject: [PATCH 1/2] #12 added --resource-pool parameter --- .travis.yml | 3 ++ ezmomi/cli.py | 3 +- ezmomi/ezmomi.py | 82 ++++++++++++++++++++++++++++++++++++++++-------- ezmomi/params.py | 8 +++-- 4 files changed, 80 insertions(+), 16 deletions(-) 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..4f8a53c 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( @@ -13,6 +12,8 @@ def cli(): ) subparsers = parser.add_subparsers(help='Command', dest='mode') + from pprint import pprint + # set up each command section add_params(subparsers) diff --git a/ezmomi/ezmomi.py b/ezmomi/ezmomi.py index ff94c10..e19ea78 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 @@ -582,12 +642,8 @@ def WaitForTasks(self, tasks): if change.name == 'info': state = change.val.state elif change.name == 'info.state': - state = change.val - else: - continue - - if not str(task) in taskList: - continue + if not str(task) in taskList: + continue if state == vim.TaskInfo.State.success: # Remove task from taskList 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( From b3271e8c9844e2b620ee80ca6e73889d18b9b91f Mon Sep 17 00:00:00 2001 From: "Ashby, Jason (IMS)" Date: Wed, 23 Sep 2015 08:14:52 -0400 Subject: [PATCH 2/2] #12 fixed waitfortasks hang issue --- ezmomi/cli.py | 2 -- ezmomi/ezmomi.py | 9 +++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ezmomi/cli.py b/ezmomi/cli.py index 4f8a53c..43e124e 100644 --- a/ezmomi/cli.py +++ b/ezmomi/cli.py @@ -12,8 +12,6 @@ def cli(): ) subparsers = parser.add_subparsers(help='Command', dest='mode') - from pprint import pprint - # set up each command section add_params(subparsers) diff --git a/ezmomi/ezmomi.py b/ezmomi/ezmomi.py index e19ea78..6d6cf99 100644 --- a/ezmomi/ezmomi.py +++ b/ezmomi/ezmomi.py @@ -576,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 @@ -600,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 @@ -642,8 +642,9 @@ def WaitForTasks(self, tasks): if change.name == 'info': state = change.val.state elif change.name == 'info.state': - if not str(task) in taskList: - continue + state = change.val + else: + continue if state == vim.TaskInfo.State.success: # Remove task from taskList