Don't raise or catch StandardError in amazon modules

This commit is contained in:
Toshio Kuratomi 2015-11-30 19:02:28 -08:00
parent 2dba0d8d3a
commit cd9a7667aa
9 changed files with 104 additions and 114 deletions

View file

@ -281,7 +281,6 @@ def get_properties(autoscaling_group):
if getattr(autoscaling_group, "tags", None): if getattr(autoscaling_group, "tags", None):
properties['tags'] = dict((t.key, t.value) for t in autoscaling_group.tags) properties['tags'] = dict((t.key, t.value) for t in autoscaling_group.tags)
return properties return properties
def elb_dreg(asg_connection, module, group_name, instance_id): def elb_dreg(asg_connection, module, group_name, instance_id):
@ -298,7 +297,6 @@ def elb_dreg(asg_connection, module, group_name, instance_id):
else: else:
return return
exists = True
for lb in as_group.load_balancers: for lb in as_group.load_balancers:
elb_connection.deregister_instances(lb, instance_id) elb_connection.deregister_instances(lb, instance_id)
log.debug("De-registering {0} from ELB {1}".format(instance_id, lb)) log.debug("De-registering {0} from ELB {1}".format(instance_id, lb))
@ -315,12 +313,10 @@ def elb_dreg(asg_connection, module, group_name, instance_id):
time.sleep(10) time.sleep(10)
if wait_timeout <= time.time(): if wait_timeout <= time.time():
# waiting took too long # waiting took too long
module.fail_json(msg = "Waited too long for instance to deregister. {0}".format(time.asctime())) module.fail_json(msg = "Waited too long for instance to deregister. {0}".format(time.asctime()))
def elb_healthy(asg_connection, elb_connection, module, group_name): def elb_healthy(asg_connection, elb_connection, module, group_name):
healthy_instances = [] healthy_instances = []
as_group = asg_connection.get_all_groups(names=[group_name])[0] as_group = asg_connection.get_all_groups(names=[group_name])[0]
@ -337,7 +333,7 @@ def elb_healthy(asg_connection, elb_connection, module, group_name):
# but has not yet show up in the ELB # but has not yet show up in the ELB
try: try:
lb_instances = elb_connection.describe_instance_health(lb, instances=instances) lb_instances = elb_connection.describe_instance_health(lb, instances=instances)
except boto.exception.InvalidInstance, e: except boto.exception.InvalidInstance:
pass pass
for i in lb_instances: for i in lb_instances:
if i.state == "InService": if i.state == "InService":
@ -346,7 +342,6 @@ def elb_healthy(asg_connection, elb_connection, module, group_name):
return len(healthy_instances) return len(healthy_instances)
def wait_for_elb(asg_connection, module, group_name): def wait_for_elb(asg_connection, module, group_name):
region, ec2_url, aws_connect_params = get_aws_connection_info(module) region, ec2_url, aws_connect_params = get_aws_connection_info(module)
wait_timeout = module.params.get('wait_timeout') wait_timeout = module.params.get('wait_timeout')
@ -370,7 +365,7 @@ def wait_for_elb(asg_connection, module, group_name):
log.debug("ELB thinks {0} instances are healthy.".format(healthy_instances)) log.debug("ELB thinks {0} instances are healthy.".format(healthy_instances))
time.sleep(10) time.sleep(10)
if wait_timeout <= time.time(): if wait_timeout <= time.time():
# waiting took too long # waiting took too long
module.fail_json(msg = "Waited too long for ELB instances to be healthy. %s" % time.asctime()) module.fail_json(msg = "Waited too long for ELB instances to be healthy. %s" % time.asctime())
log.debug("Waiting complete. ELB thinks {0} instances are healthy.".format(healthy_instances)) log.debug("Waiting complete. ELB thinks {0} instances are healthy.".format(healthy_instances))
@ -396,7 +391,7 @@ def create_autoscaling_group(connection, module):
region, ec2_url, aws_connect_params = get_aws_connection_info(module) region, ec2_url, aws_connect_params = get_aws_connection_info(module)
try: try:
ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params) ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
elif vpc_zone_identifier: elif vpc_zone_identifier:
vpc_zone_identifier = ','.join(vpc_zone_identifier) vpc_zone_identifier = ','.join(vpc_zone_identifier)
@ -433,7 +428,7 @@ def create_autoscaling_group(connection, module):
try: try:
connection.create_auto_scaling_group(ag) connection.create_auto_scaling_group(ag)
if wait_for_instances == True: if wait_for_instances:
wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances')
wait_for_elb(connection, module, group_name) wait_for_elb(connection, module, group_name)
as_group = connection.get_all_groups(names=[group_name])[0] as_group = connection.get_all_groups(names=[group_name])[0]
@ -475,7 +470,7 @@ def create_autoscaling_group(connection, module):
dead_tags = [] dead_tags = []
for tag in as_group.tags: for tag in as_group.tags:
have_tags[tag.key] = [tag.value, tag.propagate_at_launch] have_tags[tag.key] = [tag.value, tag.propagate_at_launch]
if not tag.key in want_tags: if tag.key not in want_tags:
changed = True changed = True
dead_tags.append(tag) dead_tags.append(tag)
@ -492,14 +487,13 @@ def create_autoscaling_group(connection, module):
changed = True changed = True
as_group.load_balancers = module.params.get('load_balancers') as_group.load_balancers = module.params.get('load_balancers')
if changed: if changed:
try: try:
as_group.update() as_group.update()
except BotoServerError, e: except BotoServerError, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
if wait_for_instances == True: if wait_for_instances:
wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances')
wait_for_elb(connection, module, group_name) wait_for_elb(connection, module, group_name)
try: try:
@ -525,7 +519,7 @@ def delete_autoscaling_group(connection, module):
if tmp_groups: if tmp_groups:
tmp_group = tmp_groups[0] tmp_group = tmp_groups[0]
if not tmp_group.instances: if not tmp_group.instances:
instances = False instances = False
time.sleep(10) time.sleep(10)
group.delete() group.delete()
@ -580,10 +574,10 @@ def replace(connection, module):
changed = True changed = True
return(changed, props) return(changed, props)
# we don't want to spin up extra instances if not necessary # we don't want to spin up extra instances if not necessary
if num_new_inst_needed < batch_size: if num_new_inst_needed < batch_size:
log.debug("Overriding batch size to {0}".format(num_new_inst_needed)) log.debug("Overriding batch size to {0}".format(num_new_inst_needed))
batch_size = num_new_inst_needed batch_size = num_new_inst_needed
if not old_instances: if not old_instances:
changed = False changed = False
@ -659,10 +653,10 @@ def list_purgeable_instances(props, lc_check, replace_instances, initial_instanc
# and they have a non-current launch config # and they have a non-current launch config
if lc_check: if lc_check:
for i in instances: for i in instances:
if props['instance_facts'][i]['launch_config_name'] != props['launch_config_name']: if props['instance_facts'][i]['launch_config_name'] != props['launch_config_name']:
instances_to_terminate.append(i) instances_to_terminate.append(i)
else: else:
for i in instances: for i in instances:
if i in initial_instances: if i in initial_instances:
instances_to_terminate.append(i) instances_to_terminate.append(i)
return instances_to_terminate return instances_to_terminate
@ -826,13 +820,13 @@ def main():
if state == 'present': if state == 'present':
create_changed, asg_properties=create_autoscaling_group(connection, module) create_changed, asg_properties=create_autoscaling_group(connection, module)
elif state == 'absent': elif state == 'absent':
changed = delete_autoscaling_group(connection, module) changed = delete_autoscaling_group(connection, module)
module.exit_json( changed = changed ) module.exit_json( changed = changed )
if replace_all_instances or replace_instances: if replace_all_instances or replace_instances:
replace_changed, asg_properties=replace(connection, module) replace_changed, asg_properties=replace(connection, module)
if create_changed or replace_changed: if create_changed or replace_changed:
changed = True changed = True
module.exit_json( changed = changed, **asg_properties ) module.exit_json( changed = changed, **asg_properties )
if __name__ == '__main__':
main() main()

View file

@ -257,7 +257,7 @@ class ElbManager:
try: try:
elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params) elb = connect_to_aws(boto.ec2.elb, self.region, **self.aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
self.module.fail_json(msg=str(e)) self.module.fail_json(msg=str(e))
elbs = [] elbs = []
@ -290,7 +290,7 @@ class ElbManager:
try: try:
asg = connect_to_aws(boto.ec2.autoscale, self.region, **self.aws_connect_params) asg = connect_to_aws(boto.ec2.autoscale, self.region, **self.aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
self.module.fail_json(msg=str(e)) self.module.fail_json(msg=str(e))
asg_instances = asg.get_all_autoscaling_instances([self.instance_id]) asg_instances = asg.get_all_autoscaling_instances([self.instance_id])
@ -314,7 +314,7 @@ class ElbManager:
"""Returns a boto.ec2.InstanceObject for self.instance_id""" """Returns a boto.ec2.InstanceObject for self.instance_id"""
try: try:
ec2 = connect_to_aws(boto.ec2, self.region, **self.aws_connect_params) ec2 = connect_to_aws(boto.ec2, self.region, **self.aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
self.module.fail_json(msg=str(e)) self.module.fail_json(msg=str(e))
return ec2.get_only_instances(instance_ids=[self.instance_id])[0] return ec2.get_only_instances(instance_ids=[self.instance_id])[0]
@ -374,4 +374,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

View file

@ -492,7 +492,7 @@ class ElbManager(object):
try: try:
return connect_to_aws(boto.ec2.elb, self.region, return connect_to_aws(boto.ec2.elb, self.region,
**self.aws_connect_params) **self.aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
self.module.fail_json(msg=str(e)) self.module.fail_json(msg=str(e))
def _delete_elb(self): def _delete_elb(self):
@ -981,4 +981,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

View file

@ -311,7 +311,7 @@ def main():
try: try:
connection = connect_to_aws(boto.ec2.autoscale, region, **aws_connect_params) connection = connect_to_aws(boto.ec2.autoscale, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
state = module.params.get('state') state = module.params.get('state')

View file

@ -115,8 +115,6 @@ EXAMPLES = '''
''' '''
import sys
try: try:
import boto.ec2.cloudwatch import boto.ec2.cloudwatch
from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm
@ -274,7 +272,7 @@ def main():
if region: if region:
try: try:
connection = connect_to_aws(boto.ec2.cloudwatch, region, **aws_connect_params) connection = connect_to_aws(boto.ec2.cloudwatch, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
else: else:
module.fail_json(msg="region must be specified") module.fail_json(msg="region must be specified")
@ -288,4 +286,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

View file

@ -178,7 +178,7 @@ def main():
try: try:
connection = connect_to_aws(boto.ec2.autoscale, region, **aws_connect_params) connection = connect_to_aws(boto.ec2.autoscale, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg = str(e)) module.fail_json(msg = str(e))
if state == 'present': if state == 'present':
@ -187,4 +187,5 @@ def main():
delete_scaling_policy(connection, module) delete_scaling_policy(connection, module)
main() if __name__ == '__main__':
main()

View file

@ -258,8 +258,6 @@ def boto_supports_volume_encryption():
def create_volume(module, ec2, zone): def create_volume(module, ec2, zone):
changed = False changed = False
name = module.params.get('name') name = module.params.get('name')
id = module.params.get('id')
instance = module.params.get('instance')
iops = module.params.get('iops') iops = module.params.get('iops')
encrypted = module.params.get('encrypted') encrypted = module.params.get('encrypted')
volume_size = module.params.get('volume_size') volume_size = module.params.get('volume_size')
@ -397,8 +395,6 @@ def main():
name = module.params.get('name') name = module.params.get('name')
instance = module.params.get('instance') instance = module.params.get('instance')
volume_size = module.params.get('volume_size') volume_size = module.params.get('volume_size')
volume_type = module.params.get('volume_type')
iops = module.params.get('iops')
encrypted = module.params.get('encrypted') encrypted = module.params.get('encrypted')
device_name = module.params.get('device_name') device_name = module.params.get('device_name')
zone = module.params.get('zone') zone = module.params.get('zone')
@ -424,7 +420,7 @@ def main():
if region: if region:
try: try:
ec2 = connect_to_aws(boto.ec2, region, **aws_connect_params) ec2 = connect_to_aws(boto.ec2, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
else: else:
module.fail_json(msg="region must be specified") module.fail_json(msg="region must be specified")
@ -489,4 +485,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

View file

@ -93,9 +93,6 @@ EXAMPLES = '''
''' '''
import time
import sys
try: try:
import boto import boto
import boto.ec2 import boto.ec2
@ -219,7 +216,7 @@ def main():
if region: if region:
try: try:
connection = connect_to_aws(boto.vpc, region, **aws_connect_params) connection = connect_to_aws(boto.vpc, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, StandardError), e: except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
else: else:
module.fail_json(msg="region must be specified") module.fail_json(msg="region must be specified")
@ -253,7 +250,6 @@ def main():
except BotoServerError, e: except BotoServerError, e:
module.fail_json(msg=e) module.fail_json(msg=e)
# Note: Boto currently doesn't currently provide an interface to ec2-describe-vpc-attribute # Note: Boto currently doesn't currently provide an interface to ec2-describe-vpc-attribute
# which is needed in order to detect the current status of DNS options. For now we just update # which is needed in order to detect the current status of DNS options. For now we just update
# the attribute each time and is not used as a changed-factor. # the attribute each time and is not used as a changed-factor.
@ -294,4 +290,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()

View file

@ -112,7 +112,7 @@ except ImportError:
# returns a tuple: (whether or not a parameter was changed, the remaining parameters that weren't found in this parameter group) # returns a tuple: (whether or not a parameter was changed, the remaining parameters that weren't found in this parameter group)
class NotModifiableError(StandardError): class NotModifiableError(Exception):
def __init__(self, error_message, *args): def __init__(self, error_message, *args):
super(NotModifiableError, self).__init__(error_message, *args) super(NotModifiableError, self).__init__(error_message, *args)
self.error_message = error_message self.error_message = error_message
@ -175,7 +175,7 @@ def modify_group(group, params, immediate=False):
new_params = dict(params) new_params = dict(params)
for key in new_params.keys(): for key in new_params.keys():
if group.has_key(key): if key in group:
param = group[key] param = group[key]
new_value = new_params[key] new_value = new_params[key]
@ -281,7 +281,6 @@ def main():
else: else:
break break
except BotoServerError, e: except BotoServerError, e:
module.fail_json(msg = e.error_message) module.fail_json(msg = e.error_message)
@ -297,4 +296,5 @@ def main():
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
main() if __name__ == '__main__':
main()