From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.weber@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 02B4696AC5
 for <pve-devel@lists.proxmox.com>; Thu, 26 Jan 2023 09:33:28 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 85A352021E
 for <pve-devel@lists.proxmox.com>; Thu, 26 Jan 2023 09:32:57 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Thu, 26 Jan 2023 09:32:56 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1BD3E46519
 for <pve-devel@lists.proxmox.com>; Thu, 26 Jan 2023 09:32:56 +0100 (CET)
From: Friedrich Weber <f.weber@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 26 Jan 2023 09:32:11 +0100
Message-Id: <20230126083214.711099-2-f.weber@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20230126083214.711099-1-f.weber@proxmox.com>
References: <20230126083214.711099-1-f.weber@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.769 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [data.id]
Subject: [pve-devel] [RFC manager 1/4] fix #4474: ui: vm stop: ask if active
 shutdown tasks should be aborted
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 26 Jan 2023 08:33:28 -0000

Before stopping a VM (KVM or LXC), the UI now checks if there is an
active shutdown task for the same VM by the logged-in user. If there
is at least one, the UI asks whether these tasks should be aborted
before trying to stop the VM. If the user answers "Yes", the UI sends
a VM stop request with the `overrule-shutdown` parameter set to 1. If
there are no active shutdown tasks, or the user answers "No", the UI
sends a VM stop request without `overrule-shutdown`.

To avoid an additional API request for querying active shutdown tasks,
we check the UI's current view of cluster tasks instead, which we
fetch from the `pve-cluster-tasks` store.

As the UI might hold an outdated task list, there are some
opportunities for races, e.g., the UI may miss a new shutdown task or
consider a shutdown task active even though it has already terminated.
These races either result in a surviving shutdown task that the user
still needs to abort manually, or a superfluous `override-shutdown=1`
parameter that does not actually abort any tasks. Since "stop
overrules shutdown" is merely a convenience feature, both outcomes
seem bearable.

Signed-off-by: Friedrich Weber <f.weber@proxmox.com>
---
 www/manager6/Utils.js        | 27 +++++++++++++++++++++++++++
 www/manager6/dc/Tasks.js     |  2 +-
 www/manager6/lxc/CmdMenu.js  | 14 +++++++++++++-
 www/manager6/lxc/Config.js   |  6 +++++-
 www/manager6/qemu/CmdMenu.js | 14 +++++++++++++-
 www/manager6/qemu/Config.js  |  9 ++++++++-
 6 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 3dd287e3..9e25e647 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -1426,6 +1426,33 @@ Ext.define('PVE.Utils', {
 	}
     },
 
