Instead of trying to add a HA resource in any case, which fails if it
already exists and will then not overwrite the status, check if the VM
already is a HA resource. If the resource already exists, set the status
of the existing resource, otherwise add the resource as before.

Signed-off-by: Michael Köppl <[email protected]>
---
 src/PVE/API2/Qemu.pm | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 25b6529c..4d4f0873 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -1417,9 +1417,17 @@ __PACKAGE__->register_method({
             }
 
             if ($ha_managed) {
-                print "Add as HA resource\n";
+                my $resource_exists = 
PVE::HA::Config::service_is_configured("vm:$vmid");
                 my $state = $start_after_create || $live_restore ? 'started' : 
'stopped';
-                my $cmd = ['ha-manager', 'add', "vm:$vmid", '--state', $state];
+                my $cmd;
+                if ($resource_exists) {
+                    print "Update state of HA resource\n";
+                    $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 
$state];
+                } else {
+                    print "Add as HA resource\n";
+                    $cmd = ['ha-manager', 'add', "vm:$vmid", '--state', 
$state];
+                }
+
                 eval { PVE::Tools::run_command($cmd); };
                 warn $@ if $@;
             }
-- 
2.47.3



_______________________________________________
pve-devel mailing list
[email protected]
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to