Skip to content

Commit

Permalink
Initial commmit
Browse files Browse the repository at this point in the history
Just proof of concept, so far no alerting
  • Loading branch information
brablc committed May 31, 2024
1 parent 1ea8b9c commit 94077ad
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM alpine:latest

ENV DOCKERIZE_VERSION v0.7.0

WORKDIR /app

COPY *.sh ./

RUN apk update --no-cache \
&& apk add --no-cache bash curl jq openssl \
&& curl -s -L https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz | tar xzf - -C .

HEALTHCHECK \
--interval=10s \
--timeout=301s \
--retries=2 \
--start-period=300s \
CMD ./docker-healthcheck.sh

CMD ["./docker-cmd.sh"]
3 changes: 3 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OK_FILE="/tmp/ok"
LOGGER_USE_TS=1
SCRIPT_NAME=${0##*/}
28 changes: 28 additions & 0 deletions docker-cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

source ./config.sh
source ./logger.sh

SLEEP=${SLEEP-10s}

function get_cmd() {
cmd=(./dockerize -timeout 300s -wait-retry-interval 5s)
while read SERVICE; do
cmd+=(-wait $SERVICE)
done < <(./services.sh)
cmd+=(touch $OK_FILE)
echo ${cmd[@]}
}

log_info "Entering loop with ${SLEEP} sleep ..."

while true; do
eval $(get_cmd)
if [ -f $OK_FILE ]; then
log_info OK
sleep $SLEEP
rm -f $OK_FILE
else
log_error TIMEOUT
fi
done
27 changes: 27 additions & 0 deletions docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

LOOP_SLEEP=${LOOP_SLEEP-60s}

source ./config.sh
source ./logger.sh

log_info "Using email: $CERTBOT_EMAIL"
log_info "Initial list of domains from certbot.domain labels ..."
./domains.sh

LAST_DATE=$(date +"%Y-%m-%d")

log_info "Entering loop with $LOOP_SLEEP sleep ..."
while true; do
sleep $LOOP_SLEEP

NEW_DATE=$(date +"%Y-%m-%d")

if [[ $LAST_DATE != $NEW_DATE ]]; then
LAST_DATE=$NEW_DATE
log_info "New date detected renewing ..."
./renew.sh
else
./issue.sh
fi
done
47 changes: 47 additions & 0 deletions logger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
log() {
local level color message
level=$1
message=$2

case "$level" in
INFO)
color=$'\e[36m'
;;
WARNING)
color=$'\e[35m'
;;
ERROR)
color=$'\e[31m'
;;
*)
color=$'\e[39m'
;;
esac

reset=$'\e[39m'
if [[ -n $LOGGER_USE_TS ]]; then
timestamp=$(date +"%Y-%m-%d %H:%M:%S|")
else
timestamp=""
fi

if [[ -t 1 || -n $CONTENT_TYPE ]]; then
echo -e "${color}-${level:0:1}|${timestamp}${message}${reset}"
else
echo "-${level:0:1}|${timestamp}${message}"
fi

logger -t $SCRIPT_NAME -p user.${level,,} "${level} - ${message}"
}

log_info() {
log "INFO" "$1"
}

log_warn() {
log "WARNING" "$1"
}

log_error() {
log "ERROR" "$1"
}
18 changes: 18 additions & 0 deletions services.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

SOCK=/var/run/docker.sock
URL=http://v1.45/services

LABEL="swarm-health-alerter.port"

function get_services() {
curl -s --unix-socket $SOCK $URL \
| jq -r '.[] | select(.Spec.Labels["com.docker.stack.namespace"] != null) | .Spec.Name'
}

while read service; do
ports=$(curl -s --unix-socket $SOCK $URL/$service | jq -r '.Spec.Labels["'$LABEL'"]')
test "$ports" != "null" || continue
network_alias=$(curl -s --unix-socket $SOCK $URL/$service | jq -r '.Spec.TaskTemplate.Networks[].Aliases[]' | sort | head -1)
echo $ports | sed 's/,/\n/g' | while read port; do echo "tcp://$network_alias:$port"; done
done < <(get_services)

0 comments on commit 94077ad

Please sign in to comment.