in cloud-vmware-server/src/main/java/jetbrains/buildServer/clouds/vmware/VmwareCloudImage.java [154:239]
public synchronized VmwareCloudInstance startNewInstance(@NotNull final CloudInstanceUserData cloudInstanceUserData) throws QuotaException{
final VmwareCloudInstance instanceCandidate = getStartableInstanceFast();
instanceCandidate.setStatus(InstanceStatus.SCHEDULED_TO_START);
myAsyncTaskExecutor.submit("Preparing to start new instance...", () -> {
VmwareInstance sourceVm = null;
try {
boolean willClone = true;
VmwareCloudInstance instance = instanceCandidate;
if (myImageDetails.getBehaviour().isUseOriginal()){
willClone = false;
} else {
sourceVm = myApiConnector.getInstanceDetails(myImageDetails.getSourceVmName());
final VmwareSourceState sourceState;
if (instanceCandidate.getSourceState().getSnapshotName() == null) {
String latestSnapshotName;
latestSnapshotName = myApiConnector.getLatestSnapshot(sourceVm.getId(), myImageDetails.getSnapshotName());
if (latestSnapshotName == null){
if (!myImageDetails.useCurrentVersion()) {
updateErrors(new TypedCloudErrorInfo("No such snapshot: " + getSnapshotName()));
LOG.warn("Unable to find snapshot: " + myImageDetails.getSnapshotName() + ". Won't start " + instanceCandidate.getInstanceId());
return;
}
}
sourceState = VmwareSourceState.from(latestSnapshotName, sourceVm.getId());
instanceCandidate.setSourceState(sourceState);
} else {
sourceState = sourceVm.getVmSourceState();
}
if (!instance.isReady()) {
assert sourceState != null;
// need to resolve the real instance
VmwareCloudInstance existingInstanceToStart = getExistingInstanceToStart(sourceState);
if (existingInstanceToStart != null) {
removeInstance(instance.getInstanceId());
instance = existingInstanceToStart;
willClone = false;
} else {
final String newVmName = generateNewVmName();
instance.setName(newVmName);
instance.setInstanceId(newVmName);
instance.setSourceState(sourceState);
instance.setReady(true);
}
}
final int instancesCount = getInstances().size();
LOG.info("Should clone into " + instance.getName() + ": " + willClone + ". Already have instances: " + instancesCount);
if (willClone && myImageDetails.getMaxInstances() < instancesCount) {
LOG.info("Cannot clone - instances limit exceeded. Will try to clean up some old instances");
cleanupOldInstances();
// don't attempt to start so far
removeInstance(instance.getInstanceId());
return;
}
}
if (willClone) {
final VmwareCloudInstance finalInstance = instance;
myAsyncTaskExecutor.executeAsync(
new VmwareTaskWrapper(() -> myApiConnector.cloneAndStartVm(finalInstance), "Clone and start instance " + instance.getName()),
new ImageStatusTaskWrapper(instance) {
@Override
public void onSuccess() {
reconfigureVmTask(finalInstance, cloudInstanceUserData);
}
@Override
public void onError(final Throwable th) {
super.onError(th);
}
});
} else {
startVM(instance, cloudInstanceUserData);
}
} catch (Exception ex) {
ex.printStackTrace();
LOG.warnAndDebugDetails("Unexpected error while trying to start vSphere cloud instance", ex);
}
});
LOG.info(String.format("Instance '%s' is scheduled for start. Total instances count: %d", instanceCandidate.getInstanceId(), getInstances().size()));
return instanceCandidate;
}