Search in sources :

Example 1 with ModifySshKeysCommand

use of com.cloud.agent.api.ModifySshKeysCommand 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 ModifySshKeysCommand

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

the class SshKeysDistriMonitor method processConnect.

@Override
public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
    if (cmd instanceof StartupRoutingCommand) {
        if (((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.KVM || ((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.XenServer || ((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.LXC) {
            /*TODO: Get the private/public keys here*/
            String pubKey = _configDao.getValue("ssh.publickey");
            String prvKey = _configDao.getValue("ssh.privatekey");
            try {
                ModifySshKeysCommand cmds = new ModifySshKeysCommand(pubKey, prvKey);
                Commands c = new Commands(cmds);
                _agentMgr.send(host.getId(), c, this);
            } catch (AgentUnavailableException e) {
                s_logger.debug("Failed to send keys to agent: " + host.getId());
            }
        }
    }
}
Also used : AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) Commands(com.cloud.agent.manager.Commands) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 3 with ModifySshKeysCommand

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

the class LibvirtComputingResourceTest method testModifySshKeysCommand.

@Test
public void testModifySshKeysCommand() {
    final ModifySshKeysCommand command = new ModifySshKeysCommand("", "");
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtUtilitiesHelper.retrieveSshKeysPath()).thenReturn("/path/keys");
    when(libvirtUtilitiesHelper.retrieveSshPubKeyPath()).thenReturn("/path/pub/keys");
    when(libvirtUtilitiesHelper.retrieveSshPrvKeyPath()).thenReturn("/path/pvt/keys");
    when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getTimeout();
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with ModifySshKeysCommand

use of com.cloud.agent.api.ModifySshKeysCommand in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testModifySshKeysCommand.

@Test
public void testModifySshKeysCommand() {
    final ModifySshKeysCommand command = new ModifySshKeysCommand("", "");
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtUtilitiesHelper.retrieveSshKeysPath()).thenReturn("/path/keys");
    when(libvirtUtilitiesHelper.retrieveSshPubKeyPath()).thenReturn("/path/pub/keys");
    when(libvirtUtilitiesHelper.retrieveSshPrvKeyPath()).thenReturn("/path/pvt/keys");
    when(libvirtComputingResource.getScriptsTimeout()).thenReturn(0);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getScriptsTimeout();
}
Also used : Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 5 with ModifySshKeysCommand

use of com.cloud.agent.api.ModifySshKeysCommand in project cosmic by MissionCriticalCloud.

the class SshKeysDistriMonitor method processConnect.

@Override
public void processConnect(final Host host, final StartupCommand cmd, final boolean forRebalance) throws ConnectionException {
    if (cmd instanceof StartupRoutingCommand) {
        if (((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.KVM || ((StartupRoutingCommand) cmd).getHypervisorType() == HypervisorType.XenServer) {
            /*TODO: Get the private/public keys here*/
            final String pubKey = _configDao.getValue("ssh.publickey");
            final String prvKey = _configDao.getValue("ssh.privatekey");
            try {
                final ModifySshKeysCommand cmds = new ModifySshKeysCommand(pubKey, prvKey);
                final Commands c = new Commands(cmds);
                _agentMgr.send(host.getId(), c, this);
            } catch (final AgentUnavailableException e) {
                s_logger.debug("Failed to send keys to agent: " + host.getId());
            }
        }
    }
}
Also used : AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ModifySshKeysCommand(com.cloud.agent.api.ModifySshKeysCommand) Commands(com.cloud.agent.manager.Commands) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Aggregations

ModifySshKeysCommand (com.cloud.agent.api.ModifySshKeysCommand)8 Answer (com.cloud.agent.api.Answer)6 Test (org.junit.Test)4 RebootAnswer (com.cloud.agent.api.RebootAnswer)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)2 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)2 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)2 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)2 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)1 AttachIsoAnswer (com.cloud.agent.api.AttachIsoAnswer)1 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)1 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)1 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)1 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)1 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)1 CheckNetworkAnswer (com.cloud.agent.api.CheckNetworkAnswer)1 CheckNetworkCommand (com.cloud.agent.api.CheckNetworkCommand)1 CheckOnHostAnswer (com.cloud.agent.api.CheckOnHostAnswer)1