From 03c81756e04825c0ee93d7a6a2fc022f864fcd20 Mon Sep 17 00:00:00 2001 From: Heavy Date: Thu, 12 Oct 2023 22:29:57 -0600 Subject: [PATCH] dont scale depl, unmount mounted apps when start --- .default.config.ini | 7 +++- .../backup_restore/database/cnpg_dump.sh | 38 ++++++++++++++----- utils/generate_config.sh | 20 +++------- utils/start_app.sh | 11 ++++++ 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/.default.config.ini b/.default.config.ini index 119a907d..23f706c2 100644 --- a/.default.config.ini +++ b/.default.config.ini @@ -64,4 +64,9 @@ enabled=true ## String options ## # File path for database dump folder -dump_folder="./database_dumps" \ No newline at end of file +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="" \ No newline at end of file diff --git a/functions/backup_restore/database/cnpg_dump.sh b/functions/backup_restore/database/cnpg_dump.sh index 3e121c84..8edfbda7 100644 --- a/functions/backup_restore/database/cnpg_dump.sh +++ b/functions/backup_restore/database/cnpg_dump.sh @@ -189,9 +189,17 @@ 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 @@ -199,8 +207,13 @@ backup_cnpg_databases() { 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 @@ -214,13 +227,15 @@ 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 @@ -228,11 +243,16 @@ backup_cnpg_databases() { 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.") diff --git a/utils/generate_config.sh b/utils/generate_config.sh index dfb94f10..49ebc113 100644 --- a/utils/generate_config.sh +++ b/utils/generate_config.sh @@ -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 -} diff --git a/utils/start_app.sh b/utils/start_app.sh index 69e1e642..82a31716 100644 --- a/utils/start_app.sh +++ b/utils/start_app.sh @@ -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