summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_power_state_change.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/config/cc_power_state_change.py')
-rw-r--r--cloudinit/config/cc_power_state_change.py58
1 files changed, 9 insertions, 49 deletions
diff --git a/cloudinit/config/cc_power_state_change.py b/cloudinit/config/cc_power_state_change.py
index 6fcb8a7d..5780a7e9 100644
--- a/cloudinit/config/cc_power_state_change.py
+++ b/cloudinit/config/cc_power_state_change.py
@@ -22,7 +22,7 @@ The ``delay`` key specifies a duration to be added onto any shutdown command
used. Therefore, if a 5 minute delay and a 120 second shutdown are specified,
the maximum amount of time between cloud-init starting and the system shutting
down is 7 minutes, and the minimum amount of time is 5 minutes. The ``delay``
-key must have an argument in either the form ``+5`` for 5 minutes or ``now``
+key must have an argument in either the form ``'+5'`` for 5 minutes or ``now``
for immediate shutdown.
Optionally, a command can be run to determine whether or not
@@ -117,7 +117,7 @@ def check_condition(cond, log=None):
def handle(_name, cfg, cloud, log, _args):
try:
- (args, timeout, condition) = load_power_state(cfg, cloud.distro.name)
+ (args, timeout, condition) = load_power_state(cfg, cloud.distro)
if args is None:
log.debug("no power_state provided. doing nothing")
return
@@ -144,19 +144,7 @@ def handle(_name, cfg, cloud, log, _args):
condition, execmd, [args, devnull_fp])
-def convert_delay(delay, fmt=None, scale=None):
- if not fmt:
- fmt = "+%s"
- if not scale:
- scale = 1
-
- if delay != "now":
- delay = fmt % int(int(delay) * int(scale))
-
- return delay
-
-
-def load_power_state(cfg, distro_name):
+def load_power_state(cfg, distro):
# returns a tuple of shutdown_command, timeout
# shutdown_command is None if no config found
pstate = cfg.get('power_state')
@@ -167,44 +155,16 @@ def load_power_state(cfg, distro_name):
if not isinstance(pstate, dict):
raise TypeError("power_state is not a dict.")
- opt_map = {'halt': '-H', 'poweroff': '-P', 'reboot': '-r'}
-
+ modes_ok = ['halt', 'poweroff', 'reboot']
mode = pstate.get("mode")
- if mode not in opt_map:
+ if mode not in distro.shutdown_options_map:
raise TypeError(
"power_state[mode] required, must be one of: %s. found: '%s'." %
- (','.join(opt_map.keys()), mode))
-
- delay = pstate.get("delay", "now")
- message = pstate.get("message")
- scale = 1
- fmt = "+%s"
- command = ["shutdown", opt_map[mode]]
-
- if distro_name == 'alpine':
- # Convert integer 30 or string '30' to '1800' (seconds) as Alpine's
- # halt/poweroff/reboot commands take seconds rather than minutes.
- scale = 60
- # No "+" in front of delay value as not supported by Alpine's commands.
- fmt = "%s"
- if delay == "now":
- # Alpine's commands do not understand "now".
- delay = "0"
- command = [mode, "-d"]
- # Alpine's commands don't support a message.
- message = None
-
- try:
- delay = convert_delay(delay, fmt=fmt, scale=scale)
- except ValueError as e:
- raise TypeError(
- "power_state[delay] must be 'now' or '+m' (minutes)."
- " found '%s'." % delay
- ) from e
+ (','.join(modes_ok), mode))
- args = command + [delay]
- if message:
- args.append(message)
+ args = distro.shutdown_command(mode=mode,
+ delay=pstate.get("delay", "now"),
+ message=pstate.get("message"))
try:
timeout = float(pstate.get('timeout', 30.0))