New Version of TrueTool

This commit is contained in:
Kjeld Schouten-Lebbing
2022-06-14 10:36:45 +02:00
parent e487c84139
commit 975493bb85
21 changed files with 907 additions and 433 deletions

82
includes/backup.sh Executable file
View File

@ -0,0 +1,82 @@
#!/bin/bash
## Simple shortcut to just list the backups without promts and such
listBackups(){
echo -e "${BWhite}Backup Listing Tool${Color_Off}"
clear -x && echo "pulling all restore points.."
list_backups=$(cli -c 'app kubernetes list_backups' | grep -v system-update | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
[[ -z "$list_backups" ]] && echo -e "${IRed}No restore points available${Color_Off}" && exit || echo "Detected Backups:" && echo "$list_backups"
}
export -f listBackups
## Lists backups, except system-created backups, and promts which one to delete
deleteBackup(){
echo -e "${BWhite}Backup Deletion Tool${Color_Off}"
clear -x && echo "pulling all restore points.."
list_delete_backups=$(cli -c 'app kubernetes list_delete_backups' | grep -v system-update | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
clear -x
# shellcheck disable=SC2015
[[ -z "$list_delete_backups" ]] && echo -e "${IRed}No restore points available${Color_Off}" && exit || { title; echo -e "Choose a restore point to delete\nThese may be out of order if they are not TrueTool backups" ; }
# shellcheck disable=SC2015
echo "$list_delete_backups" && read -rt 600 -p "Please type a number: " selection && restore_point=$(echo "$list_delete_backups" | grep ^"$selection " | awk '{print $2}')
[[ -z "$selection" ]] && echo "${IRed}Your selection cannot be empty${Color_Off}" && 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:\nYou CANNOT go back after deleting your restore point" || { echo "${IRed}FAILED${Color_Off}"; exit; }
# shellcheck disable=SC2015
echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -rt 120 -p "Please type a number: " yesno || { echo "${IRed}FAILED${Color_Off}"; exit; }
if [[ $yesno == "1" ]]; then
echo -e "\nDeleting $restore_point" && cli -c 'app kubernetes delete_backup backup_name=''"'"$restore_point"'"' &>/dev/null && echo -e "${IGreen}Sucessfully deleted${Color_Off}" || echo -e "${IRed}Deletion FAILED${Color_Off}"
elif [[ $yesno == "2" ]]; then
echo "You've chosen NO, killing script."
else
echo -e "${IRed}Invalid Selection${Color_Off}"
fi
}
export -f deleteBackup
## Creates backups and deletes backups if a "backups to keep"-count is exceeded.
# backups-to-keep takes only heavyscript and truetool created backups into account, as other backups aren't guaranteed to be sorted correctly
backup(){
echo -e "${BWhite}Backup Tool${Color_Off}"
echo -e "\nNumber of backups was set to $number_of_backups"
date=$(date '+%Y_%m_%d_%H_%M_%S')
[[ "$verbose" == "true" ]] && cli -c 'app kubernetes backup_chart_releases backup_name=''"'TrueTool_"$date"'"'
[[ -z "$verbose" ]] && echo -e "\nNew Backup Name:" && cli -c 'app kubernetes backup_chart_releases backup_name=''"'TrueTool_"$date"'"' | tail -n 1
mapfile -t list_create_backups < <(cli -c 'app kubernetes list_create_backups' | grep 'HeavyScript\|TrueTool_' | sort -t '_' -Vr -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r")
# shellcheck disable=SC2309
if [[ ${#list_create_backups[@]} -gt "number_of_backups" ]]; then
echo -e "\nDeleting the oldest backup(s) for exceeding limit:"
overflow=$(( ${#list_create_backups[@]} - "$number_of_backups" ))
mapfile -t list_overflow < <(cli -c 'app kubernetes list_create_backups' | grep "TrueTool_" | sort -t '_' -V -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r" | head -n "$overflow")
for i in "${list_overflow[@]}"
do
cli -c 'app kubernetes delete_backup backup_name=''"'"$i"'"' &> /dev/null || echo "${IRed}FAILED${Color_Off} to delete $i"
echo "$i"
done
fi
}
export -f backup
## Lists available backup and prompts the users to select a backup to restore
restore(){
echo -e "${BWhite}Backup Restoration Tool${Color_Off}"
clear -x && echo "pulling restore points.."
list_restore_backups=$(cli -c 'app kubernetes list_restore_backups' | grep "TrueTool_" | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
clear -x
# shellcheck disable=SC2015
[[ -z "$list_restore_backups" ]] && echo "No TrueTool restore points available" && exit || { title; echo "Choose a restore point" ; }
echo "$list_restore_backups" && read -rt 600 -p "Please type a number: " selection && restore_point=$(echo "$list_restore_backups" | grep ^"$selection " | awk '{print $2}')
[[ -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
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 "${IRed}FAILED${Color_Off}"; exit; }
# shellcheck disable=SC2015
echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -rt 120 -p "Please type a number: " yesno || { echo "${IRed}FAILED${Color_Off}"; exit; }
if [[ $yesno == "1" ]]; then
echo -e "\nStarting Backup, this will take a ${BWhite}LONG${Color_Off} time." && cli -c 'app kubernetes restore_backup backup_name=''"'"$restore_point"'"' || echo "Restore ${IRed}FAILED${Color_Off}"
elif [[ $yesno == "2" ]]; then
echo "You've chosen NO, killing script. Good luck."
else
echo -e "${IRed}Invalid Selection${Color_Off}"
fi
}
export -f restore

31
includes/chores.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
helmEnable(){
echo -e "${BWhite}Enabling Helm${Color_Off}"
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml && echo -e "${IGreen}Helm Enabled${Color_Off}"|| echo -e "${IRed}Helm Enable FAILED${Color_Off}"
}
export -f helmEnable
aptEnable(){
echo -e "${BWhite}Enabling Apt-Commands${Color_Off}"
chmod +x /usr/bin/apt* && echo -e "${IGreen}APT enabled${Color_Off}"|| echo -e "${IRed}APT Enable FAILED${Color_Off}"
}
export -f aptEnable
# Prune unused docker images to prevent dataset/snapshot bloat related slowdowns on SCALE
prune(){
echo -e "${BWhite}Docker Prune${Color_Off}"
echo "Pruning Docker Images..."
docker image prune -af | grep "^Total" && echo -e "${IGreen}Docker Prune Successfull${Color_Off}" || echo "Docker Prune ${IRed}FAILED${Color_Off}"
# TODO Switch to middleware prune on next release
# midclt call container.prune '{"remove_unused_images": true, "remove_stopped_containers": true}' &> /dev/null && echo "Docker Prune completed"|| echo "Docker Prune ${IRed}FAILED${Color_Off}"
}
export -f prune
#
sync(){
echo -e "${BWhite}Starting Catalog Sync...${Color_Off}"
cli -c 'app catalog sync_all' &> /dev/null && echo -e "${IGreen}Catalog sync complete${Color_Off}" || echo -e "${IRed}Catalog Sync Failed${Color_Off}"
}
export -f sync

113
includes/colors.sh Executable file
View File

@ -0,0 +1,113 @@
#!/bin/bash
# shellcheck disable=SC2034
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
noColor(){
# Reset
Color_Off=""
# Regular Colors
Black=""
Red=""
Green=""
Yellow=""
Blue=""
Purple=""
Cyan=""
White=""
# Bold
BBlack=""
BRed=""
BGreen=""
BYellow=""
BBlue=""
BPurple=""
BCyan=""
BWhite=""
# Underline
UBlack=""
URed=""
UGreen=""
UYellow=""
UBlue=""
UPurple=""
UCyan=""
UWhite=""
# High Intensity
IBlack=""
IRed=""
IGreen=""
IYellow=""
IBlue=""
IPurple=""
ICyan=""
IWhite=""
# Bold High Intensity
BIBlack=""
BIRed=""
BIGreen=""
BIYellow=""
BIBlue=""
BIPurple=""
BICyan=""
BIWhite=""
}
export -f noColor

27
includes/dns.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
dns(){
echo -e "${BWhite}Service DNS Names Tool${Color_Off}"
clear -x
echo "Generating Internal Service DNS Names..."
#ignored dependency pods, may need to add more in the future.
dep_ignore="\-cronjob\-|^kube-system|\ssvclb|NAME|\-memcached\-.[^custom\-app]|\-postgresql\-.[^custom\-app]|\-redis\-.[^custom\-app]|\-mariadb\-.[^custom\-app]|\-promtail\-.[^custom\-app]"
# 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}")
[[ -n "$port" ]] && 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

34
includes/help.sh Executable file
View File

@ -0,0 +1,34 @@
#!/bin/bash
help(){
[[ $help == "true" ]] && clear -x
echo ""
echo -e "${BWhite}Basic Utilities${Color_Off}"
echo "--mount | Initiates mounting feature, choose between unmounting and mounting PVC data"
echo "--restore | Opens a menu to restore a \"truetool\" backup that was taken on your \"ix-applications\" dataset"
echo "--delete-backup | Opens a menu to delete backups on your system"
echo "--list-backups | Prints a list of backups available"
echo "--helm-enable | Enables Helm command access on SCALE"
echo "--apt-enable | Enables Apt command access on SCALE"
echo "--dns | list all of your applications DNS names and their web ports"
echo
echo -e "${BWhite}Update Options${Color_Off}"
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 "-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 -e "${BWhite}Examples${Color_Off}"
echo "bash truetool.sh -b 14 -i portainer -i arch -i sonarr -i radarr -t 600 -vrsUp"
echo "bash /mnt/tank/scripts/truetool.sh -t 150 --mount"
echo "bash /mnt/tank/scripts/truetool.sh --dns"
echo "bash /mnt/tank/scripts/truetool.sh --restore"
echo "bash /mnt/tank/scripts/truetool.sh --delete-backup"
echo
exit
}
export -f help

57
includes/mount.sh Executable file
View File

@ -0,0 +1,57 @@
#!/bin/bash
mountPVC(){
echo -e "${BWhite}PVC Mounting Tool${Color_Off}"
clear -x
title
echo -e "1 Mount\n2 Unmount All" && read -rt 600 -p "Please type a number: " selection
[[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
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/ /")
echo "$list" && read -rt 120 -p "Please type a number: " selection
[[ -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- )
[[ -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" ]]
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}')
volume_name=$(echo "$pvc" | awk '{print $4}')
full_path=$(zfs list | grep "$volume_name" | awk '{print $1}')
echo -e "\nMounting\n$full_path\nTo\n/mnt/truetool/$data_name" && zfs set mountpoint="/truetool/$data_name" "$full_path" && echo -e "Mounted, Use the Unmount All option to unmount\n"
exit
elif [[ $selection == "2" ]]; then
mapfile -t unmount_array < <(basename -a /mnt/truetool/* | sed "s/*//")
[[ -z ${unmount_array[*]} ]] && echo "Theres nothing to unmount" && exit
for i in "${unmount_array[@]}"
do
main=$(k3s kubectl get pvc -A | grep -E "\s$i\s" | awk '{print $1, $2, $4}')
app=$(echo "$main" | awk '{print $1}' | cut -c 4-)
pvc=$(echo "$main" | awk '{print $3}')
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.
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 -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-)
zfs set mountpoint=legacy "$full_path""$pvc" && echo "$i unmounted" && rmdir /mnt/truetool/"$i" || echo "${IRed}FAILED${Color_Off} to unmount $i"
else
# shellcheck disable=SC2128
zfs set mountpoint=legacy "$path""$pvc" && echo "$i unmounted" && rmdir /mnt/truetool/"$i" || echo "${IRed}FAILED${Color_Off} to unmount $i"
fi
done
rmdir /mnt/truetool
else
echo -e "${IRed}Invalid selection, \"$selection\" was not an option${Color_Off}"
fi
}
export -f mountPVC

67
includes/no_args.sh Normal file
View File

@ -0,0 +1,67 @@
#!/bin/bash
no_args(){
echo "0 Show Help"
echo "1 List Internal Service DNS Names"
echo "2 Mount and Unmount PVC storage for easy access"
echo "3 List Backups"
echo "4 Create a Backup"
echo "5 Restore a Backup"
echo "6 Delete a Backup"
echo "7 Enable Helm Commands"
echo "8 Enable Apt and Apt-Get Commands"
echo "9 Update All Apps"
read -rt 600 -p "Please select an option by number: " selection
case $selection in
1)
help="true"
;;
2)
dns="true"
;;
3)
mountPVC="true"
;;
4)
listBackups="true"
;;
5)
read -rt 600 -p "Please type the max number of backups to keep: " backups
re='^[0-9]+$'
number_of_backups=$backups
! [[ $backups =~ $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
;;
6)
restore="true"
;;
7)
deleteBackup="true"
;;
8)
helmEnable="true"
;;
9)
aptEnable="true"
;;
10)
echo ""
echo "1 Update Apps Excluding likely breaking major changes"
echo "2 Update Apps Including likely breaking major changes"
read -rt 600 -p "Please select an option by number: " updateType
if [[ "$updateType" == "1" ]]; then
update_apps="true"
elif [[ "$updateType" == "2" ]]; then
update_all_apps="true"
else
echo "INVALID ENTRY" && exit 1
fi
;;
*)
echo "Unknown option" && exit 1
;;
esac
echo ""
}
export -f no_args

22
includes/title.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
# Fancy ascii title.
title(){
if [[ -z $titleShown ]]; then
echo -e "${IRed} _______ _____ _ _ ";
echo " |__ __| / ____| | | | ";
echo " | |_ __ _ _ ___| | | |__ __ _ _ __| |_ ___ ";
echo -e "${IYellow} | | '__| | | |/ _ \ | | '_ \ / _\` | '__| __/ __|";
echo " | | | | |_| | __/ |____| | | | (_| | | | |_\__ \\";
echo -e "${IGreen} __|_|_| \__,_|\___|\_____|_| |_|\__,_|_| \__|___/";
echo " |__ __| |__ __| | | ";
echo -e "${IBlue} | |_ __ _ _ ___| | ___ ___ | | ";
echo " | | '__| | | |/ _ \ |/ _ \ / _ \| | ";
echo -e "${IPurple} | | | | |_| | __/ | (_) | (_) | | ";
echo " |_|_| \__,_|\___|_|\___/ \___/|_| ";
echo " ";
echo -e "${Color_Off} ";
fi
titleShown='true'
}
export -f title

123
includes/update.sh Executable file
View File

@ -0,0 +1,123 @@
#!/bin/bash
update_apps(){
echo -e "${BWhite}App Updater${Color_Off}"
[[ -z $timeout ]] && echo -e "Default 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"
echo ""
echo "Creating list of Apps to update..."
# Render a list of ignored applications, so users can verify their ignores got parsed correctly.
if [[ -z ${ignore[*]} ]]; then
echo "No apps added to ignore list, continuing..."
else
echo "ignored applications:"
for ignored in "${ignore[@]}"
do
echo "${ignored}"
done
fi
echo ""
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 or middleware timed out" && return 0 || echo -e "\n${#array[@]} update(s) available:"
PIDlist=()
# Draft a list of app names, seperate from actuall execution
# This prevents outputs getting mixed together
for i in "${array[@]}"
do
app_name=$(echo "$i" | awk -F ',' '{print $1}') #print out first catagory, name.
echo "$app_name"
done
echo ""
echo "Updating Apps..."
# Create a background task for each update as async solution
for i in "${array[@]}"
do
executeUpdate "${i}" &
PIDlist+=($!)
done
echo ""
echo "Waiting for update results..."
# Wait for all the async updates to complete
for p in "${PIDlist[@]}"
do
wait "${p}" ||:
done
}
export -f update_apps
# This is a combination of stopping previously-stopped apps and apps stuck Deploying after update
after_update_actions(){
SECONDS=0
count=0
sleep 15
# Keep this running and exit the endless-loop based on a timer, instead of a countered-while-loop
# shellcheck disable=SC2050
while [[ "0" != "1" ]]
do
(( 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}')
if [[ "$status" == "ACTIVE" && "$startstatus" == "STOPPED" ]]; then
[[ "$verbose" == "true" ]] && echo "Returing to STOPPED state.."
midclt call chart.release.scale "$app_name" '{"replica_count": 0}' &> /dev/null && echo "Stopped"|| echo "FAILED"
break
elif [[ "$SECONDS" -ge "$timeout" && "$status" == "DEPLOYING" && "$failed" != "true" ]]; then
echo -e "Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)\nIf this is a slow starting application, set a higher timeout with -t\nIf this applicaion is always DEPLOYING, you can disable all probes under the Healthcheck Probes Liveness section in the edit configuration\nReverting update.."
midclt call chart.release.rollback "$app_name" "{\"item_version\": \"$rollback_version\"}" &> /dev/null
[[ "$startstatus" == "STOPPED" ]] && failed="true" && after_update_actions && unset failed #run back after_update_actions function if the app was stopped prior to update
break
elif [[ "$SECONDS" -ge "$timeout" && "$status" == "DEPLOYING" && "$failed" == "true" ]]; then
echo -e "Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)\nThe application failed to be ACTIVE even after a rollback,\nManual intervention is required\nAbandoning"
break
elif [[ "$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 && -z "$verbose" ]] && sleep 15 && continue #if reports stopped on FIRST time through loop, double check
echo "Stopped" && break #if reports stopped any time after the first loop, assume its extermal services.
elif [[ "$status" == "ACTIVE" ]]; then
[[ "$count" -le 1 && "$verbose" == "true" ]] && echo "Verifying Active.." && sleep 15 && continue #if reports active on FIRST time through loop, double check
[[ "$count" -le 1 && -z "$verbose" ]] && sleep 15 && continue #if reports active on FIRST time through loop, double check
echo "Active" && break #if reports active any time after the first loop, assume actually active.
else
[[ "$verbose" == "true" ]] && echo "Waiting $((timeout-SECONDS)) more seconds for $app_name to be ACTIVE"
sleep 15
continue
fi
done
}
export -f after_update_actions
# Determine what all the required information for the App to update, check it and execute the update using the SCALE API
executeUpdate(){
app_name=$(echo "$1" | awk -F ',' '{print $1}') #print out first catagory, name.
old_app_ver=$(echo "$1" | awk -F ',' '{print $4}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #previous/current Application MAJOR Version
new_app_ver=$(echo "$1" | awk -F ',' '{print $5}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #new Application MAJOR Version
old_chart_ver=$(echo "$1" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # Old Chart MAJOR version
new_chart_ver=$(echo "$1" | awk -F ',' '{print $5}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # New Chart MAJOR version
status=$(echo "$1" | 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_chart=$(diff <(echo "$old_chart_ver") <(echo "$new_chart_ver")) #caluclating difference in Chart versions
old_full_ver=$(echo "$1" | awk -F ',' '{print $4}') #Upgraded From
new_full_ver=$(echo "$1" | awk -F ',' '{print $5}') #Upraded To
rollback_version=$(echo "$1" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}')
printf '%s\0' "${ignore[@]}" | grep -iFxqz "${app_name}" && echo -e "\n$app_name\nIgnored, skipping" && return #If application is on ignore list, skip
if [[ "$diff_app" == "$diff_chart" || "$update_all_apps" == "true" ]]; then #continue to update
[[ "$verbose" == "true" ]] && echo "Updating.."
# shellcheck disable=SC2015
cli -c 'app chart_release upgrade release_name=''"'"$app_name"'"' &> /dev/null && echo -e "Updated $app_name\n$old_full_ver\n$new_full_ver" && after_update_actions || { echo -e "$app_name: update ${IRed}FAILED${Color_Off}"; return; }
else
echo -e "\n$app_name\nMajor Release, update manually"
return
fi
}
export -f executeUpdate

31
includes/update_self.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
## AutoUpdate TrueTool using Git
updater(){
echo -e "${BWhite}Checking for updates...${Color_Off}"
git remote set-url origin "${targetRepo}"
BRANCH=$(git rev-parse --abbrev-ref HEAD)
git fetch -q
git update-index -q --refresh
if [[ $(git status --branch --porcelain) == *"behind"* ]]; then
echo -e "${IPurple}TrueTool requires update${Color_Off}"
git reset --hard -q
git checkout -q "${BRANCH}"
git pull -q
echo "script updated"
if [[ "$CHANGED" == "true" ]]; then
echo "LOOP DETECTED, exiting"
exit 1
else
echo "restarting script after update..."
export CHANGED="true"
. "${SCRIPT_DIR}/truetool.sh" "$@"
exit
fi
else
echo -e "${IGreen}script up-to-date${Color_Off}"
export CHANGED="false"
fi
echo ""
}
export -f updater