use of com.orion.remote.channel.SessionStore in project orion-ops by lijiahangmax.
the class SftpBasicExecutorHolder method getBasicExecutor.
/**
* 获取 sftp 基本操作 executor
*
* @param machineId machineId
* @return SftpExecutor
*/
public SftpExecutor getBasicExecutor(Long machineId) {
SftpExecutor executor = basicExecutorHolder.get(machineId);
if (executor != null) {
if (!executor.isConnected()) {
try {
executor.connect();
} catch (Exception e) {
// 无法连接则重新创建实例
executor = null;
}
}
}
// 如果没有重新建立连接
if (executor == null) {
// 获取charset
String charset = machineEnvService.getSftpCharset(machineId);
// 打开sftp连接
SessionStore sessionStore = machineInfoService.openSessionStore(machineId);
executor = sessionStore.getSftpExecutor(charset);
executor.connect();
basicExecutorHolder.put(machineId, executor);
}
executorUsing.put(machineId, System.currentTimeMillis());
return executor;
}
use of com.orion.remote.channel.SessionStore in project orion-ops by lijiahangmax.
the class MachineInfoServiceImpl method connectSessionStore.
/**
* 打开sessionStore
*
* @param machine machine
* @return SessionStore
*/
private SessionStore connectSessionStore(MachineInfoDO machine) {
Valid.notNull(machine, MessageConst.INVALID_MACHINE);
Long proxyId = machine.getProxyId();
SessionStore session;
try {
session = SessionHolder.getSession(machine.getMachineHost(), machine.getSshPort(), machine.getUsername());
String password = machine.getPassword();
if (Strings.isNotBlank(password)) {
session.setPassword(ValueMix.decrypt(password));
}
MachineProxyDO proxy = null;
if (proxyId != null) {
proxy = machineProxyDAO.selectById(proxyId);
}
if (proxy != null) {
ProxyType proxyType = ProxyType.of(proxy.getProxyType());
String proxyPassword = proxy.getProxyPassword();
if (!Strings.isBlank(proxyPassword)) {
proxyPassword = ValueMix.decrypt(proxyPassword);
}
if (ProxyType.HTTP.equals(proxyType)) {
session.setHttpProxy(proxy.getProxyHost(), proxy.getProxyPort(), proxy.getProxyUsername(), proxyPassword);
} else if (ProxyType.SOCKET4.equals(proxyType)) {
session.setSocket4Proxy(proxy.getProxyHost(), proxy.getProxyPort(), proxy.getProxyUsername(), proxyPassword);
} else if (ProxyType.SOCKET5.equals(proxyType)) {
session.setSocket5Proxy(proxy.getProxyHost(), proxy.getProxyPort(), proxy.getProxyUsername(), proxyPassword);
}
session.setHttpProxy(proxy.getProxyHost(), proxy.getProxyPort(), proxy.getProxyUsername(), proxyPassword);
}
session.connect(MachineConst.CONNECT_TIMEOUT);
log.info("远程机器建立连接-成功 {}@{}:{}", machine.getUsername(), machine.getMachineHost(), machine.getSshPort());
return session;
} catch (Exception e) {
log.error("远程机器建立连接-失败 {}@{}:{} {}", machine.getUsername(), machine.getMachineHost(), machine.getSshPort(), e);
throw e;
}
}
use of com.orion.remote.channel.SessionStore in project orion-ops by lijiahangmax.
the class BuildMachineProcessor method openMachineSession.
@Override
protected void openMachineSession() {
boolean hasCommand = store.getActions().values().stream().map(ApplicationActionLogDO::getActionType).anyMatch(ActionType.BUILD_COMMAND.getType()::equals);
if (!hasCommand) {
return;
}
// 打开session
SessionStore sessionStore = machineInfoService.openSessionStore(Const.HOST_MACHINE_ID);
store.setMachineId(Const.HOST_MACHINE_ID);
store.setSessionStore(sessionStore);
}
use of com.orion.remote.channel.SessionStore in project orion-ops by lijiahangmax.
the class ReleaseMachineProcessor method openMachineSession.
@Override
protected void openMachineSession() {
// 打开session
SessionStore sessionStore = machineInfoService.openSessionStore(machine.getMachineId());
store.setSessionStore(sessionStore);
}
use of com.orion.remote.channel.SessionStore 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