From 1aa697f9634e87cbecd0f6174b01633cd4865af0 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 14 May 2024 16:23:11 +0000 Subject: [PATCH] First add of threaded start --- bin/start | 60 +++++++++++++++++------ bin/start.old | 123 +++++++++++++++++++++++++++++++++++++++++++++++ lib/functions.sh | 42 +++++++++++++++- 3 files changed, 208 insertions(+), 17 deletions(-) mode change 100755 => 100644 bin/start create mode 100755 bin/start.old diff --git a/bin/start b/bin/start old mode 100755 new mode 100644 index b987490..5787ba2 --- a/bin/start +++ b/bin/start @@ -10,9 +10,6 @@ usage() { exit 2 } 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 spin & SPINPID=$! @@ -23,7 +20,7 @@ startservice_jitsi() { disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null - echo -ne "done.\n" + echo -ne "done." else spin & SPINPID=$! @@ -34,18 +31,18 @@ startservice_jitsi() { disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null - echo -ne "done.\n" + echo -ne "done." fi - fi } startservice() { - if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then - echo -ne "* $SERVICE is already running.\n" - else + echo -ne "* Starting $SERVICE.." 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/$SERVICE.sh | awk -F\" '{ print $6 }'` - eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null @@ -62,7 +59,6 @@ startservice() { kill -9 $SPINPID &> /dev/null echo -ne "done.\n" fi - fi } startservice_all() { for SERVICE in "${SERVICES[@]}"; do @@ -70,7 +66,7 @@ startservice_all() { [ ! -d "/federated/apps/$SERVICE" ] && echo "* $SERVICE not installed, skipping." && continue FAILED="no" - if [ "$SERVICE" = "jitsi" ]; then + 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 @@ -86,7 +82,7 @@ startservice_all() { 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 }'` - eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null @@ -97,7 +93,7 @@ startservice_all() { 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 }'` - eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" + start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" disown $SPINPID &> /dev/null kill -9 $SPINPID &> /dev/null @@ -107,16 +103,48 @@ startservice_all() { fi done } +startservice_coreapp() { + echo "* Starting $1." + NC_COMMAND=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $6 }'` + eval start_service_withalert2 "$1" "$NC_COMMAND" "$RETRY" "no" +} +startservice_all_new() { + echo "* Starting $1." + NC_COMMAND=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $4 }'` + RETRY=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $6 }'` + eval start_service_withalert2 "$1" "$NC_COMMAND" "$RETRY" "no" +} -#get_installedapps [ $# != 1 ] && usage SERVICE=$1 if [ "$SERVICE" = "jitsi" ]; then + # If jitsi is already running then skip + [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ] && echo "* $SERVICE is already running." && exit 2 startservice_jitsi elif [ "$SERVICE" = "all" ]; then - startservice_all + # Starting all apps + # First start the CORE_APPS one by one + for i in "${CORE_APPS[@]}"; do + [ ! -d "/federated/apps/$i" ] && echo "* $i not installed, skipping." && continue + [ "$(docker container inspect -f '{{.State.Status}}' $i 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue + startservice_coreapp "$i" + done + + # Next start EXTRA_APPS in parallel + for i in "${EXTRA_APPS[@]}"; do + [ ! -d "/federated/apps/$i" ] && echo "* $i not installed, skipping." && continue + [ "$(docker container inspect -f '{{.State.Status}}' $i 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue + [ "$i" = "jitsi" ] && [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue + startservice_all_new "$i" & + done + + wait + exit 0 elif printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then + # If app is already running then skip + [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ] && echo "* $SERVICE is already running." && exit 2 startservice else usage diff --git a/bin/start.old b/bin/start.old new file mode 100755 index 0000000..b987490 --- /dev/null +++ b/bin/start.old @@ -0,0 +1,123 @@ +#!/bin/bash +# +# Federated Start Script +. /federated/lib/functions.sh +. /etc/federated + +usage() { + printf -v SERVICES_JOINED '%s|' "${SERVICES[@]}" + echo "$0: ${SERVICES_JOINED%|}" + exit 2 +} +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 + 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" + + 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/jitsi.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() { + 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 }'` + eval 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 }'` + eval 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() { + for SERVICE in "${SERVICES[@]}"; do + # If app isn't installed then skip + [ ! -d "/federated/apps/$SERVICE" ] && echo "* $SERVICE not installed, skipping." && continue + + 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 + 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/$SERVICE.sh | awk -F\" '{ print $6 }'` + eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no" + + 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 }'` + eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes" + + disown $SPINPID &> /dev/null + kill -9 $SPINPID &> /dev/null + echo -ne "done.\n" + fi + fi + fi + done +} + +#get_installedapps +[ $# != 1 ] && usage +SERVICE=$1 + +if [ "$SERVICE" = "jitsi" ]; then + startservice_jitsi +elif [ "$SERVICE" = "all" ]; then + startservice_all +elif printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then + startservice +else + usage +fi diff --git a/lib/functions.sh b/lib/functions.sh index 2a9f3ed..201c603 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -1,7 +1,9 @@ # Federated Computer functions # Define all services -SERVICES=("pdnsmysql" "pdns" "pdnsadmin" "traefik" "postgresql" "ldap" "mail" "collabora" "nextcloud" "matrix" "element" "listmonk" "vaultwarden" "panel" "wireguard" "jitsi" "baserow" "gitea" "caddy" "autodiscover" "castopod" "wordpress" "coturn" "bookstack" "freescout" "msp" "espocrm" "nginx" "matrixslack" "matrixsignal" "matrixwhatsapp") +CORE_APPS=("pdnsmysql" "pdns" "pdnsadmin" "traefik" "postgresql" "ldap") +EXTRA_APPS=("mail" "collabora" "nextcloud" "matrix" "element" "listmonk" "vaultwarden" "panel" "wireguard" "jitsi" "baserow" "gitea" "caddy" "autodiscover" "castopod" "wordpress" "coturn" "bookstack" "freescout" "msp" "espocrm" "nginx" "matrixslack" "matrixsignal" "matrixwhatsapp") +SERVICES=("${CORE_APPS[@]}" "${EXTRA_APPS[@]}") fail() { echo -ne "FAILED\n\n$1\n\n" @@ -186,6 +188,44 @@ start_service_upgrade() { done fi } +start_service_withalert2() { + SERVICE="$1" + COMMAND="$2" + RETRY="$3" + ALERT="$4" + + # First start the service + docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE up -d &> /dev/null + + # Keep trying service COMMAND to make sure it's up before we proceed + while [ "$RETRY" -gt "0" ]; do + bash -c "eval $COMMAND" &> /dev/null + if [ $? -eq 0 ]; then + break + else + # When RETRY gets down to 1 then we have failed + if [ "$RETRY" == 1 ]; then +# 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 "\nThere was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' while starting or run\ndocker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE up\n" + else + docker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE down &> /dev/null + echo -ne "\nThere was a problem starting service /federated/apps/$SERVICE\nCheck the output of 'docker logs $SERVICE' while starting or run\ndocker-compose -f /federated/apps/$SERVICE/docker-compose.yml -p $SERVICE up\n" + fi + echo -ne "\n" + fi + ((RETRY--)) + sleep 7 + fi + done +} start_service_withalert() { SERVICE="$1" COMMAND="$2"