scale-catalog/functions/update_apps.sh

298 lines
13 KiB
Bash
Raw Normal View History

2022-07-25 02:29:58 +00:00
#!/bin/bash
2022-07-25 20:16:08 +00:00
2022-07-25 04:11:04 +00:00
commander(){
2022-07-25 04:03:57 +00:00
mapfile -t array < <(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,container_images_update_available,status' | tr -d " \t\r" | grep -E ",true($|,)" | sort)
2022-07-31 19:03:45 +00:00
echo -e "🅄 🄿 🄳 🄰 🅃 🄴 🅂"
2022-07-31 19:16:40 +00:00
[[ -z ${array[*]} ]] && echo "There are no updates available" && echo -e "\n" && return 0 || echo "Update(s) Available: ${#array[@]}"
2022-07-27 22:40:44 +00:00
echo "Asynchronous Updates: $update_limit"
2022-07-27 00:00:03 +00:00
[[ -z $timeout ]] && echo "Default Timeout: 500" && timeout=500 || echo "Custom Timeout: $timeout"
2022-07-25 02:29:58 +00:00
[[ "$timeout" -le 120 ]] && echo "Warning: Your timeout is set low and may lead to premature rollbacks or skips"
2022-07-25 20:16:08 +00:00
2022-08-05 03:23:50 +00:00
# previous 20% 2 min 9 seconds
2022-08-11 03:46:08 +00:00
# it=0
# while_status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' 2>/dev/null)
# echo "$while_status" > temp.txt
# rm trigger &>/dev/null
# delay=2
# final_check=0
# while true
# do
# if [[ -f trigger ]]; then
# delay=4
# if while_status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' 2>/dev/null) ; then
# echo "$while_status" > temp.txt
# else
# echo "Middlewared timed out. Consider setting a lower number for async applications"
# continue
# fi
# fi
# proc_count=${#processes[@]}
# count=0
# for proc in "${processes[@]}"
# do
# kill -0 "$proc" &> /dev/null || { unset "processes[$count]"; ((proc_count--)); }
# ((count++))
# done
# if [[ "$proc_count" -ge "$update_limit" ]]; then
# sleep $delay
# elif [[ $it -lt ${#array[@]} ]]; then
# until [[ "$proc_count" -ge "$update_limit" || $it -ge ${#array[@]} ]]
# do
# update_apps "${array[$it]}" &
# processes+=($!)
# sleep 4
# ((it++))
# ((proc_count++))
# done
# elif [[ $proc_count != 0 ]]; then # Wait for all processes to finish
# sleep $delay
# else # All processes must be completed, break out of loop
# [[ $final_check == 0 ]] && ((final_check++)) && continue
# break
# fi
# done
# rm temp.txt
# [[ -f trigger ]] && rm trigger
# echo
# echo
it=0
2022-08-11 04:51:58 +00:00
while_count=0
2022-07-27 20:13:40 +00:00
while true
2022-07-25 20:42:57 +00:00
do
2022-08-11 03:46:08 +00:00
if while_status=$(cli -m csv -c 'app chart_release query name,update_available,human_version,human_latest_version,status' 2>/dev/null) ; then
2022-08-11 04:51:58 +00:00
((while_count++))
2022-08-11 04:58:51 +00:00
echo -e "$while_count\n$while_status" > temp.txt
2022-08-11 03:46:08 +00:00
else
echo "Middlewared timed out. Consider setting a lower number for async applications"
continue
2022-08-09 05:47:34 +00:00
fi
2022-08-05 01:52:08 +00:00
proc_count=${#processes[@]}
2022-07-26 05:44:09 +00:00
count=0
2022-07-26 05:38:29 +00:00
for proc in "${processes[@]}"
do
2022-07-26 05:44:55 +00:00
kill -0 "$proc" &> /dev/null || { unset "processes[$count]"; ((proc_count--)); }
2022-07-27 21:07:53 +00:00
((count++))
2022-07-26 05:38:29 +00:00
done
2022-07-27 19:47:44 +00:00
if [[ "$proc_count" -ge "$update_limit" ]]; then
2022-08-11 03:46:08 +00:00
sleep 3
2022-07-27 20:13:40 +00:00
elif [[ $it -lt ${#array[@]} ]]; then
# loop=0
# until [[ $loop -ge 2 || $it -ge ${#array[@]} ]];
# do
update_apps "${array[$it]}" &
processes+=($!)
((loop++))
((it++))
# done
elif [[ $proc_count != 0 ]]; then # Wait for all processes to finish
2022-08-11 03:46:08 +00:00
sleep 3
2022-07-27 21:07:53 +00:00
else # All processes must be completed, break out of loop
2022-07-27 20:50:52 +00:00
break
2022-07-25 20:42:57 +00:00
fi
done
2022-07-27 19:56:22 +00:00
rm temp.txt
2022-07-31 19:03:45 +00:00
echo
echo
2022-08-11 03:46:08 +00:00
2022-07-25 04:11:04 +00:00
}
export -f commander
2022-07-25 04:03:57 +00:00
2022-07-27 01:04:27 +00:00
2022-07-25 04:03:57 +00:00
update_apps(){
2022-07-25 21:20:32 +00:00
app_name=$(echo "${array[$it]}" | awk -F ',' '{print $1}') #print out first catagory, name.
2022-07-26 05:38:29 +00:00
printf '%s\0' "${ignore[@]}" | grep -iFxqz "${app_name}" && echo -e "\n$app_name\nIgnored, skipping" && return 0 #If application is on ignore list, skip
2022-07-25 21:20:32 +00:00
old_app_ver=$(echo "${array[$it]}" | awk -F ',' '{print $4}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #previous/current Application MAJOR Version
new_app_ver=$(echo "${array[$it]}" | awk -F ',' '{print $5}' | awk -F '_' '{print $1}' | awk -F '.' '{print $1}') #new Application MAJOR Version
old_chart_ver=$(echo "${array[$it]}" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # Old Chart MAJOR version
new_chart_ver=$(echo "${array[$it]}" | awk -F ',' '{print $5}' | awk -F '_' '{print $2}' | awk -F '.' '{print $1}') # New Chart MAJOR version
2022-07-27 21:47:11 +00:00
startstatus=$(echo "${array[$it]}" | awk -F ',' '{print $2}') #status of the app: STOPPED / DEPLOYING / ACTIVE
2022-07-25 04:03:57 +00:00
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
2022-07-25 21:20:32 +00:00
old_full_ver=$(echo "${array[$it]}" | awk -F ',' '{print $4}') #Upgraded From
new_full_ver=$(echo "${array[$it]}" | awk -F ',' '{print $5}') #Upraded To
rollback_version=$(echo "${array[$it]}" | awk -F ',' '{print $4}' | awk -F '_' '{print $2}')
2022-08-04 20:14:32 +00:00
if grep -qs "^$app_name," failed.txt ; then
2022-08-04 20:46:00 +00:00
failed_ver=$(grep "^$app_name," failed.txt | awk -F ',' '{print $2}')
if [[ "$failed_ver" == "$new_full_ver" ]] ; then
2022-08-04 20:20:40 +00:00
echo -e "\n$app_name"
2022-08-04 22:08:45 +00:00
echo -e "Skipping previously failed version:\n$new_full_ver"
2022-08-04 20:14:32 +00:00
return 0
2022-08-04 20:46:00 +00:00
else
2022-08-04 22:08:45 +00:00
sed -i /"$app_name",/d failed.txt
2022-08-04 20:14:32 +00:00
fi
fi
2022-08-01 21:47:08 +00:00
if [[ "$diff_app" == "$diff_chart" || "$update_all_apps" == "true" ]]; then #continue to update
if [[ $stop_before_update == "true" ]]; then # Check to see if user is using -S or not
if [[ "$startstatus" == "STOPPED" ]]; then # if status is already stopped, skip while loop
2022-07-25 23:06:59 +00:00
echo_array+=("\n$app_name")
[[ "$verbose" == "true" ]] && echo_array+=("Updating..")
2022-08-09 23:46:59 +00:00
if update ;then
echo_array+=("Updated\n$old_full_ver\n$new_full_ver")
else
echo_array+=("Failed to update")
return
fi
2022-08-01 21:47:08 +00:00
else # if status was not STOPPED, stop the app prior to updating
echo_array+=("\n$app_name")
[[ "$verbose" == "true" ]] && echo_array+=("Stopping prior to update..")
2022-08-04 23:31:21 +00:00
midclt call chart.release.scale "$app_name" '{"replica_count": 0}' &> /dev/null || echo_array+=("Error: Failed to stop $app_name")
SECONDS=0
2022-08-11 03:46:08 +00:00
# [[ ! -e trigger ]] && touch trigger
2022-08-01 21:47:08 +00:00
while [[ "$status" != "STOPPED" ]]
do
status=$( grep "^$app_name," temp.txt | awk -F ',' '{print $2}')
if [[ "$status" == "STOPPED" ]]; then
echo_array+=("Stopped")
[[ "$verbose" == "true" ]] && echo_array+=("Updating..")
2022-08-09 23:46:59 +00:00
if update ;then
echo_array+=("Updated\n$old_full_ver\n$new_full_ver")
else
echo_array+=("Failed to update")
return
fi
2022-08-01 21:47:08 +00:00
elif [[ "$SECONDS" -ge "$timeout" ]]; then
echo_array+=("Error: Run Time($SECONDS) has exceeded Timeout($timeout)")
break
elif [[ "$status" != "STOPPED" ]]; then
[[ "$verbose" == "true" ]] && echo_array+=("Waiting $((timeout-SECONDS)) more seconds for $app_name to be STOPPED")
2022-08-09 06:31:04 +00:00
sleep 5
2022-08-01 21:47:08 +00:00
fi
done
2022-07-25 02:29:58 +00:00
fi
2022-08-01 21:47:08 +00:00
else #user must not be using -S, just update
echo_array+=("\n$app_name")
[[ "$verbose" == "true" ]] && echo_array+=("Updating..")
2022-08-09 23:46:59 +00:00
if update ;then
echo_array+=("Updated\n$old_full_ver\n$new_full_ver")
else
echo_array+=("Failed to update")
return
fi
2022-07-25 02:29:58 +00:00
fi
2022-08-01 21:47:08 +00:00
else
2022-08-09 18:16:15 +00:00
echo -e "\n$app_name\nMajor Release, update manually"
2022-08-01 21:47:08 +00:00
return 0
fi
2022-08-04 22:51:10 +00:00
after_update_actions
2022-07-25 22:50:44 +00:00
}
2022-07-25 02:29:58 +00:00
export -f update_apps
2022-08-09 23:32:47 +00:00
update(){
count=0
while [[ $count -lt 3 ]]
do
2022-08-10 04:47:59 +00:00
update_avail=$(grep "^$app_name," temp.txt | awk -F ',' '{print $3}')
if [[ $update_avail == "true" ]]; then
if ! cli -c 'app chart_release upgrade release_name=''"'"$app_name"'"' &> /dev/null ; then
2022-08-11 03:46:08 +00:00
# [[ ! -e trigger ]] && touch trigger && sleep 10
2022-08-10 04:47:59 +00:00
sleep 6
((count++))
continue
fi
break
elif [[ $update_avail == "false" ]]; then
2022-08-09 23:32:47 +00:00
break
2022-08-09 23:52:11 +00:00
else
2022-08-10 04:47:59 +00:00
((count++))
sleep 6
2022-08-09 23:32:47 +00:00
fi
done
}
export -f update
2022-07-25 02:29:58 +00:00
after_update_actions(){
SECONDS=0
count=0
2022-08-04 22:48:55 +00:00
if [[ $rollback == "true" || "$startstatus" == "STOPPED" ]]; then
2022-08-11 03:46:08 +00:00
# [[ ! -e trigger ]] && touch trigger && sleep 10
2022-07-25 02:29:58 +00:00
while true
do
if [[ $count -lt 1 ]]; then
old_status=$(grep "^$app_name," temp.txt)
2022-08-11 05:27:11 +00:00
else
before_loop=$(head -n 1 temp.txt)
until [[ "$new_status" != "$old_status" || $current_loop -gt 3 ]] # Wait for a change in the file BEFORE continuing
2022-08-11 05:27:11 +00:00
do
new_status=$(grep "^$app_name," temp.txt)
2022-08-11 05:37:53 +00:00
sleep 1
if ! echo -e "$(head -n 1 temp.txt)" | grep -qs ^"$before_loop" ; then
before_loop=$(head -n 1 temp.txt)
((current_loop++))
fi
2022-08-11 05:27:11 +00:00
done
old_status=$new_status
2022-08-11 05:27:11 +00:00
fi
# if [[ $count -lt 1 ]]; then
# while_count=$(head -n 1 temp.txt)
# else
# until [[ $while_count -lt $current_count ]] # Wait for a change in the file BEFORE continuing
# do
# current_count=$(head -n 1 temp.txt)
# sleep 1
# done
# while_count=$current_count
# fi
2022-07-27 19:56:22 +00:00
status=$( grep "^$app_name," temp.txt | awk -F ',' '{print $2}')
2022-08-11 05:27:11 +00:00
(( count++ ))
2022-08-04 23:19:44 +00:00
if [[ "$status" == "ACTIVE" ]]; then
if [[ "$startstatus" == "STOPPED" ]]; then
[[ "$count" -le 1 && "$verbose" == "true" ]] && echo_array+=("Verifying Active..") && continue #if reports active on FIRST time through loop, double check
[[ "$count" -le 1 && -z "$verbose" ]] && continue #if reports active on FIRST time through loop, double check
2022-08-04 23:19:44 +00:00
[[ "$verbose" == "true" ]] && echo_array+=("Returing to STOPPED state..")
2022-08-04 23:31:21 +00:00
midclt call chart.release.scale "$app_name" '{"replica_count": 0}' &> /dev/null || { echo_array+=("Error: Failed to stop $app_name") ; break ; }
echo_array+=("Stopped")
2022-08-04 23:19:44 +00:00
break
else
[[ "$count" -le 1 && "$verbose" == "true" ]] && echo_array+=("Verifying Active..") && continue #if reports active on FIRST time through loop, double check
[[ "$count" -le 1 && -z "$verbose" ]] && continue #if reports active on FIRST time through loop, double check
2022-08-04 23:19:44 +00:00
echo_array+=("Active")
break #if reports active any time after the first loop, assume actually active.
fi
elif [[ "$status" == "STOPPED" ]]; then
[[ "$count" -le 1 && "$verbose" == "true" ]] && echo_array+=("Verifying Stopped..") && continue #if reports stopped on FIRST time through loop, double check
[[ "$count" -le 1 && -z "$verbose" ]] && continue #if reports stopped on FIRST time through loop, double check
2022-08-04 23:19:44 +00:00
echo_array+=("Stopped")
break #if reports stopped any time after the first loop, assume its extermal services.
2022-08-04 23:31:21 +00:00
elif [[ "$SECONDS" -ge "$timeout" && "$status" == "DEPLOYING" ]]; then
2022-08-04 22:48:55 +00:00
if [[ $rollback == "true" ]]; then
if [[ "$failed" != "true" ]]; then
2022-08-04 23:19:44 +00:00
echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)")
echo_array+=("If this is a slow starting application, set a higher timeout with -t")
echo_array+=("If this applicaion is always DEPLOYING, you can disable all probes under the Healthcheck Probes Liveness section in the edit configuration")
echo_array+=("Reverting update..")
2022-08-04 23:31:21 +00:00
midclt call chart.release.rollback "$app_name" "{\"item_version\": \"$rollback_version\"}" &> /dev/null || { echo_array+=("Error: Failed to rollback $app_name") ; break ; }
[[ "$startstatus" == "STOPPED" ]] && failed="true" && after_update_actions #run back after_update_actions function if the app was stopped prior to update
2022-08-04 22:48:55 +00:00
echo "$app_name,$new_full_ver" >> failed.txt
break
2022-08-04 23:19:44 +00:00
else
echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)")
echo_array+=("The application failed to be ACTIVE even after a rollback")
echo_array+=("Manual intervention is required\nAbandoning")
2022-08-04 22:48:55 +00:00
break
fi
else
2022-08-04 23:19:44 +00:00
echo_array+=("Error: Run Time($SECONDS) for $app_name has exceeded Timeout($timeout)")
echo_array+=("If this is a slow starting application, set a higher timeout with -t")
echo_array+=("If this applicaion is always DEPLOYING, you can disable all probes under the Healthcheck Probes Liveness section in the edit configuration")
2022-08-04 22:48:55 +00:00
break
fi
2022-07-25 02:29:58 +00:00
else
2022-07-25 23:11:56 +00:00
[[ "$verbose" == "true" ]] && echo_array+=("Waiting $((timeout-SECONDS)) more seconds for $app_name to be ACTIVE")
sleep 10
2022-07-25 02:29:58 +00:00
continue
fi
done
fi
2022-07-25 23:21:41 +00:00
2022-08-04 22:48:55 +00:00
2022-07-25 23:38:10 +00:00
#Dump the echo_array, ensures all output is in a neat order.
2022-07-25 23:21:41 +00:00
for i in "${echo_array[@]}"
do
echo -e "$i"
done
2022-07-25 02:29:58 +00:00
}
export -f after_update_actions