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

Commit

Permalink
dont scale depl, unmount mounted apps when start
Browse files Browse the repository at this point in the history
  • Loading branch information
Heavybullets8 committed Oct 13, 2023
1 parent c0008e4 commit 03c8175
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .default.config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ enabled=true

## String options ##
# File path for database dump folder
dump_folder="./database_dumps"
dump_folder="./database_dumps"

# Apps listed here will have their deployments shut down prior to their CNPG Database dump
# This is usually uneccesary, and unless otherwise recommended, leave blank
# Example: stop_before_dump=nextcloud,appname,appname
stop_before_dump=""
38 changes: 29 additions & 9 deletions functions/backup_restore/database/cnpg_dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,31 @@ backup_cnpg_databases() {
local retention=$1
local timestamp=$2
local dump_folder=$3
local stop_before_dump=()

mapfile -t app_status_lines < <(db_dump_get_app_status)


# Get the stop_before_dump value from config.ini
temp="${DATABASES__databases__stop_before_dump:-}"
# Split comma-separated values into an array
IFS=',' read -ra stop_before_dump <<< "$temp"
unset temp

if [[ ${#app_status_lines[@]} -eq 0 ]]; then
return
fi

echo_backup+=("--CNPG Database Backups--")

for app in "${app_status_lines[@]}"; do
scale_deployments_bool=false
IFS=',' read -r app_name app_status <<< "$app"

if printf '%s\0' "${stop_before_dump[@]}" | grep -iFxqz "${app_name}"; then
scale_deployments_bool=true
fi

# Start the app if it is stopped
if [[ $app_status == "STOPPED" ]]; then
start_app "$app_name" 1
Expand All @@ -214,25 +227,32 @@ backup_cnpg_databases() {
original_replicas["$key"]=$value
done

# Scale down all deployments in the app to 0
for deployment in "${!original_replicas[@]}"; do
if [[ ${original_replicas[$deployment]} -ne 0 ]] && ! scale_deployments "$app_name" 300 0 "$deployment" > /dev/null 2>&1; then
echo_backup+=("Failed to scale down $app_name's $deployment deployment.")
return
fi
done
if [[ $scale_deployments_bool == true ]]; then
# Scale down all deployments in the app to 0
for deployment in "${!original_replicas[@]}"; do
if [[ ${original_replicas[$deployment]} -ne 0 ]] && ! scale_deployments "$app_name" 300 0 "$deployment" > /dev/null 2>&1; then
echo_backup+=("Failed to scale down $app_name's $deployment deployment.")
return
fi
done
fi

# Dump the database
if ! dump_database "$app_name" "$dump_folder"; then
echo_backup+=("Failed to back up $app_name's database.")
return
fi

# Scale up all deployments in the app to their original replica counts, or stop the app if it was stopped
# Stop the app if it was stopped
if [[ $app_status == "STOPPED" ]]; then
wait_for_redeploy_jobs "$app_name"
stop_app "direct" "$app_name"
else
break
fi


if [[ $scale_deployments_bool == true ]]; then
# Scale up all deployments in the app to their original replica counts
for deployment in "${!original_replicas[@]}"; do
if [[ ${original_replicas[$deployment]} -ne 0 ]] && ! scale_deployments "$app_name" 300 "${original_replicas[$deployment]}" "$deployment" > /dev/null 2>&1; then
echo_backup+=("Failed to scale up $app_name's $deployment deployment.")
Expand Down
20 changes: 5 additions & 15 deletions utils/generate_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,11 @@ generate_config_ini() {
add_database_options() {
config_file="config.ini"

# Check if the [databases] section exists
if ! grep -q "^\[databases\]" "$config_file"; then
# Add the [databases] section to the config file
echo -e "\n[databases]" >> "$config_file"
# Check if the stop_before_dump option exists
if ! grep -q "^stop_before_dump=" "$config_file"; then
# Add the stop_before_dump option with a default value and description
awk -i inplace -v stop_before_dump_option="\n# Apps listed here will have their deployments shut down prior to their CNPG Database dump\n# This is usually unnecessary, and unless otherwise recommended, leave blank\n# Example: stop_before_dump=nextcloud,appname,appname\nstop_before_dump=\"\"\n" '/^dump_folder=.*/ { print; print stop_before_dump_option; next }1' "$config_file"
fi
}

# Check if the enabled option exists
if ! grep -q "^enabled=" "$config_file"; then
# Add the enabled option with a default value and description
awk -i inplace -v enable_option="## true/false options ##\n# Enable or disable database dumps\nenabled=true\n" '/^\[databases\]/ { print; print enable_option; next }1' "$config_file"
fi

# Check if the dump_folder option exists
if ! grep -q "^dump_folder=" "$config_file"; then
# Add the dump_folder option with a default value and description
awk -i inplace -v dump_folder_option="\n## String options ##\n# File path for database dump folder\ndump_folder=\"./database_dumps\"" '/^enabled=true/ { print; print dump_folder_option; next }1' "$config_file"
fi
}
11 changes: 11 additions & 0 deletions utils/start_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,21 @@ get_running_job_id(){
'.[] | 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'
}

check_mounted(){
local app_name=$1

if [[ -d /mnt/mounted_apps/"$app_name" ]]; then
unmount_app_func "$app_name" > /dev/null 2>&1
fi
}

start_app(){
local app_name=$1
local job_id

#check if app is currently mounted
check_mounted "$app_name"

# Check if app is a cnpg instance, or an operator instance
output=$(check_filtered_apps "$app_name")
if [[ $output == *"${app_name},stopAll-on"* ]]; then
Expand Down

0 comments on commit 03c8175

Please sign in to comment.