+    // If there is an active task of type `taskType` by the current user for the
+    // given VMID, ask whether it should be aborted. Call `callback` accordingly.
+    askForOverruleShutdown: function(guestType, vm, taskType, callback) {
+	const vmidString = vm.vmid.toString();
+	const haEnabled = vm.hastate && vm.hastate !== 'unmanaged';
+	let shutdownTask = Ext.getStore('pve-cluster-tasks')?.findBy(function(task) {
+	    return task.data.user === Proxmox.UserName &&
+		   task.data.id === vmidString &&
+		   task.data.status === undefined &&
+		   task.data.type === taskType;
+	});
+	if (!haEnabled && shutdownTask >= 0) {
+	    const msg = gettext('There are active {0} tasks for {1} {2}. Would you like to abort them before proceeding?');
+	    Ext.Msg.show({
+			 title: gettext('Confirm'),
+			 icon: Ext.Msg.QUESTION,
+			 buttons: Ext.Msg.YESNO,
+			 msg: Ext.String.format(msg, taskType, guestType, vmidString),
+			 callback: function(btn) {
+			     callback(btn === 'yes');
+			 },
+	    });
+	} else {
+	    callback(false);
+	}
+    },
+
     // test automation helper
     call_menu_handler: function(menu, text) {
 	let item = menu.query('menuitem').find(el => el.text === text);
diff --git a/www/manager6/dc/Tasks.js b/www/manager6/dc/Tasks.js
index 5344ede4..2001bf76 100644
--- a/www/manager6/dc/Tasks.js
+++ b/www/manager6/dc/Tasks.js
@@ -11,7 +11,7 @@ Ext.define('PVE.dc.Tasks', {
 	let me = this;
 
 	let taskstore = Ext.create('Proxmox.data.UpdateStore', {
-	    storeid: 'pve-cluster-tasks',
+	    storeId: 'pve-cluster-tasks',
 	    model: 'proxmox-tasks',
 	    proxy: {
 		type: 'proxmox',
diff --git a/www/manager6/lxc/CmdMenu.js b/www/manager6/lxc/CmdMenu.js
index 56f36b5e..01631b1d 100644
--- a/www/manager6/lxc/CmdMenu.js
+++ b/www/manager6/lxc/CmdMenu.js
@@ -66,7 +66,19 @@ Ext.define('PVE.lxc.CmdMenu', {
 		iconCls: 'fa fa-fw fa-stop',
 		disabled: stopped,
 		tooltip: Ext.String.format(gettext('Stop {0} immediately'), 'CT'),
-		handler: () => confirmedVMCommand('stop'),
+		handler: () => {
+		    let msg = Proxmox.Utils.format_task_description('vzstop', info.vmid);
+		    Ext.Msg.confirm(gettext('Confirm'), msg, btn => {
+			if (btn === 'yes') {
+			    PVE.Utils.askForOverruleShutdown('CT', info,
+							     'vzshutdown', overruleShutdown => {
+				const param = overruleShutdown ? { 'overrule-shutdown': 1 }
+								     : undefined;
+				vm_command('stop', param);
+			    });
+			}
+		    });
+		},
 	    },
 	    {
 		text: gettext('Reboot'),
diff --git a/www/manager6/lxc/Config.js b/www/manager6/lxc/Config.js
index 23c17d2e..21d93058 100644
--- a/www/manager6/lxc/Config.js
+++ b/www/manager6/lxc/Config.js
@@ -81,7 +81,11 @@ Ext.define('PVE.lxc.Config', {
 		    tooltip: Ext.String.format(gettext('Stop {0} immediately'), 'CT'),
 		    dangerous: true,
 		    handler: function() {
-			vm_command("stop");
+			PVE.Utils.askForOverruleShutdown('CT', vm,
+							 'vzshutdown', overruleShutdown => {
+			    const param = overruleShutdown ? { 'overrule-shutdown': 1 } : undefined;
+			    vm_command('stop', param);
+			});
 		    },
 		    iconCls: 'fa fa-stop',
 		}],
diff --git a/www/manager6/qemu/CmdMenu.js b/www/manager6/qemu/CmdMenu.js
index ccc5f74d..3edd2550 100644
--- a/www/manager6/qemu/CmdMenu.js
+++ b/www/manager6/qemu/CmdMenu.js
@@ -93,7 +93,19 @@ Ext.define('PVE.qemu.CmdMenu', {
 		iconCls: 'fa fa-fw fa-stop',
 		disabled: stopped,
 		tooltip: Ext.String.format(gettext('Stop {0} immediately'), 'VM'),
-		handler: () => confirmedVMCommand('stop'),
+		handler: () => {
+		    let msg = Proxmox.Utils.format_task_description('qmstop', info.vmid);
+		    Ext.Msg.confirm(gettext('Confirm'), msg, btn => {
+			if (btn === 'yes') {
+			    PVE.Utils.askForOverruleShutdown('VM', info,
+							     'qmshutdown', overruleShutdown => {
+				const param = overruleShutdown ? { 'overrule-shutdown': 1 }
+								     : undefined;
+				vm_command('stop', param);
+			    });
+			}
+		    });
+		},
 	    },
 	    {
 		text: gettext('Reboot'),
diff --git a/www/manager6/qemu/Config.js b/www/manager6/qemu/Config.js
index 94c540c5..68c41454 100644
--- a/www/manager6/qemu/Config.js
+++ b/www/manager6/qemu/Config.js
@@ -180,7 +180,14 @@ Ext.define('PVE.qemu.Config', {
 		    tooltip: Ext.String.format(gettext('Stop {0} immediately'), 'VM'),
 		    confirmMsg: Proxmox.Utils.format_task_description('qmstop', vmid),
 		    handler: function() {
-			vm_command("stop", { timeout: 30 });
+			PVE.Utils.askForOverruleShutdown('VM', vm,
+							 'qmshutdown', overruleShutdown => {
+			    const param = { timeout: 30 };
+			    if (overruleShutdown) {
+				param['overrule-shutdown'] = 1;
+			    }
+			    vm_command('stop', param);
+			});
 		    },
 		    iconCls: 'fa fa-stop',
 		}, {
-- 
2.30.2