summaryrefslogtreecommitdiff
path: root/cloudinit/distros
diff options
context:
space:
mode:
authorEmmanuel Thomé <Emmanuel.Thome@inria.fr>2020-09-15 21:51:52 +0200
committerGitHub <noreply@github.com>2020-09-15 15:51:52 -0400
commit6d332e5c8dbfb6521a530b1fa49d73da51efff96 (patch)
tree501e0dfcead71b501f1a1be1fc08df7c988975d4 /cloudinit/distros
parent839016e3014d783354bc380799d914ff81ee4efa (diff)
downloadvyos-cloud-init-6d332e5c8dbfb6521a530b1fa49d73da51efff96.tar.gz
vyos-cloud-init-6d332e5c8dbfb6521a530b1fa49d73da51efff96.zip
create a shutdown_command method in distro classes (#567)
Under FreeBSD, we want to use "shutdown -p" for poweroff. Alpine Linux also has some specificities. We choose to define a method that returns the shutdown command line to use, rather than a method that actually does the shutdown. This makes it easier to have the tests in test_handler_power_state do their verifications. Two tests are added for the special behaviours that are known so far.
Diffstat (limited to 'cloudinit/distros')
-rwxr-xr-xcloudinit/distros/__init__.py19
-rw-r--r--cloudinit/distros/alpine.py26
-rw-r--r--cloudinit/distros/bsd.py4
3 files changed, 49 insertions, 0 deletions
diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py
index 2bd9bae8..fac8cf67 100755
--- a/cloudinit/distros/__init__.py
+++ b/cloudinit/distros/__init__.py
@@ -73,6 +73,9 @@ class Distro(metaclass=abc.ABCMeta):
renderer_configs = {}
_preferred_ntp_clients = None
networking_cls = LinuxNetworking
+ # This is used by self.shutdown_command(), and can be overridden in
+ # subclasses
+ shutdown_options_map = {'halt': '-H', 'poweroff': '-P', 'reboot': '-r'}
def __init__(self, name, cfg, paths):
self._paths = paths
@@ -750,6 +753,22 @@ class Distro(metaclass=abc.ABCMeta):
subp.subp(['usermod', '-a', '-G', name, member])
LOG.info("Added user '%s' to group '%s'", member, name)
+ def shutdown_command(self, *, mode, delay, message):
+ # called from cc_power_state_change.load_power_state
+ command = ["shutdown", self.shutdown_options_map[mode]]
+ try:
+ if delay != "now":
+ delay = "+%d" % int(delay)
+ except ValueError as e:
+ raise TypeError(
+ "power_state[delay] must be 'now' or '+m' (minutes)."
+ " found '%s'." % (delay,)
+ ) from e
+ args = command + [delay]
+ if message:
+ args.append(message)
+ return args
+
def _apply_hostname_transformations_to_url(url: str, transformations: list):
"""
diff --git a/cloudinit/distros/alpine.py b/cloudinit/distros/alpine.py
index e42443fc..e92ff3fb 100644
--- a/cloudinit/distros/alpine.py
+++ b/cloudinit/distros/alpine.py
@@ -162,4 +162,30 @@ class Distro(distros.Distro):
return self._preferred_ntp_clients
+ def shutdown_command(self, mode='poweroff', delay='now', message=None):
+ # called from cc_power_state_change.load_power_state
+ # Alpine has halt/poweroff/reboot, with the following specifics:
+ # - we use them rather than the generic "shutdown"
+ # - delay is given with "-d [integer]"
+ # - the integer is in seconds, cannot be "now", and takes no "+"
+ # - no message is supported (argument ignored, here)
+
+ command = [mode, "-d"]
+
+ # Convert delay from minutes to seconds, as Alpine's
+ # halt/poweroff/reboot commands take seconds rather than minutes.
+ if delay == "now":
+ # Alpine's commands do not understand "now".
+ command += ['0']
+ else:
+ try:
+ command.append(str(int(delay) * 60))
+ except ValueError as e:
+ raise TypeError(
+ "power_state[delay] must be 'now' or '+m' (minutes)."
+ " found '%s'." % (delay,)
+ ) from e
+
+ return command
+
# vi: ts=4 expandtab
diff --git a/cloudinit/distros/bsd.py b/cloudinit/distros/bsd.py
index 2ed7a7d5..f717a667 100644
--- a/cloudinit/distros/bsd.py
+++ b/cloudinit/distros/bsd.py
@@ -17,6 +17,10 @@ class BSD(distros.Distro):
hostname_conf_fn = '/etc/rc.conf'
rc_conf_fn = "/etc/rc.conf"
+ # This differs from the parent Distro class, which has -P for
+ # poweroff.
+ shutdown_options_map = {'halt': '-H', 'poweroff': '-p', 'reboot': '-r'}
+
# Set in BSD distro subclasses
group_add_cmd_prefix = []
pkg_cmd_install_prefix = []