Skip to content

Commit

Permalink
Merge pull request #50 from snobear/resourcepool
Browse files Browse the repository at this point in the history
Resourcepool
  • Loading branch information
snobear committed Sep 23, 2015
2 parents fe600da + b3271e8 commit ee479d3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 0 additions & 1 deletion ezmomi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from params import add_params
from ezmomi import EZMomi


def cli():
# Set up command line arguments
parser = argparse.ArgumentParser(
Expand Down
81 changes: 69 additions & 12 deletions ezmomi/ezmomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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))
Expand Down
8 changes: 6 additions & 2 deletions ezmomi/params.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'''
Command line option definitions
'''


def add_params(subparsers):
# list
list_parser = subparsers.add_parser(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ee479d3

Please sign in to comment.