Search in sources :

Example 1 with ShutdownCommand

use of com.cloud.agent.api.ShutdownCommand in project CloudStack-archive by CloudStack-extras.

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 (s_logger.isDebugEnabled()) {
                    // this is a hack to make sure we do NOT log the ssh keys
                    if ((cmd instanceof ModifySshKeysCommand)) {
                        s_logger.debug("Received the request for command: ModifySshKeysCommand");
                    } else {
                        if (// ensures request is logged only once per method call
                        !requestLogged) {
                            s_logger.debug("Request:" + request.toString());
                            requestLogged = true;
                        }
                    }
                    s_logger.debug("Processing command: " + cmd.toString());
                }
                if (cmd instanceof CronCommand) {
                    final CronCommand watch = (CronCommand) cmd;
                    scheduleWatch(link, request, watch.getInterval() * 1000, watch.getInterval() * 1000);
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof UpgradeCommand) {
                    final UpgradeCommand upgrade = (UpgradeCommand) cmd;
                    answer = upgradeAgent(upgrade.getUpgradeUrl(), upgrade);
                } else if (cmd instanceof ShutdownCommand) {
                    ShutdownCommand shutdown = (ShutdownCommand) cmd;
                    s_logger.debug("Received shutdownCommand, due to: " + shutdown.getReason());
                    cancelTasks();
                    _reconnectAllowed = false;
                    answer = new Answer(cmd, true, null);
                } else if (cmd instanceof MaintainCommand) {
                    s_logger.debug("Received maintainCommand");
                    cancelTasks();
                    _reconnectAllowed = false;
                    answer = new MaintainAnswer((MaintainCommand) cmd);
                } else if (cmd instanceof AgentControlCommand) {
                    answer = null;
                    synchronized (_controlListeners) {
                        for (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 {
                    _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()) {
            s_logger.debug(response != null ? response.toString() : "response is null");
        }
        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) UpgradeCommand(com.cloud.agent.api.UpgradeCommand) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) CronCommand(com.cloud.agent.api.CronCommand) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) UpgradeAnswer(com.cloud.agent.api.UpgradeAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) MaintainCommand(com.cloud.agent.api.MaintainCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Command(com.cloud.agent.api.Command) PingCommand(com.cloud.agent.api.PingCommand) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) UpgradeCommand(com.cloud.agent.api.UpgradeCommand) CronCommand(com.cloud.agent.api.CronCommand) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) PrintWriter(java.io.PrintWriter)

Example 2 with ShutdownCommand

use of com.cloud.agent.api.ShutdownCommand in project cloudstack by apache.

the class Agent method stop.

public void stop(final String reason, final String detail) {
    s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
    if (_connection != null) {
        final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
        try {
            if (_link != null) {
                final Request req = new Request(_id != null ? _id : -1, -1, cmd, false);
                _link.send(req.toBytes());
            }
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send: " + cmd.toString());
        } catch (final Exception e) {
            s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
        }
        s_logger.debug("Sending shutdown to management server");
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            s_logger.debug("Who the heck interrupted me here?");
        }
        _connection.stop();
        _connection = null;
    }
    if (_resource != null) {
        _resource.stop();
        _resource = null;
    }
    _ugentTaskPool.shutdownNow();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Request(com.cloud.agent.transport.Request) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) AgentControlChannelException(com.cloud.exception.AgentControlChannelException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NioConnectionException(com.cloud.utils.exception.NioConnectionException)

Example 3 with ShutdownCommand

use of com.cloud.agent.api.ShutdownCommand in project CloudStack-archive by CloudStack-extras.

the class Agent method stop.

public void stop(final String reason, final String detail) {
    s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
    if (_connection != null) {
        final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
        try {
            if (_link != null) {
                Request req = new Request((_id != null ? _id : -1), -1, cmd, false);
                _link.send(req.toBytes());
            }
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send: " + cmd.toString());
        } catch (Exception e) {
            s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
        }
        s_logger.debug("Sending shutdown to management server");
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            s_logger.debug("Who the heck interrupted me here?");
        }
        _connection.stop();
        _connection = null;
    }
    if (_resource != null) {
        _resource.stop();
        _resource = null;
    }
    _ugentTaskPool.shutdownNow();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Request(com.cloud.agent.transport.Request) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AgentControlChannelException(com.cloud.exception.AgentControlChannelException)

Example 4 with ShutdownCommand

use of com.cloud.agent.api.ShutdownCommand 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();
                    _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");
                    cancelTasks();
                    _reconnectAllowed = false;
                    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 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) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) Response(com.cloud.agent.transport.Response) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) StringWriter(java.io.StringWriter) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) PingCommand(com.cloud.agent.api.PingCommand) MaintainCommand(com.cloud.agent.api.MaintainCommand) 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) PrintWriter(java.io.PrintWriter)

Example 5 with ShutdownCommand

use of com.cloud.agent.api.ShutdownCommand in project cloudstack by apache.

the class LibvirtServerDiscoverer method deleteHost.

@Override
public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage) throws UnableDeleteHostException {
    if (host.getType() != Host.Type.Routing || (host.getHypervisorType() != HypervisorType.KVM && host.getHypervisorType() != HypervisorType.LXC)) {
        return null;
    }
    _resourceMgr.deleteRoutingHost(host, isForced, isForceDeleteStorage);
    try {
        ShutdownCommand cmd = new ShutdownCommand(ShutdownCommand.DeleteHost, null);
        _agentMgr.send(host.getId(), cmd);
    } catch (AgentUnavailableException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    } catch (OperationTimedoutException e) {
        s_logger.warn("Sending ShutdownCommand failed: ", e);
    }
    return new DeleteHostAnswer(true);
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ShutdownCommand(com.cloud.agent.api.ShutdownCommand)

Aggregations

ShutdownCommand (com.cloud.agent.api.ShutdownCommand)5 ClosedChannelException (java.nio.channels.ClosedChannelException)4 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)2 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)2 Answer (com.cloud.agent.api.Answer)2 Command (com.cloud.agent.api.Command)2 CronCommand (com.cloud.agent.api.CronCommand)2 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)2 MaintainCommand (com.cloud.agent.api.MaintainCommand)2 PingCommand (com.cloud.agent.api.PingCommand)2 StartupAnswer (com.cloud.agent.api.StartupAnswer)2 StartupCommand (com.cloud.agent.api.StartupCommand)2 Request (com.cloud.agent.transport.Request)2 Response (com.cloud.agent.transport.Response)2 AgentControlChannelException (com.cloud.exception.AgentControlChannelException)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 UnknownHostException (java.net.UnknownHostException)2