code cleanup
This commit is contained in:
		
							
								
								
									
										231
									
								
								heavy_script.sh
									
									
									
									
									
								
							
							
						
						
									
										231
									
								
								heavy_script.sh
									
									
									
									
									
								
							@@ -4,108 +4,37 @@
 | 
			
		||||
[[ -z "$*" || "-" == "$*" || "--" == "$*"  ]] && echo "This script requires an argument, use --help for help" && exit
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
while getopts ":si:rb:t:uUpSRv-:" opt
 | 
			
		||||
do
 | 
			
		||||
  case $opt in
 | 
			
		||||
     -)
 | 
			
		||||
        case "${OPTARG}" in
 | 
			
		||||
           help)
 | 
			
		||||
                clear -x
 | 
			
		||||
                echo "Basic Utilities"
 | 
			
		||||
                echo "--mount         | Initiates mounting feature, choose between unmounting and mounting PVC data"
 | 
			
		||||
                echo "--restore       | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset"
 | 
			
		||||
                echo "--delete backup | Opens a menu to delete backups on your system"
 | 
			
		||||
                echo "--dns           | list all of your applications DNS names and their web ports"
 | 
			
		||||
                echo
 | 
			
		||||
                echo "Update Options"
 | 
			
		||||
                echo "-U | Update all applications, ignores versions"
 | 
			
		||||
                echo "-u | Update all applications, does not update Major releases"
 | 
			
		||||
                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 | THIS OPTION WILL DEPRICATE SOON, USE \"-r\" instead. Roll-back applications if they fail to update"
 | 
			
		||||
                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 "-p | Prune unused/old docker images"
 | 
			
		||||
                echo 
 | 
			
		||||
                echo "Examples"
 | 
			
		||||
                echo "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vrsUp"
 | 
			
		||||
                echo "bash /mnt/tank/scripts/heavy_script.sh -t 150 --mount"
 | 
			
		||||
                echo "bash /mnt/tank/scripts/heavy_script.sh --dns"
 | 
			
		||||
                echo "bash /mnt/tank/scripts/heavy_script.sh --restore"  
 | 
			
		||||
                echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup"
 | 
			
		||||
                echo         
 | 
			
		||||
                exit
 | 
			
		||||
                ;;
 | 
			
		||||
            dns)
 | 
			
		||||
                dns="true"
 | 
			
		||||
                ;;
 | 
			
		||||
        restore)
 | 
			
		||||
                restore="true"
 | 
			
		||||
                ;;
 | 
			
		||||
          mount)
 | 
			
		||||
                mount="true"
 | 
			
		||||
                ;;
 | 
			
		||||
  delete-backup)
 | 
			
		||||
                deleteBackup="true"
 | 
			
		||||
                ;;
 | 
			
		||||
              *)
 | 
			
		||||
                echo "Invalid Option --$OPTARG, type --help for help"
 | 
			
		||||
                exit
 | 
			
		||||
                ;;
 | 
			
		||||
        esac
 | 
			
		||||
        ;;
 | 
			
		||||
    \?)
 | 
			
		||||
      echo "Invalid Option -$OPTARG, type --help for help"
 | 
			
		||||
      exit
 | 
			
		||||
      ;;
 | 
			
		||||
    :)
 | 
			
		||||
      echo "Option: -$OPTARG requires an argument" >&2
 | 
			
		||||
      exit
 | 
			
		||||
      ;;
 | 
			
		||||
    b)
 | 
			
		||||
      re='^[0-9]+$'
 | 
			
		||||
      number_of_backups=$OPTARG
 | 
			
		||||
      ! [[ $OPTARG =~ $re  ]] && echo -e "Error: -b needs to be assigned an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit
 | 
			
		||||
      [[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit
 | 
			
		||||
      ;;
 | 
			
		||||
    r)
 | 
			
		||||
      rollback="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    i)
 | 
			
		||||
      ignore+=("$OPTARG")
 | 
			
		||||
      ;;
 | 
			
		||||
    t)
 | 
			
		||||
      re='^[0-9]+$'
 | 
			
		||||
      timeout=$OPTARG
 | 
			
		||||
      ! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit
 | 
			
		||||
      ;;
 | 
			
		||||
    s)
 | 
			
		||||
      sync="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    U)
 | 
			
		||||
      update_all_apps="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    u)
 | 
			
		||||
      update_apps="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    S)
 | 
			
		||||
      stop_before_update="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    p)
 | 
			
		||||
      prune="true"
 | 
			
		||||
      ;;
 | 
			
		||||
    R)
 | 
			
		||||
      rollback="true"
 | 
			
		||||
      echo "WARNING: -R is being transisitioned to -r, this is due to a refactor in the script. Please Make the change ASAP!"
 | 
			
		||||
      ;;
 | 
			
		||||
    v)
 | 
			
		||||
      verbose="true"
 | 
			
		||||
      ;;
 | 
			
		||||
  esac
 | 
			
		||||
