package logs rpc endpoint (#427)

* wip: package logs

* fixes from code review

* fix reverse flag behavior

* only embassyd uses -u, packages use CONTAINER_NAME

* make id param an enum, clean up code

* changes from rebase

* change package IDs to be of type PackageId

Co-authored-by: Aiden McClelland <me@drbonez.dev>
This commit is contained in:
Chris Guida
2021-09-03 13:04:41 -05:00
committed by GitHub
parent bbea347e84
commit 38d55a4299
5 changed files with 298 additions and 55 deletions

View File

@@ -1,61 +1,22 @@
use std::fmt;
use chrono::{DateTime, Utc};
use clap::ArgMatches;
use rpc_toolkit::command;
use tokio::process::Command;
use tokio::sync::RwLock;
use crate::context::RpcContext;
use crate::logs::{LogResponse, LogSource, fetch_logs};
use crate::{Error, ErrorKind, ResultExt};
pub const SYSTEMD_UNIT: &'static str = "embassyd";
fn parse_datetime(text: &str, _matches: &ArgMatches) -> Result<DateTime<Utc>, Error> {
text.parse().with_kind(ErrorKind::ParseTimestamp)
}
#[command(rpc_only)]
pub async fn logs(
#[context] _ctx: RpcContext,
#[arg(parse(crate::system::parse_datetime))] before: Option<DateTime<Utc>>,
#[context] ctx: RpcContext,
#[arg] limit: Option<usize>,
) -> Result<Vec<(String, String)>, Error> {
let before = before.unwrap_or(Utc::now());
let limit = limit.unwrap_or(50);
// Journalctl has unexpected behavior where "until" does not play well with "lines" unless the output is reversed.
// Keep this in mind if you are changing the code below
let out = Command::new("journalctl")
.args(&[
"-u",
SYSTEMD_UNIT,
&format!(
"-U\"{} {} UTC\"",
before.date().naive_utc(),
before.time().format("%H:%M:%S")
),
"--output=short-iso",
"--no-hostname",
"--utc",
"--reverse",
&format!("-n{}", limit),
])
.output()
.await?
.stdout;
let out_string = String::from_utf8(out)?;
let lines = out_string.lines();
let mut split_lines = lines
.skip(1) // ditch the journalctl header
.map(|s| {
// split the timestamp off from the log line
let (ts, l) = s.split_once(" ").unwrap();
(ts.to_owned(), l.to_owned())
})
.collect::<Vec<(String, String)>>();
// reverse output again because we reversed it above
split_lines.reverse();
Ok(split_lines)
#[arg] cursor: Option<String>,
#[arg] before_flag: Option<bool>,
) -> Result<LogResponse, Error> {
Ok(fetch_logs(LogSource::Service(SYSTEMD_UNIT), limit, cursor, before_flag.unwrap_or(false)).await?)
}
#[derive(serde::Serialize, Clone, Debug)]
@@ -482,15 +443,6 @@ async fn get_disk_info() -> Result<MetricsDisk, Error> {
})?
}
#[test]
pub fn test_datetime_output() {
println!(
"{} {} UTC",
Utc::now().date().naive_utc(),
Utc::now().time().format("%H:%M:%S")
)
}
#[tokio::test]
pub async fn test_get_temp() {
println!("{}", get_temp().await.unwrap())