Skip to content

Commit 085c7a2

Browse files
committed
ec2: Fix --allow-recreate not recreating terminated spot instances.
Until now, `nixops deploy --check --allow-recreate` would fail to recreate terminated spot instances, instead emitting the error: Exception: spot instance request got fulfilled unexpectedly as instance ‘i-0543dd70c68337dea’ This was because `_reset_state()` did not reset the `self._cached_instance`, and so the `_get_instance()` call in `create_instance()` would continue to refer to the ID of the disappeared instance, eventually producing the error.
1 parent ce9e0ae commit 085c7a2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

nixops_aws/backends/ec2.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ def _reset_state(self):
201201
self.spot_instance_request_id = None
202202
self.spot_instance_price = None
203203

204+
# The `_cached_instance` also contains an instance `.id`;
205+
# reset it just as we reset `vm_id`, otherwise `_get_instance()`
206+
# will incorrectly refer to the old one after resetting.
207+
self._cached_instance = None
208+
204209
def get_ssh_name(self):
205210
retVal = None
206211
if self.use_private_ip_address:
@@ -907,6 +912,8 @@ def _wait_for_spot_request_fulfillment(self, request_id):
907912
time.sleep(3)
908913
self.log_end("")
909914

915+
assert self._cached_instance is None
916+
910917
instance = self._retry(
911918
lambda: self._get_instance(instance_id=request.instance_id)
912919
)
@@ -1031,8 +1038,9 @@ def _cancel_spot_request(self):
10311038
if request.instance_id is not None and request.instance_id != self.vm_id:
10321039
if self.vm_id is not None:
10331040
raise Exception(
1034-
"spot instance request got fulfilled unexpectedly as instance ‘{0}’".format(
1035-
request.instance_id
1041+
"spot instance request got fulfilled unexpectedly as instance ‘{0}’; expected ‘{1}’".format(
1042+
request.instance_id,
1043+
self.vm_id,
10361044
)
10371045
)
10381046
self.vm_id = request.instance_id

0 commit comments

Comments
 (0)