From b9fff986a6eec5a44e0d1d077ecb6158f3710b10 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Sat, 12 Jan 2019 17:55:29 +0000
Subject: [PATCH] Improve killing of canceled app installs

---
 src/freedombone-addremove | 25 +++++++---
 src/freedombone-installer | 99 ++++++++++++++++++++++++---------------
 2 files changed, 80 insertions(+), 44 deletions(-)

diff --git a/src/freedombone-addremove b/src/freedombone-addremove
index 8afc6c92e..532b2f216 100755
--- a/src/freedombone-addremove
+++ b/src/freedombone-addremove
@@ -55,14 +55,25 @@ fi
             webadmin_install_dir="/var/www/${local_hostname}/htdocs/admin"
             pending_installs="$webadmin_install_dir/pending_installs.txt"
 
-            # show the installation failed screen
+            # remove from pending installs
             sed -i "/install_$arg2/d" "$pending_installs"
-            #web_admin_create_add_apps
-            cp "$webadmin_install_dir/app_installing_failed.html" "$webadmin_install_dir/index.html"
-            rm "$webadmin_install_dir/app_installing_failed.html"
-            chown www-data:www-data "$webadmin_install_dir/index.html"
-            cp "$webadmin_install_dir/app_installing_failed.html" "$webadmin_install_dir/installing_progress.html"
-            chown www-data:www-data "$webadmin_install_dir/installing_progress.html"
+
+            cancel_install_file="$webadmin_install_dir/.cancel_install.txt"
+            if [ ! -f "$cancel_install_file" ]; then
+                # show the failed screen
+                cp "$webadmin_install_dir/app_installing_failed.html" "$webadmin_install_dir/index.html"
+                rm "$webadmin_install_dir/app_installing_failed.html"
+                chown www-data:www-data "$webadmin_install_dir/index.html"
+                cp "$webadmin_install_dir/app_installing_failed.html" "$webadmin_install_dir/installing_progress.html"
+                chown www-data:www-data "$webadmin_install_dir/installing_progress.html"
+            else
+                cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/index.html"
+                rm "$webadmin_install_dir/index_app_installing.html"
+                chown www-data:www-data "$webadmin_install_dir/index.html"
+                cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/installing_progress.html"
+                chown www-data:www-data "$webadmin_install_dir/installing_progress.html"
+                echo "Restored index screen" >> "$webadmin_install_dir/applog.txt";
+            fi
 
             exit 1
         else
