Compare commits

...

3 Commits

Author SHA1 Message Date
notplants f6c30a327a Remove debug statements 2022-07-28 14:56:27 +02:00
notplants dbf1c648ae Working in docker 2022-07-28 14:53:45 +02:00
notplants 8d9aeb6662 Change default name of go-sbot.service to go-sbot 2022-07-28 14:01:19 +02:00
6 changed files with 52 additions and 8 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -61,7 +61,7 @@ pub fn get_peach_config_defaults() -> HashMap<String, String> {
("ADMIN_PASSWORD_HASH", "47"),
("TEMPORARY_PASSWORD_HASH", ""),
("GO_SBOT_DATADIR", "/home/peach/.ssb-go"),
("GO_SBOT_SERVICE", "go-sbot.service"),
("GO_SBOT_SERVICE", "go-sbot"),
("PEACH_CONFIGDIR", "/var/lib/peachcloud"),
("PEACH_HOMEDIR", "/home/peach"),
("PEACH_WEBDIR", "/usr/share/peach-web"),

View File

@ -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<GolgiError> for PeachError {
PeachError::Golgi(err)
}
}

View File

@ -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,41 @@ impl Default for SbotStatus {
impl SbotStatus {
/// Retrieve statistics for the go-sbot systemd process by querying `systemctl`.
pub fn read() -> Result<Self, PeachError> {
let system_manager = config_manager::get_config_value("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();
let info_output = Command::new("supervisorctl")
.arg("status")
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.output()?;
let service_info = std::str::from_utf8(&info_output.stdout)?;
for line in service_info.lines() {
// 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();
// note this command does not need to be run as sudo
@ -110,8 +145,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 +157,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())
}

View File

@ -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;