Search in sources :

Example 1 with RollingMaintenanceAnswer

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);
}
Also used : Answer(com.cloud.agent.api.Answer) RollingMaintenanceAnswer(com.cloud.agent.api.RollingMaintenanceAnswer) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) Ternary(com.cloud.utils.Ternary) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) RollingMaintenanceCommand(com.cloud.agent.api.RollingMaintenanceCommand) RollingMaintenanceAnswer(com.cloud.agent.api.RollingMaintenanceAnswer)

Example 2 with RollingMaintenanceAnswer

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);
    }
}
Also used : RollingMaintenanceExecutor(com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceExecutor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) File(java.io.File) RollingMaintenanceAnswer(com.cloud.agent.api.RollingMaintenanceAnswer) RollingMaintenanceAgentExecutor(com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceAgentExecutor)

Aggregations

RollingMaintenanceAnswer (com.cloud.agent.api.RollingMaintenanceAnswer)2 Answer (com.cloud.agent.api.Answer)1 RollingMaintenanceCommand (com.cloud.agent.api.RollingMaintenanceCommand)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 RollingMaintenanceAgentExecutor (com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceAgentExecutor)1 RollingMaintenanceExecutor (com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceExecutor)1 Ternary (com.cloud.utils.Ternary)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 File (java.io.File)1