Split up Linux Daemon disable and stop (#60)

* Split up Linux Daemon disable and stop

* Detect state and act on it

* Fixup pathes

* Handle linked
This commit is contained in:
Ana Hobden 2022-11-21 12:54:32 -08:00 committed by GitHub
parent b06ad289fc
commit 7255c7e5a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -207,21 +207,52 @@ impl Action for ConfigureNixDaemonService {
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
}, },
_ => { _ => {
execute_command( // We separate stop and disable (instead of using `--now`) to avoid cases where the service isn't started, but is enabled.
Command::new("systemctl")
.args(["disable", SOCKET_SRC, "--now"])
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
let socket_is_active = is_active("nix-daemon.socket").await?;
let socket_is_enabled = is_enabled("nix-daemon.socket").await?;
let service_is_active = is_active("nix-daemon.service").await?;
let service_is_enabled = is_enabled("nix-daemon.service").await?;
if socket_is_active {
execute_command( execute_command(
Command::new("systemctl") Command::new("systemctl")
.args(["disable", SERVICE_SRC, "--now"]) .args(["stop", "nix-daemon.socket"])
.stdin(std::process::Stdio::null()), .stdin(std::process::Stdio::null()),
) )
.await .await
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
}
if socket_is_enabled {
execute_command(
Command::new("systemctl")
.args(["disable", "nix-daemon.socket"])
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
}
if service_is_active {
execute_command(
Command::new("systemctl")
.args(["stop", "nix-daemon.service"])
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
}
if service_is_enabled {
execute_command(
Command::new("systemctl")
.args(["disable", "nix-daemon.service"])
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?;
}
execute_command( execute_command(
Command::new("systemd-tmpfiles") Command::new("systemd-tmpfiles")
@ -278,3 +309,34 @@ pub enum ConfigureNixDaemonServiceError {
#[error("No supported init system found")] #[error("No supported init system found")]
InitNotSupported, InitNotSupported,
} }
async fn is_active(unit: &str) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
let output = Command::new("systemctl")
.arg("is-active")
.arg(unit)
.output()
.await?;
if String::from_utf8(output.stdout)?.starts_with("active") {
tracing::trace!(%unit, "Is active");
Ok(true)
} else {
tracing::trace!(%unit, "Is not active");
Ok(false)
}
}
async fn is_enabled(unit: &str) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
let output = Command::new("systemctl")
.arg("is-enabled")
.arg(unit)
.output()
.await?;
let stdout = String::from_utf8(output.stdout)?;
if stdout.starts_with("enabled") || stdout.starts_with("linked") {
tracing::trace!(%unit, "Is enabled");
Ok(true)
} else {
tracing::trace!(%unit, "Is not enabled");
Ok(false)
}
}