From b82ebdde19ae5b721778456cadc5594d699ceb3a Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 5 Oct 2015 13:22:32 -0500 Subject: [PATCH 1/5] sys.exit removal for cloud/xenserver_facts.py --- cloud/xenserver_facts.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cloud/xenserver_facts.py b/cloud/xenserver_facts.py index 149030925f9..d679afce853 100644 --- a/cloud/xenserver_facts.py +++ b/cloud/xenserver_facts.py @@ -28,7 +28,6 @@ author: ''' import platform -import sys import XenAPI EXAMPLES = ''' @@ -75,12 +74,9 @@ class XenServerFacts: def get_xenapi_session(): - try: - session = XenAPI.xapi_local() - session.xenapi.login_with_password('', '') - return session - except XenAPI.Failure: - sys.exit(1) + session = XenAPI.xapi_local() + session.xenapi.login_with_password('', '') + return session def get_networks(session): @@ -163,8 +159,10 @@ def main(): module = AnsibleModule({}) obj = XenServerFacts() - session = get_xenapi_session() - + try: + session = get_xenapi_session() + except XenAPI.Failure, e: + module.fail_json(msg='%s' % e) data = { 'xenserver_version': obj.version, From 55dab7cd1b52ebed035ed79eae5bdd954eebb2d7 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 5 Oct 2015 13:22:43 -0500 Subject: [PATCH 2/5] sys.exit removal for cloud/misc/ovirt.py --- cloud/misc/ovirt.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/cloud/misc/ovirt.py b/cloud/misc/ovirt.py index 6e8f3281dc5..10f8e5c0a44 100644 --- a/cloud/misc/ovirt.py +++ b/cloud/misc/ovirt.py @@ -207,14 +207,13 @@ action: ovirt > ''' -import sys try: from ovirtsdk.api import API from ovirtsdk.xml import params + HAS_OVIRTSDK = True except ImportError: - print "failed=True msg='ovirtsdk required for this module'" - sys.exit(1) + HAS_OVIRTSDK = False # ------------------------------------------------------------------- # # create connection with API @@ -224,8 +223,7 @@ def conn(url, user, password): try: value = api.test() except: - print "error connecting to the oVirt API" - sys.exit(1) + raise Exception("error connecting to the oVirt API") return api # ------------------------------------------------------------------- # @@ -253,17 +251,16 @@ def create_vm(conn, vmtype, vmname, zone, vmdisk_size, vmcpus, vmnic, vmnetwork, try: conn.vms.add(vmparams) except: - print "Error creating VM with specified parameters" - sys.exit(1) + raise Exception("Error creating VM with specified parameters") vm = conn.vms.get(name=vmname) try: vm.disks.add(vmdisk) except: - print "Error attaching disk" + raise Exception("Error attaching disk") try: vm.nics.add(nic_net1) except: - print "Error adding nic" + raise Exception("Error adding nic") # create an instance from a template @@ -272,8 +269,7 @@ def create_vm_template(conn, vmname, image, zone): try: conn.vms.add(vmparams) except: - print 'error adding template %s' % image - sys.exit(1) + raise Exception('error adding template %s' % image) # 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'] user = module.params['user'] url = module.params['url'] @@ -377,16 +376,25 @@ def main(): sdomain = module.params['sdomain'] # storage domain to store disk on region = module.params['region'] # oVirt Datacenter #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 get_vm(c, vmname) == "empty": 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)) elif resource_type == 'new': # 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) else: module.exit_json(changed=False, msg="You did not specify a resource type") From abf7ee579e50154c67d39a76931d59ce6d7a9a1e Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 5 Oct 2015 13:23:00 -0500 Subject: [PATCH 3/5] sys.exit removal for cloud/google/gce_img.py and cloud/google/gce_tag.py --- cloud/google/gce_img.py | 1 - cloud/google/gce_tag.py | 1 - 2 files changed, 2 deletions(-) diff --git a/cloud/google/gce_img.py b/cloud/google/gce_img.py index 5775a94794d..a4a55c16dec 100644 --- a/cloud/google/gce_img.py +++ b/cloud/google/gce_img.py @@ -201,7 +201,6 @@ def main(): changed = delete_image(gce, name, module) module.exit_json(changed=changed, name=name) - sys.exit(0) # import module snippets from ansible.module_utils.basic import * diff --git a/cloud/google/gce_tag.py b/cloud/google/gce_tag.py index 186f570b3f1..4f60f58f760 100644 --- a/cloud/google/gce_tag.py +++ b/cloud/google/gce_tag.py @@ -219,7 +219,6 @@ def main(): changed, tags_changed = remove_tags(gce, module, instance_name, tags) module.exit_json(changed=changed, instance_name=instance_name, tags=tags_changed, zone=zone) - sys.exit(0) # import module snippets from ansible.module_utils.basic import * From eac449342178dafee57a24b0792d3ad2fc96a2cc Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 5 Oct 2015 13:23:34 -0500 Subject: [PATCH 4/5] sys.exit removal for notification/mail.py --- notification/mail.py | 1 - 1 file changed, 1 deletion(-) diff --git a/notification/mail.py b/notification/mail.py index 8be9a589cbf..e63a9536996 100644 --- a/notification/mail.py +++ b/notification/mail.py @@ -285,7 +285,6 @@ def main(): msg.attach(part) except Exception, 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() From e68d9315c17fbf6ce37f323d0ee57b1eba1fa0a4 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 5 Oct 2015 13:24:25 -0500 Subject: [PATCH 5/5] sys.exit removal for system/capabilities.py --- system/capabilities.py | 1 - 1 file changed, 1 deletion(-) diff --git a/system/capabilities.py b/system/capabilities.py index ce8ffcfa632..aa0785f6f62 100644 --- a/system/capabilities.py +++ b/system/capabilities.py @@ -180,7 +180,6 @@ def main(): CapabilitiesModule(module) - sys.exit(0) # import module snippets from ansible.module_utils.basic import *