Search in sources :

Example 1 with ConnectionRuntimeException

use of com.orion.exception.ConnectionRuntimeException in project orion-ops by lijiahangmax.

the class MachineInfoServiceImpl method openSessionStore.

@Override
public SessionStore openSessionStore(MachineInfoDO machine) {
    Valid.notNull(machine, MessageConst.INVALID_MACHINE);
    // 检查状态
    if (!Const.ENABLE.equals(machine.getMachineStatus())) {
        throw Exceptions.codeArgument(HttpWrapper.HTTP_ERROR_CODE, MessageConst.MACHINE_NOT_ENABLE);
    }
    Exception ex = null;
    String msg = MessageConst.CONNECT_ERROR;
    for (int i = 0, t = MachineConst.CONNECT_RETRY_TIMES; i < t; i++) {
        log.info("远程机器建立连接-尝试连接远程服务器 第{}次尝试 machineId: {}, host: {}", (i + 1), machine.getId(), machine.getMachineHost());
        try {
            return this.connectSessionStore(machine);
        } catch (Exception e) {
            ex = e;
            if (e instanceof ConnectionRuntimeException) {
            // retry
            } else if (e instanceof AuthenticationException) {
                msg = MessageConst.AUTH_EXCEPTION_MESSAGE;
                break;
            } else {
                break;
            }
        }
    }
    ex.printStackTrace();
    throw Exceptions.codeArgument(HttpWrapper.HTTP_ERROR_CODE, "机器 " + machine.getMachineHost() + " " + msg, ex);
}
Also used : ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) AuthenticationException(com.orion.exception.AuthenticationException) AuthenticationException(com.orion.exception.AuthenticationException) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) IOException(java.io.IOException)

Example 2 with ConnectionRuntimeException

use of com.orion.exception.ConnectionRuntimeException in project orion-ops by lijiahangmax.

the class MachineInfoServiceImpl method runRemoteCommand.

/**
 * 执行远程机器命令
 */
private String runRemoteCommand(Long id, String command) {
    SessionStore session = null;
    CommandExecutor executor = null;
    try {
        session = this.openSessionStore(id);
        executor = session.getCommandExecutor(Strings.replaceCRLF(command));
        executor.connect();
        String res = SessionStore.getCommandOutputResultString(executor);
        log.info("执行机器命令-成功 {} {} {}", id, command, res);
        return res;
    } catch (Exception e) {
        log.error("执行机器命令-失败 {} {} {}", id, command, e);
        if (e instanceof IOException) {
            throw Exceptions.ioRuntime(e);
        } else if (e instanceof ConnectionRuntimeException) {
            throw (ConnectionRuntimeException) e;
        } else if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        }
        return null;
    } finally {
        Streams.close(executor);
        Streams.close(session);
    }
}
Also used : SessionStore(com.orion.remote.channel.SessionStore) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) AuthenticationException(com.orion.exception.AuthenticationException) CommandExecutor(com.orion.remote.channel.ssh.CommandExecutor) IOException(java.io.IOException) AuthenticationException(com.orion.exception.AuthenticationException) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) IOException(java.io.IOException)

Aggregations

AuthenticationException (com.orion.exception.AuthenticationException)2 ConnectionRuntimeException (com.orion.exception.ConnectionRuntimeException)2 IOException (java.io.IOException)2 SessionStore (com.orion.remote.channel.SessionStore)1 CommandExecutor (com.orion.remote.channel.ssh.CommandExecutor)1