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);
}
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);
}
}
Aggregations