VcResourcePool rp = VcVmUtil.getTargetRp(clusterName, groupName, node);
String recoverVmName = node.getVmName() + RECOVERY_VM_NAME_POSTFIX;
if (node.getMoId() != null) {
VcVirtualMachine vm = VcCache.getIgnoreMissing(node.getMoId());
// the vm id is null if the vm is removed
if (vm == null) {
throw ClusterHealServiceException.ERROR_STATUS(nodeName,
"Serengeti and VC are inconsistent as vm " + nodeName
+ " is recorded in Seregeti, but not found in VC.");
}
VcVirtualMachine recoverVm = VcVmUtil.findVmInRp(rp, recoverVmName);
if (recoverVm != null) {
try {
VcVmUtil.destroyVm(recoverVm.getId(), false);
} catch (Exception e) {
logger.error("failed to remove obsolete recovery vm for node "
+ nodeName);
throw ClusterHealServiceException
.FAILED_DELETE_VM(recoverVmName);
}
}
return null;
} else {
VcVirtualMachine oldVm = VcVmUtil.findVmInRp(rp, nodeName);
VcVirtualMachine recoverVm = VcVmUtil.findVmInRp(rp, recoverVmName);
if (oldVm != null && recoverVm != null) {
logger.error("vm " + oldVm.getId() + " and recover vm "
+ recoverVm.getId() + " both exist, this is not expected!.");
throw ClusterHealServiceException.ERROR_STATUS(nodeName, "vm "
+ nodeName + " and its recovery vm " + recoverVmName
+ " both exist, remove the one you created manually!");
} else if (oldVm == null && recoverVm == null) {
logger.error("original vm and recover vm for node " + nodeName
+ " both missed, this is not expected!.");
throw ClusterHealServiceException.ERROR_STATUS(nodeName, "vm "
+ nodeName + " and its recovery vm " + recoverVmName
+ " both missed, it's not expected!");
} else if (recoverVm != null) {
logger.info("recover vm " + recoverVm.getId()
+ " exists, rename it to " + nodeName);
VcVmUtil.rename(recoverVm.getId(), nodeName);
return recoverVm;
} else {
logger.info("recovery probably failed at power on last time, simply return vm "
+ oldVm.getId());
return oldVm;