Merge pull request #1059 from sivel/sys-exit-removal

sys.exit removal from various modules
reviewable/pr18780/r1
Brian Coca 9 years ago
commit 6336838354

@ -201,7 +201,6 @@ def main():
changed = delete_image(gce, name, module) changed = delete_image(gce, name, module)
module.exit_json(changed=changed, name=name) module.exit_json(changed=changed, name=name)
sys.exit(0)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

@ -219,7 +219,6 @@ def main():
changed, tags_changed = remove_tags(gce, module, instance_name, tags) changed, tags_changed = remove_tags(gce, module, instance_name, tags)
module.exit_json(changed=changed, instance_name=instance_name, tags=tags_changed, zone=zone) module.exit_json(changed=changed, instance_name=instance_name, tags=tags_changed, zone=zone)
sys.exit(0)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

@ -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")

@ -28,7 +28,6 @@ author:
''' '''
import platform import platform
import sys
import XenAPI import XenAPI
EXAMPLES = ''' EXAMPLES = '''
@ -75,12 +74,9 @@ class XenServerFacts:
def get_xenapi_session(): def get_xenapi_session():
try: session = XenAPI.xapi_local()
session = XenAPI.xapi_local() session.xenapi.login_with_password('', '')
session.xenapi.login_with_password('', '') return session
return session
except XenAPI.Failure:
sys.exit(1)
def get_networks(session): def get_networks(session):
@ -163,8 +159,10 @@ def main():
module = AnsibleModule({}) module = AnsibleModule({})
obj = XenServerFacts() obj = XenServerFacts()
session = get_xenapi_session() try:
session = get_xenapi_session()
except XenAPI.Failure, e:
module.fail_json(msg='%s' % e)
data = { data = {
'xenserver_version': obj.version, 'xenserver_version': obj.version,

@ -285,7 +285,6 @@ def main():
msg.attach(part) msg.attach(part)
except Exception, e: except Exception, e:
module.fail_json(rc=1, msg="Failed to send mail: can't attach file %s: %s" % (file, e)) module.fail_json(rc=1, msg="Failed to send mail: can't attach file %s: %s" % (file, e))
sys.exit()
composed = msg.as_string() composed = msg.as_string()

@ -180,7 +180,6 @@ def main():
CapabilitiesModule(module) CapabilitiesModule(module)
sys.exit(0)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

Loading…
Cancel
Save