Added start script with alerts

This commit is contained in:
root 2023-06-30 17:55:52 +00:00
parent 49a08fa097
commit 4c106eb011
4 changed files with 107 additions and 37 deletions

View File

@ -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"

114
bin/start
View File

@ -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

View File

@ -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

View File

@ -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