add check befor fencing

Resolve the issur : Should not shutting down a server which is already shut.
Thanks for DaanHoogland which provide code.
This commit is contained in:
leo79901 2024-12-04 21:39:15 +08:00 committed by dahn
parent a208db54ea
commit 8feecbc23b

View File

@ -35,6 +35,7 @@ import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement.PowerOperat
import org.apache.cloudstack.outofbandmanagement.OutOfBandManagementService; import org.apache.cloudstack.outofbandmanagement.OutOfBandManagementService;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import com.cloud.host.Status;
import javax.inject.Inject; import javax.inject.Inject;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
@ -86,17 +87,23 @@ public final class KVMHAProvider extends HAAbstractHostProvider implements HAPro
@Override @Override
public boolean fence(Host r) throws HAFenceException { public boolean fence(Host r) throws HAFenceException {
try { try {
if (outOfBandManagementService.isOutOfBandManagementEnabled(r)){ // host exists and is managed OOB
final OutOfBandManagementResponse resp = outOfBandManagementService.executePowerOperation(r, PowerOperation.OFF, null); if (r != null && outOfBandManagementService.isOutOfBandManagementEnabled(r)) {
return resp.getSuccess(); // check host status
if (Status.Down.equals(r.getStatus())) {
logger.info("Host " + r.getName() + " is already down. Returning success.");
return true;
} else {
final OutOfBandManagementResponse resp = outOfBandManagementService.executePowerOperation(r, PowerOperation.OFF, null);
return resp.getSuccess();
}
} else { } else {
LOG.warn("OOBM fence operation failed for this host " + r.getName()); logger.warn("OOBM fence operation failed for this host " + r.getName());
return false; return false;
} }
} catch (Exception e){ } catch (Exception e){
LOG.warn("OOBM service is not configured or enabled for this host " + r.getName() + " error is " + e.getMessage()); logger.warn("OOBM service is not configured or enabled for this host " + r.getName() + " error is " + e.getMessage());
throw new HAFenceException("OBM service is not configured or enabled for this host " + r.getName() , e); throw new HAFenceException("OBM service is not configured or enabled for this host " + r.getName() , e);
} }
} }