Add ability to use pre-existing sysd units and stop them too
This commit is contained in:
parent
17f6ce6eac
commit
1b202f0dd4
@ -18,7 +18,7 @@
|
|||||||
from scone.default.steps.systemd_steps import (
|
from scone.default.steps.systemd_steps import (
|
||||||
cook_systemd_daemon_reload,
|
cook_systemd_daemon_reload,
|
||||||
cook_systemd_enable,
|
cook_systemd_enable,
|
||||||
cook_systemd_start,
|
cook_systemd_start, cook_systemd_stop,
|
||||||
)
|
)
|
||||||
from scone.head.head import Head
|
from scone.head.head import Head
|
||||||
from scone.head.kitchen import Kitchen, Preparation
|
from scone.head.kitchen import Kitchen, Preparation
|
||||||
@ -42,15 +42,27 @@ class SystemdUnit(Recipe):
|
|||||||
|
|
||||||
unit = check_type(args.get("unit"), str)
|
unit = check_type(args.get("unit"), str)
|
||||||
self.unit_name = unit if "." in unit else unit + ".service"
|
self.unit_name = unit if "." in unit else unit + ".service"
|
||||||
self.at = check_type(args.get("at"), str)
|
already_installed = check_type(args.get("already_installed", False), bool)
|
||||||
|
self.at = check_type_opt(args.get("at", None), str)
|
||||||
|
|
||||||
|
if not (already_installed or self.at):
|
||||||
|
# already_installed is for when the unit already exists on the system
|
||||||
|
# and is not created by scone.
|
||||||
|
raise ValueError(
|
||||||
|
"Must supply either already_installed = true or "
|
||||||
|
f"at = /path/to/{self.unit_name}"
|
||||||
|
)
|
||||||
|
|
||||||
self.enabled = check_type_opt(args.get("enabled"), bool)
|
self.enabled = check_type_opt(args.get("enabled"), bool)
|
||||||
self.restart_on = check_type_opt(args.get("restart_on"), list)
|
self.restart_on = check_type_opt(args.get("restart_on"), list)
|
||||||
self.started = check_type_opt(args.get("started"), bool)
|
self.started = check_type_opt(args.get("started"), bool)
|
||||||
|
|
||||||
def prepare(self, preparation: Preparation, head: Head) -> None:
|
def prepare(self, preparation: Preparation, head: Head) -> None:
|
||||||
super().prepare(preparation, head)
|
super().prepare(preparation, head)
|
||||||
# TODO(potential future): preparation.provides("systemd-unit", self.unit_name)
|
if self.at:
|
||||||
preparation.needs("file", self.at)
|
# TODO(potential future): preparation.provides("systemd-unit",
|
||||||
|
# self.unit_name)
|
||||||
|
preparation.needs("file", self.at)
|
||||||
|
|
||||||
async def cook(self, kitchen: Kitchen) -> None:
|
async def cook(self, kitchen: Kitchen) -> None:
|
||||||
if self.enabled is not None or self.started is not None:
|
if self.enabled is not None or self.started is not None:
|
||||||
@ -62,3 +74,5 @@ class SystemdUnit(Recipe):
|
|||||||
if self.started is not None:
|
if self.started is not None:
|
||||||
if self.started:
|
if self.started:
|
||||||
await cook_systemd_start(kitchen, self.unit_name)
|
await cook_systemd_start(kitchen, self.unit_name)
|
||||||
|
else:
|
||||||
|
await cook_systemd_stop(kitchen, self.unit_name)
|
||||||
|
@ -50,3 +50,12 @@ async def cook_systemd_start(kitchen: Kitchen, unit_name: str):
|
|||||||
|
|
||||||
if result.exit_code != 0:
|
if result.exit_code != 0:
|
||||||
raise RuntimeError(f"Failed to start {unit_name}: {result.stderr.decode()}")
|
raise RuntimeError(f"Failed to start {unit_name}: {result.stderr.decode()}")
|
||||||
|
|
||||||
|
|
||||||
|
async def cook_systemd_stop(kitchen: Kitchen, unit_name: str):
|
||||||
|
result = await kitchen.ut1areq(
|
||||||
|
SimpleExec(["systemctl", "stop", unit_name], "/",), SimpleExec.Result,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.exit_code != 0:
|
||||||
|
raise RuntimeError(f"Failed to stop {unit_name}: {result.stderr.decode()}")
|
||||||
|
Loading…
Reference in New Issue
Block a user