From b451cbd37b7e9b02fcd051efa919d7411cd2fa9c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Barth Date: Tue, 6 Jan 2015 17:10:11 +0100 Subject: [PATCH 1/2] EC2: move logic about terminated instances up (#423) As stated in #423, the commit 7f11c3d broke ec2 spot instance launching after 1.7.2. This is because it acts on the 'res' variable which have 2 different types in the method, and in case we request spot instances, the resulting object is not a result of ec2.run_instances() but ec2.request_spot_instances(). Actually this fix doesn't seem to be relevant in the spot instances case, because by construction we won't retrieve 'terminated' instances in the end. --- cloud/amazon/ec2.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) mode change 100644 => 100755 cloud/amazon/ec2.py diff --git a/cloud/amazon/ec2.py b/cloud/amazon/ec2.py old mode 100644 new mode 100755 index 93b496cb5e8..1d58721bfe3 --- a/cloud/amazon/ec2.py +++ b/cloud/amazon/ec2.py @@ -915,6 +915,17 @@ def create_instances(module, ec2, override_count=None): continue else: module.fail_json(msg = str(e)) + + # The instances returned through ec2.run_instances above can be in + # terminated state due to idempotency. See commit 7f11c3d for a complete + # explanation. + terminated_instances = [ str(instance.id) for instance in res.instances + if instance.state == 'terminated' ] + if terminated_instances: + module.fail_json(msg = "Instances with id(s) %s " % terminated_instances + + "were created previously but have since been terminated - " + + "use a (possibly different) 'instanceid' parameter") + else: if private_ip: module.fail_json( @@ -952,15 +963,6 @@ def create_instances(module, ec2, override_count=None): except boto.exception.BotoServerError, e: module.fail_json(msg = "Instance creation failed => %s: %s" % (e.error_code, e.error_message)) - # The instances returned through run_instances can be in - # terminated state due to idempotency. - terminated_instances = [ str(instance.id) for instance in res.instances - if instance.state == 'terminated' ] - if terminated_instances: - module.fail_json(msg = "Instances with id(s) %s " % terminated_instances + - "were created previously but have since been terminated - " + - "use a (possibly different) 'instanceid' parameter") - # wait here until the instances are up num_running = 0 wait_timeout = time.time() + wait_timeout From 6090c4e109995929ab175e7795868f1b5b882eb2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Barth Date: Tue, 6 Jan 2015 17:34:57 +0100 Subject: [PATCH 2/2] Improve formatting after previous commit --- cloud/amazon/ec2.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cloud/amazon/ec2.py b/cloud/amazon/ec2.py index 1d58721bfe3..d34931c9914 100755 --- a/cloud/amazon/ec2.py +++ b/cloud/amazon/ec2.py @@ -919,12 +919,13 @@ def create_instances(module, ec2, override_count=None): # The instances returned through ec2.run_instances above can be in # terminated state due to idempotency. See commit 7f11c3d for a complete # explanation. - terminated_instances = [ str(instance.id) for instance in res.instances - if instance.state == 'terminated' ] + terminated_instances = [ + str(instance.id) for instance in res.instances if instance.state == 'terminated' + ] if terminated_instances: module.fail_json(msg = "Instances with id(s) %s " % terminated_instances + - "were created previously but have since been terminated - " + - "use a (possibly different) 'instanceid' parameter") + "were created previously but have since been terminated - " + + "use a (possibly different) 'instanceid' parameter") else: if private_ip: