Skip to content

Commit

Permalink
v0.2.3 refinements to variables and http connect
Browse files Browse the repository at this point in the history
  • Loading branch information
tmknight committed Jan 8, 2024
1 parent 7d98b5e commit 94f36fa
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 21 deletions.
83 changes: 82 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "docker-autoheal"
version = "0.2.2"
version = "0.2.3"
authors = ["Travis M Knight <travis.knight@tmknight.net>"]
license = "MIT"
description = "Monitor and restart unhealthy docker containers"
Expand All @@ -13,6 +13,7 @@ rust-version = "1.74.1"
bollard = "*"
chrono = "0.4.*"
futures = "0.3.*"
rustls = "0.22.*"
tokio = { version = "1.*", features = ["full"] }

[[bin]]
Expand Down
57 changes: 38 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bollard::container::{ListContainersOptions, RestartContainerOptions};
use bollard::Docker;
use bollard::{Docker, API_DEFAULT_VERSION};
use chrono::prelude::*;
use std::collections::HashMap;
use std::io::{stdout, Write};
Expand All @@ -16,20 +16,28 @@ async fn log_message(msg: &str) {
fn get_env(key: &str, default: &str) -> String {
match std::env::var(key) {
Ok(val) => return val.to_lowercase(),
Err(e) => return default.to_string().to_lowercase(),
Err(_e) => return default.to_string().to_lowercase(),
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Autoheal variables
let autoheal_connection_type = get_env("AUTOHEAL_CONNECTION_TYPE", "local");
let autoheal_container_label = get_env("AUTOHEAL_CONTAINER_LABEL", "autoheal");
let autoheal_default_stop_timeout = get_env("AUTOHEAL_DEFAULT_STOP_TIMEOUT", "10")
.parse()
.unwrap();
let autoheal_interval = get_env("AUTOHEAL_INTERVAL", "5").parse().unwrap();
let autoheal_start_period = get_env("AUTOHEAL_START_PERIOD", "0").parse().unwrap();
// Autoheal core variables
let autoheal_connection_type: String = get_env("AUTOHEAL_CONNECTION_TYPE", "local");
let autoheal_container_label: String = get_env("AUTOHEAL_CONTAINER_LABEL", "autoheal");
let autoheal_stop_timeout: isize = get_env("AUTOHEAL_STOP_TIMEOUT", "10").parse().unwrap();
let autoheal_interval: u64 = get_env("AUTOHEAL_INTERVAL", "5").parse().unwrap();
let autoheal_start_delay: u64 = get_env("AUTOHEAL_START_DELAY", "0").parse().unwrap();
// Autoheal tcp variables
let autoheal_tcp_host: String = get_env("AUTOHEAL_TCP_HOST", "localhost");
let autoheal_tcp_port: u64 = get_env("AUTOHEAL_TCP_PORT", "2375").parse().unwrap();
let autoheal_tcp_address: String = autoheal_tcp_host + ":" + &autoheal_tcp_port.to_string();
let autoheal_tcp_timeout: u64 = get_env("AUTOHEAL_TCP_TIMEOUT", "10").parse().unwrap();
let autoheal_key_path: String =
get_env("AUTOHEAL_KEY_PATH", "/opt/docker-autoheal/tls/key.pem");
let autoheal_cert_path: String =
get_env("AUTOHEAL_CERT_PATH", "/opt/docker-autoheal/tls/cert.pem");
let autoheal_ca_path: String = get_env("AUTOHEAL_CA_PATH", "/opt/docker-autoheal/tls/ca.pem");

// todo
// Webhook variables
Expand All @@ -42,18 +50,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
match autoheal_connection_type.as_str() {
"socket" => {
docker_tmp = Some(
#[cfg(unix)]
// #[cfg(unix)]
Docker::connect_with_socket_defaults()?,
);
}
"http" => {
docker_tmp = Some(Docker::connect_with_http_defaults()?);
docker_tmp = Some(Docker::connect_with_http(
&autoheal_tcp_address,
autoheal_tcp_timeout,
API_DEFAULT_VERSION,
)?);
}
// todo
// "ssl" => {
// docker_tmp = Some(
// // #[cfg(feature = "ssl")]
// Docker::connect_with_ssl_defaults()?,
// #[cfg(feature = "ssl")]
// Docker::connect_with_ssl(
// autoheal_tcp_address,
// autoheal_tcp_timeout,
// Path::new(autoheal_key_path),
// Path::new(autoheal_cert_path),
// Path::new(autoheal_ca_path),
// API_DEFAULT_VERSION
// )?,
// );
// }
&_ => {
Expand All @@ -66,10 +85,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let docker = docker_tmp.unwrap();

// Delay start of loop if specified
if autoheal_start_period > 0 {
let msg0 = format!("Delaying evaluation {}s on request", autoheal_start_period);
if autoheal_start_delay > 0 {
let msg0 = format!("Delaying evaluation {}s on request", autoheal_start_delay);
log_message(&msg0).await;
std::thread::sleep(Duration::from_secs(autoheal_start_period));
std::thread::sleep(Duration::from_secs(autoheal_start_delay));
}

// Establish loop interval
Expand Down Expand Up @@ -106,15 +125,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if !matches!(state.as_str(), "paused" | "restarting") {
// Build restart options
let restart_options = Some(RestartContainerOptions {
t: autoheal_default_stop_timeout,
t: autoheal_stop_timeout,
..Default::default()
});

// Report what is transpiring
let msg0 = format!("Container '{}' ({}) unhealthy", name, id);
let msg1 = format!(
"Restarting '{}' with {}s timeout",
name, autoheal_default_stop_timeout
name, autoheal_stop_timeout
);
log_message(&msg0).await;
log_message(&msg1).await;
Expand Down

0 comments on commit 94f36fa

Please sign in to comment.