diff --git a/peach-lib/src/config_manager.rs b/peach-lib/src/config_manager.rs index 49d6d38..f4e460a 100644 --- a/peach-lib/src/config_manager.rs +++ b/peach-lib/src/config_manager.rs @@ -57,6 +57,7 @@ pub fn get_peach_config_defaults() -> HashMap { ("DYN_NAMESERVER", "ns.peachcloud.org"), ("DYN_ENABLED", "false"), ("SSB_ADMIN_IDS", ""), + ("SYSTEM_MANAGER", "systemd"), ("ADMIN_PASSWORD_HASH", "47"), ("TEMPORARY_PASSWORD_HASH", ""), ("GO_SBOT_DATADIR", "/home/peach/.ssb-go"), diff --git a/peach-web/src/error.rs b/peach-web/src/error.rs index 0d5453b..f3a78f6 100644 --- a/peach-web/src/error.rs +++ b/peach-web/src/error.rs @@ -18,6 +18,7 @@ pub enum PeachWebError { Json(JsonError), OsString, PeachLib { source: PeachError, msg: String }, + System(String), Yaml(YamlError), } @@ -31,6 +32,7 @@ impl std::error::Error for PeachWebError { PeachWebError::Json(ref source) => Some(source), PeachWebError::OsString => None, PeachWebError::PeachLib { ref source, .. } => Some(source), + PeachWebError::System(_) => None, 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" ), 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), } } diff --git a/peach-web/src/utils/sbot.rs b/peach-web/src/utils/sbot.rs index 24c1b15..c3310a5 100644 --- a/peach-web/src/utils/sbot.rs +++ b/peach-web/src/utils/sbot.rs @@ -24,23 +24,50 @@ use crate::{error::PeachWebError, utils::sbot}; // SBOT HELPER FUNCTIONS -/// Executes a systemctl command for the go-sbot.service process. -pub fn systemctl_sbot_cmd(cmd: &str) -> Result { - let output = Command::new("sudo") - .arg("systemctl") - .arg(cmd) - .arg(config_manager::get_config_value("GO_SBOT_SERVICE")?) - .output()?; - Ok(output) +/// On non-docker based deployments (peachcloud, yunohost), we use systemctl +/// On docker-based deployments, we use supervisord +/// This utility function calls the correct system calls based on these parameters. +pub fn system_sbot_cmd(cmd: &str) -> Result { + let system_manager = config_manager::get_config_value("SYSTEM_MANAGER")?; + match system_manager.as_str() { + "systemd" => { + 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. pub fn restart_sbot_process() -> (String, String) { debug!("Restarting go-sbot.service"); - match systemctl_sbot_cmd("stop") { + match system_sbot_cmd("stop") { // if stop was successful, try to start the process - Ok(_) => match systemctl_sbot_cmd("start") { + Ok(_) => match system_sbot_cmd("start") { Ok(_) => ( "success".to_string(), "Updated configuration and restarted the sbot process".to_string(),