peach-workspace/peach-web/src/routes/settings/scuttlebutt.rs

153 lines
4.7 KiB
Rust

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;
// HELPERS AND ROUTES FOR /settings/scuttlebutt
/// Scuttlebutt settings menu.
#[get("/")]
pub fn ssb_settings_menu(flash: Option<FlashMessage>, _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()));
if let Some(flash) = flash {
context.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
Template::render("settings/scuttlebutt/menu", &context.into_json())
}
/// Sbot configuration page (includes form for updating configuration parameters).
#[get("/configure")]
pub fn configure_sbot(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = Context::new();
context.insert("back", &Some("/settings/scuttlebutt".to_string()));
context.insert("title", &Some("Sbot Configuration".to_string()));
if let Some(flash) = flash {
context.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
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<Redirect> {
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<Redirect> {
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<Redirect> {
// 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<Output> {
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<Output> {
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<Output> {
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<Output> {
info!("Stopping go-sbot.service");
Command::new("systemctl")
.arg("--user")
.arg("stop")
.arg("go-sbot.service")
.output()
}