From 6d1a9fc77bbadbd678fad8f7c2a3282de5ea74a9 Mon Sep 17 00:00:00 2001 From: Heavybullets8 Date: Fri, 10 Jun 2022 13:40:35 -0600 Subject: [PATCH] longopts + dns + refactor --- heavy_script.sh | 92 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/heavy_script.sh b/heavy_script.sh index 70e327cf..0bed593e 100644 --- a/heavy_script.sh +++ b/heavy_script.sh @@ -1,30 +1,49 @@ #!/bin/bash + #If no argument is passed, kill the script. +[[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && echo "This script requires an argument, use --help for help" && exit -[[ -z "$*" ]] && echo "This script requires an argument, use -h for help" && exit -while getopts ":hsi:mrb:t:uUpSRv" opt +while getopts ":si:rb:t:uUpSRv-:" opt do case $opt in - h) - echo "-m | Initiates mounting feature, choose between unmounting and mounting PVC data" - echo "-r | Opens a menu to restore a heavy_script backup that was taken on you ix-applications pool" - echo "-b | Back-up your ix-applications dataset, specify a number after -b" - echo "-i | Add application to ignore list, one by one, see example below." - echo "-R | Roll-back applications if they fail to update" - echo "-S | Shutdown applications prior to updating" - echo "-v | verbose output" - echo "-t | Set a custom timeout in seconds when checking if either an App or Mountpoint correctly Started, Stopped or (un)Mounted. Defaults to 500 seconds" - echo "-s | sync catalog" - echo "-U | Update all applications, ignores versions" - echo "-u | Update all applications, does not update Major releases" - echo "-p | Prune unused/old docker images" - echo "EX | bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vRsUp" - echo "EX | bash /mnt/tank/scripts/heavy_script.sh -t 8812 -m" - exit - ;; + -) + case "${OPTARG}" in + dns) + dns="true" + ;; + restore) + restore="true" + ;; + mount) + mount="true" + ;; + help) + echo "-m | Initiates mounting feature, choose between unmounting and mounting PVC data" + echo "-r | Opens a menu to restore a heavy_script backup that was taken on you ix-applications pool" + echo "-b | Back-up your ix-applications dataset, specify a number after -b" + echo "-i | Add application to ignore list, one by one, see example below." + echo "-R | Roll-back applications if they fail to update" + echo "-S | Shutdown applications prior to updating" + echo "-v | verbose output" + echo "-t | Set a custom timeout in seconds when checking if either an App or Mountpoint correctly Started, Stopped or (un)Mounted. Defaults to 500 seconds" + echo "-s | sync catalog" + echo "-U | Update all applications, ignores versions" + echo "-u | Update all applications, does not update Major releases" + echo "-p | Prune unused/old docker images" + echo "EX | bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vRsUp" + echo "EX | bash /mnt/tank/scripts/heavy_script.sh -t 8812 -m" + exit + ;; + + *) + echo "Invalid Option -$OPTARG, type --help for help" + exit + ;; + esac + ;; \?) - echo "Invalid Option -$OPTARG, type -h for help" + echo "Invalid Option -$OPTARG, type --help for help" exit ;; :) @@ -38,7 +57,7 @@ do [[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit ;; r) - restore="true" + rollback="true" ;; i) ignore+=("$OPTARG") @@ -48,9 +67,6 @@ do timeout=$OPTARG ! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit ;; - m) - mount="true" - ;; s) sync="true" ;; @@ -318,10 +334,38 @@ echo ' |___/ |_| ' echo } export -f title + +dns(){ +clear -x +echo "Generating DNS Names.." +#ignored dependency pods, No change required +ignore="cron|kube-system|nvidia|svclb|NAME|memcached" + +# Pulling pod names + +mapfile -t main < <(k3s kubectl get pods -A | grep -Ev "$ignore" | sort) + +# Pulling all ports +all_ports=$(k3s kubectl get service -A) + +clear -x +count=0 +for i in "${main[@]}" +do + [[ count -le 0 ]] && echo -e "\n" && ((count++)) + appName=$(echo "$i" | awk '{print $2}' | sed 's/-[^-]*-[^-]*$//' | sed 's/-0//') + ixName=$(echo "$i" | awk '{print $1}') + port=$(echo "$all_ports" | grep -E "\s$appName\s" | awk '{print $6}' | grep -Eo "^[[:digit:]]+{1}") + echo -e "$appName.$ixName.svc.cluster.local $port" +done | uniq | nl -b t | sed 's/\s\s\s$/- -------- ----/' | column -t -R 1 -N "#,DNS_Name,Port" -L +} +export -f dns + #exit if incompatable functions are called [[ "$restore" == "true" && "$mount" == "true" ]] && echo -e "The Restore Function(-r)\nand\nMount Function(-m)\nCannot both be called at the same time." && exit [[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && exit #Continue to call functions in specific order +[[ "$dns" == "true" ]] && dns && exit [[ "$restore" == "true" ]] && restore && exit [[ "$mount" == "true" ]] && mount && exit [[ "$number_of_backups" -ge 1 ]] && backup