Skip to content

ec2: Usinng boto3 in operations: stop/start/reboot/console-output #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions nixops/backends/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ def destroy(self, wipe=False):
# The latter allows us to destroy instances that were "leaked"
# in create() due to it being interrupted after the instance
# was created but before it registered the ID in the database.
self.connect()
self.connect_boto3()
instance = None
if self.vm_id:
instance = self._get_instance(allow_missing=True)
Expand All @@ -1354,7 +1354,7 @@ def destroy(self, wipe=False):
instance = reservations[0].instances[0]

if instance:
instance.terminate()
self._conn_boto3.terminate_instances(InstanceIds=[instance.id])

# Wait until it's really terminated.
while True:
Expand Down Expand Up @@ -1386,8 +1386,9 @@ def stop(self):

self.log_start("stopping EC2 machine... ")

self.connect_boto3()
instance = self._get_instance()
instance.stop() # no-op if the machine is already stopped
self._conn_boto3.stop_instances(InstanceIds=[instance.id]) # no-op if the machine is already stopped

self.state = self.STOPPING

Expand Down Expand Up @@ -1426,10 +1427,9 @@ def start(self):
return

self.log("starting EC2 machine...")

self.connect_boto3()
instance = self._get_instance()
instance.start() # no-op if the machine is already started

self._conn_boto3.start_instances(InstanceIds=[instance.id]) # no-op if the machine is already started
self.state = self.STARTING

# Wait until it's really started, and obtain its new IP
Expand Down Expand Up @@ -1519,15 +1519,16 @@ def _check(self, res):
def reboot(self, hard=False):
self.log("rebooting EC2 machine...")
instance = self._get_instance()
instance.reboot()
self.connect_boto3()
self._conn_boto3.reboot_instances(InstanceIds=[instance.id])
self.state = self.STARTING


def get_console_output(self):
if not self.vm_id:
raise Exception("cannot get console output of non-existant machine ‘{0}’".format(self.name))
self.connect()
return self._conn.get_console_output(self.vm_id).output or "(not available)"
self.connect_boto3()
return self._conn_boto3.get_console_output(InstanceId=self.vm_id)['Output'] or "(not available)"


def next_charge_time(self):
Expand All @@ -1539,4 +1540,4 @@ def next_charge_time(self):

def sorted_block_device_mapping(self):
"""In order to preserve nvme devices names volumes should be attached in lexicographic order (ordered by device name)."""
return sorted(self.block_device_mapping.items())
return sorted(self.block_device_mapping.items())