From 4470f949bdecc87f35262f45d2433e6425c03689 Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 27 Jan 2022 10:56:39 +0200 Subject: [PATCH] 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 -%}