From 4b6d603a46bb9000882dc2c8600feada47813429 Mon Sep 17 00:00:00 2001 From: Ondrej Brablc Date: Mon, 3 Jun 2024 21:39:05 +0200 Subject: [PATCH] Unify script bootstrap --- checks.sh | 11 +++++++++++ config.sh | 27 +++++++-------------------- docker-cmd.sh | 18 +----------------- integrations/zenduty.sh | 8 ++++---- port-alerter.sh | 4 +--- services.sh | 7 +++---- 6 files changed, 27 insertions(+), 48 deletions(-) create mode 100644 checks.sh diff --git a/checks.sh b/checks.sh new file mode 100644 index 0000000..3546694 --- /dev/null +++ b/checks.sh @@ -0,0 +1,11 @@ +mkdir -p "$DATA_DIR" + +if [[ ! -S /var/run/docker.sock ]]; then + log_error "Mount to /var/run/docker.sock missing?" + exit 1 +fi + +if [[ -z $ALERT_SCRIPT ]]; then + log_error "Alert script not defined, alerting to console." + export ALERT_SCRIPT="jq ." +fi diff --git a/config.sh b/config.sh index 548a2e7..4f253fc 100644 --- a/config.sh +++ b/config.sh @@ -1,26 +1,13 @@ SCRIPT_NAME=${0##*/} +SCRIPT_PATH=$(readlink -f $0) +SCRIPT_DIR=${SCRIPT_PATH%/*} +cd "$SCRIPT_DIR" -export LOOP_SLEEP=${LOOP_SLEEP:-10} export ALERTER_URL=${ALERTER_URL:-http://alerter:80} -export SWARM_NAME=${SWARM_NAME:-Swarm} -export LOGGER_USE_TS=1 +export LABEL="swarm-health-alerter.port" export LOGGER_USE_SYSLOG=0 +export LOGGER_USE_TS=1 +export LOOP_SLEEP=${LOOP_SLEEP:-10} +export SWARM_NAME=${SWARM_NAME:-Swarm} export DATA_DIR=${DATA_DIR:-$script_dir/data} -mkdir -p $DATA_DIR - -if [[ ! -S /var/run/docker.sock ]]; then - log_error "Mount to /var/run/docker.sock missing?" - exit 1 -fi - -if [[ -z $ALERT_SCRIPT ]]; then - log_error "Alert script not defined!" - export ALERT_SCRIPT="jq ." -fi - -if [[ ! -f $ALERT_SCRIPT ]]; then - || ! -f $ALERT_SCRIPT ]]; then - log_error "Alert script not accessible on $ALERT_SCRIPT path!" - export ALERT_SCRIPT="jq ." -fi diff --git a/docker-cmd.sh b/docker-cmd.sh index 817361d..094e35e 100755 --- a/docker-cmd.sh +++ b/docker-cmd.sh @@ -1,24 +1,8 @@ #!/usr/bin/env bash -script_path=$(readlink -f $0) -script_dir=${script_path%/*} -cd "$script_dir" source "./config.sh" source "./logger.sh" - -export LOOP_SLEEP=${LOOP_SLEEP:-10} -export ALERTER_URL=${ALERTER_URL:-http://alerter:80} -export SWARM_NAME=${SWARM_NAME:-Swarm} - -if [[ ! -S /var/run/docker.sock ]]; then - log_error "Mount to /var/run/docker.sock missing?" - exit 1 -fi - -if [[ -z $ALERT_SCRIPT || ! -f $ALERT_SCRIPT ]]; then - log_error "Alert script not defined or not accessible on \"$ALERT_SCRIPT\" path!" - export ALERT_SCRIPT="jq ." -fi +source "./checks.sh" log_info "Starting event alerter ..." ./event-alerter.py & diff --git a/integrations/zenduty.sh b/integrations/zenduty.sh index 11e879e..807d869 100755 --- a/integrations/zenduty.sh +++ b/integrations/zenduty.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -script_path=$(readlink -f $0) -script_dir=${script_path%/*} -source "$script_dir/../logger.sh" +SCRIPT_PATH=$(readlink -f $0) +SCRIPT_DIR=${SCRIPT_PATH%/*} +source "$SCRIPT_DIR/../logger.sh" -DATA_DIR=${DATA_DIR:-$script_dir/../data} +DATA_DIR=${DATA_DIR:-$SCRIPT_DIR/../data} input_file=$(mktemp $DATA_DIR/zenduty-input.XXXXXX) trap "rm -f $input_file" EXIT diff --git a/port-alerter.sh b/port-alerter.sh index 87cf8f1..1ac5f8a 100755 --- a/port-alerter.sh +++ b/port-alerter.sh @@ -1,10 +1,8 @@ #!/usr/bin/env bash -script_path=$(readlink -f $0) -script_dir=${script_path%/*} -cd "$script_dir" source "./config.sh" source "./logger.sh" +source "./checks.sh" function check_services() { local swarm_name=$SWARM_NAME diff --git a/services.sh b/services.sh index c617662..e9bbf3b 100755 --- a/services.sh +++ b/services.sh @@ -1,9 +1,8 @@ #!/usr/bin/env bash -source ./config.sh -source ./logger.sh - -LABEL="swarm-health-alerter.port" +source "./config.sh" +source "./logger.sh" +source "./checks.sh" ./docker-api.sh /services > /tmp/services if [ $? -ne 0 ]; then