sudo87 commented on code in PR #11541:
URL: https://github.com/apache/cloudstack/pull/11541#discussion_r2322754901


##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -2014,31 +2019,52 @@ public boolean unmanage(String vmUuid) {
             throw new ConcurrentOperationException(msg);
         }
 
-        Boolean result = Transaction.execute(new 
TransactionCallback<Boolean>() {
-            @Override
-            public Boolean doInTransaction(TransactionStatus status) {
+        boolean isDomainXMLPreserved = true;
+        // persist domain for kvm host
+        if (HypervisorType.KVM.equals(vm.getHypervisorType())) {
+            long hostId = vm.getHostId();
+            UnmanageInstanceCommand unmanageInstanceCommand;
+            if (State.Stopped.equals(vm.getState())) {
+                hostId = vm.getLastHostId();
+                unmanageInstanceCommand = new 
UnmanageInstanceCommand(prepareVmTO(vm.getId(), hostId)); // reconstruct vmSpec
+            } else {
+                unmanageInstanceCommand = new 
UnmanageInstanceCommand(vm.getName());
+            }
+            try {
+                Answer answer = _agentMgr.send(hostId, 
unmanageInstanceCommand);
+                isDomainXMLPreserved = (answer instanceof 
UnmanageInstanceAnswer && answer.getResult());
+            } catch (Exception ex) {
+                isDomainXMLPreserved = false;
+            }
+        }
 
-                logger.debug("Unmanaging VM {}", vm);
+        if (isDomainXMLPreserved) {
+            logger.debug("Unmanaging VM {}", vm);
+            Boolean result = Transaction.execute(new 
TransactionCallback<Boolean>() {
+                @Override
+                public Boolean doInTransaction(TransactionStatus status) {
+                    final VirtualMachineProfile profile = new 
VirtualMachineProfileImpl(vm);
+                    final VirtualMachineGuru guru = getVmGuru(vm);
 
-                final VirtualMachineProfile profile = new 
VirtualMachineProfileImpl(vm);
-                final VirtualMachineGuru guru = getVmGuru(vm);
+                    try {
+                        unmanageVMSnapshots(vm);
+                        unmanageVMNics(profile, vm);
+                        unmanageVMVolumes(vm);
 
-                try {
-                    unmanageVMSnapshots(vm);
-                    unmanageVMNics(profile, vm);
-                    unmanageVMVolumes(vm);
+                        guru.finalizeUnmanage(vm);
+                    } catch (Exception e) {
+                        logger.error("Error while unmanaging VM {}", vm, e);
+                        return false;
+                    }
 
-                    guru.finalizeUnmanage(vm);
-                } catch (Exception e) {
-                    logger.error("Error while unmanaging VM {}", vm, e);
-                    return false;
+                    return true;
                 }
-
-                return true;
-            }
-        });
-
-        return BooleanUtils.isTrue(result);
+            });
+            return BooleanUtils.isTrue(result);

Review Comment:
   moved domainXML related code block to separate method for clarity.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to