done
 | 
			
		||||
help(){
 | 
			
		||||
[[ $help == "true" ]] && clear -x
 | 
			
		||||
echo "Basic Utilities"
 | 
			
		||||
echo "--mount         | Initiates mounting feature, choose between unmounting and mounting PVC data"
 | 
			
		||||
echo "--restore       | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset"
 | 
			
		||||
echo "--delete backup | Opens a menu to delete backups on your system"
 | 
			
		||||
echo "--dns           | list all of your applications DNS names and their web ports"
 | 
			
		||||
echo
 | 
			
		||||
echo "Update Options"
 | 
			
		||||
echo "-U | Update all applications, ignores versions"
 | 
			
		||||
echo "-u | Update all applications, does not update Major releases"
 | 
			
		||||
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 | THIS OPTION WILL DEPRICATE SOON, USE \"-r\" instead. Roll-back applications if they fail to update"
 | 
			
		||||
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 "-p | Prune unused/old docker images"
 | 
			
		||||
echo 
 | 
			
		||||
echo "Examples"
 | 
			
		||||
echo "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vrsUp"
 | 
			
		||||
echo "bash /mnt/tank/scripts/heavy_script.sh -t 150 --mount"
 | 
			
		||||
echo "bash /mnt/tank/scripts/heavy_script.sh --dns"
 | 
			
		||||
echo "bash /mnt/tank/scripts/heavy_script.sh --restore"  
 | 
			
		||||
echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup"
 | 
			
		||||
echo         
 | 
			
		||||
exit
 | 
			
		||||
}
 | 
			
		||||
export -f help
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
deleteBackup(){
 | 
			
		||||
@@ -126,6 +55,7 @@ else
 | 
			
		||||
    echo "Invalid Selection"
 | 
			
		||||
fi
 | 
			
		||||
}
 | 
			
		||||
export -f deleteBackup
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
backup(){
 | 
			
		||||
@@ -245,7 +175,7 @@ elif [[ $selection == "2" ]]; then
 | 
			
		||||
    done
 | 
			
		||||
    rmdir /mnt/heavyscript
 | 
			
		||||
else
 | 
			
		||||
  echo "Invalid selection, type -h for help"
 | 
			
		||||
    echo "Invalid selection, \"$selection\" was not an option"
 | 
			
		||||
fi
 | 
			
		||||
}
 | 
			
		||||
export -f mount
 | 
			
		||||
