diff --git a/scone/default/recipes/systemd.py b/scone/default/recipes/systemd.py index 1a5a147..cc221eb 100644 --- a/scone/default/recipes/systemd.py +++ b/scone/default/recipes/systemd.py @@ -18,7 +18,7 @@ from scone.default.steps.systemd_steps import ( cook_systemd_daemon_reload, cook_systemd_enable, - cook_systemd_start, + cook_systemd_start, cook_systemd_stop, ) from scone.head.head import Head from scone.head.kitchen import Kitchen, Preparation @@ -42,15 +42,27 @@ class SystemdUnit(Recipe): unit = check_type(args.get("unit"), str) 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.restart_on = check_type_opt(args.get("restart_on"), list) self.started = check_type_opt(args.get("started"), bool) def prepare(self, preparation: Preparation, head: Head) -> None: super().prepare(preparation, head) - # TODO(potential future): preparation.provides("systemd-unit", self.unit_name) - preparation.needs("file", self.at) + if self.at: + # TODO(potential future): preparation.provides("systemd-unit", + # self.unit_name) + preparation.needs("file", self.at) async def cook(self, kitchen: Kitchen) -> 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: await cook_systemd_start(kitchen, self.unit_name) + else: + await cook_systemd_stop(kitchen, self.unit_name) diff --git a/scone/default/steps/systemd_steps.py b/scone/default/steps/systemd_steps.py index b94d9ec..9c869e6 100644 --- a/scone/default/steps/systemd_steps.py +++ b/scone/default/steps/systemd_steps.py @@ -50,3 +50,12 @@ async def cook_systemd_start(kitchen: Kitchen, unit_name: str): if result.exit_code != 0: 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()}")