Search in sources :

Example 1 with SshException

use of com.cloud.utils.ssh.SshException in project cloudstack by apache.

the class ResourceManagerImpl method connectAndRestartAgentOnHost.

/**
 * True if agent is restarted via SSH. Assumes kvm.ssh.to.agent = true and host status is not Up
 */
protected void connectAndRestartAgentOnHost(HostVO host, String username, String password, String privateKey) {
    final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(host.getPrivateIpAddress(), 22, username, password, privateKey);
    if (connection == null) {
        throw new CloudRuntimeException(String.format("SSH to agent is enabled, but failed to connect to %s via IP address [%s].", host, host.getPrivateIpAddress()));
    }
    try {
        SSHCmdHelper.SSHCmdResult result = SSHCmdHelper.sshExecuteCmdOneShot(connection, "service cloudstack-agent restart");
        if (result.getReturnCode() != 0) {
            throw new CloudRuntimeException(String.format("Could not restart agent on %s due to: %s", host, result.getStdErr()));
        }
        s_logger.debug("cloudstack-agent restart result: " + result.toString());
    } catch (final SshException e) {
        throw new CloudRuntimeException("SSH to agent is enabled, but agent restart failed", e);
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SshException(com.cloud.utils.ssh.SshException) SSHCmdHelper(com.cloud.utils.ssh.SSHCmdHelper)

Example 2 with SshException

use of com.cloud.utils.ssh.SshException in project cloudstack by apache.

the class ResourceManagerImplTest method testConnectAndRestartAgentOnHostCannotRestart.

@Test(expected = CloudRuntimeException.class)
public void testConnectAndRestartAgentOnHostCannotRestart() throws Exception {
    BDDMockito.given(SSHCmdHelper.sshExecuteCmdOneShot(eq(sshConnection), eq("service cloudstack-agent restart"))).willThrow(new SshException("exception"));
    resourceManager.connectAndRestartAgentOnHost(host, hostUsername, hostPassword, hostPrivateKey);
}
Also used : SshException(com.cloud.utils.ssh.SshException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with SshException

use of com.cloud.utils.ssh.SshException in project cosmic by MissionCriticalCloud.

the class ResourceManagerImpl method doCancelMaintenance.

private boolean doCancelMaintenance(final long hostId) {
    final HostVO host;
    host = _hostDao.findById(hostId);
    if (host == null || host.getRemoved() != null) {
        s_logger.warn("Unable to find host " + hostId);
        return true;
    }
    /*
         * TODO: think twice about returning true or throwing out exception, I
         * really prefer to exception that always exposes bugs
         */
    if (host.getResourceState() != ResourceState.PrepareForMaintenance && host.getResourceState() != ResourceState.Maintenance && host.getResourceState() != ResourceState.ErrorInMaintenance) {
        throw new CloudRuntimeException("Cannot perform cancelMaintenance when resource state is " + host.getResourceState() + ", hostId = " + hostId);
    }
    /* TODO: move to listener */
    _haMgr.cancelScheduledMigrations(host);
    boolean vms_migrating = false;
    final List<VMInstanceVO> vms = _haMgr.findTakenMigrationWork();
    for (final VMInstanceVO vm : vms) {
        if (vm.getHostId() != null && vm.getHostId() == hostId) {
            s_logger.warn("Unable to cancel migration because the vm is being migrated: " + vm + ", hostId = " + hostId);
            vms_migrating = true;
        }
    }
    try {
        resourceStateTransitTo(host, ResourceState.Event.AdminCancelMaintenance, _nodeId);
        _agentMgr.pullAgentOutMaintenance(hostId);
        // for kvm, need to log into kvm host, restart cosmic-agent
        if ((host.getHypervisorType() == HypervisorType.KVM && !vms_migrating)) {
            final boolean sshToAgent = Boolean.parseBoolean(_configDao.getValue(Config.KvmSshToAgentEnabled.key()));
            if (!sshToAgent) {
                s_logger.info("Configuration tells us not to SSH into Agents. Please restart the Agent (" + hostId + ")  manually");
                return true;
            }
            _hostDao.loadDetails(host);
            final String password = host.getDetail("password");
            final String username = host.getDetail("username");
            if (password == null || username == null) {
                s_logger.debug("Can't find password/username");
                return false;
            }
            final com.trilead.ssh2.Connection connection = SSHCmdHelper.acquireAuthorizedConnection(host.getPrivateIpAddress(), 22, username, password);
            if (connection == null) {
                s_logger.debug("Failed to connect to host: " + host.getPrivateIpAddress());
                return false;
            }
            try {
                SSHCmdHelper.sshExecuteCmdOneShot(connection, "systemctl restart cosmic-agent");
            } catch (final SshException e) {
                s_logger.info("Tried to restart agent but it failed. Please restart the Agent (" + hostId + ")  manually");
                // No need to send an error.
                return true;
            }
        }
        return true;
    } catch (final NoTransitionException e) {
        s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e);
        return false;
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VMInstanceVO(com.cloud.vm.VMInstanceVO) SshException(com.cloud.utils.ssh.SshException) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO)

Aggregations

SshException (com.cloud.utils.ssh.SshException)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 HostVO (com.cloud.host.HostVO)1 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)1 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)1 SSHCmdHelper (com.cloud.utils.ssh.SSHCmdHelper)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1