From d40f0313e2b1c6cf91ada4cb35e19a24ef404675 Mon Sep 17 00:00:00 2001 From: Viktor Utkin Date: Thu, 7 Feb 2019 19:08:19 +0300 Subject: [PATCH] spot instance request stay opened when module exit with timeout (#51535) Fixes: #51534 * set valid_until equal to current time + spot_wait_timeout * add setting ValidUntil to value * add changelog fragment * fix shebang issue --- .../fragments/51535-ec2-fix-spot-request-end-date.yaml | 2 ++ lib/ansible/modules/cloud/amazon/ec2.py | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml diff --git a/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml b/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml new file mode 100644 index 00000000000..8fbe2c09f4d --- /dev/null +++ b/changelogs/fragments/51535-ec2-fix-spot-request-end-date.yaml @@ -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. diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index 1de6ccea4f1..f7130b723c7 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -540,6 +540,7 @@ EXAMPLES = ''' ''' import time +import datetime import traceback from ast import literal_eval from distutils.version import LooseVersion @@ -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