From 4c106eb011e9fb6aba835fc038c89ffca364bc25 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 30 Jun 2023 17:55:52 +0000 Subject: [PATCH] Added start script with alerts --- bin/.env | 2 +- bin/start | 114 +++++++++++++++++++++++++++++++++----------- lib/functions.sh | 26 +++++++--- lib/latest-versions | 2 +- 4 files changed, 107 insertions(+), 37 deletions(-) diff --git a/bin/.env b/bin/.env index 2152ab6..98d5b31 100644 --- a/bin/.env +++ b/bin/.env @@ -23,4 +23,4 @@ B2_APPLICATION_KEY_ID="3239c6765fdc" B2_APPLICATION_KEY="0050ac8837466cbca0e0aa574b5f8332f706a5e26c" # Email address for alerts on services that fail to start -ALERTS_EMAIL="derek@federated.computer" +ALERTS_EMAIL="alerts@federated.computer" diff --git a/bin/start b/bin/start index 1bddb06..4c1cf8e 100755 --- a/bin/start +++ b/bin/start @@ -1,56 +1,111 @@ -#!/bin/bash -x +#!/bin/bash # # Federated Start Script . /federated/lib/functions.sh +. /federated/bin/.env usage() { printf -v SERVICES_JOINED '%s|' "${SERVICES[@]}" echo "$0: ${SERVICES_JOINED%|}" exit 2 } -startservice() { - . /federated/bin/.env - echo -ne "* Starting $SERVICE.." - - if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then - echo -ne "is already running.\n" +startservice_jitsi() { + if [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ]; then + echo -ne "* $SERVICE is already running.\n" else if [ -z "$ALERTS_EMAIL" ]; then - cd /federated/apps/$SERVICE && docker-compose -f docker-compose.yml -p $SERVICE up -d &> /dev/null - echo -ne "done.\n" + spin & + SPINPID=$! + + NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/jitsi.sh | awk -F\" '{ print $6 }'` + start_service_withalert "jitsi" "$NC_COMMAND" "$RETRY" "no" + + if [ "$FAILED" = "no" ]; then + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done.\n" + fi else spin & SPINPID=$! NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` - RETRY=`grep start_service /federated/lib/caddy.sh | awk -F\" '{ print $6 }'` - start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" + RETRY=`grep start_service /federated/lib/jitsi.sh | awk -F\" '{ print $6 }'` + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" + if [ "$FAILED" = "no" ]; then + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done.\n" + fi + fi + fi +} +startservice() { + if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then + echo -ne "* $SERVICE is already running.\n" + else + if [ -z "$ALERTS_EMAIL" ]; then + NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'` + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" + + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done." + else + spin & + SPINPID=$! + + NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'` + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" + + disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null echo -ne "done.\n" fi fi } startservice_all() { - . /federated/bin/.env for SERVICE in "${SERVICES[@]}"; do - echo -ne "* Starting $SERVICE.." - if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then - echo -ne "is already running.\n" - else - if [ -z "$ALERTS_EMAIL" ]; then - cd /federated/apps/$SERVICE && docker-compose -f docker-compose.yml -p $SERVICE up -d &> /dev/null - echo -ne "done.\n" + FAILED="no" + if [ "$SERVICE" = "jitsi" ]; then + if [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ]; then + echo -ne "* $SERVICE is already running.\n" + else + startservice_jitsi + fi else - spin & - SPINPID=$! + if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then + echo -ne "* $SERVICE is already running.\n" + else + if [ -z "$ALERTS_EMAIL" ]; then + spin & + SPINPID=$! - NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` - RETRY=`grep start_service /federated/lib/caddy.sh | awk -F\" '{ print $6 }'` - start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" + NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'` + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" - kill -9 $SPINPID &> /dev/null - echo -ne "done.\n" + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done.\n" + else + spin & + SPINPID=$! + + NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'` + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" + + if [ "$FAILED" = "no" ]; then + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done.\n" + fi + fi fi fi done @@ -59,8 +114,11 @@ startservice_all() { [ $# != 1 ] && usage SERVICE=$1 -[ "$SERVICE" = "all" ] && startservice_all -if printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then +if [ "$SERVICE" = "jitsi" ]; then + startservice_jitsi +elif [ "$SERVICE" = "all" ]; then + startservice_all +elif printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then startservice else usage diff --git a/lib/functions.sh b/lib/functions.sh index 5156866..b5572e8 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -161,6 +161,9 @@ start_service_withalert() { SERVICE="$1" COMMAND="$2" RETRY="$3" + ALERT="$4" + + echo -ne "* Starting $SERVICE.." docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE up -d &> /dev/null @@ -172,11 +175,20 @@ start_service_withalert() { break else if [ "$RETRY" == 1 ]; then - EXTERNALIP=`dig @resolver4.opendns.com myip.opendns.com +short 2> /dev/null` - docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE down &> /dev/null - echo "Generated by /federated/bin/start" > /federated/apps/mail/data/root/certs/mailfile - docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"$SERVICE failed to start on $EXTERNALIP\" $ALERTS_EMAIL < /root/certs/mailfile" - fail "There was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' or turn on\ndebug with -d" + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + FAILED="yes" + + if [ "$ALERT" = "yes" ]; then + EXTERNALIP=`dig @resolver4.opendns.com myip.opendns.com +short 2> /dev/null` + docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE down &> /dev/null + echo "Generated by /federated/bin/start" > /federated/apps/mail/data/root/certs/mailfile + docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"$SERVICE failed to start on $EXTERNALIP\" $ALERTS_EMAIL < /root/certs/mailfile" + echo -ne "\n\nThere was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' or turn on\ndebug with set -x\n\n" + else + docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE down &> /dev/null + echo -ne "\n\nThere was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' or turn on\ndebug with set -x\n\n" + fi fi ((RETRY--)) sleep 7 @@ -210,7 +222,7 @@ start_service() { RETRY="$3" # Start /federated/apps/SERVICE with output to /dev/null - echo -ne "\n* Starting /federated/apps/$SERVICE service.." + echo -ne "* Starting $SERVICE.." spin & SPINPID=$! @@ -231,7 +243,7 @@ start_service() { if [ "$RETRY" == 1 ]; then docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE down &> /dev/null kill -9 $SPINPID &> /dev/null - fail "There was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' or turn on\ndebug with -d" + fail "There was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' or turn on\ndebug with set -x" fi ((RETRY--)) sleep 7 diff --git a/lib/latest-versions b/lib/latest-versions index beeb6b6..8152b66 100644 --- a/lib/latest-versions +++ b/lib/latest-versions @@ -5,7 +5,7 @@ traefik=v2.10.1 caddy=latest baserow=1.18.0 collabora=22.05.13.1.1 -element=v1.11.33 +element=v1.11.34 postgresql=14 jitsi=stable-8615 nextcloud=27.0