Working in docker

This commit is contained in:
notplants 2022-07-28 14:53:45 +02:00
parent 8d9aeb6662
commit dbf1c648ae
5 changed files with 58 additions and 7 deletions

2
Cargo.lock generated
View File

@ -2241,7 +2241,7 @@ dependencies = [
[[package]] [[package]]
name = "peach-lib" name = "peach-lib"
version = "1.3.4" version = "1.3.5"
dependencies = [ dependencies = [
"async-std", "async-std",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "peach-lib" name = "peach-lib"
version = "1.3.4" version = "1.3.5"
authors = ["Andrew Reid <glyph@mycelial.technology>"] authors = ["Andrew Reid <glyph@mycelial.technology>"]
edition = "2018" edition = "2018"

View File

@ -106,6 +106,10 @@ pub enum PeachError {
/// Represents a Golgi error /// Represents a Golgi error
Golgi(GolgiError), Golgi(GolgiError),
/// Represents a generic system error, whose details are specified in the string message
System(String),
} }
impl std::error::Error for PeachError { impl std::error::Error for PeachError {
@ -135,6 +139,7 @@ impl std::error::Error for PeachError {
PeachError::Utf8ToString(_) => None, PeachError::Utf8ToString(_) => None,
PeachError::Write { ref source, .. } => Some(source), PeachError::Write { ref source, .. } => Some(source),
PeachError::Golgi(_) => None, PeachError::Golgi(_) => None,
PeachError::System(_) => None,
} }
} }
} }
@ -193,6 +198,9 @@ impl std::fmt::Display for PeachError {
write!(f, "Write error: {}", path) write!(f, "Write error: {}", path)
} }
PeachError::Golgi(ref err) => err.fmt(f), PeachError::Golgi(ref err) => err.fmt(f),
PeachError::System(ref msg) => {
write!(f, "system error: {}", msg)
}
} }
} }
} }
@ -268,3 +276,4 @@ impl From<GolgiError> for PeachError {
PeachError::Golgi(err) PeachError::Golgi(err)
} }
} }

View File

@ -3,7 +3,7 @@
use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str};
use golgi::{sbot::Keystore, Sbot}; use golgi::{sbot::Keystore, Sbot};
use log::debug; use log::{debug, info};
use crate::config_manager; use crate::config_manager;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -64,6 +64,45 @@ impl Default for SbotStatus {
impl SbotStatus { impl SbotStatus {
/// Retrieve statistics for the go-sbot systemd process by querying `systemctl`. /// Retrieve statistics for the go-sbot systemd process by querying `systemctl`.
pub fn read() -> Result<Self, PeachError> { pub fn read() -> Result<Self, PeachError> {
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<Self, PeachError> {
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<Self, PeachError> {
let mut status = SbotStatus::default(); let mut status = SbotStatus::default();
// note this command does not need to be run as sudo // 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 ';' // using the index of the first ';' + 2 and the last ';'
status.boot_state = Some(line[start + 2..end].to_string()); status.boot_state = Some(line[start + 2..end].to_string());
} }
// example of the output line we're looking for here: // 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` // `Active: active (running) since Mon 2022-01-24 16:22:51 SAST; 4min 14s ago`
} else if line.contains("Active:") { } else if line.contains("Active:") {
let before_time = line.find(';'); let before_time = line.find(';');
let after_time = line.find(" ago"); let after_time = line.find(" ago");
@ -122,7 +161,7 @@ impl SbotStatus {
// if service is active then the `time` reading is uptime // if service is active then the `time` reading is uptime
if status.state == Some("active".to_string()) { if status.state == Some("active".to_string()) {
status.uptime = time.map(|t| t.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()) { } else if status.state == Some("inactive".to_string()) {
status.downtime = time.map(|t| t.to_string()) status.downtime = time.map(|t| t.to_string())
} }

View File

@ -14,7 +14,7 @@ use futures::stream::TryStreamExt;
use golgi::{ use golgi::{
api::friends::RelationshipQuery, blobs, messages::SsbMessageKVT, sbot::Keystore, Sbot, api::friends::RelationshipQuery, blobs, messages::SsbMessageKVT, sbot::Keystore, Sbot,
}; };
use log::debug; use log::{debug, info};
use peach_lib::config_manager; use peach_lib::config_manager;
use peach_lib::sbot::SbotConfig; use peach_lib::sbot::SbotConfig;
use rouille::input::post::BufferedFile; 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. /// This utility function calls the correct system calls based on these parameters.
pub fn system_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> { pub fn system_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> {
let system_manager = config_manager::get_config_value("SYSTEM_MANAGER")?; let system_manager = config_manager::get_config_value("SYSTEM_MANAGER")?;
info!("SYSTEM_MANAGER: {:?}", system_manager);
match system_manager.as_str() { match system_manager.as_str() {
"systemd" => { "systemd" => {
let output = Command::new("sudo") let output = Command::new("sudo")
@ -54,10 +55,12 @@ pub fn system_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> {
Ok(output) Ok(output)
} }
_ => { _ => {
info!("supervisorctl {:?} {:?}", cmd, config_manager::get_config_value("GO_SBOT_SERVICE")?);
let output = Command::new("supervisorctl") let output = Command::new("supervisorctl")
.arg(cmd) .arg(cmd)
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?) .arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.output()?; .output()?;
info!("logging: {:?}", output);
Ok(output) Ok(output)
} }
} }