code cleanup

This commit is contained in:
Heavybullets8 2022-06-11 00:13:09 -06:00
parent 9e429e2a5b
commit 45127be73a

View File

@ -4,108 +4,37 @@
[[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && echo "This script requires an argument, use --help for help" && exit [[ -z "$*" || "-" == "$*" || "--" == "$*" ]] && echo "This script requires an argument, use --help for help" && exit
while getopts ":si:rb:t:uUpSRv-:" opt help(){
do [[ $help == "true" ]] && clear -x
case $opt in echo "Basic Utilities"
-) echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data"
case "${OPTARG}" in echo "--restore | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset"
help) echo "--delete backup | Opens a menu to delete backups on your system"
clear -x echo "--dns | list all of your applications DNS names and their web ports"
echo "Basic Utilities" echo
echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data" echo "Update Options"
echo "--restore | Opens a menu to restore a \"heavy_script\" backup that was taken on your \"ix-applications\" dataset" echo "-U | Update all applications, ignores versions"
echo "--delete backup | Opens a menu to delete backups on your system" echo "-u | Update all applications, does not update Major releases"
echo "--dns | list all of your applications DNS names and their web ports" echo "-b | Back-up your ix-applications dataset, specify a number after -b"
echo echo "-i | Add application to ignore list, one by one, see example below."
echo "Update Options" echo "-R | THIS OPTION WILL DEPRICATE SOON, USE \"-r\" instead. Roll-back applications if they fail to update"
echo "-U | Update all applications, ignores versions" echo "-r | Roll-back applications if they fail to update"
echo "-u | Update all applications, does not update Major releases" echo "-S | Shutdown applications prior to updating"
echo "-b | Back-up your ix-applications dataset, specify a number after -b" echo "-v | verbose output"
echo "-i | Add application to ignore list, one by one, see example below." 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 "-R | THIS OPTION WILL DEPRICATE SOON, USE \"-r\" instead. Roll-back applications if they fail to update" echo "-s | sync catalog"
echo "-r | Roll-back applications if they fail to update" echo "-p | Prune unused/old docker images"
echo "-S | Shutdown applications prior to updating" echo
echo "-v | verbose output" echo "Examples"
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 "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vrsUp"
echo "-s | sync catalog" echo "bash /mnt/tank/scripts/heavy_script.sh -t 150 --mount"
echo "-p | Prune unused/old docker images" echo "bash /mnt/tank/scripts/heavy_script.sh --dns"
echo echo "bash /mnt/tank/scripts/heavy_script.sh --restore"
echo "Examples" echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup"
echo "bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vrsUp" echo
echo "bash /mnt/tank/scripts/heavy_script.sh -t 150 --mount" exit
echo "bash /mnt/tank/scripts/heavy_script.sh --dns" }
echo "bash /mnt/tank/scripts/heavy_script.sh --restore" export -f help
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
deleteBackup(){ deleteBackup(){
@ -126,6 +55,7 @@ else
echo "Invalid Selection" echo "Invalid Selection"
fi fi
} }
export -f deleteBackup
backup(){ backup(){
@ -245,7 +175,7 @@ elif [[ $selection == "2" ]]; then
done done
rmdir /mnt/heavyscript rmdir /mnt/heavyscript
else else
echo "Invalid selection, type -h for help" echo "Invalid selection, \"$selection\" was not an option"
fi fi
} }
export -f mount export -f mount
@ -258,10 +188,10 @@ export -f sync
update_apps(){ 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) 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 $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" [[ -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" [[ "$timeout" -le 120 ]] && echo "Warning: Your timeout is set low and may lead to premature rollbacks or skips"
for i in "${array[@]}" for i in "${array[@]}"
do do
app_name=$(echo "$i" | awk -F ',' '{print $1}') #print out first catagory, name. app_name=$(echo "$i" | awk -F ',' '{print $1}') #print out first catagory, name.
@ -319,6 +249,7 @@ update_apps(){
} }
export -f update_apps export -f update_apps
after_update_actions(){ after_update_actions(){
SECONDS=0 SECONDS=0
count=0 count=0
@ -384,11 +315,13 @@ fi
} }
export -f after_update_actions export -f after_update_actions
prune(){ prune(){
echo -e "\nPruning Docker Images" && docker image prune -af | grep "^Total" || echo "Failed to Prune Docker Images" echo -e "\nPruning Docker Images" && docker image prune -af | grep "^Total" || echo "Failed to Prune Docker Images"
} }
export -f prune export -f prune
title(){ title(){
echo ' _ _ _____ _ _ ' echo ' _ _ _____ _ _ '
echo '| | | | / ___| (_) | | ' echo '| | | | / ___| (_) | | '
@ -402,10 +335,94 @@ echo
} }
export -f title 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 #exit if incompatable functions are called
[[ "$update_all_apps" == "true" && "$update_apps" == "true" ]] && echo -e "-U and -u cannot BOTH be called" && 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 #Continue to call functions in specific order
[[ "$help" == "true" ]] && help
[[ "$deleteBackup" == "true" ]] && deleteBackup && exit [[ "$deleteBackup" == "true" ]] && deleteBackup && exit
[[ "$dns" == "true" ]] && dns && exit [[ "$dns" == "true" ]] && dns && exit
[[ "$restore" == "true" ]] && restore && exit [[ "$restore" == "true" ]] && restore && exit