Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Lifecycle (#111)
Browse files Browse the repository at this point in the history
[Bugfixes]
- Suppress errors
- Better verbose message for time to become active in update function
- Abort "scale" jobs when starting applications
-  - Fixes errors when starting various applications
  • Loading branch information
Heavybullets8 authored Jun 12, 2023
1 parent 8789410 commit 2a3bbcc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 26 deletions.
3 changes: 1 addition & 2 deletions functions/backup_restore/database/cnpg_dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ wait_for_postgres_pod() {

# shellcheck disable=SC2034
for i in {1..30}; do
pod_status=$(k3s kubectl get pods "${app_name}-cnpg-main-1" -n "ix-${app_name}" -o jsonpath="{.status.phase}")
pod_status=$(k3s kubectl get pods "${app_name}-cnpg-main-1" -n "ix-${app_name}" -o jsonpath="{.status.phase}" 2>/dev/null)

if [[ "$pod_status" == "Running" ]]; then
return 0
Expand All @@ -158,7 +158,6 @@ wait_for_postgres_pod() {
return 1
}


backup_cnpg_databases() {
retention=$1
timestamp=$2
Expand Down
2 changes: 0 additions & 2 deletions functions/enable/apt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ if [ "$success" = true ]; then
echo -e "\n${bold}Example usage:${reset}"
echo -e "For most use cases, use ${blue}apt${reset}:"
echo -e " ${blue}apt update${reset}"
echo -e " ${blue}apt upgrade${reset}"
echo -e " ${blue}apt install package_name${reset}"

echo -e "\nFor advanced users or specific cases, you can use ${blue}apt-get${reset}:"
echo -e " ${blue}apt-get update${reset}"
echo -e " ${blue}apt-get upgrade${reset}"
echo -e " ${blue}apt-get install package_name${reset}"
fi
}
Expand Down
1 change: 0 additions & 1 deletion functions/update/commander.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ get_apps_with_status() {
apps_with_status+=("$app_name,$status")
fi
done < <(check_filtered_apps "${array[@]/,*}")

}

# Skip if the image update should be ignored
Expand Down
13 changes: 5 additions & 8 deletions functions/update/post_process.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ rollbacks_disabled(){
echo_array+=("Manual intervention is required\nStopping, then Abandoning")
}

handle_wait() {
if [[ "$verbose" == true ]]; then
echo_array+=("Waiting $((timeout-SECONDS)) more seconds for $app_name to be ACTIVE")
fi
sleep 5
}

handle_rollback() {
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")
Expand Down Expand Up @@ -81,6 +74,9 @@ post_process(){
status=$(update_status)

if [[ "$status" == "ACTIVE" ]]; then
if [[ "$verbose" == true ]]; then
echo_array+=("Became Active after $SECONDS seconds")
fi
if [[ "$startstatus" == "STOPPED" ]]; then
update_stop_handler 'Returing to STOPPED state...'
else
Expand All @@ -107,10 +103,11 @@ post_process(){
break
fi
else
handle_wait
sleep 5
fi
done

echo_array
}
export -f post_process

7 changes: 7 additions & 0 deletions functions/update/pre_process.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ pre_process() {
return
fi

if [[ $stop_before_update == true && "$startstatus" != "STOPPED" ]]; then
if ! start_app "$app_name"; then
echo_array
return
fi
fi

if [[ $rollback == true || "$startstatus" == "STOPPED" ]]; then
if printf '%s\0' "${apps_with_status[@]}" | grep -iFxqz "${app_name},stopAll-on"; then
echo_array+=("Stopped")
Expand Down
71 changes: 63 additions & 8 deletions utils/start_app.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,77 @@
#!/bin/bash

abort_job(){
local app_name=$1
job_ids=""

# shellcheck disable=SC2034
for i in {1..60}; do
job_ids=$(get_running_job_id "$app_name")

if [[ -n "$job_ids" ]]; then
while IFS= read -r job_id; do
midclt call core.job_abort "$job_id" > /dev/null 2>&1
done <<< "$job_ids"
return 0
fi

sleep 1
done
return 1
}

wait_for_redeploy_methods(){
local app_name=$1
local methods=""

# shellcheck disable=SC2034
for i in {1..60}; do
methods=$(get_running_methods "$app_name")

if [[ "$methods" == *"chart.release.redeploy"* && "$methods" == *"chart.release.redeploy_internal"* ]]; then
return 0
fi

sleep 1
done
return 1
}

get_running_methods(){
local app_name=$1
midclt call core.get_jobs | jq -r --arg app_name "$app_name" \
'.[] | select( .time_finished == null and .state == "RUNNING" and (.arguments[0] == $app_name)) | .method'
}

get_running_job_id(){
local app_name=$1
midclt call core.get_jobs | jq -r --arg app_name "$app_name" \
'.[] | select( .time_finished == null and .state == "RUNNING" and (.progress.description | test("Waiting for pods to be scaled to [0-9]+ replica\\(s\\)$")) and (.arguments[0] == $app_name and .method == "chart.release.scale") ) | .id'
}

start_app(){
local app_name=$1
local replica_count=$2
local replica_count=${2:-$(pull_replicas "$app_name")}
local job_id

# Check if app is a cnpg instance, or an operator instance
output=$(check_filtered_apps "$app_name")
if echo "$output" | grep -q "${app_name},stopAll-on"; then
if ! cli -c 'app chart_release scale release_name='\""$app_name"\"\ 'scale_options={"replica_count": '"$replica_count}" > /dev/null; then
return 1
fi
if ! cli -c "app chart_release update chart_release=\"$app_name\" values={\"global\": {\"stopAll\": false}}" > /dev/null; then
if [[ $output == *"${app_name},stopAll-on"* ]]; then
if ! cli -c "app chart_release update chart_release=\"$app_name\" values={\"global\": {\"stopAll\": false}}" > /dev/null 2>&1; then
return 1
fi
abort_job "$app_name"
job_id=$(midclt call chart.release.scale "$app_name" '{"replica_count": '"$replica_count"'}') || return 1
wait_for_redeploy_methods "$app_name"
midclt call core.job_abort "$job_id" > /dev/null 2>&1
elif [[ $output == *"${app_name},stopAll-off"* ]]; then
job_id=$(midclt call chart.release.scale "$app_name" '{"replica_count": '"$replica_count"'}') || return 1
wait_for_redeploy_methods "$app_name"
midclt call core.job_abort "$job_id" > /dev/null 2>&1
else
if ! cli -c 'app chart_release scale release_name='\""$app_name"\"\ 'scale_options={"replica_count": '"$replica_count}" > /dev/null; then
if ! cli -c 'app chart_release scale release_name='\""$app_name"\"\ 'scale_options={"replica_count": '"$replica_count}" > /dev/null 2>&1; then
return 1
fi
fi
return 0
}
}
10 changes: 5 additions & 5 deletions utils/stop_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ stop_app() {
fi

# Check if the output contains the desired namespace and "cnpg" or "operator"
if echo "$output" | grep -q "${app_name},stopAll-*"; then
timeout "${timeout}s" cli -c "app chart_release update chart_release=\"$app_name\" values={\"global\": {\"stopAll\": true}}" > /dev/null
handle_timeout $?
elif echo "$output" | grep -q "${app_name},operator"; then
if echo "$output" | grep -q "${app_name},operator"; then
return 3
elif echo "$output" | grep -q "${app_name},stopAll-*"; then
timeout "${timeout}s" cli -c "app chart_release update chart_release=\"$app_name\" values={\"global\": {\"stopAll\": true}}" > /dev/null 2>&1
handle_timeout $?
else
timeout "${timeout}s" cli -c 'app chart_release scale release_name='\""$app_name"\"\ 'scale_options={"replica_count": 0}' > /dev/null
timeout "${timeout}s" cli -c "app chart_release update chart_release=\"$app_name\" values={\"global\": {\"stopAll\": true}}" > /dev/null 2>&1
handle_timeout $?
fi
}

0 comments on commit 2a3bbcc

Please sign in to comment.