diff --git a/src/freedombone-installer b/src/freedombone-installer
index 2e4e3dee9..20dffef18 100755
--- a/src/freedombone-installer
+++ b/src/freedombone-installer
@@ -3170,50 +3170,75 @@ function update_bridges {
 
 function cancel_app_installs {
     if [ -f "$cancel_install_file" ]; then
-        kill_pid=$(pgrep "addremove add" | head -n 1)
-        if [ "$kill_pid" ]; then
-            #shellcheck disable=SC2086
-            kill -9 $kill_pid
-
-            if [ -f "$currently_installing_app_domain" ]; then
-                app_domain=$(cat "$currently_installing_app_domain")
-
-                # if an nginx config was added for the app then remove it
-                # and restart the web server
-                if [ -f "/etc/nginx/sites-available/${app_domain}" ]; then
-                    echo "Removing /etc/nginx/sites-available/${app_domain}" >> "$webadmin_install_dir/applog.txt"
-                    nginx_dissite "${app_domain}"
-                    rm "/etc/nginx/sites-available/${app_domain}"
-                    echo "Restarting nginx" >> "$webadmin_install_dir/applog.txt"
-                    systemctl restart nginx
+        #shellcheck disable=SC2009
+        p_str=$(ps a | grep 'addremove-base' | grep -v grep)
+        if [[ "$p_str" == *'/bin/bash '* ]]; then
+            kill_pid=$(echo "$p_str" | awk -F ' ' '{print $1}')
+            if [ "$kill_pid" ]; then
+                #shellcheck disable=SC2086
+                kill -9 $kill_pid
+
+                # wait for the kill to happen
+                kill_ctr=0
+                while true
+                do
+                    sleep 1
+                    #shellcheck disable=SC2009
+                    p_str=$(ps a | grep 'addremove-base' | grep -v grep)
+                    if [ ! "$p_str" ]; then
+                        echo "app install process was killed" >> "$webadmin_install_dir/applog.txt"
+                        break
+                    fi
+                    kill_ctr=$((kill_ctr+1))
+                    if [ $kill_ctr -gt 20 ]; then
+                        echo "Timed out waiting for app install process to be killed" >> "$webadmin_install_dir/applog.txt"
+                        return
+                    fi
+                done
+
+                # wait for any file copies within addremove command to happen
+                sleep 5
+
+                if [ -f "$currently_installing_app_domain" ]; then
+                    app_domain=$(cat "$currently_installing_app_domain")
+
+                    # if an nginx config was added for the app then remove it
+                    # and restart the web server
+                    if [ -f "/etc/nginx/sites-available/${app_domain}" ]; then
+                        echo "Removing /etc/nginx/sites-available/${app_domain}" >> "$webadmin_install_dir/applog.txt"
+                        nginx_dissite "${app_domain}"
+                        rm "/etc/nginx/sites-available/${app_domain}"
+                        echo "Restarting nginx" >> "$webadmin_install_dir/applog.txt"
+                        systemctl restart nginx
+                    fi
+                    rm "$currently_installing_app_domain"
                 fi
-                rm "$currently_installing_app_domain"
-            fi
 
-            if [ -f "$currently_installing_app_name" ]; then
-                app_name=$(cat "$currently_installing_app_name")
+                if [ -f "$currently_installing_app_name" ]; then
+                    app_name=$(cat "$currently_installing_app_name")
 
-                # remove from pending installs
-                sed -i "/install_${app_name}/d" "$pending_installs"
+                    # remove from pending installs
+                    sed -i "/install_${app_name}/d" "$pending_installs"
 
-                # restore index screen
-                if [ -f "$webadmin_install_dir/index_app_installing.html" ]; then
-                    cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/index.html"
-                    rm "$webadmin_install_dir/index_app_installing.html"
-                    chown www-data:www-data "$webadmin_install_dir/index.html"
-                    cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/installing_progress.html"
-                    chown www-data:www-data "$webadmin_install_dir/installing_progress.html"
-                    echo "Restored index screen" >> "$webadmin_install_dir/applog.txt";
-                fi
+                    # restore index screen
+                    if [ -f "$webadmin_install_dir/index_app_installing.html" ]; then
+                        cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/index.html"
+                        rm "$webadmin_install_dir/index_app_installing.html"
+                        chown www-data:www-data "$webadmin_install_dir/index.html"
+                        cp "$webadmin_install_dir/index_app_installing.html" "$webadmin_install_dir/installing_progress.html"
+                        chown www-data:www-data "$webadmin_install_dir/installing_progress.html"
+                        echo "Restored index screen" >> "$webadmin_install_dir/applog.txt";
+                    fi
 
-                notification_str="Failed to install. For details see admin/applog.txt"
-                translated_notification_str=$(web_admin_translate_text "$notification_str")
-                /usr/local/bin/${PROJECT_NAME}-notification -s "[${PROJECT_NAME}] ${app_name} ${translated_notification_str}" -m "${app_name} ${translated_notification_str}" &
+                    notification_str="Failed to install. For details see admin/applog.txt"
+                    translated_notification_str=$(web_admin_translate_text "$notification_str")
+                    /usr/local/bin/${PROJECT_NAME}-notification -s "[${PROJECT_NAME}] ${app_name} ${translated_notification_str}" -m "${app_name} ${translated_notification_str}" &
 
-                rm "$currently_installing_app_name"
+                    rm "$currently_installing_app_name"
+                fi
+                echo "App install manually cancelled" >> "$webadmin_install_dir/applog.txt";
+                chown www-data:www-data "$webadmin_install_dir/applog.txt"
             fi
-            echo "App install manually cancelled" >> "$webadmin_install_dir/applog.txt";
-            chown www-data:www-data "$webadmin_install_dir/applog.txt"
         fi
         rm "$cancel_install_file"
     fi
-- 
GitLab