Search in sources :

Example 26 with Command

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

the class XenServerGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(VirtualMachine vm) {
    List<Command> commands = new ArrayList<Command>();
    List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    // will simply lead to the SR that supports the root volume being removed
    if (volumes != null) {
        for (VolumeVO volume : volumes) {
            StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged()) {
                DataTO volTO = _volFactory.getVolume(volume.getId()).getTO();
                DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
                cmd.setManaged(true);
                cmd.setStorageHost(storagePool.getHostAddress());
                cmd.setStoragePort(storagePool.getPort());
                cmd.set_iScsiName(volume.get_iScsiName());
                commands.add(cmd);
            }
        }
    }
    return commands;
}
Also used : DataTO(com.cloud.agent.api.to.DataTO) VolumeVO(com.cloud.storage.VolumeVO) StorageSubSystemCommand(org.apache.cloudstack.storage.command.StorageSubSystemCommand) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Command(com.cloud.agent.api.Command) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ArrayList(java.util.ArrayList) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 27 with Command

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

the class Agent method processOtherTask.

public void processOtherTask(final Task task) {
    final Object obj = task.get();
    if (obj instanceof Response) {
        if (System.currentTimeMillis() - _lastPingResponseTime > _pingInterval * _shell.getPingRetries()) {
            s_logger.error("Ping Interval has gone past " + _pingInterval * _shell.getPingRetries() + ". Won't reconnect to mgt server, as connection is still alive");
            return;
        }
        final PingCommand ping = _resource.getCurrentStatus(getId());
        final Request request = new Request(_id, -1, ping, false);
        request.setSequence(getNextSequence());
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Sending ping: " + request.toString());
        }
        try {
            task.getLink().send(request.toBytes());
            //if i can send pingcommand out, means the link is ok
            setLastPingResponseTime();
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send request: " + request.toString());
        }
    } else if (obj instanceof Request) {
        final Request req = (Request) obj;
        final Command command = req.getCommand();
        if (command.getContextParam("logid") != null) {
            MDC.put("logcontextid", command.getContextParam("logid"));
        }
        Answer answer = null;
        _inProgress.incrementAndGet();
        try {
            answer = _resource.executeRequest(command);
        } finally {
            _inProgress.decrementAndGet();
        }
        if (answer != null) {
            final Response response = new Response(req, answer);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Watch Sent: " + response.toString());
            }
            try {
                task.getLink().send(response.toBytes());
            } catch (final ClosedChannelException e) {
                s_logger.warn("Unable to send response: " + response.toString());
            }
        }
    } else {
        s_logger.warn("Ignoring an unknown task");
    }
}
Also used : Response(com.cloud.agent.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) StartupAnswer(com.cloud.agent.api.StartupAnswer) 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) Request(com.cloud.agent.transport.Request) PingCommand(com.cloud.agent.api.PingCommand)

Example 28 with Command

use of com.cloud.agent.api.Command 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 29 with Command

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

the class Agent method sendStartup.

public void sendStartup(final Link link) {
    final StartupCommand[] startup = _resource.initialize();
    if (startup != null) {
        final Command[] commands = new Command[startup.length];
        for (int i = 0; i < startup.length; i++) {
            setupStartupCommand(startup[i]);
            commands[i] = startup[i];
        }
        final Request request = new Request(_id != null ? _id : -1, -1, commands, false, false);
        request.setSequence(getNextSequence());
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Sending Startup: " + request.toString());
        }
        lockStartupTask(link);
        try {
            link.send(request.toBytes());
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send reques: " + request.toString());
        }
    }
}
Also used : StartupCommand(com.cloud.agent.api.StartupCommand) ClosedChannelException(java.nio.channels.ClosedChannelException) 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) Request(com.cloud.agent.transport.Request)

Example 30 with Command

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

the class GsonHelper method setDefaultGsonConfig.

static Gson setDefaultGsonConfig(GsonBuilder builder) {
    builder.setVersion(1.5);
    InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<DataStoreTO>();
    builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor);
    InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<DataTO>();
    builder.registerTypeAdapter(DataTO.class, dtAdaptor);
    ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<Command>();
    builder.registerTypeAdapter(Command[].class, cmdAdaptor);
    ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<Answer>();
    builder.registerTypeAdapter(Answer[].class, ansAdaptor);
    builder.registerTypeAdapter(new TypeToken<List<PortConfig>>() {
    }.getType(), new PortConfigListTypeAdaptor());
    builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {
    }.getType(), new NwGroupsCommandTypeAdaptor());
    Gson gson = builder.create();
    dsAdaptor.initGson(gson);
    dtAdaptor.initGson(gson);
    cmdAdaptor.initGson(gson);
    ansAdaptor.initGson(gson);
    return gson;
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) InterfaceTypeAdaptor(com.cloud.agent.transport.InterfaceTypeAdaptor) Gson(com.google.gson.Gson) NwGroupsCommandTypeAdaptor(com.cloud.agent.transport.Request.NwGroupsCommandTypeAdaptor) Answer(com.cloud.agent.api.Answer) DataTO(com.cloud.agent.api.to.DataTO) PortConfigListTypeAdaptor(com.cloud.agent.transport.Request.PortConfigListTypeAdaptor) Command(com.cloud.agent.api.Command) TypeToken(com.google.gson.reflect.TypeToken) ArrayTypeAdaptor(com.cloud.agent.transport.ArrayTypeAdaptor) PortConfig(com.cloud.agent.api.SecStorageFirewallCfgCommand.PortConfig)

Aggregations

Command (com.cloud.agent.api.Command)33 Answer (com.cloud.agent.api.Answer)17 StartupCommand (com.cloud.agent.api.StartupCommand)17 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)13 PingCommand (com.cloud.agent.api.PingCommand)13 StartupRoutingCommand (com.cloud.agent.api.StartupRoutingCommand)11 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)10 PingRoutingCommand (com.cloud.agent.api.PingRoutingCommand)8 ReadyCommand (com.cloud.agent.api.ReadyCommand)8 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)8 MaintainCommand (com.cloud.agent.api.MaintainCommand)7 StartupAnswer (com.cloud.agent.api.StartupAnswer)7 StartupStorageCommand (com.cloud.agent.api.StartupStorageCommand)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)6 CronCommand (com.cloud.agent.api.CronCommand)6 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)5 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)5 StartCommand (com.cloud.agent.api.StartCommand)5 Request (com.cloud.agent.transport.Request)5