logger.info("syncCreateVMs, start to create VMs.");
// update vm info in vc cache, in case snapshot is removed by others
VcVmUtil.updateVm(templateVm.getId());
VmCreateSpec sourceSpec = new VmCreateSpec();
sourceSpec.setVmId(templateVm.getId());
sourceSpec.setVmName(templateVm.getName());
sourceSpec.setTargetHost(templateVm.getHost());
List<VmCreateSpec> specs = new ArrayList<VmCreateSpec>();
Map<String, BaseNode> nodeMap = new HashMap<String, BaseNode>();
for (BaseNode vNode : vNodes) {
// prepare for cloning result
nodeMap.put(vNode.getVmName(), vNode);
vNode.setSuccess(false);
vNode.setFinished(false);
// generate create spec for fast clone
VmCreateSpec spec = new VmCreateSpec();
VmSchema createSchema = getVmSchema(vNode);
spec.setSchema(createSchema);
String defaultPgName = null;
GuestMachineIdSpec machineIdSpec =
new GuestMachineIdSpec(networkAdds,
vNode.fetchPortGroupToIpV4Map(),
vNode.getPrimaryMgtPgName());
logger.info("machine id of vm " + vNode.getVmName() + ":\n"
+ machineIdSpec.toString());
spec.setBootupConfigs(machineIdSpec.toGuestVariable());
// timeout is 10 mintues
StartVmPostPowerOn query =
new StartVmPostPowerOn(vNode.getNics().keySet(),
Constants.VM_POWER_ON_WAITING_SEC);
spec.setPostPowerOn(query);
spec.setPrePowerOn(getPrePowerOnFunc(vNode, reserveRawDisks));
spec.setLinkedClone(false);
spec.setTargetDs(getVcDatastore(vNode));
spec.setTargetFolder(folders.get(vNode.getGroupName()));
spec.setTargetHost(VcResourceUtils.findHost(vNode.getTargetHost()));
spec.setTargetRp(getVcResourcePool(vNode, clusterRpName));
spec.setVmName(vNode.getVmName());
specs.add(spec);
}
BaseProgressCallback callback = new BaseProgressCallback(statusUpdator);
logger.info("ClusteringService, start to clone template.");
AuAssert.check(specs.size() > 0);
VmSchema vmSchema = specs.get(0).getSchema();
VcVmUtil.checkAndCreateSnapshot(vmSchema);
// call clone service to copy templates
List<VmCreateResult<?>> results =
cloneService.createCopies(sourceSpec, cloneConcurrency, specs,
callback);
if (results == null || results.isEmpty()) {
for (VmCreateSpec spec : specs) {
BaseNode node = nodeMap.get(spec.getVmName());
node.setFinished(true);
node.setSuccess(false);
}
return false;
}
boolean success = true;
int total = 0;
for (VmCreateResult<?> result : results) {
VmCreateSpec spec = (VmCreateSpec) result.getSpec();
BaseNode node = nodeMap.get(spec.getVmName());
node.setVmMobId(spec.getVmId());
node.setSuccess(true);
node.setFinished(true);
boolean vmSucc = VcVmUtil.setBaseNodeForVm(node, spec.getVmId());
if (!vmSucc || !result.isSuccess()) {
success = false;
node.setSuccess(false);
if (result.getErrMessage() != null) {
node.setErrMessage(result.getErrTimestamp() + " "