Skip to content

Commit

Permalink
Add stop hibernate operation
Browse files Browse the repository at this point in the history
  • Loading branch information
PsyanticY committed Apr 30, 2019
1 parent ccd3f69 commit 226ec83
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion nixops/backends/azure_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def start(self):
self.wait_for_ssh(check=True)
self.send_keys()

def stop(self):
def stop(self, hibernate=False):
if self.vm_id:
#FIXME: there's also "stopped deallocated" version of this. how to integrate?
self.log("stopping Azure machine... ")
Expand Down
2 changes: 1 addition & 1 deletion nixops/backends/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def destroy(self, wipe=False):

return True

def stop(self):
def stop(self, hibernate=False):
if not self.vm_id: return True
self.log("stopping container...")
self.state = self.STOPPING
Expand Down
24 changes: 18 additions & 6 deletions nixops/backends/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,18 +1406,30 @@ def destroy(self, wipe=False):
return True


def stop(self):
def stop(self, hibernate=False):
if not self._booted_from_ebs():
self.warn("cannot stop non-EBS-backed instance")
return

if not self.depl.logger.confirm("are you sure you want to stop machine '{}'".format(self.name)):
return
self.connect_boto3()
instance = self._get_instance()

self.log_start("stopping EC2 machine... ")
if hibernate:
if not self.depl.logger.confirm("are you sure you want to stop hibernate machine '{}'".format(self.name)):
return

instance = self._get_instance()
instance.stop() # no-op if the machine is already stopped
self.log_start("stopping EC2 machine... ")

self._conn_boto3.stop_instances(InstanceIds=[instance.id], Hibernate=hibernate)
# no-op if the machine is already hibernated
else:
if not self.depl.logger.confirm("are you sure you want to stop machine '{}'".format(self.name)):
return

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

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

self.state = self.STOPPING

Expand Down
2 changes: 1 addition & 1 deletion nixops/backends/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def start(self):
self.send_keys()


def stop(self):
def stop(self, hibernate=False):
if not self.vm_id: return

try:
Expand Down
2 changes: 1 addition & 1 deletion nixops/backends/hetzner.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def _wait_stop(self):

self.state = self.STOPPED

def stop(self):
def stop(self, hibernate=False):
"""
Stops the server by shutting it down without powering it off.
"""
Expand Down
2 changes: 1 addition & 1 deletion nixops/backends/libvirtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def get_ssh_name(self):
self.private_ipv4 = self._parse_ip()
return self.private_ipv4

def stop(self):
def stop(self, hibernate=False):
assert self.vm_id
if self._is_running():
self.log_start("shutting down... ")
Expand Down
2 changes: 1 addition & 1 deletion nixops/backends/virtualbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def destroy(self, wipe=False):
return True


def stop(self):
def stop(self, hibernate=False):
state = self._get_vm_state()

if state not in ['poweroff', 'aborted']:
Expand Down
4 changes: 2 additions & 2 deletions nixops/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,12 +1178,12 @@ def worker(m):
nixops.parallel.run_tasks(nr_workers=-1, tasks=self.active.itervalues(), worker_fun=worker)


def stop_machines(self, include=[], exclude=[]):
def stop_machines(self, include=[], exclude=[], hibernate=False):
"""Stop all active machines."""

def worker(m):
if not should_do(m, include, exclude): return
m.stop()
m.stop(hibernate=hibernate)

nixops.parallel.run_tasks(nr_workers=-1, tasks=self.active.itervalues(), worker_fun=worker)

Expand Down
5 changes: 4 additions & 1 deletion scripts/nixops
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ def op_stop():
depl = open_deployment()
if args.confirm:
depl.logger.set_autoresponse("y")
depl.stop_machines(include=args.include or [], exclude=args.exclude or [])
depl.stop_machines(include=args.include or [],
exclude=args.exclude or [],
hibernate=args.hibernate)


def op_start():
Expand Down Expand Up @@ -850,6 +852,7 @@ subparser = add_subparser('stop', help='stop all virtual machines in the network
subparser.set_defaults(op=op_stop)
subparser.add_argument('--include', nargs='+', metavar='MACHINE-NAME', help='stop only the specified machines')
subparser.add_argument('--exclude', nargs='+', metavar='MACHINE-NAME', help='stop all except the specified machines')
subparser.add_argument('--hibernate', action='store_true', default=False, help='hibernate then stop the specified machines')

subparser = add_subparser('start', help='start all virtual machines in the network')
subparser.set_defaults(op=op_start)
Expand Down

0 comments on commit 226ec83

Please sign in to comment.