Skip to content

Commit 9f0bcc0

Browse files
author
Ashby, Jason (IMS)
committed
snobear#12 added --resource-pool parameter
1 parent fe600da commit 9f0bcc0

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ env: PYTHONPATH=$PWD:$PYTHONPATH PATH=$PWD/bin:$PATH
66
# command to install dependencies
77
install: "pip install -r requirements.txt"
88
script: "ezmomi --help"
9+
script: "python -m py_compile ezmomi/cli.py"
10+
script: "python -m py_compile ezmomi/params.py"
11+
script: "python -m py_compile ezmomi/ezmomi.py"

ezmomi/cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from params import add_params
66
from ezmomi import EZMomi
77

8-
98
def cli():
109
# Set up command line arguments
1110
parser = argparse.ArgumentParser(
1211
description='Perform common vSphere API tasks'
1312
)
1413
subparsers = parser.add_subparsers(help='Command', dest='mode')
1514

15+
from pprint import pprint
16+
1617
# set up each command section
1718
add_params(subparsers)
1819

ezmomi/ezmomi.py

+69-13
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,27 @@ def clone(self):
182182
cluster = self.get_obj([vim.ClusterComputeResource],
183183
ip_settings[0]['cluster']
184184
)
185-
# use same root resource pool that my desired cluster uses
186-
resource_pool = cluster.resourcePool
185+
186+
resource_pool_str = self.config['resource_pool']
187+
188+
# resource_pool setting in config file takes priority over the
189+
# default 'Resources' pool
190+
if resource_pool_str == 'Resources' and ('resource_pool' in ip_settings[key]):
191+
resource_pool_str = ip_settings[key]['resource_pool']
192+
193+
194+
resource_pool = self.get_resource_pool(cluster, resource_pool_str)
195+
196+
if resource_pool is None:
197+
print "Error: Unable to find Resource Pool '%s'" % resource_pool_str
198+
sys.exit(1)
199+
187200
datastore = self.get_obj([vim.Datastore], ip_settings[0]['datastore'])
201+
202+
if datastore is None:
203+
print "Error: Unable to find Datastore '%s'" % ip_settings[0]['datastore']
204+
sys.exit(1)
205+
188206
template_vm = self.get_vm_failfast(self.config['template'], False, 'Template VM')
189207

190208
# Relocation spec
@@ -201,7 +219,7 @@ def clone(self):
201219
# don't clone nic devices from template
202220
for device in template_vm.config.hardware.device:
203221
if hasattr(device, 'addressType'):
204-
# this is a VirtualEthernetCard, so we'll delete it,
222+
# this is a VirtualEthernetCard, so we'll delete it
205223
nic = vim.vm.device.VirtualDeviceSpec()
206224
nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove
207225
nic.device = device
@@ -241,7 +259,7 @@ def clone(self):
241259
guest_map.adapter.ip.ipAddress = str(ip_settings[key]['ip'])
242260
guest_map.adapter.subnetMask = str(ip_settings[key]['subnet_mask'])
243261

244-
# these may not be set for certain IPs, e.g. storage IPs
262+
# these may not be set for certain IPs
245263
try:
246264
guest_map.adapter.gateway = ip_settings[key]['gateway']
247265
except:
@@ -484,14 +502,56 @@ def send_email(self):
484502
s.quit()
485503

486504
'''
487-
Get the vsphere object associated with a given text name
505+
Find a resource pool given a pool name for desired cluster
488506
'''
489-
def get_obj(self, vimtype, name):
490-
obj = None
507+
def get_resource_pool(self, cluster, pool_name):
508+
pool_obj = None
509+
510+
# get a list of all resource pools in this cluster
511+
cluster_pools_list = cluster.resourcePool.resourcePool
512+
513+
# get list of all resource pools with a given text name
514+
pool_selections = self.get_obj([vim.ResourcePool], pool_name, return_all=True)
515+
516+
# get the first pool that exists in a given cluster
517+
for p in pool_selections:
518+
if p in cluster_pools_list:
519+
pool_obj = p
520+
break
521+
522+
return pool_obj
523+
524+
'''
525+
Get the vsphere object associated with a given text name
526+
'''
527+
def get_obj(self, vimtype, name, return_all=False):
528+
obj = list()
491529
container = self.content.viewManager.CreateContainerView(
492530
self.content.rootFolder, vimtype, True)
531+
493532
for c in container.view:
494533
if c.name == name:
534+
if return_all is False:
535+
return c
536+
break
537+
else:
538+
obj.append(c)
539+
540+
if len(obj) > 0:
541+
return obj
542+
else:
543+
# for backwards-compat
544+
return None
545+
546+
'''
547+
Get the vsphere object associated with a given MoId
548+
'''
549+
def get_obj_by_moid(self, vimtype, moid):
550+
obj = None
551+
container = self.content.viewManager.CreateContainerView(
552+
self.content.rootFolder, vimtype, True)
553+
for c in container.view:
554+
if c._GetMoId() == moid:
495555
obj = c
496556
break
497557
return obj
@@ -582,12 +642,8 @@ def WaitForTasks(self, tasks):
582642
if change.name == 'info':
583643
state = change.val.state
584644
elif change.name == 'info.state':
585-
state = change.val
586-
else:
587-
continue
588-
589-
if not str(task) in taskList:
590-
continue
645+
if not str(task) in taskList:
646+
continue
591647

592648
if state == vim.TaskInfo.State.success:
593649
# Remove task from taskList

ezmomi/params.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'''
22
Command line option definitions
33
'''
4-
5-
64
def add_params(subparsers):
75
# list
86
list_parser = subparsers.add_parser(
@@ -176,6 +174,12 @@ def add_params(subparsers):
176174
type=str,
177175
help='Domain, e.g. "example.com"'
178176
)
177+
clone_parser.add_argument(
178+
'--resource-pool',
179+
type=str,
180+
default='Resources',
181+
help='Resource Pool, e.g. "Linux Servers"'
182+
)
179183

180184
# destroy
181185
destroy_parser = subparsers.add_parser(

0 commit comments

Comments
 (0)