Skip to content

Commit

Permalink
error out if spot request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nchammas committed Feb 20, 2016
1 parent e163743 commit d5b086c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions flintrock/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ def get_ec2_block_device_mappings(
ec2 = boto3.resource(service_name='ec2', region_name=region)
block_device_mappings = []

# An IndexError here is probably a sign of this problem:
# https://github.com/boto/boto3/issues/496
image = list(
ec2.images.filter(ImageIds=[ami]))[0]

Expand Down Expand Up @@ -453,9 +455,19 @@ def launch(
time.sleep(30)
spot_requests = client.describe_spot_instance_requests(
SpotInstanceRequestIds=request_ids)['SpotInstanceRequests']

failed_requests = [r for r in spot_requests if r['State'] == 'failed']
if failed_requests:
failure_reasons = {r['Status']['Code'] for r in failed_requests}
raise Error(
"The spot request failed for the following reason{s}: {reasons}"
.format(
s='' if len(failure_reasons) == 1 else 's',
reasons=', '.join(failure_reasons)))

pending_request_ids = [
r['SpotInstanceRequestId'] for r in spot_requests
if r['State'] != 'active']
if r['State'] == 'open']

print("All {c} instances granted.".format(c=num_instances))

Expand Down Expand Up @@ -518,7 +530,7 @@ def launch(

except (Exception, KeyboardInterrupt) as e:
# TODO: Cleanup cluster security group here.
print(e, file=sys.stderr)
print("There was a problem with the launch. Cleaning up...", file=sys.stderr)

if spot_requests:
request_ids = [r['SpotInstanceRequestId'] for r in spot_requests]
Expand Down

0 comments on commit d5b086c

Please sign in to comment.