Skip to content

Commit

Permalink
Merge pull request #2895 from albinsuresh/fix/2835/unified-software-m…
Browse files Browse the repository at this point in the history
…gmt-logs

fix(#2835): Include software plugin output in workflow log
  • Loading branch information
albinsuresh authored Jun 3, 2024
2 parents ba2b00a + a507d7a commit dfc31ed
Show file tree
Hide file tree
Showing 32 changed files with 588 additions and 503 deletions.
18 changes: 3 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ json-writer = { path = "crates/common/json_writer" }
lazy_static = "1.4"
log = "0.4"
log_manager = { path = "crates/common/log_manager" }
logged_command = { path = "crates/common/logged_command" }
maplit = "1.0"
miette = { version = "5.5.0", features = ["fancy"] }
mime = "0.3.17"
Expand Down
29 changes: 0 additions & 29 deletions crates/common/logged_command/Cargo.toml

This file was deleted.

4 changes: 0 additions & 4 deletions crates/common/logged_command/src/lib.rs

This file was deleted.

18 changes: 18 additions & 0 deletions crates/common/tedge_utils/src/signals.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nix::unistd::Pid;
use std::io;

#[cfg(not(windows))]
Expand All @@ -16,3 +17,20 @@ pub async fn interrupt() -> io::Result<()> {
pub async fn interrupt() -> io::Result<()> {
tokio::signal::ctrl_c().await
}

pub enum Signal {
SIGTERM,
SIGKILL,
}

pub fn terminate_process(pid: u32, signal_type: Signal) {
let pid: Pid = nix::unistd::Pid::from_raw(pid as nix::libc::pid_t);
match signal_type {
Signal::SIGTERM => {
let _ = nix::sys::signal::kill(pid, nix::sys::signal::SIGTERM);
}
Signal::SIGKILL => {
let _ = nix::sys::signal::kill(pid, nix::sys::signal::SIGKILL);
}
}
}
2 changes: 1 addition & 1 deletion crates/core/plugin_sm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository = { workspace = true }
[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
camino = { workspace = true }
csv = { workspace = true }
download = { workspace = true }
logged_command = { workspace = true }
regex = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
Expand Down
12 changes: 6 additions & 6 deletions crates/core/plugin_sm/src/log_file.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::path::Path;
use std::path::PathBuf;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use tokio::fs::File;
use tokio::io::BufWriter;

pub struct LogFile {
path: PathBuf,
path: Utf8PathBuf,
buffer: BufWriter<File>,
}

impl LogFile {
pub async fn try_new(path: PathBuf) -> Result<LogFile, std::io::Error> {
let file = File::create(path.clone()).await?;
pub async fn try_new(path: Utf8PathBuf) -> Result<LogFile, std::io::Error> {
let file = File::create(&path).await?;
let buffer = BufWriter::new(file);

Ok(LogFile { path, buffer })
}

pub fn path(&self) -> &Path {
pub fn path(&self) -> &Utf8Path {
&self.path
}

Expand Down
16 changes: 9 additions & 7 deletions crates/core/plugin_sm/src/operation_logs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use camino::Utf8PathBuf;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use time::format_description;
use time::OffsetDateTime;
use tracing::log;
Expand All @@ -23,7 +23,7 @@ pub enum OperationLogsError {

#[derive(Debug)]
pub struct OperationLogs {
pub log_dir: PathBuf,
pub log_dir: Utf8PathBuf,
}

pub enum LogKind {
Expand All @@ -36,7 +36,7 @@ const UPDATE_PREFIX: &str = "software-update";
const LIST_PREFIX: &str = "software-list";

impl OperationLogs {
pub fn try_new(log_dir: PathBuf) -> Result<OperationLogs, OperationLogsError> {
pub fn try_new(log_dir: Utf8PathBuf) -> Result<OperationLogs, OperationLogsError> {
std::fs::create_dir_all(log_dir.clone())?;
let operation_logs = OperationLogs { log_dir };

Expand Down Expand Up @@ -111,13 +111,13 @@ impl OperationLogs {

fn remove_old_logs(
log_tracker: &mut BinaryHeap<Reverse<String>>,
dir_path: &Path,
dir_path: impl AsRef<Path>,
n: usize,
) -> Result<(), OperationLogsError> {
while log_tracker.len() > n {
if let Some(rname) = log_tracker.pop() {
let name = rname.0;
let path = dir_path.join(name.clone());
let path = dir_path.as_ref().join(name.clone());
if let Err(err) = std::fs::remove_file(path) {
log::warn!("Fail to remove out-dated log file {} : {}", name, err);
}
Expand All @@ -131,6 +131,7 @@ mod tests {
use super::*;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;
use tempfile::TempDir;

#[tokio::test]
Expand All @@ -151,7 +152,7 @@ mod tests {
let unrelated_2 = create_file(log_dir.path(), "bar");

// Open the log dir
let _operation_logs = OperationLogs::try_new(log_dir.path().to_path_buf())?;
let _operation_logs = OperationLogs::try_new(log_dir.into_path().try_into().unwrap())?;

// Outdated logs are removed
assert!(!update_log_1.exists());
Expand Down Expand Up @@ -179,7 +180,8 @@ mod tests {
async fn on_new_log_keep_the_latest_logs_plus_the_new_one() -> Result<(), anyhow::Error> {
// Create a log dir
let log_dir = TempDir::new()?;
let operation_logs = OperationLogs::try_new(log_dir.path().to_path_buf())?;
let operation_logs =
OperationLogs::try_new(log_dir.path().to_path_buf().try_into().unwrap())?;

// Add a bunch of fake log files
let swlist_log_1 = create_file(log_dir.path(), "software-list-1996-02-22T16:39:57z");
Expand Down
Loading

0 comments on commit dfc31ed

Please sign in to comment.