Skip to content

Commit 9eecf3f

Browse files
nvazquezdhslove
authored andcommitted
server: Fix VM import DB sequence issue on import failure (apache#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 e9c7776 commit 9eecf3f

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
@@ -529,7 +529,31 @@ UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException
529529

530530
void collectVmNetworkStatistics (UserVm userVm);
531531

532-
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,
532+
/**
533+
* Import VM into CloudStack
534+
* @param zone importing zone
535+
* @param host importing host
536+
* @param template template for the imported VM
537+
* @param instanceNameInternal set to null to CloudStack to autogenerate from the next available VM ID on database
538+
* @param displayName display name for the imported VM
539+
* @param owner owner of the imported VM
540+
* @param userData user data for the imported VM
541+
* @param caller caller account
542+
* @param isDisplayVm true to display the imported VM
543+
* @param keyboard keyboard distribution for the imported VM
544+
* @param accountId account ID
545+
* @param userId user ID
546+
* @param serviceOffering service offering for the imported VM
547+
* @param sshPublicKey ssh key for the imported VM
548+
* @param hostName the name for the imported VM
549+
* @param hypervisorType hypervisor type for the imported VM
550+
* @param customParameters details for the imported VM
551+
* @param powerState power state of the imported VM
552+
* @param networkNicMap network to nic mapping
553+
* @return the imported VM
554+
* @throws InsufficientCapacityException in case of errors
555+
*/
556+
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,
533557
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey,
534558
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
535559
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
@@ -9561,34 +9561,47 @@ private void destroyVolumeInContext(UserVmVO vm, boolean expunge, VolumeVO volum
95619561
}
95629562
}
95639563

9564+
private String getInternalName(long accountId, long vmId) {
9565+
String instanceSuffix = _configDao.getValue(Config.InstanceName.key());
9566+
if (instanceSuffix == null) {
9567+
instanceSuffix = "DEFAULT";
9568+
}
9569+
return VirtualMachineName.getVmName(vmId, accountId, instanceSuffix);
9570+
}
9571+
95649572
@Override
9565-
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceName, final String displayName,
9573+
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceNameInternal, final String displayName,
95669574
final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
95679575
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKeys,
95689576
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
95699577
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException {
9570-
if (zone == null) {
9571-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9572-
}
9573-
if (host == null && hypervisorType == HypervisorType.VMware) {
9574-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9575-
}
9578+
return Transaction.execute((TransactionCallbackWithException<UserVm, InsufficientCapacityException>) status -> {
9579+
if (zone == null) {
9580+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9581+
}
9582+
if (host == null && hypervisorType == HypervisorType.VMware) {
9583+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9584+
}
95769585

9577-
final long id = _vmDao.getNextInSequence(Long.class, "id");
9586+
final long id = _vmDao.getNextInSequence(Long.class, "id");
9587+
String instanceName = StringUtils.isBlank(instanceNameInternal) ?
9588+
getInternalName(owner.getAccountId(), id) :
9589+
instanceNameInternal;
95789590

9579-
if (hostName != null) {
9580-
// Check is hostName is RFC compliant
9581-
checkNameForRFCCompliance(hostName);
9582-
}
9591+
if (hostName != null) {
9592+
// Check is hostName is RFC compliant
9593+
checkNameForRFCCompliance(hostName);
9594+
}
95839595

9584-
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9585-
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9586-
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9587-
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9588-
null, null, userData, null, null, isDisplayVm, keyboard,
9589-
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9590-
id, instanceName, uuidName, hypervisorType, customParameters,
9591-
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null);
9596+
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9597+
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9598+
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9599+
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9600+
null, null, userData, null, null, isDisplayVm, keyboard,
9601+
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9602+
id, instanceName, uuidName, hypervisorType, customParameters,
9603+
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null);
9604+
});
95929605
}
95939606

95949607
@Override

0 commit comments

Comments
 (0)