From 00554706cbb22b667357abe909d26077ccfe85cc Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 10:55:04 +0200 Subject: [PATCH 1/5] add go-sbot enabled / disabled check --- peach-stats/src/sbot.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/peach-stats/src/sbot.rs b/peach-stats/src/sbot.rs index 22a9579..ce4de0b 100644 --- a/peach-stats/src/sbot.rs +++ b/peach-stats/src/sbot.rs @@ -16,7 +16,9 @@ use crate::StatsError; #[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct SbotStat { /// Current process state. - pub state: String, + pub state: Option, + /// Current process boot state. + pub boot_state: Option, /// Current process memory usage in bytes. pub memory: Option, /// Uptime for the process (if state is `active`). @@ -29,7 +31,8 @@ impl SbotStat { /// Default builder for `SbotStat`. fn default() -> Self { Self { - state: String::new(), + state: None, + boot_state: None, memory: None, uptime: None, downtime: None, @@ -54,7 +57,7 @@ pub fn sbot_stats() -> Result { for line in service_info.lines() { if line.starts_with("ActiveState=") { if let Some(state) = line.strip_prefix("ActiveState=") { - status.state = state.to_string() + status.state = Some(state.to_string()) } } else if line.starts_with("MemoryCurrent=") { if let Some(memory) = line.strip_prefix("MemoryCurrent=") { @@ -68,15 +71,25 @@ pub fn sbot_stats() -> Result { .arg("status") .arg("go-sbot.service") .output() - .unwrap(); + .map_err(StatsError::Systemctl)?; let service_status = str::from_utf8(&status_output.stdout).map_err(StatsError::Utf8String)?; - // example of the output line we're looking for: - // `Active: active (running) since Mon 2022-01-24 16:22:51 SAST; 4min 14s ago` - for line in service_status.lines() { - if line.contains("Active:") { + // example of the output line we're looking for: + // `Loaded: loaded (/home/glyph/.config/systemd/user/go-sbot.service; enabled; vendor + // preset: enabled)` + if line.contains("Loaded:") { + let before_boot_state = line.find(';'); + let after_boot_state = line.rfind(';'); + if let (Some(start), Some(end)) = (before_boot_state, after_boot_state) { + // extract the enabled / disabled from the `Loaded: ...` line + // 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` + } else if line.contains("Active:") { let before_time = line.find(';'); let after_time = line.find(" ago"); if let (Some(start), Some(end)) = (before_time, after_time) { @@ -84,10 +97,10 @@ pub fn sbot_stats() -> Result { // using the index of ';' + 2 and the index of " ago" let time = Some(&line[start + 2..end]); // if service is active then the `time` reading is uptime - if status.state == "active" { + 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 - } else if status.state == "inactive" { + } else if status.state == Some("inactive".to_string()) { status.downtime = time.map(|t| t.to_string()) } } -- 2.40.1 From 8e5c29ca6d33d395942ce87516bc7c28a4644fda Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 10:55:44 +0200 Subject: [PATCH 2/5] add sbot stats to home template and set inner circle colour accordingly --- peach-web/src/routes/index.rs | 5 +++++ peach-web/templates/home.html.tera | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/peach-web/src/routes/index.rs b/peach-web/src/routes/index.rs index a703ed1..8b40d53 100644 --- a/peach-web/src/routes/index.rs +++ b/peach-web/src/routes/index.rs @@ -1,3 +1,4 @@ +use peach_stats::sbot; use rocket::{get, request::FlashMessage, State}; use rocket_dyn_templates::{tera::Context, Template}; @@ -8,7 +9,11 @@ use crate::RocketConfig; #[get("/")] pub fn home(_auth: Authenticated, config: &State) -> Template { + // retrieve go-sbot systemd process stats + let sbot_stats = sbot::sbot_stats().ok(); + let mut context = Context::new(); + context.insert("sbot_stats", &sbot_stats); context.insert("flash_name", &None::<()>); context.insert("flash_msg", &None::<()>); context.insert("title", &None::<()>); diff --git a/peach-web/templates/home.html.tera b/peach-web/templates/home.html.tera index 5d16781..fe0e298 100644 --- a/peach-web/templates/home.html.tera +++ b/peach-web/templates/home.html.tera @@ -25,7 +25,7 @@ -
+
-- 2.40.1 From 4470f949bdecc87f35262f45d2433e6425c03689 Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 10:56:39 +0200 Subject: [PATCH 3/5] add ability to stop, start and restart go-sbot process --- peach-web/src/router.rs | 8 +- peach-web/src/routes/settings/scuttlebutt.rs | 118 +++++++++++++++++- .../settings/scuttlebutt/menu.html.tera | 9 +- 3 files changed, 131 insertions(+), 4 deletions(-) diff --git a/peach-web/src/router.rs b/peach-web/src/router.rs index 99bcb7b..c4a017e 100644 --- a/peach-web/src/router.rs +++ b/peach-web/src/router.rs @@ -48,7 +48,13 @@ pub fn mount_peachpub_routes(rocket: Rocket) -> Rocket { ) .mount( "/settings/scuttlebutt", - routes![ssb_settings_menu, configure_sbot], + routes![ + ssb_settings_menu, + configure_sbot, + restart_sbot, + start_sbot, + stop_sbot + ], ) .mount( "/scuttlebutt", diff --git a/peach-web/src/routes/settings/scuttlebutt.rs b/peach-web/src/routes/settings/scuttlebutt.rs index 0ab9e70..b663106 100644 --- a/peach-web/src/routes/settings/scuttlebutt.rs +++ b/peach-web/src/routes/settings/scuttlebutt.rs @@ -1,4 +1,15 @@ -use rocket::{get, request::FlashMessage}; +use std::{ + io, + process::{Command, Output}, +}; + +use log::info; +use peach_stats::sbot; +use rocket::{ + get, + request::FlashMessage, + response::{Flash, Redirect}, +}; use rocket_dyn_templates::{tera::Context, Template}; use crate::routes::authentication::Authenticated; @@ -9,6 +20,9 @@ use crate::routes::authentication::Authenticated; #[get("/")] pub fn ssb_settings_menu(flash: Option, _auth: Authenticated) -> Template { let mut context = Context::new(); + // retrieve go-sbot systemd process stats + let sbot_stats = sbot::sbot_stats().ok(); + context.insert("sbot_stats", &sbot_stats); context.insert("back", &Some("/settings".to_string())); context.insert("title", &Some("Scuttlebutt Settings".to_string())); @@ -34,3 +48,105 @@ pub fn configure_sbot(flash: Option, _auth: Authenticated) -> Temp Template::render("settings/scuttlebutt/configure_sbot", &context.into_json()) } + +/// Attempt to start the go-sbot.service process. +/// Redirect to the Scuttlebutt settings menu and communicate the outcome of +/// the attempt via a flash message. +#[get("/start")] +pub fn start_sbot(_auth: Authenticated) -> Flash { + match start_sbot_cmd() { + Ok(_) => Flash::success( + Redirect::to("/settings/scuttlebutt"), + "Sbot process has been started", + ), + Err(_) => Flash::error( + Redirect::to("/settings/scuttlebutt"), + "Failed to start the sbot process", + ), + } +} + +/// Attempt to stop the go-sbot.service process. +/// Redirect to the Scuttlebutt settings menu and communicate the outcome of +/// the attempt via a flash message. +#[get("/stop")] +pub fn stop_sbot(_auth: Authenticated) -> Flash { + match stop_sbot_cmd() { + Ok(_) => Flash::success( + Redirect::to("/settings/scuttlebutt"), + "Sbot process has been stopped", + ), + Err(_) => Flash::error( + Redirect::to("/settings/scuttlebutt"), + "Failed to stop the sbot process", + ), + } +} + +/// Attempt to restart the go-sbot.service process. +/// Redirect to the Scuttlebutt settings menu and communicate the outcome of +/// the attempt via a flash message. +#[get("/restart")] +pub fn restart_sbot(_auth: Authenticated) -> Flash { + // try to stop the process + match stop_sbot_cmd() { + // if stop was successful, try to start the process + Ok(_) => match start_sbot_cmd() { + Ok(_) => Flash::success( + Redirect::to("/settings/scuttlebutt"), + "Sbot process has been restarted", + ), + + Err(_) => Flash::error( + Redirect::to("/settings/scuttlebutt"), + "Failed to start the sbot process", + ), + }, + Err(_) => Flash::error( + Redirect::to("/settings/scuttlebutt"), + "Failed to stop the sbot process", + ), + } +} + +// HELPER FUNCTIONS + +/// Executes a systemctl disable command for the go-sbot.service process. +pub fn disable_sbot_cmd() -> io::Result { + info!("Disabling go-sbot.service"); + Command::new("systemctl") + .arg("--user") + .arg("disable") + .arg("go-sbot.service") + .output() +} + +/// Executes a systemctl enable command for the go-sbot.service process. +pub fn enable_sbot_cmd() -> io::Result { + info!("Enabling go-sbot.service"); + Command::new("systemctl") + .arg("--user") + .arg("enable") + .arg("go-sbot.service") + .output() +} + +/// Executes a systemctl start command for the go-sbot.service process. +pub fn start_sbot_cmd() -> io::Result { + info!("Starting go-sbot.service"); + Command::new("systemctl") + .arg("--user") + .arg("start") + .arg("go-sbot.service") + .output() +} + +/// Executes a systemctl stop command for the go-sbot.service process. +pub fn stop_sbot_cmd() -> io::Result { + info!("Stopping go-sbot.service"); + Command::new("systemctl") + .arg("--user") + .arg("stop") + .arg("go-sbot.service") + .output() +} diff --git a/peach-web/templates/settings/scuttlebutt/menu.html.tera b/peach-web/templates/settings/scuttlebutt/menu.html.tera index 386e458..ae72f49 100644 --- a/peach-web/templates/settings/scuttlebutt/menu.html.tera +++ b/peach-web/templates/settings/scuttlebutt/menu.html.tera @@ -5,12 +5,17 @@
Configure Sbot - Disable Sbot - Enable Sbot + {% if sbot_stats.state == "active" %} + Stop Sbot Restart Sbot + {% else %} + Start Sbot + {% endif %} Check Filesystem Repair Filesystem Remove Blocked Feeds
+ + {% include "snippets/flash_message" %} {%- endblock card -%} -- 2.40.1 From c7cc310a32a783998d1a203b05ae5ccebb167370 Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 10:57:21 +0200 Subject: [PATCH 4/5] add form element to enable / disable running go-sbot on startup --- .../settings/scuttlebutt/configure_sbot.html.tera | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera b/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera index 1497853..f72c5a6 100644 --- a/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera +++ b/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera @@ -41,11 +41,14 @@
- -
+ +

- -
+ +
+
+ +

-- 2.40.1 From f715644e25873f2e9f80c18a0503b9906ad6595c Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 11:01:12 +0200 Subject: [PATCH 5/5] display system startup mode --- peach-web/templates/status/scuttlebutt.html.tera | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/peach-web/templates/status/scuttlebutt.html.tera b/peach-web/templates/status/scuttlebutt.html.tera index 81f06e2..d99eb5e 100644 --- a/peach-web/templates/status/scuttlebutt.html.tera +++ b/peach-web/templates/status/scuttlebutt.html.tera @@ -31,10 +31,18 @@ {% if sbot_stats.state == "active" %}

{{ sbot_stats.uptime }}

- {%- else -%} + {# render downtime element if downtime is `Some(time)` #} + {# downtime will be `None` if service is stopped and disabled #} + {%- elif sbot_stats.downtime -%}

{{ sbot_stats.downtime }}

{%- endif -%} + + {% if sbot_stats.boot_state == "enabled" %} +

Enabled

+ {% else %} +

Disabled

+ {%- endif -%}
-- 2.40.1