Skip to content

Commit 1a223fd

Browse files
authored
server: Fix VM import DB sequence issue on import failure (#11659)
* Fix VM import DB sequence issue on import failure * Remove ununsed imports * Refactor to avoid duplicating the next ID for VM sequence
1 parent 40dec99 commit 1a223fd

File tree

4 files changed

+80
-70
lines changed

4 files changed

+80
-70
lines changed

api/src/main/java/com/cloud/vm/UserVmService.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,31 @@ UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException
503503

504504
void collectVmNetworkStatistics (UserVm userVm);
505505

506-
UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceName, final String displayName, final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
506+
/**
507+
* Import VM into CloudStack
508+
* @param zone importing zone
509+
* @param host importing host
510+
* @param template template for the imported VM
511+
* @param instanceNameInternal set to null to CloudStack to autogenerate from the next available VM ID on database
512+
* @param displayName display name for the imported VM
513+
* @param owner owner of the imported VM
514+
* @param userData user data for the imported VM
515+
* @param caller caller account
516+
* @param isDisplayVm true to display the imported VM
517+
* @param keyboard keyboard distribution for the imported VM
518+
* @param accountId account ID
519+
* @param userId user ID
520+
* @param serviceOffering service offering for the imported VM
521+
* @param sshPublicKey ssh key for the imported VM
522+
* @param hostName the name for the imported VM
523+
* @param hypervisorType hypervisor type for the imported VM
524+
* @param customParameters details for the imported VM
525+
* @param powerState power state of the imported VM
526+
* @param networkNicMap network to nic mapping
527+
* @return the imported VM
528+
* @throws InsufficientCapacityException in case of errors
529+
*/
530+
UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceNameInternal, final String displayName, final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
507531
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey,
508532
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
509533
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException;

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8989,34 +8989,47 @@ private void destroyVolumeInContext(UserVmVO vm, boolean expunge, VolumeVO volum
89898989
}
89908990
}
89918991

8992+
private String getInternalName(long accountId, long vmId) {
8993+
String instanceSuffix = _configDao.getValue(Config.InstanceName.key());
8994+
if (instanceSuffix == null) {
8995+
instanceSuffix = "DEFAULT";
8996+
}
8997+
return VirtualMachineName.getVmName(vmId, accountId, instanceSuffix);
8998+
}
8999+
89929000
@Override
8993-
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceName, final String displayName,
9001+
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceNameInternal, final String displayName,
89949002
final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
89959003
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKeys,
89969004
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
89979005
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException {
8998-
if (zone == null) {
8999-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9000-
}
9001-
if (host == null && hypervisorType == HypervisorType.VMware) {
9002-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9003-
}
9006+
return Transaction.execute((TransactionCallbackWithException<UserVm, InsufficientCapacityException>) status -> {
9007+
if (zone == null) {
9008+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9009+
}
9010+
if (host == null && hypervisorType == HypervisorType.VMware) {
9011+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9012+
}
90049013

9005-
final long id = _vmDao.getNextInSequence(Long.class, "id");
9014+
final long id = _vmDao.getNextInSequence(Long.class, "id");
9015+
String instanceName = StringUtils.isBlank(instanceNameInternal) ?
9016+
getInternalName(owner.getAccountId(), id) :
9017+
instanceNameInternal;
90069018

9007-
if (hostName != null) {
9008-
// Check is hostName is RFC compliant
9009-
checkNameForRFCCompliance(hostName);
9010-
}
9019+
if (hostName != null) {
9020+
// Check is hostName is RFC compliant
9021+
checkNameForRFCCompliance(hostName);
9022+
}
90119023

9012-
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9013-
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9014-
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9015-
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9016-
null, null, userData, null, null, isDisplayVm, keyboard,
9017-
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9018-
id, instanceName, uuidName, hypervisorType, customParameters,
9019-
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null);
9024+
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9025+
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9026+
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9027+
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9028+
null, null, userData, null, null, isDisplayVm, keyboard,
9029+
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9030+
id, instanceName, uuidName, hypervisorType, customParameters,
9031+
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null);
9032+
});
90209033
}
90219034

90229035
@Override

0 commit comments

Comments
 (0)