From e7e47ca1fcd42dff151ca55501f38b3c65eb61f6 Mon Sep 17 00:00:00 2001 From: Viktor Utkin Date: Thu, 7 Feb 2019 19:08:19 +0300 Subject: [PATCH] [Backport 2.7] spot instance request stay opened when module exit with timeout Fixes: #51534 * set valid_until equal to current time + spot_wait_timeout * add setting ValidUntil to value * add changelog fragment * fix shebang issue (cherry picked from commit d40f0313e2b1c6cf91ada4cb35e19a24ef404675) --- .../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 ceb28f0dde4..20c23b472d4 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -539,6 +539,7 @@ EXAMPLES = ''' ''' import time +import datetime import traceback from ast import literal_eval from distutils.version import LooseVersion @@ -1190,6 +1191,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