Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.8.0 #72

Merged
merged 19 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/github-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
workflow_run:
workflows: ["Github Tag"]
types: [completed]
# branches: [main]

env:
# Use docker.io for Docker Hub if empty
Expand All @@ -16,8 +15,6 @@ env:
IMAGE: docker-autoheal
# github.repository as <account>/<repo>
IMAGE_NAME: tmknight/docker-autoheal
# cosign version
COSIGN_VER: 'v2.1.1'
# Build args
CONTEXT: .
DISTRO: alpine
Expand Down
11 changes: 0 additions & 11 deletions .github/workflows/github-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ name: "Github Tag"

on:
workflow_dispatch:
# push:
# paths-ignore:
# - '.github/**'
# - 'examples/**'
# - 'docker/**'
# - 'test/**'
# - '*.md'
# - '.*'
# - '*.lock'
# - '.LICENSE'
# branches: [ "main", "develop" ]
pull_request:
types: [closed]
branches: [main]
Expand Down
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# Changelog

All notable changes to docker-autoheal are documented in this file.
The sections should follow the order `Packaging`, `Added`, `Changed`, `Fixed` and `Removed`.
The sections should follow the order `Security`, `Added`, `Changed`, `Fixed`, and `Removed`.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]

## 0.8.0

### Added

- Binary option for `tcp-timeout`, now in alignment with environment `AUTOHEAL_TCP_TIMEOUT`
- Breaking changes
- `stop-timeout` is now `s`
- `tcp-timeout` is now `t`
- Additional checks, balances & error handling

### Changed

- Refactored binary options into separate function for more efficient parsing
- Refactored environment variables into separate function for better organization

## 0.7.0

### Added
Expand All @@ -17,7 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Fixed

- Corrected binary options descriptions and hints for webhook entries
- Binary options descriptions and hints for webhook entries

## 0.6.0

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "docker-autoheal"
version = "0.7.0"
version = "0.8.0"
authors = ["Travis M Knight"]
license = "MIT"
description = "A cross-platform tool to monitor and remediate unhealthy Docker containers"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Options:
One of local, socket, http, or ssl
-l, --container-label <CONTAINER_LABEL>
Container label to monitor (e.g. autoheal)
-t, --stop-timeout <STOP_TIMEOUT>
-s, --stop-timeout <STOP_TIMEOUT>
Time in seconds to wait for action to complete
-i, --interval <INTERVAL>
Time in seconds to check health
Expand All @@ -62,6 +62,8 @@ Options:
-p, --tcp-port <TCP_PORT>
The tcp port number of the Docker host (when -c http
or ssl)
-t, --tcp-timeout <TCP_TIMEOUT>
Time in seconds to wait for connection to complete
-k, --key-path <KEY_PATH>
The fully qualified path to requisite ssl PEM files
-a, --apprise-url <APPRISE_URL>
Expand Down
40 changes: 20 additions & 20 deletions src/execute/connect.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
use crate::{report::logging::log_message, ERROR, INFO};
use crate::{log_message, ERROR, INFO};
use bollard::{Docker, API_DEFAULT_VERSION};

