modifications to system commands

This commit is contained in:
notplants 2022-07-20 14:26:37 +02:00
parent 6407495292
commit ceb7e502ce
3 changed files with 44 additions and 11 deletions

View File

@ -57,6 +57,7 @@ pub fn get_peach_config_defaults() -> HashMap<String, String> {
("DYN_NAMESERVER", "ns.peachcloud.org"), ("DYN_NAMESERVER", "ns.peachcloud.org"),
("DYN_ENABLED", "false"), ("DYN_ENABLED", "false"),
("SSB_ADMIN_IDS", ""), ("SSB_ADMIN_IDS", ""),
("SYSTEM_MANAGER", "systemd"),
("ADMIN_PASSWORD_HASH", "47"), ("ADMIN_PASSWORD_HASH", "47"),
("TEMPORARY_PASSWORD_HASH", ""), ("TEMPORARY_PASSWORD_HASH", ""),
("GO_SBOT_DATADIR", "/home/peach/.ssb-go"), ("GO_SBOT_DATADIR", "/home/peach/.ssb-go"),

View File

@ -18,6 +18,7 @@ pub enum PeachWebError {
Json(JsonError), Json(JsonError),
OsString, OsString,
PeachLib { source: PeachError, msg: String }, PeachLib { source: PeachError, msg: String },
System(String),
Yaml(YamlError), Yaml(YamlError),
} }
@ -31,6 +32,7 @@ impl std::error::Error for PeachWebError {
PeachWebError::Json(ref source) => Some(source), PeachWebError::Json(ref source) => Some(source),
PeachWebError::OsString => None, PeachWebError::OsString => None,
PeachWebError::PeachLib { ref source, .. } => Some(source), PeachWebError::PeachLib { ref source, .. } => Some(source),
PeachWebError::System(_) => None,
PeachWebError::Yaml(ref source) => Some(source), PeachWebError::Yaml(ref source) => Some(source),
} }
} }
@ -54,6 +56,9 @@ impl std::fmt::Display for PeachWebError {
"Filesystem error: failed to convert OsString to String for go-ssb directory path" "Filesystem error: failed to convert OsString to String for go-ssb directory path"
), ),
PeachWebError::PeachLib { ref source, .. } => write!(f, "{}", source), PeachWebError::PeachLib { ref source, .. } => write!(f, "{}", source),
PeachWebError::System(ref msg) => {
write!(f, "system error: {}", msg)
}
PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source), PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source),
} }
} }

View File

@ -24,23 +24,50 @@ use crate::{error::PeachWebError, utils::sbot};
// SBOT HELPER FUNCTIONS // SBOT HELPER FUNCTIONS
/// Executes a systemctl command for the go-sbot.service process. /// On non-docker based deployments (peachcloud, yunohost), we use systemctl
pub fn systemctl_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> { /// On docker-based deployments, we use supervisord
let output = Command::new("sudo") /// This utility function calls the correct system calls based on these parameters.
.arg("systemctl") pub fn system_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> {
.arg(cmd) let system_manager = config_manager::get_config_value("SYSTEM_MANAGER")?;
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?) match system_manager.as_str() {
.output()?; "systemd" => {
Ok(output) let output = Command::new("sudo")
.arg("systemctl")
.arg(cmd)
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.output()?;
Ok(output)
},
"supervisord" => {
match cmd {
"enable" => {
// TODO: implement this
},
"disable" => {
// TODO: implement this
},
_ => {
let output = Command::new("supervisorctl")
.arg(cmd)
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.output()?;
Ok(output)
}
}
},
_ => {
PeachWebError::System(format!("Invalid configuration for SYSTEM_MANAGER: {:?}", system_manager))
}
}
} }
/// Executes a systemctl stop command followed by start command. /// Executes a system stop command followed by start command.
/// Returns a redirect with a flash message stating the output of the restart attempt. /// Returns a redirect with a flash message stating the output of the restart attempt.
pub fn restart_sbot_process() -> (String, String) { pub fn restart_sbot_process() -> (String, String) {
debug!("Restarting go-sbot.service"); debug!("Restarting go-sbot.service");
match systemctl_sbot_cmd("stop") { match system_sbot_cmd("stop") {
// if stop was successful, try to start the process // if stop was successful, try to start the process
Ok(_) => match systemctl_sbot_cmd("start") { Ok(_) => match system_sbot_cmd("start") {
Ok(_) => ( Ok(_) => (
"success".to_string(), "success".to_string(),
"Updated configuration and restarted the sbot process".to_string(), "Updated configuration and restarted the sbot process".to_string(),