First version of the version checker

This commit is contained in:
Herbert Wolverson 2023-08-30 15:40:00 +00:00
parent c34c28541f
commit d8e7a20177
7 changed files with 93 additions and 0 deletions

1
src/VERSION_STRING Normal file
View File

@ -0,0 +1 @@
1.4-rc10-devel

1
src/rust/Cargo.lock generated
View File

@ -1574,6 +1574,7 @@ dependencies = [
"lqos_utils",
"nix 0.27.1",
"once_cell",
"reqwest",
"rocket",
"rocket_async_compression",
"sysinfo",

View File

@ -21,6 +21,7 @@ nix = "0"
once_cell = "1"
dns-lookup = "1"
dashmap = "5"
reqwest = { version = "0.11.20", features = ["json"] }
# Support JemAlloc on supported platforms
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]

View File

@ -0,0 +1,7 @@
use std::process::Command;
fn main() {
// Adds a git commit hash to the program
let output = Command::new("git").args(["rev-parse", "HEAD"]).output().unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}

View File

@ -11,6 +11,7 @@ mod auth_guard;
mod config_control;
mod network_tree;
mod queue_info;
mod toasts;
// Use JemAllocator only on supported platforms
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@ -105,6 +106,9 @@ fn rocket() -> _ {
static_pages::fontawesome_solid,
static_pages::fontawesome_webfont,
static_pages::fontawesome_woff,
// Front page toast checks
toasts::version_check,
toasts::stats_check,
],
);

View File

@ -0,0 +1,66 @@
use lqos_config::EtcLqos;
use lqos_utils::unix_time::unix_now;
use rocket::serde::json::Json;
use rocket::serde::{Deserialize, Serialize};
static LAST_VERSION_CHECK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
const ONE_HOUR_SECONDS: u64 = 60 * 60;
const VERSION_STRING: &str = include_str!("../../../VERSION_STRING");
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct VersionCheckRequest {
current_git_hash: String,
version_string: String,
node_id: String,
}
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct VersionCheckResponse {
update_available: bool,
}
async fn send_version_check() -> anyhow::Result<VersionCheckResponse> {
if let Ok(cfg) = EtcLqos::load() {
let current_hash = env!("GIT_HASH");
let request = VersionCheckRequest {
current_git_hash: current_hash.to_string(),
version_string: VERSION_STRING.to_string(),
node_id: cfg.node_id.unwrap_or("(not configured)".to_string()),
};
let response = reqwest::Client::new()
.post("https://stats.libreqos.io/api/version_check")
.json(&request)
.send()
.await?
.json::<VersionCheckResponse>()
.await?;
Ok(response)
} else {
anyhow::bail!("No config");
}
}
#[get("/api/version_check")]
pub async fn version_check() -> Json<String> {
let last_check = LAST_VERSION_CHECK.load(std::sync::atomic::Ordering::Relaxed);
if let Ok(now) = unix_now() {
if now > last_check + ONE_HOUR_SECONDS {
if let Ok(response) = send_version_check().await {
LAST_VERSION_CHECK.store(now, std::sync::atomic::Ordering::Relaxed);
if response.update_available {
return Json(String::from("Update available"));
}
}
}
}
Json(String::from("All Good"))
}
#[get("/api/stats_check")]
pub async fn stats_check() -> Json<String> {
Json(String::from("No"))
}

View File

@ -61,6 +61,8 @@
<div id="container" class="pad4">
<div id="toasts"></div>
<!-- Dashboard Row 1 -->
<div class="row mbot8">
<!-- THROUGHPUT -->
@ -351,6 +353,17 @@
updateHostCounts();
updateSiteFunnel();
OneSecondCadence();
// Version Check
$.get("/api/version_check", (data) => {
if (data != "All Good") {
let html = "<div class='alert alert-info alert-dismissible fade show' role='alert'>";
html += "<strong>LibreQoS Update Available!</strong>";
html += "<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>";
html += "</div>";
$("#toasts").append(html);
}
});
}
$(document).ready(start);