Merge pull request #2648 from Start9Labs/feat/boot-param

Boot param for logs subscription
This commit is contained in:
Matt Hill
2024-07-10 14:54:39 -06:00
committed by GitHub
13 changed files with 159 additions and 68 deletions

View File

@@ -1,9 +1,12 @@
use std::convert::Infallible;
use std::ops::{Deref, DerefMut};
use std::process::Stdio;
use std::str::FromStr;
use std::time::{Duration, UNIX_EPOCH};
use axum::extract::ws::{self, WebSocket};
use chrono::{DateTime, Utc};
use clap::builder::ValueParserFactory;
use clap::{Args, FromArgMatches, Parser};
use color_eyre::eyre::eyre;
use futures::stream::BoxStream;
@@ -14,7 +17,7 @@ use rpc_toolkit::yajrc::RpcError;
use rpc_toolkit::{
from_fn_async, CallRemote, Context, Empty, HandlerArgs, HandlerExt, HandlerFor, ParentHandler,
};
use serde::de::DeserializeOwned;
use serde::de::{self, DeserializeOwned};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
@@ -27,6 +30,7 @@ use crate::error::ResultExt;
use crate::lxc::ContainerId;
use crate::prelude::*;
use crate::rpc_continuations::{Guid, RpcContinuation, RpcContinuations};
use crate::util::clap::FromStrParser;
use crate::util::serde::Reversible;
use crate::util::Invoke;
@@ -227,6 +231,91 @@ pub struct PackageIdParams {
id: PackageId,
}
#[derive(Debug, Clone)]
pub enum BootIdentifier {
Index(i32),
Id(String),
}
impl FromStr for BootIdentifier {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.parse() {
Ok(i) => Self::Index(i),
Err(_) => Self::Id(s.to_owned()),
})
}
}
impl ValueParserFactory for BootIdentifier {
type Parser = FromStrParser<Self>;
fn value_parser() -> Self::Parser {
Self::Parser::new()
}
}
impl Serialize for BootIdentifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Index(i) => serializer.serialize_i32(*i),
Self::Id(i) => serializer.serialize_str(i),
}
}
}
impl<'de> Deserialize<'de> for BootIdentifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = BootIdentifier;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a string or integer")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Self::Value::Id(v.to_owned()))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Self::Value::Id(v))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Self::Value::Index(v as i32))
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Self::Value::Index(v as i32))
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Self::Value::Index(v as i32))
}
}
deserializer.deserialize_any(Visitor)
}
}
impl From<BootIdentifier> for String {
fn from(value: BootIdentifier) -> Self {
match value {
BootIdentifier::Index(i) => i.to_string(),
BootIdentifier::Id(i) => i,
}
}
}
#[derive(Deserialize, Serialize, Parser)]
#[serde(rename_all = "camelCase")]
#[command(rename_all = "kebab-case")]
@@ -238,6 +327,9 @@ pub struct LogsParams<Extra: FromArgMatches + Args = Empty> {
limit: Option<usize>,
#[arg(short = 'c', long = "cursor", conflicts_with = "follow")]
cursor: Option<String>,
#[arg(short = 'b', long = "boot")]
#[serde(default)]
boot: Option<BootIdentifier>,
#[arg(short = 'B', long = "before", conflicts_with = "follow")]
#[serde(default)]
before: bool,
@@ -352,12 +444,22 @@ where
extra,
limit,
cursor,
boot,
before,
},
..
}: HandlerArgs<C, Empty, LogsParams<Extra>>| {
let f = f.clone();
async move { fetch_logs(f.call(&context, extra).await?, limit, cursor, before).await }
async move {
fetch_logs(
f.call(&context, extra).await?,
limit,
cursor,
boot.map(String::from),
before,
)
.await
}
},
)
}
@@ -377,13 +479,16 @@ fn logs_follow<
from_fn_async(
move |HandlerArgs {
context,
inherited_params: LogsParams { extra, limit, .. },
inherited_params:
LogsParams {
extra, limit, boot, ..
},
..
}: HandlerArgs<C, Empty, LogsParams<Extra>>| {
let f = f.clone();
async move {
let src = f.call(&context, extra).await?;
follow_logs(context, src, limit).await
follow_logs(context, src, limit, boot.map(String::from)).await
}
},
)
@@ -416,6 +521,7 @@ pub async fn journalctl(
id: LogSource,
limit: usize,
cursor: Option<&str>,
boot: Option<&str>,
before: bool,
follow: bool,
) -> Result<LogStream, Error> {
@@ -431,6 +537,12 @@ pub async fn journalctl(
}
}
if let Some(boot) = boot {
cmd.arg(format!("--boot={boot}"));
} else {
cmd.arg("--boot=all");
}
let deserialized_entries = String::from_utf8(cmd.invoke(ErrorKind::Journald).await?)?
.lines()
.map(serde_json::from_str::<JournalctlEntry>)
@@ -516,10 +628,12 @@ pub async fn fetch_logs(
id: LogSource,
limit: Option<usize>,
cursor: Option<String>,
boot: Option<String>,
before: bool,
) -> Result<LogResponse, Error> {
let limit = limit.unwrap_or(50);
let mut stream = journalctl(id, limit, cursor.as_deref(), before, false).await?;
let mut stream =
journalctl(id, limit, cursor.as_deref(), boot.as_deref(), before, false).await?;
let mut entries = Vec::with_capacity(limit);
let mut start_cursor = None;
@@ -563,9 +677,10 @@ pub async fn follow_logs<Context: AsRef<RpcContinuations>>(
ctx: Context,
id: LogSource,
limit: Option<usize>,
boot: Option<String>,
) -> Result<LogFollowResponse, Error> {
let limit = limit.unwrap_or(50);
let mut stream = journalctl(id, limit, None, false, true).await?;
let mut stream = journalctl(id, limit, None, boot.as_deref(), false, true).await?;
let mut start_cursor = None;
let mut first_entry = None;

View File

@@ -305,7 +305,15 @@ async fn torctl(
.invoke(ErrorKind::Tor)
.await?;
let logs = journalctl(LogSource::Unit(SYSTEMD_UNIT), 0, None, false, true).await?;
let logs = journalctl(
LogSource::Unit(SYSTEMD_UNIT),
0,
None,
Some("0"),
false,
true,
)
.await?;
let mut tcp_stream = None;
for _ in 0..60 {

4
web/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "startos-ui",
"version": "0.3.5.2",
"version": "0.3.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "startos-ui",
"version": "0.3.5.2",
"version": "0.3.6",
"dependencies": {
"@angular/animations": "^14.1.0",
"@angular/common": "^14.1.0",

View File

@@ -1,6 +1,6 @@
{
"name": "startos-ui",
"version": "0.3.5.2",
"version": "0.3.6",
"author": "Start9 Labs, Inc",
"homepage": "https://start9.com/",
"scripts": {

View File

@@ -1,6 +1,6 @@
{
"name": null,
"ack-welcome": "0.3.5.2",
"ack-welcome": "0.3.6",
"marketplace": {
"selected-url": "https://registry.start9.com/",
"known-hosts": {

View File

@@ -63,7 +63,7 @@ export class LogsComponent {
| 'connected'
| 'reconnecting'
| 'disconnected' = 'connecting'
limit = 400
limit = 200
count = 0
constructor(

View File

@@ -12,40 +12,7 @@
<ion-content class="ion-padding">
<h2>This Release</h2>
<h4>0.3.5.2</h4>
<p class="note-padding">
View the complete
<a
href="https://github.com/Start9Labs/start-os/releases/tag/v0.3.5.2"
target="_blank"
noreferrer
>
release notes
</a>
for more details.
</p>
<h6>Highlights</h6>
<ul class="spaced-list">
<li>Revert perpetual performance mode for quieter fan</li>
<li>Minor bug fixes</li>
</ul>
<h2>Previous 0.3.5.x Releases</h2>
<h4>0.3.5.1</h4>
<p class="note-padding">
View the complete
<a
href="https://github.com/Start9Labs/start-os/releases/tag/v0.3.5.1"
target="_blank"
noreferrer
>
release notes
</a>
for more details.
</p>
<h4>0.3.5</h4>
<h4>0.3.6</h4>
<p class="note-padding">
View the complete
<a
@@ -59,18 +26,10 @@
</p>
<h6>Highlights</h6>
<ul class="spaced-list">
<li>
This release contains significant under-the-hood improvements to
performance and reliability
</li>
<li>Ditch Docker, replace with Podman</li>
<li>Remove locking behavior from PatchDB and optimize</li>
<li>Boost efficiency of service manager</li>
<li>Require HTTPS on LAN, and improve setup flow for trusting Root CA</li>
<li>Better default privacy settings for Firefox kiosk mode</li>
<li>Eliminate memory leak from Javascript runtime</li>
<li>Other small bug fixes</li>
<li>Update license to MIT</li>
<li>Ditch Podman, replace with LXC</li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="ion-text-center ion-padding">

View File

@@ -18,7 +18,7 @@ export class LogsPage {
loading = true
needInfinite = true
startCursor?: string
limit = 200
limit = 400
isOnBottom = true
constructor(

View File

@@ -35,7 +35,9 @@ function convertAnsi(entries: readonly any[]): string {
@Injectable({ providedIn: 'root' })
export class LogsService extends Observable<readonly string[]> {
private readonly api = inject(ApiService)
private readonly log$ = defer(() => this.api.initFollowLogs({})).pipe(
private readonly log$ = defer(() =>
this.api.initFollowLogs({ boot: 0 }),
).pipe(
switchMap(({ guid }) => this.api.openWebsocket$<Log>(guid, {})),
bufferTime(250),
filter(logs => !!logs.length),

View File

@@ -18,10 +18,10 @@ export module Mock {
shuttingDown: false,
}
export const MarketplaceEos: RR.CheckOSUpdateRes = {
version: '0.3.5.2',
version: '0.3.6',
headline: 'Our biggest release ever.',
releaseNotes: {
'0.3.5.2': 'Some **Markdown** release _notes_ for 0.3.5.2',
'0.3.6': 'Some **Markdown** release _notes_ for 0.3.6',
'0.3.5.1': 'Some **Markdown** release _notes_ for 0.3.5.1',
'0.3.4.4': 'Some **Markdown** release _notes_ for 0.3.4.4',
'0.3.4.3': 'Some **Markdown** release _notes_ for 0.3.4.3',

View File

@@ -72,7 +72,12 @@ export module RR {
export type GetServerLogsReq = ServerLogsReq // server.logs & server.kernel-logs
export type GetServerLogsRes = LogsRes
export type FollowServerLogsReq = { limit?: number } // server.logs.follow & server.kernel-logs.follow
// @param limit: BE default is 50
// @param boot: number is offset (0: current, -1 prev, +1 first), string is a specific boot id, and null is all
export type FollowServerLogsReq = {
limit?: number
boot?: number | string | null
} // server.logs.follow & server.kernel-logs.follow
export type FollowServerLogsRes = {
startCursor: string
guid: string

View File

@@ -174,8 +174,10 @@ export class LiveApiService extends ApiService {
return this.rpcRequest({ method: 'init.subscribe', params: {} })
}
async initFollowLogs(): Promise<RR.FollowServerLogsRes> {
return this.rpcRequest({ method: 'init.logs.follow', params: {} })
async initFollowLogs(
params: RR.FollowServerLogsReq,
): Promise<RR.FollowServerLogsRes> {
return this.rpcRequest({ method: 'init.logs.follow', params })
}
// server
@@ -393,8 +395,8 @@ export class LiveApiService extends ApiService {
}
async followPackageLogs(
params: RR.FollowServerLogsReq,
): Promise<RR.FollowServerLogsRes> {
params: RR.FollowPackageLogsReq,
): Promise<RR.FollowPackageLogsRes> {
return this.rpcRequest({ method: 'package.logs.follow', params })
}

View File

@@ -37,7 +37,7 @@ export const mockPatchData: DataModel = {
arch: 'x86_64',
onionAddress: 'myveryownspecialtoraddress',
id: 'abcdefgh',
version: '0.3.5.2',
version: '0.3.6',
lastBackup: new Date(new Date().valueOf() - 604800001).toISOString(),
lanAddress: 'https://adjective-noun.local',
torAddress: 'https://myveryownspecialtoraddress.onion',