@@ -258,10 +188,10 @@ export -f sync
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
update_apps(){
 | 
			
		||||
    mapfile -t array < <(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,container_images_update_available,status' | grep -E ",true(,|$)" | sort)
 | 
			
		||||
    [[ -z $array ]] && echo -e "\nThere are no updates available" && return 0 || echo -e "\n${#array[@]} update(s) available"
 | 
			
		||||
    [[ -z $timeout ]] && echo -e "\nDefault Timeout: 500" && timeout=500 || echo -e "\nCustom Timeout: $timeout"
 | 
			
		||||
    [[ "$timeout" -le 120 ]] && echo "Warning: Your timeout is set low and may lead to premature rollbacks or skips"
 | 
			
		||||
mapfile -t array < <(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,container_images_update_available,status' | grep -E ",true(,|$)" | sort)
 | 
			
		||||
[[ -z $array ]] && echo -e "\nThere are no updates available" && return 0 || echo -e "\n${#array[@]} update(s) available"
 | 
			
		||||
[[ -z $timeout ]] && echo -e "\nDefault Timeout: 500" && timeout=500 || echo -e "\nCustom Timeout: $timeout"
 | 
			
		||||
[[ "$timeout" -le 120 ]] && echo "Warning: Your timeout is set low and may lead to premature rollbacks or skips"
 | 
			
		||||
    for i in "${array[@]}"
 | 
			
		||||
    do
 | 
			
		||||
        app_name=$(echo "$i" | awk -F ',' '{print $1}') #print out first catagory, name.
 | 
			
		||||
@@ -319,6 +249,7 @@ update_apps(){
 | 
			
		||||
}
 | 
			
		||||
export -f update_apps
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
after_update_actions(){
 | 
			
		||||
SECONDS=0
 | 
			
		||||
count=0
 | 
			
		||||
@@ -384,11 +315,13 @@ fi
 | 
			
		||||
}
 | 
			
		||||
export -f after_update_actions
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
prune(){
 | 
			
		||||
echo -e "\nPruning Docker Images" && docker image prune -af | grep "^Total" || echo "Failed to Prune Docker Images"
 | 
			
		||||
}
 | 
			
		||||
export -f prune
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
title(){
 | 
			
		||||
echo ' _   _                        _____           _       _   '
 | 
			
		||||
echo '| | | |                      /  ___|         (_)     | | '
 | 
			
		||||
@@ -402,10 +335,94 @@ echo
 | 
			
		||||
}
 | 
			
		||||
export -f title
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Parse script options
 | 
			
		||||
while getopts ":si:rb:t:uUpSRv-:" opt
 | 
			
		||||
do
 | 
			
		||||
    case $opt in
 | 
			
		||||
      -)
 | 
			
		||||
          case "${OPTARG}" in
 | 
			
		||||
            help)
 | 
			
		||||
                  help="true"
 | 
			
		||||
                  ;;
 | 
			
		||||
              dns)
 | 
			
		||||
                  dns="true"
 | 
			
		||||
                  ;;
 | 
			
		||||
          restore)
 | 
			
		||||
                  restore="true"
 | 
			
		||||
                  ;;
 | 
			
		||||
            mount)
 | 
			
		||||
                  mount="true"
 | 
			
		||||
                  ;;
 | 
			
		||||
    delete-backup)
 | 
			
		||||
                  deleteBackup="true"
 | 
			
		||||
                  ;;
 | 
			
		||||
                *)
 | 
			
		||||
                  echo -e "Invalid Option \"--$OPTARG\"\n" && help
 | 
			
		||||
                  exit
 | 
			
		||||
                  ;;
 | 
			
		||||
          esac
 | 
			
		||||
          ;;
 | 
			
		||||
      \?)
 | 
			
		||||
        echo -e "Invalid Option \"-$OPTARG\"\n" && help
 | 
			
		||||
        exit
 | 
			
		||||
        ;;
 | 
			
		||||
      :)
 | 
			
		||||
        echo -e "Option: \"-$OPTARG\" requires an argument\n" && help
 | 
			
		||||
        exit
 | 
			
		||||
        ;;
 | 
			
		||||
      b)
 | 
			
		||||
        re='^[0-9]+$'
 | 
			
		||||
        number_of_backups=$OPTARG
 | 
			
		||||
        ! [[ $OPTARG =~ $re  ]] && echo -e "Error: -b needs to be assigned an interger\n\"""$number_of_backups""\" is not an interger" >&2 && exit
 | 
			
		||||
        [[ "$number_of_backups" -le 0 ]] && echo "Error: Number of backups is required to be at least 1" && exit
 | 
			
		||||
        ;;
 | 
			
		||||
      r)
 | 
			
		||||
        rollback="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      i)
 | 
			
		||||
        ignore+=("$OPTARG")
 | 
			
		||||
        ;;
 | 
			
		||||
      t)
 | 
			
		||||
        re='^[0-9]+$'
 | 
			
		||||
        timeout=$OPTARG
 | 
			
		||||
        ! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit
 | 
			
		||||
        ;;
 | 
			
		||||
      s)
 | 
			
		||||
        sync="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      U)
 | 
			
		||||
        update_all_apps="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      u)
 | 
			
		||||
        update_apps="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      S)
 | 
			
		||||
        stop_before_update="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      p)
 | 
			
		||||
        prune="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      R)
 | 
			
		||||
        rollback="true"
 | 
			
		||||
        echo "WARNING: -R is being transisitioned to -r, this is due to a refactor in the script. Please Make the change ASAP!"
 | 
			
		||||
        ;;
 | 
			
		||||
      v)
 | 
			
		||||
        verbose="true"
 | 
			
		||||
        ;;
 | 
			
		||||
      *)
 | 
			
		||||
        echo -e "Invalid Option \"--$OPTARG\"\n" && help
 | 
			
		||||
        exit
 | 
			
		||||
        ;;
 | 
			
		||||
    esac
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#exit if incompatable functions are called
 | 
			
		||||
[[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && exit
 | 
			
		||||
 | 
			
		||||
#Continue to call functions in specific order
 | 
			
		||||
[[ "$help" == "true" ]] && help
 | 
			
		||||
[[ "$deleteBackup" == "true" ]] && deleteBackup && exit
 | 
			
		||||
[[ "$dns" == "true" ]] && dns && exit
 | 
			
		||||
[[ "$restore" == "true" ]] && restore && exit
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user