Search in sources :

Example 1 with SessionStore

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;
}
Also used : SessionStore(com.orion.remote.channel.SessionStore) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor)

Example 2 with SessionStore

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;
    }
}
Also used : SessionStore(com.orion.remote.channel.SessionStore) MachineProxyDO(com.orion.ops.entity.domain.MachineProxyDO) ProxyType(com.orion.ops.consts.machine.ProxyType) AuthenticationException(com.orion.exception.AuthenticationException) ConnectionRuntimeException(com.orion.exception.ConnectionRuntimeException) IOException(java.io.IOException)

Example 3 with SessionStore

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);
}
Also used : SessionStore(com.orion.remote.channel.SessionStore)

Example 4 with 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);
}
Also used : SessionStore(com.orion.remote.channel.SessionStore)

Example 5 with 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);
    }
}
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

SessionStore (com.orion.remote.channel.SessionStore)6 IOException (java.io.IOException)3 AuthenticationException (com.orion.exception.AuthenticationException)2 ConnectionRuntimeException (com.orion.exception.ConnectionRuntimeException)2 ProxyType (com.orion.ops.consts.machine.ProxyType)1 MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)1 MachineProxyDO (com.orion.ops.entity.domain.MachineProxyDO)1 TerminalConnectDTO (com.orion.ops.entity.dto.TerminalConnectDTO)1 UserDTO (com.orion.ops.entity.dto.UserDTO)1 SftpExecutor (com.orion.remote.channel.sftp.SftpExecutor)1 CommandExecutor (com.orion.remote.channel.ssh.CommandExecutor)1