sys.exit removal for cloud/misc/ovirt.py

This commit is contained in:
Matt Martz 2015-10-05 13:22:43 -05:00 committed by Matt Clay
parent 950c951e06
commit 7cb0d86c27

View file

@ -207,14 +207,13 @@ action: ovirt >
''' '''
import sys
try: try:
from ovirtsdk.api import API from ovirtsdk.api import API
from ovirtsdk.xml import params from ovirtsdk.xml import params
HAS_OVIRTSDK = True
except ImportError: except ImportError:
print "failed=True msg='ovirtsdk required for this module'" HAS_OVIRTSDK = False
sys.exit(1)
# ------------------------------------------------------------------- # # ------------------------------------------------------------------- #
# create connection with API # create connection with API
@ -224,8 +223,7 @@ def conn(url, user, password):
try: try:
value = api.test() value = api.test()
except: except:
print "error connecting to the oVirt API" raise Exception("error connecting to the oVirt API")
sys.exit(1)
return api return api
# ------------------------------------------------------------------- # # ------------------------------------------------------------------- #
@ -253,17 +251,16 @@ def create_vm(conn, vmtype, vmname, zone, vmdisk_size, vmcpus, vmnic, vmnetwork,
try: try:
conn.vms.add(vmparams) conn.vms.add(vmparams)
except: except:
print "Error creating VM with specified parameters" raise Exception("Error creating VM with specified parameters")
sys.exit(1)
vm = conn.vms.get(name=vmname) vm = conn.vms.get(name=vmname)
try: try:
vm.disks.add(vmdisk) vm.disks.add(vmdisk)
except: except:
print "Error attaching disk" raise Exception("Error attaching disk")
try: try:
vm.nics.add(nic_net1) vm.nics.add(nic_net1)
except: except:
print "Error adding nic" raise Exception("Error adding nic")
# create an instance from a template # create an instance from a template
@ -272,8 +269,7 @@ def create_vm_template(conn, vmname, image, zone):
try: try:
conn.vms.add(vmparams) conn.vms.add(vmparams)
except: except:
print 'error adding template %s' % image raise Exception('error adding template %s' % image)
sys.exit(1)
# start instance # start instance
@ -356,6 +352,9 @@ def main():
) )
) )
if not HAS_OVIRTSDK:
module.fail_json(msg='ovirtsdk required for this module')
state = module.params['state'] state = module.params['state']
user = module.params['user'] user = module.params['user']
url = module.params['url'] url = module.params['url']
@ -377,16 +376,25 @@ def main():
sdomain = module.params['sdomain'] # storage domain to store disk on sdomain = module.params['sdomain'] # storage domain to store disk on
region = module.params['region'] # oVirt Datacenter region = module.params['region'] # oVirt Datacenter
#initialize connection #initialize connection
c = conn(url+"/api", user, password) try:
c = conn(url+"/api", user, password)
except Exception, e:
module.fail_json(msg='%s' % e)
if state == 'present': if state == 'present':
if get_vm(c, vmname) == "empty": if get_vm(c, vmname) == "empty":
if resource_type == 'template': if resource_type == 'template':
create_vm_template(c, vmname, image, zone) try:
create_vm_template(c, vmname, image, zone)
except Exception, e:
module.fail_json(msg='%s' % e)
module.exit_json(changed=True, msg="deployed VM %s from template %s" % (vmname,image)) module.exit_json(changed=True, msg="deployed VM %s from template %s" % (vmname,image))
elif resource_type == 'new': elif resource_type == 'new':
# FIXME: refactor, use keyword args. # FIXME: refactor, use keyword args.
create_vm(c, vmtype, vmname, zone, vmdisk_size, vmcpus, vmnic, vmnetwork, vmmem, vmdisk_alloc, sdomain, vmcores, vmos, vmdisk_int) try:
create_vm(c, vmtype, vmname, zone, vmdisk_size, vmcpus, vmnic, vmnetwork, vmmem, vmdisk_alloc, sdomain, vmcores, vmos, vmdisk_int)
except Exception, e:
module.fail_json(msg='%s' % e)
module.exit_json(changed=True, msg="deployed VM %s from scratch" % vmname) module.exit_json(changed=True, msg="deployed VM %s from scratch" % vmname)
else: else:
module.exit_json(changed=False, msg="You did not specify a resource type") module.exit_json(changed=False, msg="You did not specify a resource type")