add ability to stop, start and restart go-sbot process

This commit is contained in:
glyph 2022-01-27 10:56:39 +02:00
parent 8e5c29ca6d
commit 4470f949bd
3 changed files with 131 additions and 4 deletions

View File

@ -48,7 +48,13 @@ pub fn mount_peachpub_routes(rocket: Rocket<Build>) -> Rocket<Build> {
)
.mount(
"/settings/scuttlebutt",
routes![ssb_settings_menu, configure_sbot],
routes![
ssb_settings_menu,
configure_sbot,
restart_sbot,
start_sbot,
stop_sbot
],
)
.mount(
"/scuttlebutt",

View File

@ -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<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()));
@ -34,3 +48,105 @@ pub fn configure_sbot(flash: Option<FlashMessage>, _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<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()
}

View File

@ -5,12 +5,17 @@
<!-- BUTTONS -->
<div id="settingsButtons">
<a id="configureSbot" class="button button-primary center" href="/settings/scuttlebutt/configure" title="Configure Sbot">Configure Sbot</a>
<a id="disable" class="button button-primary center" href="/settings/scuttlebutt/disable" title="Disable Sbot">Disable Sbot</a>
<a id="enable" class="button button-primary center" href="/settings/scuttlebutt/enable" title="Enable Sbot">Enable Sbot</a>
{% if sbot_stats.state == "active" %}
<a id="stop" class="button button-primary center" href="/settings/scuttlebutt/stop" title="Stop Sbot">Stop Sbot</a>
<a id="restart" class="button button-primary center" href="/settings/scuttlebutt/restart" title="Restart Sbot">Restart Sbot</a>
{% else %}
<a id="start" class="button button-primary center" href="/settings/scuttlebutt/start" title="Start Sbot">Start Sbot</a>
{% endif %}
<a id="checkFilesystem" class="button button-primary center" href="/settings/scuttlebutt/check_fs" title="Check Filesystem">Check Filesystem</a>
<a id="repairFilesystem" class="button button-primary center" href="/settings/scuttlebutt/repair" title="Repair Filesystem">Repair Filesystem</a>
<a id="removeFeeds" class="button button-primary center" href="/settings/scuttlebutt/remove_feeds" title="Remove Blocked Feeds">Remove Blocked Feeds</a>
</div>
<!-- FLASH MESSAGE -->
{% include "snippets/flash_message" %}
</div>
{%- endblock card -%}