use of com.cloud.agent.api.RollingMaintenanceAnswer in project cloudstack by apache.
the class RollingMaintenanceManagerImpl method sendRollingMaintenanceCommandToHost.
/**
* Send rolling maintenance command to a host to perform a certain stage specified in cmd
* @return tuple: (SUCCESS, DETAILS, AVOID_MAINTENANCE) where:
* - SUCCESS: True if stage is successfull
* - DETAILS: Information retrieved by the host after executing the stage
* - AVOID_MAINTENANCE: True if maintenance stage must be avoided
*/
private Ternary<Boolean, String, Boolean> sendRollingMaintenanceCommandToHost(Host host, Stage stage, int timeout, String payload) throws InterruptedException {
boolean completed = false;
Answer answer = null;
long timeSpent = 0L;
long pingInterval = KvmRollingMaintenancePingInterval.value() * 1000L;
boolean avoidMaintenance = false;
RollingMaintenanceCommand cmd = new RollingMaintenanceCommand(stage.toString());
cmd.setWait(timeout);
cmd.setPayload(payload);
while (!completed && timeSpent < timeout * 1000L) {
try {
answer = agentManager.send(host.getId(), cmd);
} catch (AgentUnavailableException | OperationTimedoutException e) {
// Agent may be restarted on the scripts - continue polling until it is up
String msg = String.format("Cannot send command to %s, waiting %sms - %s", host, pingInterval, e.getMessage());
s_logger.warn(msg, e);
cmd.setStarted(true);
Thread.sleep(pingInterval);
timeSpent += pingInterval;
continue;
}
cmd.setStarted(true);
RollingMaintenanceAnswer rollingMaintenanceAnswer = (RollingMaintenanceAnswer) answer;
completed = rollingMaintenanceAnswer.isFinished();
if (!completed) {
Thread.sleep(pingInterval);
timeSpent += pingInterval;
} else {
avoidMaintenance = rollingMaintenanceAnswer.isAvoidMaintenance();
}
}
if (timeSpent >= timeout * 1000L) {
return new Ternary<>(false, "Timeout exceeded for rolling maintenance on host " + host.getUuid() + " and stage " + stage.toString(), avoidMaintenance);
}
return new Ternary<>(answer.getResult(), answer.getDetails(), avoidMaintenance);
}
use of com.cloud.agent.api.RollingMaintenanceAnswer in project cloudstack by apache.
the class LibvirtRollingMaintenanceCommandWrapper method execute.
@Override
public RollingMaintenanceAnswer execute(RollingMaintenanceCommand command, LibvirtComputingResource resource) {
RollingMaintenanceExecutor executor = resource.getRollingMaintenanceExecutor();
String stage = command.isCheckMaintenanceScript() ? RollingMaintenanceManager.Stage.Maintenance.toString() : command.getStage();
int timeout = command.getWait();
String payload = command.getPayload();
try {
File scriptFile = executor.getStageScriptFile(stage);
if (command.isCheckMaintenanceScript()) {
return new RollingMaintenanceAnswer(command, scriptFile != null);
} else if (scriptFile == null) {
s_logger.info("No script file defined for stage " + stage + ". Skipping stage...");
return new RollingMaintenanceAnswer(command, true, "Skipped stage " + stage, true);
}
if (command.isStarted() && executor instanceof RollingMaintenanceAgentExecutor) {
String msg = "Stage has been started previously and the agent restarted, setting stage as finished";
s_logger.info(msg);
return new RollingMaintenanceAnswer(command, true, msg, true);
}
s_logger.info("Processing stage " + stage);
if (!command.isStarted()) {
executor.startStageExecution(stage, scriptFile, timeout, payload);
}
if (executor.isStageRunning(stage, scriptFile, payload)) {
return new RollingMaintenanceAnswer(command, true, "Stage " + stage + " still running", false);
}
boolean success = executor.getStageExecutionSuccess(stage, scriptFile);
String output = executor.getStageExecutionOutput(stage, scriptFile);
RollingMaintenanceAnswer answer = new RollingMaintenanceAnswer(command, success, output, true);
if (executor.getStageAvoidMaintenance(stage, scriptFile)) {
s_logger.info("Avoid maintenance flag added to the answer for the stage " + stage);
answer.setAvoidMaintenance(true);
}
s_logger.info("Finished processing stage " + stage);
return answer;
} catch (CloudRuntimeException e) {
return new RollingMaintenanceAnswer(command, false, e.getMessage(), false);
}
}
Aggregations