Search in sources :

Example 1 with SetupMSListCommand

use of org.apache.cloudstack.agent.lb.SetupMSListCommand in project cloudstack by apache.

the class Agent method processRequest.

protected void processRequest(final Request request, final Link link) {
    boolean requestLogged = false;
    Response response = null;
    try {
        final Command[] cmds = request.getCommands();
        final Answer[] answers = new Answer[cmds.length];
        for (int i = 0; i < cmds.length; i++) {
            final Command cmd = cmds[i];
            Answer answer;
            try {
                if (cmd.getContextParam("logid") != null) {
                    MDC.put("logcontextid", cmd.getContextParam("logid"));
                }
                if (s_logger.isDebugEnabled()) {
                    if (// ensures request is logged only once per method call
                    !requestLogged) {
                        final String requestMsg = request.toString();
                        if (requestMsg != null) {
                            s_logger.debug("Request:" + requestMsg);
                        }
                        requestLogged = true;
                    }
                    s_logger.debug("Processing command: " + cmd.toString());
                }
                if (cmd instanceof CronCommand) {
                    final CronCommand watch = (CronCommand) cmd;
                    scheduleWatch(link, request, (long) watch.getInterval() * 1000, watch.getInterval() * 1000);
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof ShutdownCommand) {
                    final ShutdownCommand shutdown = (ShutdownCommand) cmd;
                    s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                    cancelTasks();
                    if (shutdown.isRemoveHost()) {
                        cleanupAgentZoneProperties();
                    }
                    _reconnectAllowed = false;
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof ReadyCommand && ((ReadyCommand) cmd).getDetails() != null) {
                    s_logger.debug("Not ready to connect to mgt server: " + ((ReadyCommand) cmd).getDetails());
                    System.exit(1);
                    return;
                } else if (cmd instanceof MaintainCommand) {
                    s_logger.debug("Received maintainCommand, do not cancel current tasks");
                    answer = new MaintainAnswer((MaintainCommand) cmd);
                } else if (cmd instanceof AgentControlCommand) {
                    answer = null;
                    synchronized (_controlListeners) {
                        for (final IAgentControlListener listener : _controlListeners) {
                            answer = listener.processControlRequest(request, (AgentControlCommand) cmd);
                            if (answer != null) {
                                break;
                            }
                        }
                    }
                    if (answer == null) {
                        s_logger.warn("No handler found to process cmd: " + cmd.toString());
                        answer = new AgentControlAnswer(cmd);
                    }
                } else if (cmd instanceof SetupKeyStoreCommand && ((SetupKeyStoreCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentKeystore((SetupKeyStoreCommand) cmd);
                } else if (cmd instanceof SetupCertificateCommand && ((SetupCertificateCommand) cmd).isHandleByAgent()) {
                    answer = setupAgentCertificate((SetupCertificateCommand) cmd);
                    if (Host.Type.Routing.equals(_resource.getType())) {
                        scheduleServicesRestartTask();
                    }
                } else if (cmd instanceof SetupMSListCommand) {
                    answer = setupManagementServerList((SetupMSListCommand) cmd);
                } else {
                    if (cmd instanceof ReadyCommand) {
                        processReadyCommand(cmd);
                    }
                    _inProgress.incrementAndGet();
                    try {
                        answer = _resource.executeRequest(cmd);
                    } finally {
                        _inProgress.decrementAndGet();
                    }
                    if (answer == null) {
                        s_logger.debug("Response: unsupported command" + cmd.toString());
                        answer = Answer.createUnsupportedCommandAnswer(cmd);
                    }
                }
            } catch (final Throwable th) {
                s_logger.warn("Caught: ", th);
                final StringWriter writer = new StringWriter();
                th.printStackTrace(new PrintWriter(writer));
                answer = new Answer(cmd, false, writer.toString());
            }
            answers[i] = answer;
            if (!answer.getResult() && request.stopOnError()) {
                for (i++; i < cmds.length; i++) {
                    answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
                }
                break;
            }
        }
        response = new Response(request, answers);
    } finally {
        if (s_logger.isDebugEnabled()) {
            final String responseMsg = response.toString();
            if (responseMsg != null) {
                s_logger.debug(response.toString());
            }
        }
        if (response != null) {
            try {
                link.send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    }
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeystoreAnswer(org.apache.cloudstack.ca.SetupKeystoreAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) SetupMSListAnswer(org.apache.cloudstack.agent.lb.SetupMSListAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) SetupCertificateAnswer(org.apache.cloudstack.ca.SetupCertificateAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) SetupCertificateCommand(org.apache.cloudstack.ca.SetupCertificateCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PingCommand(com.cloud.agent.api.PingCommand) PostCertificateRenewalCommand(org.apache.cloudstack.ca.PostCertificateRenewalCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) SetupMSListCommand(org.apache.cloudstack.agent.lb.SetupMSListCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) CronCommand(com.cloud.agent.api.CronCommand) ReadyCommand(com.cloud.agent.api.ReadyCommand) SetupKeyStoreCommand(org.apache.cloudstack.ca.SetupKeyStoreCommand) PrintWriter(java.io.PrintWriter)

Aggregations

AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)1 Answer (com.cloud.agent.api.Answer)1 Command (com.cloud.agent.api.Command)1 CronCommand (com.cloud.agent.api.CronCommand)1 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)1 MaintainCommand (com.cloud.agent.api.MaintainCommand)1 PingCommand (com.cloud.agent.api.PingCommand)1 ReadyCommand (com.cloud.agent.api.ReadyCommand)1 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)1 StartupAnswer (com.cloud.agent.api.StartupAnswer)1 StartupCommand (com.cloud.agent.api.StartupCommand)1 Response (com.cloud.agent.transport.Response)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 SetupMSListAnswer (org.apache.cloudstack.agent.lb.SetupMSListAnswer)1 SetupMSListCommand (org.apache.cloudstack.agent.lb.SetupMSListCommand)1 PostCertificateRenewalCommand (org.apache.cloudstack.ca.PostCertificateRenewalCommand)1 SetupCertificateAnswer (org.apache.cloudstack.ca.SetupCertificateAnswer)1