diff --git a/Cargo.lock b/Cargo.lock index 77182b8..f219d80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2241,7 +2241,7 @@ dependencies = [ [[package]] name = "peach-lib" -version = "1.3.4" +version = "1.3.5" dependencies = [ "async-std", "chrono", diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index 7c8ea4a..c63bcef 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-lib" -version = "1.3.4" +version = "1.3.5" authors = ["Andrew Reid "] edition = "2018" diff --git a/peach-lib/src/error.rs b/peach-lib/src/error.rs index 9c55c1d..9a59fe4 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -106,6 +106,10 @@ pub enum PeachError { /// Represents a Golgi error Golgi(GolgiError), + + /// Represents a generic system error, whose details are specified in the string message + System(String), + } impl std::error::Error for PeachError { @@ -135,6 +139,7 @@ impl std::error::Error for PeachError { PeachError::Utf8ToString(_) => None, PeachError::Write { ref source, .. } => Some(source), PeachError::Golgi(_) => None, + PeachError::System(_) => None, } } } @@ -193,6 +198,9 @@ impl std::fmt::Display for PeachError { write!(f, "Write error: {}", path) } PeachError::Golgi(ref err) => err.fmt(f), + PeachError::System(ref msg) => { + write!(f, "system error: {}", msg) + } } } } @@ -268,3 +276,4 @@ impl From for PeachError { PeachError::Golgi(err) } } + diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index 730df22..ef391cd 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -3,7 +3,7 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; use golgi::{sbot::Keystore, Sbot}; -use log::debug; +use log::{debug, info}; use crate::config_manager; use serde::{Deserialize, Serialize}; @@ -64,6 +64,45 @@ impl Default for SbotStatus { impl SbotStatus { /// Retrieve statistics for the go-sbot systemd process by querying `systemctl`. pub fn read() -> Result { + let system_manager = config_manager::get_config_value("SYSTEM_MANAGER")?; + info!("SYSTEM_MANAGER: {:?}", system_manager); + match system_manager.as_str() { + "systemd" => { + SbotStatus::read_from_systemctl() + }, + "supervisord" => { + SbotStatus::read_from_supervisorctl() + }, + _ => Err(PeachError::System(format!( + "Invalid configuration for SYSTEM_MANAGER: {:?}", + system_manager))) + } + } + + pub fn read_from_supervisorctl() -> Result { + let mut status = SbotStatus::default(); + // TODO: implement this + let info_output = Command::new("supervisorctl") + .arg("status") + .arg(config_manager::get_config_value("GO_SBOT_SERVICE")?) + .output()?; + info!("info: {:?}", info_output); + + let service_info = std::str::from_utf8(&info_output.stdout)?; + + for line in service_info.lines() { + info!("line: {:?}", line); + // example line + // go-sbot RUNNING pid 11, uptime 0:04:23 + if line.contains("RUNNING") { + // TODO: this should be an enum + status.state = Some("active".to_string()); + } + } + Ok(status) + } + + pub fn read_from_systemctl() -> Result { let mut status = SbotStatus::default(); // note this command does not need to be run as sudo @@ -110,8 +149,8 @@ impl SbotStatus { // using the index of the first ';' + 2 and the last ';' status.boot_state = Some(line[start + 2..end].to_string()); } - // example of the output line we're looking for here: - // `Active: active (running) since Mon 2022-01-24 16:22:51 SAST; 4min 14s ago` + // example of the output line we're looking for here: + // `Active: active (running) since Mon 2022-01-24 16:22:51 SAST; 4min 14s ago` } else if line.contains("Active:") { let before_time = line.find(';'); let after_time = line.find(" ago"); @@ -122,7 +161,7 @@ impl SbotStatus { // if service is active then the `time` reading is uptime if status.state == Some("active".to_string()) { status.uptime = time.map(|t| t.to_string()) - // if service is inactive then the `time` reading is downtime + // if service is inactive then the `time` reading is downtime } else if status.state == Some("inactive".to_string()) { status.downtime = time.map(|t| t.to_string()) } diff --git a/peach-web/src/utils/sbot.rs b/peach-web/src/utils/sbot.rs index e192560..735330b 100644 --- a/peach-web/src/utils/sbot.rs +++ b/peach-web/src/utils/sbot.rs @@ -14,7 +14,7 @@ use futures::stream::TryStreamExt; use golgi::{ api::friends::RelationshipQuery, blobs, messages::SsbMessageKVT, sbot::Keystore, Sbot, }; -use log::debug; +use log::{debug, info}; use peach_lib::config_manager; use peach_lib::sbot::SbotConfig; use rouille::input::post::BufferedFile; @@ -29,6 +29,7 @@ use crate::{error::PeachWebError, utils::sbot}; /// 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")?; + info!("SYSTEM_MANAGER: {:?}", system_manager); match system_manager.as_str() { "systemd" => { let output = Command::new("sudo") @@ -54,10 +55,12 @@ pub fn system_sbot_cmd(cmd: &str) -> Result { Ok(output) } _ => { + info!("supervisorctl {:?} {:?}", cmd, config_manager::get_config_value("GO_SBOT_SERVICE")?); let output = Command::new("supervisorctl") .arg(cmd) .arg(config_manager::get_config_value("GO_SBOT_SERVICE")?) .output()?; + info!("logging: {:?}", output); Ok(output) } }