Skip to content

Commit

Permalink
Merge pull request #4 from snobear/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
snobear committed Jul 11, 2014
2 parents 5610b35 + d50087a commit 1aa11f3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: python
python:
- "2.6"
- "2.7"
# command to install dependencies
install: "pip install -r requirements.txt"
script: nosetests
101 changes: 65 additions & 36 deletions ezmomi/ezmomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
from netaddr import IPNetwork, IPAddress
import yaml
from netaddr import IPNetwork, IPAddress

class EZMomi(object):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -82,7 +81,7 @@ def get_configs(self, kwargs):
def connect(self):
# connect to vCenter server
try:
si = SmartConnect(host = self.config['server'],
self.si = SmartConnect(host = self.config['server'],
user = self.config['username'],
pwd = self.config['password'],
port = int(self.config['port']),
Expand All @@ -92,9 +91,9 @@ def connect(self):
sys.exit()

# add a clean up routine
atexit.register(Disconnect, si)
atexit.register(Disconnect, self.si)

self.content = si.RetrieveContent()
self.content = self.si.RetrieveContent()

'''
Command Section: list
Expand All @@ -103,7 +102,7 @@ def connect(self):
def list_objects(self):
vimtype = self.config['type']
vim_obj = "vim.%s" % vimtype

try:
container = self.content.viewManager.CreateContainerView(self.content.rootFolder, [eval(vim_obj)], True)
except AttributeError:
Expand All @@ -127,7 +126,7 @@ def clone(self):
ip_settings = list()

# Get network settings for each IP
for key, ip_string in enumerate(kwargs['ips']):
for key, ip_string in enumerate(self.config['ips']):

# convert ip from string to the 'IPAddress' type
ip = IPAddress(ip_string)
Expand All @@ -140,10 +139,10 @@ def clone(self):
ipnet = IPNetwork(network)
self.config['networks'][network]['subnet_mask'] = str(ipnet.netmask)
ip_settings.append(self.config['networks'][network])

# throw an error if we couldn't find a network for this ip
if not any(d['ip'] == ip for d in ip_settings):
print "I don't know what network %s is in. You can supply settings for this network in self.config.yml." % ip_string
print "I don't know what network %s is in. You can supply settings for this network in config.yml." % ip_string
sys.exit(1)

# network to place new VM in
Expand Down Expand Up @@ -243,23 +242,23 @@ def clone(self):
clonespec.template = False

# fire the clone task
task = template_vm.Clone(folder=destfolder, name=self.config['hostname'], spec=clonespec)
result = self.WaitTask(task, 'VM clone task')
tasks = [template_vm.Clone(folder=destfolder, name=self.config['hostname'], spec=clonespec)]
result = self.WaitForTasks(tasks)

self.send_email()

def destroy(self):
tasks = list()
print "Finding VM named %s..." % self.config['name']
vm = self.get_obj([vim.VirtualMachine], self.config['name'])


# need to shut the VM down before destorying it
if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
print "Powering off %s..." % self.config['name']
task = vm.PowerOff()
result = self.WaitTask(task, 'VM power off')

tasks.append(vm.PowerOff())

tasks.append(vm.Destroy())
print "Destroying %s..." % self.config['name']
task = vm.Destroy()
result = self.WaitTask(task, 'VM destroy task')
result = self.WaitForTasks(tasks)

'''
Helper methods
Expand All @@ -270,7 +269,7 @@ def send_email(self):

# get user who ran this script
me = os.getenv('USER')

email_body = 'Your VM is ready!'
msg = MIMEText(email_body)
msg['Subject'] = '%s - VM deploy complete' % self.config['hostname']
Expand All @@ -293,21 +292,51 @@ def get_obj(self, vimtype, name):
break
return obj

'''
Waits and provides updates on a vSphere task
'''
def WaitTask(self, task, actionName='job', hideResult=False):
while task.info.state == vim.TaskInfo.State.running:
time.sleep(2)

if task.info.state == vim.TaskInfo.State.success:
if task.info.result is not None and not hideResult:
out = '%s completed successfully, result: %s' % (actionName, task.info.result)
else:
out = '%s completed successfully.' % actionName
else:
out = '%s did not complete successfully: %s' % (actionName, task.info.error)
print out
raise task.info.error
def WaitForTasks(self, tasks):
'''
Given the service instance si and tasks, it returns after all the
tasks are complete
'''

pc = self.si.content.propertyCollector

taskList = [str(task) for task in tasks]

# Create filter
objSpecs = [vmodl.query.PropertyCollector.ObjectSpec(obj=task) for task in tasks]
propSpec = vmodl.query.PropertyCollector.PropertySpec(type=vim.Task, pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = objSpecs
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)

try:
version, state = None, None

return task.info.result
# Loop looking for updates till the state moves to a completed state.
while len(taskList):
update = pc.WaitForUpdates(version)
for filterSet in update.filterSet:
for objSet in filterSet.objectSet:
task = objSet.obj
for change in objSet.changeSet:
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 state == vim.TaskInfo.State.success:
# Remove task from taskList
taskList.remove(str(task))
elif state == vim.TaskInfo.State.error:
raise task.info.error
# Move to next version
version = update.version
finally:
if filter:
filter.Destroy()
1 change: 1 addition & 0 deletions ezmomi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def add_params(subparsers):
type=str,
help='VM template name to clone from')
clone_parser.add_argument('--hostname',
required=True,
type=str,
help='New host name',
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
url='https://github.com/snobear/ezmomi',
license='LICENSE.txt',
description='VMware vSphere Command line tool',
long_description=open('README.tx').read(),
long_description=open('README.txt').read(),
install_requires=[
"PyYAML==3.11",
"argparse==1.2.1",
Expand Down

0 comments on commit 1aa11f3

Please sign in to comment.