diff --git a/functions/mount.sh b/functions/mount.sh index c0785e64..ece5861d 100644 --- a/functions/mount.sh +++ b/functions/mount.sh @@ -76,41 +76,62 @@ read -rt 120 -p "Unmount All Please type a number: " selection case $selection in 0) - echo "Exitting.." + echo "Exiting.." exit ;; 1) - list=$(k3s kubectl get pvc -A | sort -u | awk '{print NR-1, "\t" $1 "\t" $2 "\t" $4}' | column -t | sed "s/^0/ /") - echo "$list" - read -rt 120 -p "Please type a number: " selection - app=$(echo -e "$list" | grep ^"$selection " | awk '{print $2}' | cut -c 4- ) - [[ -z "$app" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script - pvc=$(echo -e "$list" | grep ^"$selection ") - status=$(cli -m csv -c 'app chart_release query name,status' | grep -E "^$app\b" | awk -F ',' '{print $2}'| tr -d " \t\n\r") - if [[ "$status" != "STOPPED" ]]; then - [[ -z $timeout ]] && echo -e "\nDefault Timeout: 500" && timeout=500 || echo -e "\nCustom Timeout: $timeout" - SECONDS=0 && echo -e "\nScaling down $app" && midclt call chart.release.scale "$app" '{"replica_count": 0}' &> /dev/null - else - echo -e "\n$app is already stopped" - fi - while [[ "$SECONDS" -le "$timeout" && "$status" != "STOPPED" ]] + while true do + list=$(k3s kubectl get pvc -A | sort -u | awk '{print NR-1, "\t" $1 "\t" $2 "\t" $4}' | column -t | sed "s/^0/ /") + echo "$list" + read -rt 120 -p "Please type a number: " selection + app=$(echo -e "$list" | grep ^"$selection " | awk '{print $2}' | cut -c 4- ) + [[ -z "$app" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script + pvc=$(echo -e "$list" | grep ^"$selection ") status=$(cli -m csv -c 'app chart_release query name,status' | grep -E "^$app\b" | awk -F ',' '{print $2}'| tr -d " \t\n\r") - echo -e "Waiting $((timeout-SECONDS)) more seconds for $app to be STOPPED" && sleep 5 + if [[ "$status" != "STOPPED" ]]; then + [[ -z $timeout ]] && echo -e "\nDefault Timeout: 500" && timeout=500 || echo -e "\nCustom Timeout: $timeout" + SECONDS=0 && echo -e "\nScaling down $app" && midclt call chart.release.scale "$app" '{"replica_count": 0}' &> /dev/null + else + echo -e "\n$app is already stopped" + fi + while [[ "$SECONDS" -le "$timeout" && "$status" != "STOPPED" ]] + do + status=$(cli -m csv -c 'app chart_release query name,status' | grep -E "^$app\b" | awk -F ',' '{print $2}'| tr -d " \t\n\r") + echo -e "Waiting $((timeout-SECONDS)) more seconds for $app to be STOPPED" && sleep 5 + done + data_name=$(echo "$pvc" | awk '{print $3}') + mount=$(echo "$pvc" | awk '{print $4}') + volume_name=$(echo "$pvc" | awk '{print $4}') + mapfile -t full_path < <(zfs list | grep "$volume_name" | awk '{print $1}') + if [[ "${#full_path[@]}" -gt 1 ]]; then #if there is another app with the same name on another pool, use the current pools application, since the other instance is probably old, or unused, or a backup. + echo "$app is using the same volume identifier on more than one pool.. attempting to use your current kubernetes apps pool" + pool=$(cli -c 'app kubernetes config' | grep -E "dataset\s\|" | awk -F '|' '{print $3}' | awk -F '/' '{print $1}' | tr -d " \t\n\r") + full_path=$(zfs list | grep "$volume_name" | grep "$pool" | awk '{print $1}') + fi + echo -e "\nMounting\n$full_path\nTo\n/mnt/heavyscript/$data_name" + zfs set mountpoint=/heavyscript/"$data_name" "$full_path" || echo "Failed to mount $app" + echo -e "Mounted\n\nUnmount with:\nzfs set mountpoint=legacy $full_path && rmdir /mnt/heavyscript/$data_name\n\nOr use the Unmount All option\n" + while [[ $yesno != 0 ]] + do + echo "Would you like to mount anything else?" + echo "1 Yes" + echo "2 No" + read -rt 120 -p "Unmount All Please type a number: " yesno + case $yesno in + 1) + continue + ;; + 2) + exit + ;; + *) + echo "Invalid selection \"$yesno\" was not an option" sleep 3 + continue + ;; + esac + done done - data_name=$(echo "$pvc" | awk '{print $3}') - mount=$(echo "$pvc" | awk '{print $4}') - volume_name=$(echo "$pvc" | awk '{print $4}') - mapfile -t full_path < <(zfs list | grep "$volume_name" | awk '{print $1}') - if [[ "${#full_path[@]}" -gt 1 ]]; then #if there is another app with the same name on another pool, use the current pools application, since the other instance is probably old, or unused, or a backup. - echo "$app is using the same volume identifier on more than one pool.. attempting to use your current kubernetes apps pool" - pool=$(cli -c 'app kubernetes config' | grep -E "dataset\s\|" | awk -F '|' '{print $3}' | awk -F '/' '{print $1}' | tr -d " \t\n\r") - full_path=$(zfs list | grep "$volume_name" | grep "$pool" | awk '{print $1}') - fi - echo -e "\nMounting\n$full_path\nTo\n/mnt/heavyscript/$data_name" - zfs set mountpoint=/heavyscript/"$data_name" "$full_path" || echo "Failed to mount $app" - echo -e "Mounted\n\nUnmount with:\nzfs set mountpoint=legacy $full_path && rmdir /mnt/heavyscript/$data_name\n\nOr use the Unmount All option\n" - exit ;; 2) mapfile -t unmount_array < <(basename -a /mnt/heavyscript/* | sed "s/*//")