src/tools/systemd.rs: implement daemon_reload, start_unit, stop_unit and enable_unit

This commit is contained in:
Dietmar Maurer 2020-06-10 08:54:41 +02:00
parent fc6047fcb1
commit 669c137fec
1 changed files with 45 additions and 0 deletions

View File

@ -7,6 +7,8 @@ pub mod time;
use anyhow::{bail, Error}; use anyhow::{bail, Error};
pub const SYSTEMCTL_BIN_PATH: &str = "/usr/bin/systemctl";
/// Escape strings for usage in systemd unit names /// Escape strings for usage in systemd unit names
pub fn escape_unit(mut unit: &str, is_path: bool) -> String { pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
@ -72,3 +74,46 @@ pub fn unescape_unit(text: &str) -> Result<String, Error> {
Ok(text) Ok(text)
} }
pub fn reload_daemon() -> Result<(), Error> {
let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH);
command.arg("daemon-reload");
crate::tools::run_command(command, None)?;
Ok(())
}
pub fn enable_unit(unit: &str) -> Result<(), Error> {
let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH);
command.arg("enable");
command.arg(unit);
crate::tools::run_command(command, None)?;
Ok(())
}
pub fn start_unit(unit: &str) -> Result<(), Error> {
let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH);
command.arg("start");
command.arg(unit);
crate::tools::run_command(command, None)?;
Ok(())
}
pub fn stop_unit(unit: &str) -> Result<(), Error> {
let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH);
command.arg("stop");
command.arg(unit);
crate::tools::run_command(command, None)?;
Ok(())
}