Skip to content

Commit

Permalink
spot instance request stay opened when module exit with timeout (ansi…
Browse files Browse the repository at this point in the history
…ble#51535)

Fixes: ansible#51534

* set valid_until equal to current time + spot_wait_timeout
* add setting ValidUntil to  value
* add changelog fragment
* fix shebang issue
  • Loading branch information
vutkin authored and Akasurde committed Feb 7, 2019
1 parent 890394a commit d40f031
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2 - Correctly sets the end date of the Spot Instance request. Sets `ValidUntil` value in proper way so it will be auto-canceled through `spot_wait_timeout` interval.
10 changes: 10 additions & 0 deletions lib/ansible/modules/cloud/amazon/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
'''

import time
import datetime
import traceback
from ast import literal_eval
from distutils.version import LooseVersion
Expand Down Expand Up @@ -1191,6 +1192,15 @@ def create_instances(module, ec2, vpc, override_count=None):
count=count_remaining,
type=spot_type,
))

# Set spot ValidUntil
# ValidUntil -> (timestamp). The end date of the request, in
# UTC format (for example, YYYY -MM -DD T*HH* :MM :SS Z).
utc_valid_until = (
datetime.datetime.utcnow()
+ datetime.timedelta(seconds=spot_wait_timeout))
params['valid_until'] = utc_valid_until.strftime('%Y-%m-%dT%H:%M:%S.000Z')

res = ec2.request_spot_instances(spot_price, **params)

# Now we have to do the intermediate waiting
Expand Down

0 comments on commit d40f031

Please sign in to comment.