|
150 | 150 | import com.cloud.agent.api.StopCommand; |
151 | 151 | import com.cloud.agent.api.UnPlugNicAnswer; |
152 | 152 | import com.cloud.agent.api.UnPlugNicCommand; |
153 | | -import com.cloud.agent.api.UnmanageInstanceAnswer; |
154 | 153 | import com.cloud.agent.api.UnmanageInstanceCommand; |
155 | 154 | import com.cloud.agent.api.UnregisterVMCommand; |
156 | 155 | import com.cloud.agent.api.VmDiskStatsEntry; |
@@ -1173,8 +1172,6 @@ protected void checkAndAttemptMigrateVmAcrossCluster(final VMInstanceVO vm, fina |
1173 | 1172 | markVolumesInPool(vm, answer); |
1174 | 1173 | } |
1175 | 1174 |
|
1176 | | - |
1177 | | - |
1178 | 1175 | protected void updateVmMetadataManufacturerAndProduct(VirtualMachineTO vmTO, VMInstanceVO vm) { |
1179 | 1176 | String metadataManufacturer = VmMetadataManufacturer.valueIn(vm.getDataCenterId()); |
1180 | 1177 | if (StringUtils.isBlank(metadataManufacturer)) { |
@@ -2019,52 +2016,61 @@ public boolean unmanage(String vmUuid) { |
2019 | 2016 | throw new ConcurrentOperationException(msg); |
2020 | 2017 | } |
2021 | 2018 |
|
2022 | | - boolean isDomainXMLPreserved = true; |
2023 | | - // persist domain for kvm host |
2024 | 2019 | if (HypervisorType.KVM.equals(vm.getHypervisorType())) { |
2025 | | - long hostId = vm.getHostId(); |
2026 | | - UnmanageInstanceCommand unmanageInstanceCommand; |
2027 | | - if (State.Stopped.equals(vm.getState())) { |
2028 | | - hostId = vm.getLastHostId(); |
2029 | | - unmanageInstanceCommand = new UnmanageInstanceCommand(prepareVmTO(vm.getId(), hostId)); // reconstruct vmSpec |
2030 | | - } else { |
2031 | | - unmanageInstanceCommand = new UnmanageInstanceCommand(vm.getName()); |
2032 | | - } |
2033 | | - try { |
2034 | | - Answer answer = _agentMgr.send(hostId, unmanageInstanceCommand); |
2035 | | - isDomainXMLPreserved = (answer instanceof UnmanageInstanceAnswer && answer.getResult()); |
2036 | | - } catch (Exception ex) { |
2037 | | - isDomainXMLPreserved = false; |
2038 | | - } |
| 2020 | + persistDomainForKVM(vm); |
2039 | 2021 | } |
| 2022 | + Boolean result = Transaction.execute(new TransactionCallback<Boolean>() { |
| 2023 | + @Override |
| 2024 | + public Boolean doInTransaction(TransactionStatus status) { |
2040 | 2025 |
|
2041 | | - if (isDomainXMLPreserved) { |
2042 | | - logger.debug("Unmanaging VM {}", vm); |
2043 | | - Boolean result = Transaction.execute(new TransactionCallback<Boolean>() { |
2044 | | - @Override |
2045 | | - public Boolean doInTransaction(TransactionStatus status) { |
2046 | | - final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); |
2047 | | - final VirtualMachineGuru guru = getVmGuru(vm); |
| 2026 | + logger.debug("Unmanaging VM {}", vm); |
2048 | 2027 |
|
2049 | | - try { |
2050 | | - unmanageVMSnapshots(vm); |
2051 | | - unmanageVMNics(profile, vm); |
2052 | | - unmanageVMVolumes(vm); |
| 2028 | + final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); |
| 2029 | + final VirtualMachineGuru guru = getVmGuru(vm); |
2053 | 2030 |
|
2054 | | - guru.finalizeUnmanage(vm); |
2055 | | - } catch (Exception e) { |
2056 | | - logger.error("Error while unmanaging VM {}", vm, e); |
2057 | | - return false; |
2058 | | - } |
| 2031 | + try { |
| 2032 | + unmanageVMSnapshots(vm); |
| 2033 | + unmanageVMNics(profile, vm); |
| 2034 | + unmanageVMVolumes(vm); |
2059 | 2035 |
|
2060 | | - return true; |
| 2036 | + guru.finalizeUnmanage(vm); |
| 2037 | + } catch (Exception e) { |
| 2038 | + logger.error("Error while unmanaging VM {}", vm, e); |
| 2039 | + return false; |
2061 | 2040 | } |
2062 | | - }); |
2063 | | - return BooleanUtils.isTrue(result); |
| 2041 | + |
| 2042 | + return true; |
| 2043 | + } |
| 2044 | + }); |
| 2045 | + |
| 2046 | + return BooleanUtils.isTrue(result); |
| 2047 | + } |
| 2048 | + |
| 2049 | + void persistDomainForKVM(VMInstanceVO vm) { |
| 2050 | + long hostId = vm.getHostId(); |
| 2051 | + UnmanageInstanceCommand unmanageInstanceCommand; |
| 2052 | + if (State.Stopped.equals(vm.getState())) { |
| 2053 | + hostId = vm.getLastHostId(); |
| 2054 | + unmanageInstanceCommand = new UnmanageInstanceCommand(prepVmSpecForUnmanageCmd(vm.getId(), hostId)); // reconstruct vmSpec for stopped instance |
2064 | 2055 | } else { |
2065 | | - logger.error("Error encountered persisting domainXML for vm: {}", vm.getName()); |
| 2056 | + unmanageInstanceCommand = new UnmanageInstanceCommand(vm.getName()); |
| 2057 | + } |
| 2058 | + try { |
| 2059 | + Answer answer = _agentMgr.send(hostId, unmanageInstanceCommand); |
| 2060 | + if (!answer.getResult()) { |
| 2061 | + String errorMsg = "Failed to persist domainXML for instance: " + vm.getName(); |
| 2062 | + logger.debug(errorMsg); |
| 2063 | + throw new CloudRuntimeException(errorMsg); |
| 2064 | + } |
| 2065 | + } catch (AgentUnavailableException e) { |
| 2066 | + String errorMsg = "Failed to send command, agent unavailable"; |
| 2067 | + logger.error(errorMsg, e); |
| 2068 | + throw new CloudRuntimeException(errorMsg); |
| 2069 | + } catch (OperationTimedoutException e) { |
| 2070 | + String errorMsg = "Failed to send command, operation timed out"; |
| 2071 | + logger.error(errorMsg, e); |
| 2072 | + throw new CloudRuntimeException(errorMsg); |
2066 | 2073 | } |
2067 | | - return false; |
2068 | 2074 | } |
2069 | 2075 |
|
2070 | 2076 | /** |
@@ -4030,7 +4036,13 @@ private void checkAndSetEnterSetupMode(VirtualMachineTO vmTo, Map<VirtualMachine |
4030 | 4036 | vmTo.setEnterHardwareSetup(enterSetup == null ? false : enterSetup); |
4031 | 4037 | } |
4032 | 4038 |
|
4033 | | - protected VirtualMachineTO prepareVmTO(Long vmId, Long hostId) { |
| 4039 | + /** |
| 4040 | + * This method helps constructing vmSpec for Unmanage operation for Stopped Instance |
| 4041 | + * @param vmId |
| 4042 | + * @param hostId |
| 4043 | + * @return VirtualMachineTO |
| 4044 | + */ |
| 4045 | + protected VirtualMachineTO prepVmSpecForUnmanageCmd(Long vmId, Long hostId) { |
4034 | 4046 | final VMInstanceVO vm = _vmDao.findById(vmId); |
4035 | 4047 | final Account owner = _entityMgr.findById(Account.class, vm.getAccountId()); |
4036 | 4048 | final ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()); |
|
0 commit comments