Skip to content

Commit

Permalink
fix: Add in logging for the podman in the system logs (#2451)
Browse files Browse the repository at this point in the history
* fix: Add in logging for the podman in the system logs

* https as default for main tor address

---------

Co-authored-by: agent <kn0wmad@protonmail.com>
  • Loading branch information
Blu-J and kn0wmad authored Oct 10, 2023
1 parent 7632373 commit 8549b9b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion backend/src/db/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Database {
last_wifi_region: None,
eos_version_compat: Current::new().compat().clone(),
lan_address,
tor_address: format!("http://{}", account.key.tor_address())
tor_address: format!("https://{}", account.key.tor_address())
.parse()
.unwrap(),
ip_info: BTreeMap::new(),
Expand Down
3 changes: 1 addition & 2 deletions backend/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::disk::repair;
use crate::init::SYSTEM_REBUILD_PATH;
use crate::logs::{fetch_logs, LogResponse, LogSource};
use crate::shutdown::Shutdown;
use crate::system::SYSTEMD_UNIT;
use crate::util::display_none;
use crate::Error;

Expand All @@ -29,7 +28,7 @@ pub async fn logs(
#[arg] cursor: Option<String>,
#[arg] before: bool,
) -> Result<LogResponse, Error> {
Ok(fetch_logs(LogSource::Service(SYSTEMD_UNIT), limit, cursor, before).await?)
Ok(fetch_logs(LogSource::System, limit, cursor, before).await?)
}

#[command(display(display_none))]
Expand Down
38 changes: 23 additions & 15 deletions backend/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn deserialize_string_or_utf8_array<'de, D: serde::de::Deserializer<'de>>(
String::from_utf8(
std::iter::repeat_with(|| seq.next_element::<u8>().transpose())
.take_while(|a| a.is_some())
.filter_map(|a| a)
.flatten()
.collect::<Result<Vec<u8>, _>>()?,
)
.map_err(serde::de::Error::custom)
Expand All @@ -207,13 +207,22 @@ fn deserialize_string_or_utf8_array<'de, D: serde::de::Deserializer<'de>>(
deserializer.deserialize_any(Visitor)
}

/// Defining how we are going to filter on a journalctl cli log.
/// Kernal: (-k --dmesg Show kernel message log from the current boot)
/// Unit: ( -u --unit=UNIT Show logs from the specified unit
/// --user-unit=UNIT Show logs from the specified user unit))
/// System: Unit is startd, but we also filter on the comm
/// Container: Filtering containers, like podman/docker is done by filtering on the CONTAINER_NAME
#[derive(Debug)]
pub enum LogSource {
Kernel,
Service(&'static str),
Unit(&'static str),
System,
Container(PackageId),
}

pub const SYSTEM_UNIT: &str = "startd";

#[command(
custom_cli(cli_logs(async, context(CliContext))),
subcommands(self(logs_nofollow(async)), logs_follow),
Expand Down Expand Up @@ -323,21 +332,15 @@ pub async fn cli_logs_generic_follow(
.into())
}
};
base_url.set_scheme(ws_scheme).or_else(|_| {
Err(Error::new(
eyre!("Cannot set URL scheme"),
crate::ErrorKind::ParseUrl,
))
})?;
base_url
.set_scheme(ws_scheme)
.map_err(|_| Error::new(eyre!("Cannot set URL scheme"), crate::ErrorKind::ParseUrl))?;
let (mut stream, _) =
// base_url is "http://127.0.0.1/", with a trailing slash, so we don't put a leading slash in this path:
tokio_tungstenite::connect_async(format!("{}ws/rpc/{}", base_url, res.guid)).await?;
while let Some(log) = stream.try_next().await? {
match log {
Message::Text(log) => {
println!("{}", serde_json::from_str::<LogEntry>(&log)?);
}
_ => (),
if let Message::Text(log) = log {
println!("{}", serde_json::from_str::<LogEntry>(&log)?);
}
}

Expand All @@ -361,10 +364,15 @@ pub async fn journalctl(
LogSource::Kernel => {
cmd.arg("-k");
}
LogSource::Service(id) => {
LogSource::Unit(id) => {
cmd.arg("-u");
cmd.arg(id);
}
LogSource::System => {
cmd.arg("-u");
cmd.arg(SYSTEM_UNIT);
cmd.arg(format!("_COMM={}", SYSTEM_UNIT));
}
LogSource::Container(id) => {
cmd.arg(format!(
"CONTAINER_NAME={}",
Expand All @@ -373,7 +381,7 @@ pub async fn journalctl(
}
};

let cursor_formatted = format!("--after-cursor={}", cursor.clone().unwrap_or(""));
let cursor_formatted = format!("--after-cursor={}", cursor.unwrap_or(""));
if cursor.is_some() {
cmd.arg(&cursor_formatted);
if before {
Expand Down
6 changes: 3 additions & 3 deletions backend/src/net/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ pub async fn logs_nofollow(
_ctx: (),
(limit, cursor, before, _): (Option<usize>, Option<String>, bool, bool),
) -> Result<LogResponse, Error> {
fetch_logs(LogSource::Service(SYSTEMD_UNIT), limit, cursor, before).await
fetch_logs(LogSource::Unit(SYSTEMD_UNIT), limit, cursor, before).await
}

#[command(rpc_only, rename = "follow", display(display_none))]
pub async fn logs_follow(
#[context] ctx: RpcContext,
#[parent_data] (limit, _, _, _): (Option<usize>, Option<String>, bool, bool),
) -> Result<LogFollowResponse, Error> {
follow_logs(ctx, LogSource::Service(SYSTEMD_UNIT), limit).await
follow_logs(ctx, LogSource::Unit(SYSTEMD_UNIT), limit).await
}

fn event_handler(_event: AsyncEvent<'static>) -> BoxFuture<'static, Result<(), ConnError>> {
Expand Down Expand Up @@ -305,7 +305,7 @@ async fn torctl(
.invoke(ErrorKind::Tor)
.await?;

let logs = journalctl(LogSource::Service(SYSTEMD_UNIT), 0, None, false, true).await?;
let logs = journalctl(LogSource::Unit(SYSTEMD_UNIT), 0, None, false, true).await?;

let mut tcp_stream = None;
for _ in 0..60 {
Expand Down
6 changes: 2 additions & 4 deletions backend/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use crate::util::serde::{display_serializable, IoFormat};
use crate::util::{display_none, Invoke};
use crate::{Error, ErrorKind, ResultExt};

pub const SYSTEMD_UNIT: &'static str = "startd";

#[command(subcommands(zram))]
pub async fn experimental() -> Result<(), Error> {
Ok(())
Expand Down Expand Up @@ -130,15 +128,15 @@ pub async fn logs_nofollow(
_ctx: (),
(limit, cursor, before, _): (Option<usize>, Option<String>, bool, bool),
) -> Result<LogResponse, Error> {
fetch_logs(LogSource::Service(SYSTEMD_UNIT), limit, cursor, before).await
fetch_logs(LogSource::System, limit, cursor, before).await
}

#[command(rpc_only, rename = "follow", display(display_none))]
pub async fn logs_follow(
#[context] ctx: RpcContext,
#[parent_data] (limit, _, _, _): (Option<usize>, Option<String>, bool, bool),
) -> Result<LogFollowResponse, Error> {
follow_logs(ctx, LogSource::Service(SYSTEMD_UNIT), limit).await
follow_logs(ctx, LogSource::System, limit).await
}

#[command(
Expand Down

0 comments on commit 8549b9b

Please sign in to comment.