From 67f769d9a684477ae0e4a7c5087d0937497c8270 Mon Sep 17 00:00:00 2001 From: steenzout Date: Wed, 1 Apr 2015 17:16:54 -0600 Subject: [PATCH] issue #994: use HAS_BOTO to determine if import was successful: - removed import of sys module. - HAS_BOTO constant to check if import was successful. - trigger a failure when import fails. - removed unnecessary imports. --- lib/ansible/modules/cloud/amazon/ec2.py | 10 ++++++---- lib/ansible/modules/cloud/amazon/ec2_ami.py | 10 ++++++++-- lib/ansible/modules/cloud/amazon/ec2_asg.py | 9 ++++++--- lib/ansible/modules/cloud/amazon/ec2_eip.py | 9 ++++----- lib/ansible/modules/cloud/amazon/ec2_elb.py | 10 ++++++---- lib/ansible/modules/cloud/amazon/ec2_elb_lb.py | 10 +++++----- lib/ansible/modules/cloud/amazon/ec2_group.py | 7 +++++-- lib/ansible/modules/cloud/amazon/ec2_key.py | 7 +++++-- lib/ansible/modules/cloud/amazon/ec2_lc.py | 10 +++++----- lib/ansible/modules/cloud/amazon/ec2_metric_alarm.py | 7 +++++-- .../modules/cloud/amazon/ec2_scaling_policy.py | 10 +++++----- lib/ansible/modules/cloud/amazon/ec2_snapshot.py | 9 ++++++--- lib/ansible/modules/cloud/amazon/ec2_tag.py | 9 ++++++--- lib/ansible/modules/cloud/amazon/ec2_vol.py | 9 ++++++--- lib/ansible/modules/cloud/amazon/ec2_vpc.py | 10 +++++++--- lib/ansible/modules/cloud/amazon/elasticache.py | 8 +++++--- lib/ansible/modules/cloud/amazon/rds.py | 8 ++++++-- lib/ansible/modules/cloud/amazon/rds_param_group.py | 11 ++++++----- lib/ansible/modules/cloud/amazon/rds_subnet_group.py | 11 ++++++----- lib/ansible/modules/cloud/amazon/route53.py | 9 ++++++--- lib/ansible/modules/cloud/amazon/s3.py | 12 +++++++----- 21 files changed, 121 insertions(+), 74 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index a69c94f1eee..a2dcdada0a8 100755 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -487,7 +487,6 @@ EXAMPLES = ''' ''' -import sys import time from ast import literal_eval @@ -496,9 +495,10 @@ try: from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping from boto.exception import EC2ResponseError from boto.vpc import VPCConnection + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def find_running_instances_by_count_tag(module, ec2, count_tag, zone=None): @@ -782,7 +782,6 @@ def create_instances(module, ec2, vpc, override_count=None): # group_id and group_name are exclusive of each other if group_id and group_name: module.fail_json(msg = str("Use only one type of parameter (group_name) or (group_id)")) - sys.exit(1) vpc_id = None if vpc_subnet_id: @@ -1203,6 +1202,9 @@ def main(): ], ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + ec2 = ec2_connect(module) ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) diff --git a/lib/ansible/modules/cloud/amazon/ec2_ami.py b/lib/ansible/modules/cloud/amazon/ec2_ami.py index 401b667c545..d406090275b 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_ami.py +++ b/lib/ansible/modules/cloud/amazon/ec2_ami.py @@ -129,15 +129,17 @@ EXAMPLES = ''' state: absent ''' + import sys import time try: import boto import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def create_image(module, ec2): """ @@ -226,6 +228,7 @@ def deregister_image(module, ec2): module.exit_json(msg="AMI deregister/delete operation complete", changed=True) sys.exit(0) + def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict( @@ -242,6 +245,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + try: ec2 = ec2_connect(module) except Exception, e: diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py index e040ad48067..7a6d2043e0f 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_asg.py +++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py @@ -189,7 +189,6 @@ to "replace_instances": region: us-east-1 ''' -import sys import time from ansible.module_utils.basic import * @@ -199,9 +198,9 @@ try: import boto.ec2.autoscale from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup, Tag from boto.exception import BotoServerError + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False ASG_ATTRIBUTES = ('availability_zones', 'default_cooldown', 'desired_capacity', 'health_check_period', 'health_check_type', 'launch_config_name', @@ -617,6 +616,10 @@ def main(): argument_spec=argument_spec, mutually_exclusive = [['replace_all_instances', 'replace_instances']] ) + + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + state = module.params.get('state') replace_instances = module.params.get('replace_instances') replace_all_instances = module.params.get('replace_all_instances') diff --git a/lib/ansible/modules/cloud/amazon/ec2_eip.py b/lib/ansible/modules/cloud/amazon/ec2_eip.py index fd0e8d04568..5f84ff24ff5 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_eip.py +++ b/lib/ansible/modules/cloud/amazon/ec2_eip.py @@ -90,10 +90,9 @@ EXAMPLES = ''' try: import boto.ec2 + HAS_BOTO = True except ImportError: - boto_found = False -else: - boto_found = True + HAS_BOTO = False wait_timeout = 0 @@ -251,8 +250,8 @@ def main(): supports_check_mode=True ) - if not boto_found: - module.fail_json(msg="boto is required") + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') ec2 = ec2_connect(module) diff --git a/lib/ansible/modules/cloud/amazon/ec2_elb.py b/lib/ansible/modules/cloud/amazon/ec2_elb.py index 11abd827b2b..2a29a5e49a9 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_elb.py +++ b/lib/ansible/modules/cloud/amazon/ec2_elb.py @@ -99,17 +99,16 @@ post_tasks: """ import time -import sys -import os try: import boto import boto.ec2 import boto.ec2.elb from boto.regioninfo import RegionInfo + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + class ElbManager: """Handles EC2 instance ELB registration and de-registration""" @@ -299,6 +298,9 @@ def main(): argument_spec=argument_spec, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + region, ec2_url, aws_connect_params = get_aws_connection_info(module) if not region: diff --git a/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py b/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py index 25a3f1f13fc..442578b9c86 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py +++ b/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py @@ -240,18 +240,15 @@ EXAMPLES = """ instance_port: 80 """ -import sys -import os - try: import boto import boto.ec2.elb import boto.ec2.elb.attributes from boto.ec2.elb.healthcheck import HealthCheck from boto.regioninfo import RegionInfo + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False class ElbManager(object): @@ -652,6 +649,9 @@ def main(): argument_spec=argument_spec, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + region, ec2_url, aws_connect_params = get_aws_connection_info(module) if not region: module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file") diff --git a/lib/ansible/modules/cloud/amazon/ec2_group.py b/lib/ansible/modules/cloud/amazon/ec2_group.py index 5eb4317b97e..f84467740b3 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_group.py +++ b/lib/ansible/modules/cloud/amazon/ec2_group.py @@ -113,9 +113,9 @@ EXAMPLES = ''' try: import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False def make_rule_key(prefix, rule, group_id, cidr_ip): @@ -202,6 +202,9 @@ def main(): supports_check_mode=True, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + name = module.params['name'] description = module.params['description'] vpc_id = module.params['vpc_id'] diff --git a/lib/ansible/modules/cloud/amazon/ec2_key.py b/lib/ansible/modules/cloud/amazon/ec2_key.py index 9f548496c4a..6bc9d936ee3 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_key.py +++ b/lib/ansible/modules/cloud/amazon/ec2_key.py @@ -84,9 +84,9 @@ EXAMPLES = ''' try: import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False import random import string @@ -107,6 +107,9 @@ def main(): supports_check_mode=True, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + name = module.params['name'] state = module.params.get('state') key_material = module.params.get('key_material') diff --git a/lib/ansible/modules/cloud/amazon/ec2_lc.py b/lib/ansible/modules/cloud/amazon/ec2_lc.py index 30f532c9e4f..18a736600d0 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_lc.py +++ b/lib/ansible/modules/cloud/amazon/ec2_lc.py @@ -129,9 +129,6 @@ EXAMPLES = ''' ''' -import sys -import time - from ansible.module_utils.basic import * from ansible.module_utils.ec2 import * @@ -140,9 +137,9 @@ try: import boto.ec2.autoscale from boto.ec2.autoscale import LaunchConfiguration from boto.exception import BotoServerError + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False def create_block_device(module, volume): @@ -260,6 +257,9 @@ def main(): module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + region, ec2_url, aws_connect_params = get_aws_connection_info(module) try: diff --git a/lib/ansible/modules/cloud/amazon/ec2_metric_alarm.py b/lib/ansible/modules/cloud/amazon/ec2_metric_alarm.py index 7f0caac82cb..365ac50d321 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_metric_alarm.py +++ b/lib/ansible/modules/cloud/amazon/ec2_metric_alarm.py @@ -122,9 +122,9 @@ try: import boto.ec2.cloudwatch from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm from boto.exception import BotoServerError + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False def create_metric_alarm(connection, module): @@ -266,6 +266,9 @@ def main(): module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + state = module.params.get('state') region, ec2_url, aws_connect_params = get_aws_connection_info(module) diff --git a/lib/ansible/modules/cloud/amazon/ec2_scaling_policy.py b/lib/ansible/modules/cloud/amazon/ec2_scaling_policy.py index 8e7d459e3e3..28eda56f07b 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_scaling_policy.py +++ b/lib/ansible/modules/cloud/amazon/ec2_scaling_policy.py @@ -55,8 +55,6 @@ EXAMPLES = ''' ''' -import sys - from ansible.module_utils.basic import * from ansible.module_utils.ec2 import * @@ -64,10 +62,9 @@ try: import boto.ec2.autoscale from boto.ec2.autoscale import ScalingPolicy from boto.exception import BotoServerError - + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False def create_scaling_policy(connection, module): @@ -157,6 +154,9 @@ def main(): module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + region, ec2_url, aws_connect_params = get_aws_connection_info(module) state = module.params.get('state') diff --git a/lib/ansible/modules/cloud/amazon/ec2_snapshot.py b/lib/ansible/modules/cloud/amazon/ec2_snapshot.py index 20cb017d81c..ae3df140cce 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_snapshot.py +++ b/lib/ansible/modules/cloud/amazon/ec2_snapshot.py @@ -106,14 +106,14 @@ EXAMPLES = ''' state: absent ''' -import sys import time try: import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def main(): argument_spec = ec2_argument_spec() @@ -132,6 +132,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + volume_id = module.params.get('volume_id') snapshot_id = module.params.get('snapshot_id') description = module.params.get('description') diff --git a/lib/ansible/modules/cloud/amazon/ec2_tag.py b/lib/ansible/modules/cloud/amazon/ec2_tag.py index 409041f906b..f82e8ca782d 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_tag.py +++ b/lib/ansible/modules/cloud/amazon/ec2_tag.py @@ -72,13 +72,13 @@ tasks: ''' import sys -import time try: import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def main(): argument_spec = ec2_argument_spec() @@ -90,6 +90,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + resource = module.params.get('resource') tags = module.params.get('tags') state = module.params.get('state') diff --git a/lib/ansible/modules/cloud/amazon/ec2_vol.py b/lib/ansible/modules/cloud/amazon/ec2_vol.py index 01a539ae4b0..b6fca82be9a 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vol.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vol.py @@ -186,16 +186,16 @@ EXAMPLES = ''' device_name: /dev/xvdf ''' -import sys import time from distutils.version import LooseVersion try: import boto.ec2 + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def get_volume(module, ec2): name = module.params.get('name') @@ -364,6 +364,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + id = module.params.get('id') name = module.params.get('name') instance = module.params.get('instance') diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc.py b/lib/ansible/modules/cloud/amazon/ec2_vpc.py index f8fd9ab27c4..23c25c27a87 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc.py @@ -156,16 +156,17 @@ the delete will fail until those dependencies are removed. ''' -import sys import time try: import boto.ec2 import boto.vpc from boto.exception import EC2ResponseError + + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def get_vpc_info(vpc): """ @@ -576,6 +577,9 @@ def main(): argument_spec=argument_spec, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + state = module.params.get('state') region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) diff --git a/lib/ansible/modules/cloud/amazon/elasticache.py b/lib/ansible/modules/cloud/amazon/elasticache.py index e3946e1327d..43faf481a53 100644 --- a/lib/ansible/modules/cloud/amazon/elasticache.py +++ b/lib/ansible/modules/cloud/amazon/elasticache.py @@ -131,16 +131,15 @@ EXAMPLES = """ """ import sys -import os import time try: import boto from boto.elasticache.layer1 import ElastiCacheConnection from boto.regioninfo import RegionInfo + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False class ElastiCacheManager(object): @@ -497,6 +496,9 @@ def main(): argument_spec=argument_spec, ) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) name = module.params['name'] diff --git a/lib/ansible/modules/cloud/amazon/rds.py b/lib/ansible/modules/cloud/amazon/rds.py index 879143c03f6..dd2a11e2afc 100644 --- a/lib/ansible/modules/cloud/amazon/rds.py +++ b/lib/ansible/modules/cloud/amazon/rds.py @@ -294,9 +294,9 @@ import time try: import boto.rds + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False try: import boto.rds2 @@ -984,6 +984,10 @@ def main(): module = AnsibleModule( argument_spec=argument_spec, ) + + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + invocations = { 'create': create_db_instance, 'replicate': replicate_db_instance, diff --git a/lib/ansible/modules/cloud/amazon/rds_param_group.py b/lib/ansible/modules/cloud/amazon/rds_param_group.py index 4f07dae3ae1..5f4cb3bf552 100644 --- a/lib/ansible/modules/cloud/amazon/rds_param_group.py +++ b/lib/ansible/modules/cloud/amazon/rds_param_group.py @@ -87,9 +87,6 @@ EXAMPLES = ''' name: norwegian_blue ''' -import sys -import time - VALID_ENGINES = [ 'mysql5.1', 'mysql5.5', @@ -112,9 +109,10 @@ VALID_ENGINES = [ try: import boto.rds from boto.exception import BotoServerError + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + # returns a tuple: (whether or not a parameter was changed, the remaining parameters that weren't found in this parameter group) @@ -220,6 +218,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + state = module.params.get('state') group_name = module.params.get('name').lower() group_engine = module.params.get('engine') diff --git a/lib/ansible/modules/cloud/amazon/rds_subnet_group.py b/lib/ansible/modules/cloud/amazon/rds_subnet_group.py index 22500dff26b..59d051b4333 100644 --- a/lib/ansible/modules/cloud/amazon/rds_subnet_group.py +++ b/lib/ansible/modules/cloud/amazon/rds_subnet_group.py @@ -73,15 +73,13 @@ EXAMPLES = ''' name: norwegian-blue ''' -import sys -import time - try: import boto.rds from boto.exception import BotoServerError + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def main(): argument_spec = ec2_argument_spec() @@ -94,6 +92,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + state = module.params.get('state') group_name = module.params.get('name').lower() group_description = module.params.get('description') diff --git a/lib/ansible/modules/cloud/amazon/route53.py b/lib/ansible/modules/cloud/amazon/route53.py index fb5c402718f..082ef38c3ab 100644 --- a/lib/ansible/modules/cloud/amazon/route53.py +++ b/lib/ansible/modules/cloud/amazon/route53.py @@ -159,7 +159,6 @@ EXAMPLES = ''' ''' -import sys import time try: @@ -167,9 +166,10 @@ try: from boto import route53 from boto.route53 import Route53Connection from boto.route53.record import ResourceRecordSets + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def commit(changes, retry_interval): """Commit changes, but retry PriorRequestNotComplete errors.""" @@ -203,6 +203,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + command_in = module.params.get('command') zone_in = module.params.get('zone').lower() ttl_in = module.params.get('ttl') diff --git a/lib/ansible/modules/cloud/amazon/s3.py b/lib/ansible/modules/cloud/amazon/s3.py index e7d017f58ea..fe8aeaf0611 100644 --- a/lib/ansible/modules/cloud/amazon/s3.py +++ b/lib/ansible/modules/cloud/amazon/s3.py @@ -107,20 +107,19 @@ EXAMPLES = ''' - s3: bucket=mybucket mode=delete ''' -import sys import os import urlparse import hashlib -from boto.s3.connection import OrdinaryCallingFormat - try: import boto from boto.s3.connection import Location + from boto.s3.connection import OrdinaryCallingFormat from boto.s3.connection import S3Connection + HAS_BOTO = True except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) + HAS_BOTO = False + def key_check(module, s3, bucket, obj): try: @@ -278,6 +277,9 @@ def main(): ) module = AnsibleModule(argument_spec=argument_spec) + if not HAS_BOTO: + module.fail_json(msg='boto required for this module') + bucket = module.params.get('bucket') obj = module.params.get('object') src = module.params.get('src')