Skip to content

Commit

Permalink
feat: webhooks support ntfy
Browse files Browse the repository at this point in the history
  • Loading branch information
kristof-mattei committed Nov 28, 2023
1 parent 55aa397 commit a6f110b
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 122 deletions.
45 changes: 0 additions & 45 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ coverage = []
color-eyre = "0.6.2"
hex = "0.4.3"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = [
"env-filter",
"time",
"tracing-log",
] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
http-body-util = "0.1.0"
hyper = { version = "1.0.1", default-features = false }

Expand Down
Empty file removed fails.txt
Empty file.
4 changes: 2 additions & 2 deletions integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Currently setup to a very basic exit 1 on invalid restart and exit 0 on valid re
## Run tests

```
cd tests
cd integration-tests
./tests.sh
```

## Run tests in CI

```
cd tests
cd integration-tests
export "AUTOHEAL_CONTAINER_LABEL=autoheal-123456"
./tests.sh "MY_UNIQUE_BUILD_NUMBER_123456"
```
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ services:
autoheal:
build:
context: ../
args:
APPLICATION_NAME: "autoheal-rs"
3 changes: 2 additions & 1 deletion integration-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ services:
environment:
AUTOHEAL_CONTAINER_LABEL: "${AUTOHEAL_CONTAINER_LABEL:-all}"
AUTOHEAL_INTERVAL: "10"
# WEBHOOK_URL: https://ntfy.sh/...
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
network_mode: none
network_mode: host
1 change: 0 additions & 1 deletion integration-tests/watch-autoheal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM alpine:latest@sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c0162

RUN apk --update add bash docker


RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

Expand Down
24 changes: 11 additions & 13 deletions integration-tests/watch-autoheal/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env bash
set -euxo pipefail

listenToDockerEvents()
{
local expected_restarts
local LOGLINE
expected_restarts=0
docker events --filter 'com.docker.compose.service=should-keep-restarting' --filter 'com.docker.compose.service=shouldnt-restart-*' --filter 'event=restart' | while read -r LOGLINE
do
echo "$LOGLINE"
# may be more elaborate checks here.
[[ $LOGLINE == *"container restart "*"com.docker.compose.service=shouldnt-restart-"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "ERR: No restarts expected on shouldnt-restart-* containers!" 1>&2 && pkill -9 docker && exit 1
[[ $LOGLINE == *"container restart "*"com.docker.compose.service=should-keep-restarting"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "OK: Expected restart on should-keep-restarting container!" && pkill -9 docker && expected_restarts=$((expected_restarts + 1))
[[ $expected_restarts == 1 ]] && echo "OK: All expected restarts happened" && exit 0
done
listenToDockerEvents() {
local expected_restarts
local LOGLINE
expected_restarts=0
docker events --filter 'com.docker.compose.service=should-keep-restarting' --filter 'com.docker.compose.service=shouldnt-restart-*' --filter 'event=restart' | while read -r LOGLINE; do
echo "$LOGLINE"
# may be more elaborate checks here.
[[ $LOGLINE == *"container restart "*"com.docker.compose.service=shouldnt-restart-"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "ERR: No restarts expected on shouldnt-restart-* containers!" 1>&2 && pkill -9 docker && exit 1
[[ $LOGLINE == *"container restart "*"com.docker.compose.service=should-keep-restarting"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "OK: Expected restart on should-keep-restarting container!" && pkill -9 docker && expected_restarts=$((expected_restarts + 1))
[[ $expected_restarts == 1 ]] && echo "OK: All expected restarts happened" && exit 0
done
}

export -f listenToDockerEvents
Expand Down
16 changes: 9 additions & 7 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use hyper::{Method, Response, StatusCode};
use hyper_tls::HttpsConnector;
use hyper_unix_socket::UnixSocketConnector;
use tokio::time::timeout;
use tracing::{event, Level};

use crate::app_config::AppConfig;
use crate::container::Container;
Expand Down Expand Up @@ -102,11 +103,12 @@ impl Docker {

match &container_info.names[..] {
[] => {
tracing::error!("Container name of {} is null, which implies container does not exist - don't restart.", container_short_id);
event!(Level::ERROR, "Container name of {} is null, which implies container does not exist - don't restart.", container_short_id);
},
container_names => {
if container_info.state == "restarting" {
tracing::info!(
event!(
Level::INFO,
"Container {} ({}) found to be restarting - don't restart.",
container_names.join(", "),
container_short_id
Expand All @@ -116,7 +118,7 @@ impl Docker {
.timeout
.unwrap_or(app_config.autoheal_default_stop_timeout);

tracing::info!(
event!(Level::INFO,
"Container {} ({}) found to be unhealthy - Restarting container now with {}s timeout.",
container_names.join(", "),
container_short_id, timeout
Expand All @@ -131,7 +133,7 @@ impl Docker {
);
},
Err(e) => {
tracing::info!(
event!(Level::INFO,
error = ?e,
"Restarting container {} ({}) failed.",
container_names.join(", "),
Expand All @@ -140,9 +142,9 @@ impl Docker {

notify_webhook_failure(
app_config,
&container_names.join(", "),
container_short_id,
&e,
container_names.join(", "),
container_short_id.to_owned(),
e,
);
},
}
Expand Down
15 changes: 11 additions & 4 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tracing::{event, Level};

fn try_parse_env_variable<T>(env_variable_name: &str) -> Result<Option<T>, color_eyre::Report>
where
T: std::str::FromStr,
Expand Down Expand Up @@ -36,11 +38,11 @@ where
{
match try_parse_env_variable(env_variable_name) {
Ok(Some(ct)) => {
tracing::info!("{} set to {:?}", env_variable_name, ct);
event!(Level::INFO, "{} set to {:?}", env_variable_name, ct);
Ok(Some(ct))
},
Ok(None) => {
tracing::info!("{} not set", env_variable_name);
event!(Level::INFO, "{} not set", env_variable_name);
Ok(None)
},
Err(e) => Err(e),
Expand All @@ -61,12 +63,17 @@ where
{
match try_parse_env_variable(env_variable_name) {
Ok(Some(ct)) => {
tracing::info!("{} set to {:?}", env_variable_name, ct);
event!(Level::INFO, "{} set to {:?}", env_variable_name, ct);
Ok(ct)
},

Ok(None) => {
tracing::info!("{} not set, defaulting to {:?}", env_variable_name, default);
event!(
Level::INFO,
"{} not set, defaulting to {:?}",
env_variable_name,
default
);
Ok(default)
},
Err(e) => Err(e),
Expand Down
6 changes: 1 addition & 5 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ use crate::wrap_and_report;

#[no_mangle]
pub extern "C" fn sig_handler(signal: i32) {
event!(
Level::INFO,
message = "Stopping the engine",
raw_signal = signal
);
event!(Level::INFO, raw_signal = signal, "Stopping the engine");
// std::process::exit(128 + signal);
std::process::exit(0);
// RUNNING.store(false, Ordering::SeqCst);
Expand Down
Loading

0 comments on commit a6f110b

Please sign in to comment.