Merge pull request #6 from Heavybullets8/beta

Merge beta with Main
This commit is contained in:
Heavybullets8 2022-06-11 19:47:34 +00:00 committed by GitHub
commit 85fd3bd672
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 356 additions and 221 deletions

View File

@ -1,53 +1,85 @@
# heavy_script # heavy_script
Update | Backup | Restore | Mount PVC | Rollback Applications | Sync Catalog | Prune Docker Images
## Table of contents:
* [Arguments](#arguments)
* [Examples](#examples)
* [How to Install](#how-to-install)
* [How to Update](#how-to-update)
* [Creating a Cron Job](#creating-a-cron-job)
* [Additional Information](#additional-information)
<br>
## Arguments
| Flag | Example | Parameter | Description | | Flag | Example | Parameter | Description |
|------ |------------------------ |----------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |----------------- |------------------------ |----------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| -r | -r | None | Restore HeavyScript specific 'ix-applications dataset' snapshot | | --delete-backup | --delete-backup | None | Opens a menu to delete backups<br>_Useful if you need to delete old system backups or backups from other scripts_ |
| -m | -m | None | Initiates mounting feature<br>Choose between unmounting and mounting PVC data | | --restore | --restore | None | Restore HeavyScript specific `ix-applications dataset` snapshot |
| -b | -b 14 | int | Backup 'ix-appliactions' dataset<br>Creates backups up to the number you've chosen | | --mount | --mount | None | Initiates mounting feature<br>Choose between unmounting and mounting PVC data |
| -i | -i nextcloud -i sonarr | String | Applications listed will be ignored during updating | | --dns | --dns | None | list all of your applications DNS names and their web ports |
| -R | -R | None | Monitors applications after they update<br>If the app does not become "ACTIVE" after either:<br>The custom Timeout, or Default Timeout,<br>rollback the application. |
| -v | -v | None | Verbose output |
| -S | -S | None | Shutdown applications prior to updating |
| -t | -t 150 | int | Set a custom timeout to be used with either:<br>-m <br>- Time the script will wait for application to be "STOPPED"<br>or<br>-u/U <br>- Time the script will wait for application to be either "STOPPED" or "ACTIVE" |
| -s | -s | None | Sync Catalog before updating |
| -U | -U | None | Update applications, ignoring major version changes | | -U | -U | None | Update applications, ignoring major version changes |
| -u | -u | None | Update applications, do NOT update if there was a major version change | | -u | -u | None | Update applications, do NOT update if there was a major version change |
| -b | -b 14 | Integer | Backup `ix-appliactions` dataset<br>_Creates backups up to the number you've chosen_ |
| -i | -i nextcloud -i sonarr | String | Applications listed will be ignored during updating<br>_List one application after another as shown in the example_ |
| (-R\|-r) | -r | None | Monitors applications after they update<br>If the app does not become "ACTIVE" after either:<br>The custom Timeout, or Default Timeout,<br>rollback the application.<br>__Warning: deprecating `-R` please begin using `-r` instead__ |
| -v | -v | None | Verbose Output<br>_Look at the bottom of this page for an example_ |
| -S | -S | None | Shutdown the application prior to updating it |
| -t | -t 150 | Integer | Set a custom timeout to be used with either:<br>`-m` <br>_Time the script will wait for application to be "STOPPED"_<br>or<br>`-(u\|U)` <br>_Time the script will wait for application to be either "STOPPED" or "ACTIVE"_ |
| -s | -s | None | Sync Catalogs prior to updating |
| -p | -p | None | Prune old/unused docker images | | -p | -p | None | Prune old/unused docker images |
<br> <br>
<br> <br>
### Examples ### Examples
#### Typical Cron Job #### Typical Cron Job
``` ```
bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -Rsup bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -rsup
``` ```
- -b is set to 14. Up to 14 snapshots of your ix-applications dataset will be saved > `-b` is set to 14. Up to 14 snapshots of your ix-applications dataset will be saved
- -i is set to ignore portainer, arch, sonarr, and radarr. These applications will be ignored when it comes to updates.
- -t I set it to 600 seconds, this means the script will wait 600 seconds for the application to become ACTIVE before timing out and continuing to a different application. > `-i` is set to ignore portainer, arch, sonarr, and radarr. These applications will be ignored when it comes to updates.
- -R Will rollback applications if they fail to deploy after updating.
- -s will just sync the repositories, ensuring you are downloading the latest updates. > `-t` I set it to 600 seconds, this means the script will wait 600 seconds for the application to become ACTIVE before timing out and continuing to a different application.
- -u update applications as long as the major version has absolutely no change, if it does have a change it will ask the user to update manually.
- -p Prune docker images. > `-r` Will rollback applications if they fail to deploy after updating.
> `-s` will just sync the repositories, ensuring you are downloading the latest updates.
> `-u` update applications as long as the major version has absolutely no change, if it does have a change it will ask the user to update manually.
> `-p` Prune docker images.
#### Mounting PVC Data #### Mounting PVC Data
``` ```
bash /mnt/tank/scripts/heavy_script.sh -t 300 -m bash /mnt/tank/scripts/heavy_script.sh -t 300 --mount
``` ```
#### Restoring ix-applications dataset #### Restoring ix-applications dataset
``` ```
bash /mnt/tank/scripts/heavy_script/heavy_script.sh -r bash /mnt/tank/scripts/heavy_script/heavy_script.sh --restore
```
#### Deleting Backups
```
bash /mnt/tank/scripts/heavy_script/heavy_script.sh --delete-backup
```
#### List All DNS Names
```
bash /mnt/tank/scripts/heavy_script/heavy_script.sh --dns
``` ```
#### My personal Cron Job #### My personal Cron Job
``` ```
git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -Rsup git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -rsup
``` ```
<br> <br>
@ -106,21 +138,21 @@ Here, we will update the script prior to running it, incase there is a bugfix, o
**Cron Job Command** **Cron Job Command**
``` ```
git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -Rsup git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -rsup
``` ```
> The important command here is the `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull` > The important command here is the `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull`
> This command will allow you to preform a `git pull` on a remote directory, which will ensure your script is udated prior to running it > This command will allow you to preform a `git pull` on a remote directory, which will ensure your script is udated prior to running it
> `&&` Is used to run a command AFTER the previous command completed successfully > `&&` Is used to run a command AFTER the previous command completed successfully
>> So once the `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull` command completes, THEN it will run the `bash /PATH/TO/HEAVY_SCRIPT_DIRECTORY/heavy_script.sh -b 14 -Rsup` command >> So once the `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull` command completes, THEN it will run the `bash /PATH/TO/HEAVY_SCRIPT_DIRECTORY/heavy_script.sh -b 14 -rsup` command
<br > <br >
<br > <br >
## Creating a Cron Job ## Creating a Cron Job
1. Truenas SCALE GUI 1. TrueNAS SCALE GUI
2. System Settings 2. System Settings
3. Advanced 3. Advanced
4. Cron Jobs 4. Cron Jobs
@ -129,7 +161,7 @@ git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_scr
| Name | Value | Reason | | Name | Value | Reason |
|------------------------ |------------------------------------------------------------------------------------------------------------------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |------------------------ |------------------------------------------------------------------------------------------------------------------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Description` | HeavyScript git pull and Update apps | This is up to you, put whatever you think is a good description in here | | `Description` | HeavyScript git pull and Update apps | This is up to you, put whatever you think is a good description in here |
| `Command` | `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull && bash /PATH/TO/HEAVY_SCRIPT_DIRECTORY/heavy_script.sh -b 14 -Rsup` | This is the command you will be running on your schedule I personally use: `git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -Rsup` | | `Command` | `git -C /PATH/TO/HEAVY_SCRIPT_DIRECTORY pull && bash /PATH/TO/HEAVY_SCRIPT_DIRECTORY/heavy_script.sh -b 14 -rsup` | This is the command you will be running on your schedule I personally use: `git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_script/heavy_script.sh -b 14 -rsup` |
| `Run As User` | `root` | Running the script as `root` is REQUIRED. You cannot access all of the kubernetes functions without this user. | | `Run As User` | `root` | Running the script as `root` is REQUIRED. You cannot access all of the kubernetes functions without this user. |
| `Schedule` | Up to you, I run mine everyday at `0400` | Again up to you | | `Schedule` | Up to you, I run mine everyday at `0400` | Again up to you |
| `Hide Standard Output` | `False` or Unticked | I like to receive an email report of how the script ran, what apps updated etc. | | `Hide Standard Output` | `False` or Unticked | I like to receive an email report of how the script ran, what apps updated etc. |
@ -141,11 +173,11 @@ git -C /mnt/speed/scripts/heavy_script pull && bash /mnt/speed/scripts/heavy_scr
<br > <br >
<br > <br >
### Additional Informaton ### Additional Information
#### Verbose vs Non-Verbose #### Verbose vs Non-Verbose
- Verbose used `bash heavy_test.sh -b 5 -SRupv` - Verbose used `bash heavy_script.sh -b 5 -Srupv`
- Non-Verbose used `bash heavy_test.sh -b 5 -SRup` - Non-Verbose used `bash heavy_script.sh -b 5 -Srup`
| Verbose | Non-Verbose | | Verbose | Non-Verbose |
|--------- |------------- | |--------- |------------- |

View File

@ -1,79 +1,62 @@
#!/bin/bash #!/bin/bash
#If no argument is passed, kill the script. #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 help(){
do [[ $help == "true" ]] && clear -x
case $opt in echo "Basic Utilities"
h) echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data"
echo "-m | 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 "-r | Opens a menu to restore a heavy_script backup that was taken on you ix-applications pool" echo "--delete backup | Opens a menu to delete backups on your system"
echo "-b | Back-up your ix-applications dataset, specify a number after -b" echo "--dns | list all of your applications DNS names and their web ports"
echo "-i | Add application to ignore list, one by one, see example below." echo
echo "-R | Roll-back applications if they fail to update" echo "Update Options"
echo "-S | Shutdown applications prior to updating" echo "-U | Update all applications, ignores versions"
echo "-v | verbose output" echo "-u | Update all applications, does not update Major releases"
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 "-b | Back-up your ix-applications dataset, specify a number after -b"
echo "-s | sync catalog" echo "-i | Add application to ignore list, one by one, see example below."
echo "-U | Update all applications, ignores versions" echo "-R | THIS OPTION WILL DEPRICATE SOON, USE \"-r\" instead. Roll-back applications if they fail to update"
echo "-u | Update all applications, does not update Major releases" 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 "EX | bash heavy_script.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vRsUp" echo "-v | verbose output"
echo "EX | bash /mnt/tank/scripts/heavy_script.sh -t 8812 -m" 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"
exit echo "-s | sync catalog"
;; echo "-p | Prune unused/old docker images"
\?) echo
echo "Invalid Option -$OPTARG, type -h for help" echo "Examples"
exit 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 "Option: -$OPTARG requires an argument" >&2 echo "bash /mnt/tank/scripts/heavy_script.sh --restore"
exit echo "bash /mnt/tank/scripts/heavy_script.sh --delete-backup"
;; echo
b) exit
re='^[0-9]+$' }
number_of_backups=$OPTARG export -f help
! [[ $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
;; deleteBackup(){
r) clear -x && echo "pulling all restore points.."
restore="true" list_backups=$(cli -c 'app kubernetes list_backups' | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
;; clear -x
i) [[ -z "$list_backups" ]] && echo "No restore points available" && exit || { title; echo -e "Choose a restore point to delete\nThese may be out of order if they are not HeavyScript backups" ; }
ignore+=("$OPTARG") echo "$list_backups" && read -t 600 -p "Please type a number: " selection && restore_point=$(echo "$list_backups" | grep ^"$selection " | awk '{print $2}')
;; [[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
t) [[ -z "$restore_point" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script
re='^[0-9]+$' echo -e "\nWARNING:\nYou CANNOT go back after deleting your restore point" || { echo "FAILED"; exit; }
timeout=$OPTARG echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -t 120 -p "Please type a number: " yesno || { echo "FAILED"; exit; }
! [[ $timeout =~ $re ]] && echo -e "Error: -t needs to be assigned an interger\n\"""$timeout""\" is not an interger" >&2 && exit if [[ $yesno == "1" ]]; then
;; echo -e "\nDeleting $restore_point" && cli -c 'app kubernetes delete_backup backup_name=''"'"$restore_point"'"' &>/dev/null && echo "Sucessfully deleted" || echo "Deletion Failed"
m) elif [[ $yesno == "2" ]]; then
mount="true" echo "You've chosen NO, killing script."
;; else
s) echo "Invalid Selection"
sync="true" fi
;; }
U) export -f deleteBackup
update_all_apps="true"
;;
u)
update_apps="true"
;;
S)
stop_before_update="true"
;;
p)
prune="true"
;;
R)
rollback="true"
;;
v)
verbose="true"
;;
esac
done
backup(){ backup(){
echo -e "\nNumber of backups was set to $number_of_backups" echo -e "\nNumber of backups was set to $number_of_backups"
@ -94,16 +77,17 @@ fi
} }
export -f backup export -f backup
restore(){ restore(){
clear -x && echo "pulling restore points.." clear -x && echo "pulling restore points.."
list_backups=$(cli -c 'app kubernetes list_backups' | grep "HeavyScript_" | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t) list_backups=$(cli -c 'app kubernetes list_backups' | grep "HeavyScript_" | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
clear -x clear -x
[[ -z "$list_backups" ]] && echo "No HeavyScript restore points available" && exit || { title; echo "Choose a restore point" ; } [[ -z "$list_backups" ]] && echo "No HeavyScript restore points available" && exit || { title; echo "Choose a restore point" ; }
echo "$list_backups" && read -p "Please type a number: " selection && restore_point=$(echo "$list_backups" | grep ^"$selection " | awk '{print $2}') echo "$list_backups" && read -t 600 -p "Please type a number: " selection && restore_point=$(echo "$list_backups" | grep ^"$selection " | awk '{print $2}')
[[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script [[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
[[ -z "$restore_point" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script [[ -z "$restore_point" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script
echo -e "\nWARNING:\nThis is NOT guranteed to work\nThis is ONLY supposed to be used as a LAST RESORT\nConsider rolling back your applications instead if possible" || { echo "FAILED"; exit; } echo -e "\nWARNING:\nThis is NOT guranteed to work\nThis is ONLY supposed to be used as a LAST RESORT\nConsider rolling back your applications instead if possible" || { echo "FAILED"; exit; }
echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -p "Please type a number: " yesno || { echo "FAILED"; exit; } echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -t 120 -p "Please type a number: " yesno || { echo "FAILED"; exit; }
if [[ $yesno == "1" ]]; then if [[ $yesno == "1" ]]; then
echo -e "\nStarting Backup, this will take a LONG time." && cli -c 'app kubernetes restore_backup backup_name=''"'"$restore_point"'"' || echo "Restore FAILED" echo -e "\nStarting Backup, this will take a LONG time." && cli -c 'app kubernetes restore_backup backup_name=''"'"$restore_point"'"' || echo "Restore FAILED"
elif [[ $yesno == "2" ]]; then elif [[ $yesno == "2" ]]; then
@ -114,14 +98,41 @@ fi
} }
export -f restore export -f restore
dns(){
clear -x
echo "Generating DNS Names.."
#ignored dependency pods, No change required
dep_ignore="cron|kube-system|nvidia|svclb|NAME|memcached"
# Pulling pod names
mapfile -t main < <(k3s kubectl get pods -A | grep -Ev "$dep_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
mount(){ mount(){
clear -x clear -x
title title
echo -e "1 Mount\n2 Unmount All" && read -p "Please type a number: " selection echo -e "1 Mount\n2 Unmount All" && read -t 600 -p "Please type a number: " selection
[[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script [[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
if [[ $selection == "1" ]]; then if [[ $selection == "1" ]]; then
list=$(k3s kubectl get pvc -A | sort -u | awk '{print NR-1, "\t" $1 "\t" $2 "\t" $4}' | column -t | sed "s/^0/ /") 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 -p "Please type a number : " selection echo "$list" && read -t 120 -p "Please type a number: " selection
[[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script [[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
app=$(echo -e "$list" | grep ^"$selection " | awk '{print $2}' | cut -c 4- ) 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 [[ -z "$app" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script
@ -155,7 +166,7 @@ elif [[ $selection == "2" ]]; then
mapfile -t path < <(find /mnt/*/ix-applications/releases/"$app"/volumes/ -maxdepth 0 | cut -c 6-) mapfile -t path < <(find /mnt/*/ix-applications/releases/"$app"/volumes/ -maxdepth 0 | cut -c 6-)
if [[ "${#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. if [[ "${#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.
echo "$i is a name used on more than one pool.. attempting to use your current kubernetes apps pool" echo "$i is a name used on more than one pool.. attempting to use your current kubernetes apps pool"
pool=$(cli -c 'app kubernetes config' | grep dataset | awk -F '|' '{print $3}' | awk -F '/' '{print $1}' | tr -d " \t\n\r") 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=$(find /mnt/"$pool"/ix-applications/releases/"$app"/volumes/ -maxdepth 0 | cut -c 6-) full_path=$(find /mnt/"$pool"/ix-applications/releases/"$app"/volumes/ -maxdepth 0 | cut -c 6-)
zfs set mountpoint=legacy "$full_path""$pvc" && echo "$i unmounted" && rmdir /mnt/heavyscript/"$i" || echo "failed to unmount $i" zfs set mountpoint=legacy "$full_path""$pvc" && echo "$i unmounted" && rmdir /mnt/heavyscript/"$i" || echo "failed to unmount $i"
else else
@ -164,34 +175,32 @@ 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
sync(){ sync(){
echo -e "\nSyncing all catalogs, please wait.." && cli -c 'app catalog sync_all' &> /dev/null && echo -e "Catalog sync complete" echo -e "\nSyncing all catalogs, please wait.." && cli -c 'app catalog sync_all' &> /dev/null && echo -e "Catalog sync complete"
} }
export -f sync export -f sync
prune(){
echo -e "\nPruning Docker Images" && docker image prune -af | grep Total || echo "Failed to Prune Docker Images"
}
export -f prune
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 ",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.
old_app_ver=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #previous/current Application MAJOR Version old_app_ver=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #previous/current Application MAJOR Version
new_app_ver=$(echo "$i" | awk -F ',' '{print $5}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #new Application MAJOR Version new_app_ver=$(echo "$i" | awk -F ',' '{print $5}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #new Application MAJOR Version
old_chart_ver=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # Old Chart MAJOR version old_chart_ver=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # Old Chart MAJOR version
new_chart_ver=$(echo "$i" | awk -F ',' '{print $5}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # New Chart MAJOR version new_chart_ver=$(echo "$i" | awk -F ',' '{print $5}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # New Chart MAJOR version
status=$(echo "$i" | awk -F ',' '{print $2}') #status of the app: STOPPED / DEPLOYING / ACTIVE status=$(echo "$i" | awk -F ',' '{print $2}') #status of the app: STOPPED / DEPLOYING / ACTIVE
startstatus=$status
diff_app=$(diff <(echo "$old_app_ver") <(echo "$new_app_ver")) #caluclating difference in major app versions diff_app=$(diff <(echo "$old_app_ver") <(echo "$new_app_ver")) #caluclating difference in major app versions
diff_chart=$(diff <(echo "$old_chart_ver") <(echo "$new_chart_ver")) #caluclating difference in Chart versions diff_chart=$(diff <(echo "$old_chart_ver") <(echo "$new_chart_ver")) #caluclating difference in Chart versions
old_full_ver=$(echo "$i" | awk -F ',' '{print $4}') #Upgraded From old_full_ver=$(echo "$i" | awk -F ',' '{print $4}') #Upgraded From
@ -199,7 +208,6 @@ update_apps(){
rollback_version=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}') rollback_version=$(echo "$i" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}')
printf '%s\0' "${ignore[@]}" | grep -iFxqz "${app_name}" && echo -e "\n$app_name\nIgnored, skipping" && continue #If application is on ignore list, skip printf '%s\0' "${ignore[@]}" | grep -iFxqz "${app_name}" && echo -e "\n$app_name\nIgnored, skipping" && continue #If application is on ignore list, skip
if [[ "$diff_app" == "$diff_chart" || "$update_all_apps" == "true" ]]; then #continue to update if [[ "$diff_app" == "$diff_chart" || "$update_all_apps" == "true" ]]; then #continue to update
startstatus=$status
if [[ $stop_before_update == "true" ]]; then # Check to see if user is using -S or not if [[ $stop_before_update == "true" ]]; then # Check to see if user is using -S or not
if [[ "$status" == "STOPPED" ]]; then # if status is already stopped, skip while loop if [[ "$status" == "STOPPED" ]]; then # if status is already stopped, skip while loop
echo -e "\n$app_name" echo -e "\n$app_name"
@ -237,10 +245,11 @@ update_apps(){
echo -e "\n$app_name\nMajor Release, update manually" echo -e "\n$app_name\nMajor Release, update manually"
continue continue
fi fi
done done
} }
export -f update_apps export -f update_apps
after_update_actions(){ after_update_actions(){
SECONDS=0 SECONDS=0
count=0 count=0
@ -248,7 +257,7 @@ if [[ $rollback == "true" ]]; then
while [[ "0" != "1" ]] while [[ "0" != "1" ]]
do do
(( count++ )) (( count++ ))
status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' | grep "$app_name," | awk -F ',' '{print $2}') status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' | grep "^$app_name," | awk -F ',' '{print $2}')
if [[ "$status" == "ACTIVE" && "$startstatus" == "STOPPED" ]]; then if [[ "$status" == "ACTIVE" && "$startstatus" == "STOPPED" ]]; then
[[ "$verbose" == "true" ]] && echo "Returing to STOPPED state.." [[ "$verbose" == "true" ]] && echo "Returing to STOPPED state.."
midclt call chart.release.scale "$app_name" '{"replica_count": 0}' &> /dev/null && echo "Stopped"|| echo "FAILED" midclt call chart.release.scale "$app_name" '{"replica_count": 0}' &> /dev/null && echo "Stopped"|| echo "FAILED"
@ -280,7 +289,7 @@ else
while [[ "0" != "1" ]] #using a constant while loop, then breaking out of the loop with break commands below. while [[ "0" != "1" ]] #using a constant while loop, then breaking out of the loop with break commands below.
do do
(( count++ )) (( count++ ))
status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' | grep "$app_name," | awk -F ',' '{print $2}') status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' | grep "^$app_name," | awk -F ',' '{print $2}')
if [[ "$status" == "STOPPED" ]]; then if [[ "$status" == "STOPPED" ]]; then
[[ "$count" -le 1 && "$verbose" == "true" ]] && echo "Verifying Stopped.." && sleep 15 && continue #if reports stopped on FIRST time through loop, double check [[ "$count" -le 1 && "$verbose" == "true" ]] && echo "Verifying Stopped.." && sleep 15 && continue #if reports stopped on FIRST time through loop, double check
[[ "$count" -le 1 && -z "$verbose" ]] && sleep 15 && continue #if reports stopped on FIRST time through loop, double check [[ "$count" -le 1 && -z "$verbose" ]] && sleep 15 && continue #if reports stopped on FIRST time through loop, double check
@ -304,8 +313,15 @@ else
fi fi
fi 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 export -f prune
title(){ title(){
echo ' _ _ _____ _ _ ' echo ' _ _ _____ _ _ '
echo '| | | | / ___| (_) | | ' echo '| | | | / ___| (_) | | '
@ -318,10 +334,97 @@ echo ' |___/ |_| '
echo 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
[[ "$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 [[ "$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
[[ "$dns" == "true" ]] && dns && exit
[[ "$restore" == "true" ]] && restore && exit [[ "$restore" == "true" ]] && restore && exit
[[ "$mount" == "true" ]] && mount && exit [[ "$mount" == "true" ]] && mount && exit
[[ "$number_of_backups" -ge 1 ]] && backup [[ "$number_of_backups" -ge 1 ]] && backup