From 96da89793cdf8bef3a2b9f0e1804628e321af0c5 Mon Sep 17 00:00:00 2001 From: Joey Hafner Date: Sat, 2 Mar 2024 17:13:44 -0800 Subject: [PATCH] Init improved share handling - Automatically attempt to connect and mount iSCSI - For each stack, check share dependency against availability Add 'clean' "subcommand" to shutdown and remove existing containers Consistently indent console printouts --- homelab/fighter/scripts/startup.sh | 89 +++++++++++++++++++----------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/homelab/fighter/scripts/startup.sh b/homelab/fighter/scripts/startup.sh index 4466bcd5..0762fc26 100644 --- a/homelab/fighter/scripts/startup.sh +++ b/homelab/fighter/scripts/startup.sh @@ -1,54 +1,80 @@ #!/bin/bash #set -x # debugging flag +# 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 +} + if "$1" == "clean"; then + echo " ==== Cleaning up old containers" # 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 -# Check for network mounted devices -# NAS SMB -if ! mount -t cifs | grep -q '/mnt/nas'; then - echo " ==== NAS SMB shares not mounted" - echo " ==== Won't online stacks which depend on SMB shares" - SMB_ONLINE=false -else - SMB_ONLINE=true +# NAS iSCSI +# First check for connection; attempt to connect and mount if not connected +echo " ==== Checking iSCSI..." +if ! sudo iscsiadm -m session | grep -q 'iqn.2020-03.net.jafner:fighter'; then + echo " ====== Attempting to connect and mount iSCSI" + { connect_iscsi && mount_iscsi && echo " ====== Success!" } || { echo " ====== Could not connect and mount iSCSI" } fi -# NAS iSCSI -if ! sudo iscsiadm -m session | grep -q 'iqn.2020-03.net.jafner:fighter'; then - echo " ==== NAS iSCSI session not connected" - if ! mount -t ext4 | grep -q '/mnt/iscsi'; then - echo " ==== /mnt/iscsi not mounted" - echo " ==== Won't online stacks which depend on iSCSI shares" - fi - ISCSI_ONLINE=false -else - ISCSI_ONLINE=true +# 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" + { mount_iscsi && echo " ====== Success!" } || { echo " ====== Could not mount /mnt/nas/iscsi" } 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. for stack in /home/admin/homelab/fighter/config/*; do + echo " ==== Processing $stack" cd $stack if ! docker compose config > /dev/null; then - echo " ==== Invalid compose config: $stack" + echo " ====== Invalid compose config: $stack" fi COMPOSE_CONFIG_TEXT=$(docker compose config) - - # If the stack needs iSCSI and iSCSI isn't available, skip. - if ( echo $COMPOSE_CONFIG_TEXT | grep -q /mnt/iscsi ) && ! $ISCSI_ONLINE; then - echo " ==== $stack is dependent on iSCSI and iSCSI is offline, skipping..." - # Else if the stack needs SMB and SMB isn't available, skip. - elif ( echo $COMPOSE_CONFIG_TEXT | grep -q /mnt/nas ) && ! $SMB_ONLINE; then - echo " ==== $stack is dependent on SMB and SMB is offline, skipping..." - # Else the stack can be onlined. We also add it to a list of onlined services - else - echo " ==== Bringing up $stack " + 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" STACKS_ONLINE+="$(echo $stack | xargs basename)\n" - echo -n " ==== Time: " + echo -n " ====== Time: " ( time docker compose --progress quiet up -d ) 2>&1 | grep real | cut -f 2 - echo " ==== Done!" + echo " ====== Done!" + else + echo " ====== Missing needed network shares. Skipping." fi cd /home/admin/homelab/fighter/config/ @@ -60,7 +86,6 @@ echo " ==== List of stacks online:" echo -e "$STACKS_ONLINE" # extra thing because my keycloak healthcheck doesn't work properly - echo " ==== Wait 15s, then bring Keycloak forwardauth containers online" cd /home/admin/homelab/fighter/config/keycloak sleep 15