pub async fn connect_docker(
autoheal_connection_type: String,
autoheal_tcp_address: String,
autoheal_tcp_timeout: u64,
autoheal_key_path: String,
autoheal_cert_path: String,
autoheal_ca_path: String,
connection_type: String,
tcp_address: String,
tcp_timeout: u64,
key_path: String,
cert_path: String,
ca_path: String,
) -> Docker {
// Log final connection paramaters
let msg0 = format!("Monitoring Docker via {}", autoheal_connection_type);
let msg0 = format!("Monitoring Docker via {}", connection_type);
log_message(&msg0, INFO).await;
match autoheal_connection_type.as_str() {
match connection_type.as_str() {
"http" => {
let msg1 = format!("Connecting to {}", autoheal_tcp_address);
let msg1 = format!("Connecting to {}", tcp_address);
log_message(&msg1, INFO).await;
}
"ssl" => {
let msg1 = format!("Connecting to {}", autoheal_tcp_address);
let msg1 = format!("Connecting to {}", tcp_address);
log_message(&msg1, INFO).await;
let msg2 = format!(
"Certificate information: {}, {}, {}",
autoheal_key_path, autoheal_cert_path, autoheal_ca_path
key_path, cert_path, ca_path
);
log_message(&msg2, INFO).await;
}
&_ => {}
}
// Connect to Docker as specified
let docker = match autoheal_connection_type.as_str() {
let docker = match connection_type.as_str() {
"http" => Docker::connect_with_http(
&autoheal_tcp_address,
autoheal_tcp_timeout,
&tcp_address,
tcp_timeout,
API_DEFAULT_VERSION,
),
"socket" => Docker::connect_with_socket_defaults(),
"ssl" => Docker::connect_with_ssl(
&autoheal_tcp_address,
std::path::Path::new(&autoheal_key_path),
std::path::Path::new(&autoheal_cert_path),
std::path::Path::new(&autoheal_ca_path),
autoheal_tcp_timeout,
&tcp_address,
std::path::Path::new(&key_path),
std::path::Path::new(&cert_path),
std::path::Path::new(&ca_path),
tcp_timeout,
API_DEFAULT_VERSION,
),
&_ => Docker::connect_with_local_defaults(),
Expand Down
10 changes: 4 additions & 6 deletions src/execute/looper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ pub async fn start_loop(
// Determine if stop override label
let s = "autoheal.stop.timeout".to_string();
let autoheal_stop_timeout: isize = match container.labels {
Some(label) => {
match label.get(&s) {
Some(v) => v.parse().unwrap_or(autoheal_stop_timeout),
None => autoheal_stop_timeout,
}
}
Some(label) => match label.get(&s) {
Some(v) => v.parse().unwrap_or(autoheal_stop_timeout),
None => autoheal_stop_timeout,
},
None => autoheal_stop_timeout,
};

Expand Down
164 changes: 163 additions & 1 deletion src/inquire/environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,169 @@
use super::options::OptionsList;
use crate::{log_message, ERROR, WARNING};

pub struct VariablesList {
pub connection_type: String,
pub container_label: String,
pub stop_timeout: isize,
pub interval: u64,
pub start_delay: u64,
pub tcp_address: String,
pub tcp_timeout: u64,
pub key_path: String,
pub cert_path: String,
pub ca_path: String,
pub apprise_url: String,
pub webhook_key: String,
pub webhook_url: String,
}

// Get environment variable
pub fn get_env(key: &str, default: &str) -> String {
fn get_env(key: &str, default: &str) -> String {
match std::env::var(key) {
Ok(val) => val.to_lowercase(),
Err(_e) => default.to_string().to_lowercase(),
}
}

// Determine if we have valid arguments, need to check env, or use defaults
pub async fn get_var(opt: OptionsList) -> VariablesList {
let autoheal_connection_type: String = match opt.connection_type {
None => {
let allowed_connection_types: Vec<&str> = vec!["local", "socket", "http", "ssl"];
let env_connection_type = get_env("AUTOHEAL_CONNECTION_TYPE", "local");
match allowed_connection_types.contains(&env_connection_type.as_str()) {
true => env_connection_type,
false => {
let msg0 = format!(
"Unexpected connection-type ({}): {}",
allowed_connection_types.join(","),
env_connection_type
);
log_message(&msg0, ERROR).await;
let msg1 = String::from("Attempting connection via default (local)");
log_message(&msg1, WARNING).await;
"local".to_string()
}
}
}
Some(o) => o,
};
let autoheal_container_label: String = match opt.container_label {
None => get_env("AUTOHEAL_CONTAINER_LABEL", "autoheal"),
Some(o) => o,
};
let autoheal_stop_timeout: isize = match opt.stop_timeout {
None => get_env("AUTOHEAL_STOP_TIMEOUT", "10").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
10
}
},
};
let autoheal_interval: u64 = match opt.interval {
None => get_env("AUTOHEAL_INTERVAL", "5").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
5
}
},
};
let autoheal_start_delay: u64 = match opt.start_delay {
None => get_env("AUTOHEAL_START_DELAY", "0").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
0
}
},
};

// Autoheal tcp variables
let autoheal_tcp_host: String = match opt.tcp_host {
None => get_env("AUTOHEAL_TCP_HOST", "localhost"),
Some(o) => o,
};
let autoheal_tcp_port: u64 = match autoheal_connection_type.as_str() {
"ssl" => match opt.tcp_port {
None => get_env("AUTOHEAL_TCP_PORT", "2376").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
2376
}
},
},
&_ => match opt.tcp_port {
None => get_env("AUTOHEAL_TCP_PORT", "2375").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
2375
}
},
},
};
let autoheal_tcp_address: String = format!("{}:{}", autoheal_tcp_host, autoheal_tcp_port);
let autoheal_tcp_timeout: u64 = match opt.tcp_timeout {
None => get_env("AUTOHEAL_TCP_TIMEOUT", "10").parse().unwrap(),
Some(o) => match o.parse() {
Ok(a) => a,
Err(e) => {
let msg0 = format!("Unexpected value; using default: {}", e);
log_message(&msg0, WARNING).await;
10
}
},
};

// Autoheal ssl variables
let autoheal_pem_path: String = match opt.key_path {
None => get_env("AUTOHEAL_PEM_PATH", "/opt/docker-autoheal/tls"),
Some(o) => o,
};
let autoheal_key_path: String = format!("{}/key.pem", autoheal_pem_path);
let autoheal_cert_path: String = format!("{}/cert.pem", autoheal_pem_path);
let autoheal_ca_path: String = format!("{}/ca.pem", autoheal_pem_path);

// Webhook variables
let autoheal_apprise_url: String = match opt.apprise_url {
None => get_env("AUTOHEAL_APPRISE_URL", ""),
Some(o) => o,
};
let autoheal_webhook_key: String = match opt.webhook_key {
None => get_env("AUTOHEAL_WEBHOOK_KEY", ""),
Some(o) => o,
};
let autoheal_webhook_url: String = match opt.webhook_url {
None => get_env("AUTOHEAL_WEBHOOK_URL", ""),
Some(o) => o,
};

VariablesList {
connection_type: autoheal_connection_type,
container_label: autoheal_container_label,
stop_timeout: autoheal_stop_timeout,
interval: autoheal_interval,
start_delay: autoheal_start_delay,
tcp_address: autoheal_tcp_address,
tcp_timeout: autoheal_tcp_timeout,
key_path: autoheal_key_path,
cert_path: autoheal_cert_path,
ca_path: autoheal_ca_path,
apprise_url: autoheal_apprise_url,
webhook_key: autoheal_webhook_key,
webhook_url: autoheal_webhook_url,
}
}
2 changes: 1 addition & 1 deletion src/inquire/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{report::logging::log_message, ERROR};
use crate::{log_message, ERROR};
use bollard::Docker;

pub struct Result {
Expand Down
Loading