2024-02-17 12:21:51 -08:00
|
|
|
#!/bin/bash
|
2024-02-29 15:26:48 -08:00
|
|
|
#set -x # debugging flag
|
2024-02-17 12:21:51 -08:00
|
|
|
|
2024-03-02 17:13:44 -08:00
|
|
|
# depends on the $COMPOSE_CONFIG_TEXT variable being populated with the compose config to check
|
|
|
|
function get_shares {
|
|
|
|
SHARES=$(echo "$COMPOSE_CONFIG_TEXT" |\
|
|
|
|
grep /mnt/ |\
|
|
|
|
tr -s ' ' |\
|
|
|
|
cut -d' ' -f 3 |\
|
|
|
|
cut -d'/' -f-4 |\
|
|
|
|
sort -u)
|
|
|
|
#echo "$SHARES" # Debug echo
|
|
|
|
}
|
|
|
|
|
|
|
|
# depends on the $SHARES variable being populated with the shares to check
|
|
|
|
function check_shares {
|
|
|
|
MISSING_SHARES=false
|
|
|
|
for share in $SHARES; do
|
|
|
|
#echo -n " ==== Share $share: " # Debug echo
|
|
|
|
if ! mount | grep -q $share; then
|
|
|
|
#echo "ONLINE" # Debug echo
|
|
|
|
MISSING_SHARES=true
|
|
|
|
#else
|
|
|
|
#echo "OFFLINE" # Debug echo
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect_iscsi {
|
|
|
|
sudo iscsiadm --mode node --targetname "iqn.2020-03.net.jafner:fighter" --portal "192.168.1.10:3260" --login
|
|
|
|
}
|
|
|
|
|
|
|
|
function mount_iscsi {
|
|
|
|
sudo mount /mnt/nas/iscsi
|
|
|
|
}
|
|
|
|
|
2024-03-02 17:21:06 -08:00
|
|
|
if [ "$1" = "clean" ]; then
|
2024-03-02 17:13:44 -08:00
|
|
|
echo " ==== Cleaning up old containers"
|
2024-03-02 00:52:59 -08:00
|
|
|
# Clean up any remnants from unclean shutdown
|
|
|
|
docker stop $(docker ps -q) # shut down running containers
|
|
|
|
docker rm $(docker ps -aq) # remove all containers
|
|
|
|
fi
|
2024-02-29 19:53:36 -08:00
|
|
|
|
2024-02-17 12:21:51 -08:00
|
|
|
# NAS iSCSI
|
2024-03-02 17:13:44 -08:00
|
|
|
# First check for connection; attempt to connect and mount if not connected
|
|
|
|
echo " ==== Checking iSCSI..."
|
2024-02-17 12:21:51 -08:00
|
|
|
if ! sudo iscsiadm -m session | grep -q 'iqn.2020-03.net.jafner:fighter'; then
|
2024-03-02 17:13:44 -08:00
|
|
|
echo " ====== Attempting to connect and mount iSCSI"
|
2024-03-02 17:21:06 -08:00
|
|
|
{
|
|
|
|
connect_iscsi && mount_iscsi && echo " ====== Success!"
|
|
|
|
} || {
|
|
|
|
echo " ====== Could not connect and mount iSCSI"
|
|
|
|
}
|
2024-02-17 12:21:51 -08:00
|
|
|
fi
|
|
|
|
|
2024-03-02 17:13:44 -08:00
|
|
|
# Second check if mounted; attempt to mount if not mounted
|
|
|
|
if ! mount | grep -q '/mnt/nas/iscsi'; then
|
|
|
|
echo " ====== Attempting to mount /mnt/nas/iscsi"
|
2024-03-02 17:21:06 -08:00
|
|
|
{
|
|
|
|
mount_iscsi && echo " ====== Success!"
|
|
|
|
} || {
|
|
|
|
echo " ====== Could not mount /mnt/nas/iscsi"
|
|
|
|
}
|
2024-03-02 17:13:44 -08:00
|
|
|
fi
|
|
|
|
# Regarding the above, we will attempt to re-mount the share if the first step successfully connected and then failed to mount.
|
|
|
|
# This is because I can't be bother to build more elegant logic.
|
|
|
|
|
2024-02-17 12:21:51 -08:00
|
|
|
for stack in /home/admin/homelab/fighter/config/*; do
|
2024-03-02 17:13:44 -08:00
|
|
|
echo " ==== Processing $stack"
|
2024-02-17 12:21:51 -08:00
|
|
|
cd $stack
|
2024-02-29 15:26:48 -08:00
|
|
|
if ! docker compose config > /dev/null; then
|
2024-03-02 17:13:44 -08:00
|
|
|
echo " ====== Invalid compose config: $stack"
|
2024-02-21 13:39:38 -08:00
|
|
|
fi
|
2024-02-29 01:39:44 -08:00
|
|
|
COMPOSE_CONFIG_TEXT=$(docker compose config)
|
2024-03-02 17:13:44 -08:00
|
|
|
MISSING_SHARES=true # Ensures a failure in the checking logic doesn't online the stack
|
|
|
|
get_shares
|
|
|
|
check_shares
|
|
|
|
if ! $MISSING_SHARES; then
|
|
|
|
echo " ====== Bringing up"
|
2024-02-29 17:23:34 -08:00
|
|
|
STACKS_ONLINE+="$(echo $stack | xargs basename)\n"
|
2024-03-09 01:04:44 -08:00
|
|
|
echo -n " ======== Time: "
|
2024-02-29 17:23:34 -08:00
|
|
|
( time docker compose --progress quiet up -d ) 2>&1 | grep real | cut -f 2
|
2024-03-02 17:13:44 -08:00
|
|
|
echo " ====== Done!"
|
|
|
|
else
|
|
|
|
echo " ====== Missing needed network shares. Skipping."
|
2024-03-09 01:04:44 -08:00
|
|
|
STACKS_SKIPPED+="$(echo $stack | xargs basename)\n"
|
2024-02-29 01:39:44 -08:00
|
|
|
fi
|
|
|
|
|
2024-02-17 12:21:51 -08:00
|
|
|
cd /home/admin/homelab/fighter/config/
|
2024-02-29 01:39:44 -08:00
|
|
|
# make sure to overwrite the config text to prevent leaking secrets
|
|
|
|
COMPOSE_CONFIG_TEXT=""
|
2024-02-17 12:21:51 -08:00
|
|
|
done
|
2024-02-20 19:13:01 -08:00
|
|
|
|
2024-02-29 17:23:34 -08:00
|
|
|
echo " ==== List of stacks online:"
|
|
|
|
echo -e "$STACKS_ONLINE"
|
2024-03-09 01:04:44 -08:00
|
|
|
echo " ==== List of stacks skipped:"
|
|
|
|
echo -e "$STACKS_SKIPPED"
|
2024-02-29 17:23:34 -08:00
|
|
|
|
2024-02-20 19:13:01 -08:00
|
|
|
# extra thing because my keycloak healthcheck doesn't work properly
|
2024-02-21 13:39:38 -08:00
|
|
|
echo " ==== Wait 15s, then bring Keycloak forwardauth containers online"
|
2024-02-20 19:13:01 -08:00
|
|
|
cd /home/admin/homelab/fighter/config/keycloak
|
|
|
|
sleep 15
|
2024-02-29 15:28:06 -08:00
|
|
|
docker start keycloak_forwardauth keycloak_forwardauth-privileged
|