From 4ae2704b8021380124cfd9cc7869347b225c5ae3 Mon Sep 17 00:00:00 2001 From: TMKnight <548588+tmknight@users.noreply.github.com> Date: Mon, 27 May 2024 11:42:20 -0400 Subject: [PATCH 1/8] v0.13.4 --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8f6eb1..37d070e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Unreleased] -## 0.13.3 +## 0.13.4 + +### Fixed + +- Updated container assessment to exclude unhealthy containers that are purposely stopped ### Security From 0f7b44ec6767568d66f5ac740be27733d8130b31 Mon Sep 17 00:00:00 2001 From: TMKnight <548588+tmknight@users.noreply.github.com> Date: Mon, 27 May 2024 11:42:44 -0400 Subject: [PATCH 2/8] v0.13.4 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da6c86d..36a7126 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "docker-autoheal" -version = "0.13.3" +version = "0.13.4" authors = ["Travis M Knight"] license = "GPL-3.0" description = "A cross-platform tool to monitor and remediate unhealthy Docker containers" From cc26853e5fcbffaeaa10543748c1a2d14e1a558c Mon Sep 17 00:00:00 2001 From: TMKnight <548588+tmknight@users.noreply.github.com> Date: Mon, 27 May 2024 11:59:43 -0400 Subject: [PATCH 3/8] Ensure log length assessment does not underflow --- src/inquire/inspect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inquire/inspect.rs b/src/inquire/inspect.rs index c58c846..1bf5080 100644 --- a/src/inquire/inspect.rs +++ b/src/inquire/inspect.rs @@ -51,7 +51,7 @@ pub async fn inspect_container(docker: Docker, name: &str, id: &str) -> Result { .and_then(|s| s.health.as_ref().and_then(|h| h.log.clone())) { Some(log) => { - let last = log.len() - 1; + let last = log.len().saturating_sub(1); let reason = log[last].clone().output.unwrap_or(default_reason); failing_reason = reason.clone(); exit_code = log[last].clone().exit_code.unwrap_or(-1); From f9ac9172a6ab0ad9fe5459a06b6a4e73e94cac28 Mon Sep 17 00:00:00 2001 From: TMKnight <548588+tmknight@users.noreply.github.com> Date: Mon, 27 May 2024 12:00:38 -0400 Subject: [PATCH 4/8] v0.13.4 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d070e..023ce92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed -- Updated container assessment to exclude unhealthy containers that are purposely stopped +- Ensure log length assessment does not underflow ### Security From bba4185783aa60d46ccb4376325035f176aff556 Mon Sep 17 00:00:00 2001 From: tmknight Date: Mon, 27 May 2024 14:01:26 -0400 Subject: [PATCH 5/8] Optimize code --- src/execute/looper.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execute/looper.rs b/src/execute/looper.rs index a9d192d..1bd8a02 100644 --- a/src/execute/looper.rs +++ b/src/execute/looper.rs @@ -125,7 +125,7 @@ pub async fn start_loop( } else if autoheal_monitor_enable && (autoheal_restart_enable || log_all) { // Determine failing streak of the unhealthy container let inspection = inspect_container(docker_clone.clone(), name, &id).await; - fail_reason = inspection.failing_reason.clone(); + fail_reason.clone_from(&inspection.failing_reason); exit_code = inspection.exit_code; if inspection.failed { // Remediate From 7cc15ae7148b5e8eaf33baee5567bd27e577f520 Mon Sep 17 00:00:00 2001 From: tmknight Date: Mon, 27 May 2024 14:02:20 -0400 Subject: [PATCH 6/8] Do not count purposefully exited as unhealthy --- src/inquire/list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inquire/list.rs b/src/inquire/list.rs index 1ed09fc..5ac9083 100644 --- a/src/inquire/list.rs +++ b/src/inquire/list.rs @@ -6,7 +6,7 @@ pub async fn containers_list(docker: Docker) -> Vec { // Build container assessment criteria let mut filters = HashMap::new(); filters.insert("health", vec!["unhealthy"]); - filters.insert("status", vec!["running", "exited", "dead"]); + filters.insert("status", vec!["running", "dead"]); // Gather all containers that are unhealthy let container_options = Some(ListContainersOptions { From 3974d345dcb2f46de64dcb7dc7d563e15c293ab8 Mon Sep 17 00:00:00 2001 From: tmknight Date: Mon, 27 May 2024 14:02:52 -0400 Subject: [PATCH 7/8] Ensure empty log does not panic --- src/inquire/inspect.rs | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/inquire/inspect.rs b/src/inquire/inspect.rs index 1bf5080..596e583 100644 --- a/src/inquire/inspect.rs +++ b/src/inquire/inspect.rs @@ -42,31 +42,24 @@ pub async fn inspect_container(docker: Docker, name: &str, id: &str) -> Result { } }; // Get last 'output' and 'exitcode' from state:health - let default_reason = String::from("unknown"); - let mut failing_reason = default_reason.clone(); + let mut failing_reason = "unknown".to_string(); let mut exit_code: i64 = -1; - match container_inspect - .state - .as_ref() - .and_then(|s| s.health.as_ref().and_then(|h| h.log.clone())) - { - Some(log) => { - let last = log.len().saturating_sub(1); - let reason = log[last].clone().output.unwrap_or(default_reason); - failing_reason = reason.clone(); - exit_code = log[last].clone().exit_code.unwrap_or(-1); - } - None => { - // Log that we had an error - let msg0 = format!( - "[{} ({})] Could not reliably determine container failing reason", - name, id - ); - log_message(&msg0, ERROR).await; + if let Some(log) = container_inspect.state.as_ref().and_then(|s| s.health.as_ref().and_then(|h| h.log.clone())) { + if let Some(last) = log.last() { + failing_reason = last.output.clone().unwrap_or(failing_reason); + exit_code = last.exit_code.unwrap_or(exit_code); + } else { + failing_reason = "log is empty".to_string(); } - }; + } else { + let msg0 = format!( + "[{} ({})] Could not reliably determine container failing reason", + name, id + ); + log_message(&msg0, ERROR).await; + } Result { - failed: !matches!(failing_streak, 0), + failed: failing_streak != 0, failing_streak, failing_reason, exit_code, From 41b36fe1d62e64d692b0261f5e0a41a11d3a0773 Mon Sep 17 00:00:00 2001 From: tmknight Date: Mon, 27 May 2024 14:03:28 -0400 Subject: [PATCH 8/8] v0.13.4 --- CHANGELOG.md | 3 ++- Cargo.lock | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 023ce92..4a30059 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed -- Ensure log length assessment does not underflow +- Ensure log length assessment does not underflow and compensate for empty log +- Other code cleanup ### Security diff --git a/Cargo.lock b/Cargo.lock index 1dac960..4448f48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,7 +191,7 @@ dependencies = [ [[package]] name = "docker-autoheal" -version = "0.13.3" +version = "0.13.4" dependencies = [ "bollard", "chrono",