Search in sources :

Example 6 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class MachineTerminalServiceImpl method getAccessConfig.

@Override
public TerminalAccessVO getAccessConfig(Long machineId) {
    // 获取机器信息
    MachineInfoDO machine = machineInfoService.selectById(machineId);
    Valid.notNull(machine, MessageConst.INVALID_MACHINE);
    if (!Const.ENABLE.equals(machine.getMachineStatus())) {
        throw Exceptions.codeArgument(HttpWrapper.HTTP_ERROR_CODE, MessageConst.MACHINE_NOT_ENABLE);
    }
    // 设置accessToken
    Long userId = Currents.getUserId();
    String token = ValueMix.base62ecbEnc(userId + "_" + System.currentTimeMillis(), TerminalConst.TERMINAL);
    // 获取终端配置
    MachineTerminalVO config = this.getMachineConfig(machineId);
    // 设置数据
    TerminalAccessVO access = new TerminalAccessVO();
    access.setId(config.getId());
    access.setAccessToken(token);
    access.setHost(machine.getMachineHost());
    access.setPort(machine.getSshPort());
    access.setMachineName(machine.getMachineName());
    access.setMachineId(machineId);
    access.setUsername(machine.getUsername());
    access.setTerminalType(config.getTerminalType());
    access.setBackgroundColor(config.getBackgroundColor());
    access.setFontSize(config.getFontSize());
    access.setFontFamily(config.getFontFamily());
    access.setFontColor(config.getFontColor());
    access.setEnableWebLink(config.getEnableWebLink());
    access.setEnableWebGL(config.getEnableWebGL());
    // 设置缓存
    String cacheKey = Strings.format(KeyConst.TERMINAL_ACCESS_TOKEN, token);
    redisTemplate.opsForValue().set(cacheKey, machineId + Strings.EMPTY, KeyConst.TERMINAL_ACCESS_TOKEN_EXPIRE, TimeUnit.SECONDS);
    log.info("用户获取terminal uid: {} machineId: {} token: {}", userId, machineId, token);
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.MACHINE_NAME, machine.getMachineName());
    return access;
}
Also used : MachineTerminalVO(com.orion.ops.entity.vo.MachineTerminalVO) TerminalAccessVO(com.orion.ops.entity.vo.TerminalAccessVO) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO)

Example 7 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class ApplicationMachineServiceImpl method configAppMachines.

@Override
@Transactional(rollbackFor = Exception.class)
public void configAppMachines(ApplicationConfigRequest request) {
    Long appId = request.getAppId();
    Long profileId = request.getProfileId();
    List<Long> machineIdList = request.getMachineIdList();
    // 检查
    for (Long machineId : machineIdList) {
        MachineInfoDO machine = machineInfoDAO.selectById(machineId);
        Valid.notNull(machine, MessageConst.INVALID_MACHINE);
    }
    // 删除
    SpringHolder.getBean(ApplicationMachineService.class).deleteAppMachineByAppProfileId(appId, profileId);
    // 添加
    for (Long machineId : machineIdList) {
        ApplicationMachineDO machine = new ApplicationMachineDO();
        machine.setAppId(appId);
        machine.setProfileId(profileId);
        machine.setMachineId(machineId);
        applicationMachineDAO.insert(machine);
    }
}
Also used : ApplicationMachineService(com.orion.ops.service.api.ApplicationMachineService) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) ApplicationMachineDO(com.orion.ops.entity.domain.ApplicationMachineDO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class CommandExecServiceImpl method batchSubmitTask.

@Override
public List<CommandTaskSubmitVO> batchSubmitTask(CommandExecRequest request) {
    UserDTO user = Currents.getUser();
    List<Long> machineIdList = request.getMachineIdList();
    // 查询机器信息
    Map<Long, MachineInfoDO> machineStore = Maps.newMap();
    for (Long mid : machineIdList) {
        MachineInfoDO machine = machineInfoService.selectById(mid);
        Valid.notNull(machine, MessageConst.INVALID_MACHINE);
        machineStore.put(machine.getId(), machine);
    }
    // 设置命令
    String command = request.getCommand();
    final boolean containsEnv = command.contains(EnvConst.SYMBOL);
    if (containsEnv) {
        // 查询系统环境变量
        Map<String, String> systemEnv = systemEnvService.getFullSystemEnv();
        command = Strings.format(command, EnvConst.SYMBOL, systemEnv);
    }
    // 批量执行命令
    List<CommandTaskSubmitVO> list = Lists.newList();
    for (Long mid : machineIdList) {
        MachineInfoDO machine = machineStore.get(mid);
        // 插入执行命令
        CommandExecDO record = new CommandExecDO();
        record.setUserId(user.getId());
        record.setUserName(user.getUsername());
        record.setMachineId(mid);
        record.setMachineName(machine.getMachineName());
        record.setMachineHost(machine.getMachineHost());
        record.setMachineTag(machine.getMachineTag());
        record.setExecType(ExecType.BATCH_EXEC.getType());
        record.setExecStatus(ExecStatus.WAITING.getStatus());
        record.setDescription(request.getDescription());
        if (containsEnv) {
            // 查询机器环境变量
            Map<String, String> machineEnv = machineEnvService.getFullMachineEnv(mid);
            record.setExecCommand(Strings.format(command, EnvConst.SYMBOL, machineEnv));
        } else {
            record.setExecCommand(command);
        }
        commandExecDAO.insert(record);
        // 设置日志路径
        Long execId = record.getId();
        String logPath = PathBuilders.getExecLogPath(Const.COMMAND_DIR, execId, machine.getId());
        CommandExecDO update = new CommandExecDO();
        update.setId(execId);
        update.setLogPath(logPath);
        record.setLogPath(logPath);
        commandExecDAO.updateById(update);
        // 提交执行任务
        IExecHandler.with(execId).exec();
        // 返回
        CommandTaskSubmitVO submitVO = new CommandTaskSubmitVO();
        submitVO.setExecId(execId);
        submitVO.setMachineId(mid);
        submitVO.setMachineName(machine.getMachineName());
        submitVO.setMachineHost(machine.getMachineHost());
        list.add(submitVO);
    }
    // 设置日志参数
    List<Long> idList = list.stream().map(CommandTaskSubmitVO::getExecId).collect(Collectors.toList());
    EventParamsHolder.addParams(request);
    EventParamsHolder.addParam(EventKeys.ID_LIST, idList);
    EventParamsHolder.addParam(EventKeys.COUNT, list.size());
    return list;
}
Also used : CommandTaskSubmitVO(com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO) UserDTO(com.orion.ops.entity.dto.UserDTO) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) CommandExecDO(com.orion.ops.entity.domain.CommandExecDO)

Example 9 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class FileTailServiceImpl method getTailToken.

@Override
public FileTailVO getTailToken(FileTailType type, Long relId) {
    FileTailVO res;
    // 获取日志路径
    final boolean isLocal = type.isLocal();
    if (isLocal) {
        String path = this.getTailFilePath(type, relId);
        res = new FileTailVO();
        res.setPath(path);
        res.setOffset(machineEnvService.getTailOffset(Const.HOST_MACHINE_ID));
        res.setCharset(machineEnvService.getTailCharset(Const.HOST_MACHINE_ID));
        res.setCommand(machineEnvService.getTailDefaultCommand(Const.HOST_MACHINE_ID));
    } else {
        // tail list
        FileTailListDO fileTail = fileTailListDAO.selectById(relId);
        Valid.notNull(fileTail, MessageConst.UNKNOWN_DATA);
        res = Converts.to(fileTail, FileTailVO.class);
    }
    // 设置命令
    this.replaceTailCommand(res);
    // 设置机器信息
    MachineInfoDO machine = machineInfoService.selectById(isLocal ? Const.HOST_MACHINE_ID : res.getMachineId());
    Valid.notNull(machine, MessageConst.INVALID_MACHINE);
    res.setMachineId(machine.getId());
    res.setMachineName(machine.getMachineName());
    res.setMachineHost(machine.getMachineHost());
    // 设置token
    String token = UUIds.random19();
    res.setToken(token);
    // 设置缓存
    FileTailDTO tail = Converts.to(res, FileTailDTO.class);
    tail.setUserId(Currents.getUserId());
    // 追踪模式
    String tailMode = isLocal ? systemEnvService.getTailMode() : res.getTailMode();
    tail.setMode(tailMode);
    String key = Strings.format(KeyConst.FILE_TAIL_ACCESS_TOKEN, token);
    redisTemplate.opsForValue().set(key, JSON.toJSONString(tail), KeyConst.FILE_TAIL_ACCESS_EXPIRE, TimeUnit.SECONDS);
    // 非列表不返回命令和路径
    if (isLocal) {
        res.setPath(null);
        res.setCommand(null);
    }
    return res;
}
Also used : FileTailListDO(com.orion.ops.entity.domain.FileTailListDO) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) FileTailDTO(com.orion.ops.entity.dto.FileTailDTO) FileTailVO(com.orion.ops.entity.vo.FileTailVO)

Example 10 with MachineInfoDO

use of com.orion.ops.entity.domain.MachineInfoDO in project orion-ops by lijiahangmax.

the class MachineInfoServiceImpl method addMachine.

@Override
@Transactional(rollbackFor = Exception.class)
public Long addMachine(MachineInfoRequest request) {
    // 检查proxyId
    this.checkProxy(request.getProxyId());
    MachineInfoDO entity = new MachineInfoDO();
    String password = request.getPassword();
    this.copyProperties(request, entity);
    // 添加机器
    entity.setMachineStatus(Const.ENABLE);
    machineInfoDAO.insert(entity);
    if (Strings.isNotBlank(password)) {
        entity.setPassword(ValueMix.encrypt(password));
        machineInfoDAO.updateById(entity);
    }
    Long id = entity.getId();
    // 初始化环境变量
    machineEnvService.initEnv(id);
    // 设置日志参数
    EventParamsHolder.addParams(entity);
    return id;
}
Also used : MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)16 Transactional (org.springframework.transaction.annotation.Transactional)4 FileTailListDO (com.orion.ops.entity.domain.FileTailListDO)3 FileTailVO (com.orion.ops.entity.vo.FileTailVO)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 MachineEnvDO (com.orion.ops.entity.domain.MachineEnvDO)2 UserDTO (com.orion.ops.entity.dto.UserDTO)2 Date (java.util.Date)2 ApplicationMachineDO (com.orion.ops.entity.domain.ApplicationMachineDO)1 CommandExecDO (com.orion.ops.entity.domain.CommandExecDO)1 MachineTerminalDO (com.orion.ops.entity.domain.MachineTerminalDO)1 FileTailDTO (com.orion.ops.entity.dto.FileTailDTO)1 TerminalConnectDTO (com.orion.ops.entity.dto.TerminalConnectDTO)1 MachineInfoVO (com.orion.ops.entity.vo.MachineInfoVO)1 MachineTerminalVO (com.orion.ops.entity.vo.MachineTerminalVO)1 TerminalAccessVO (com.orion.ops.entity.vo.TerminalAccessVO)1 CommandTaskSubmitVO (com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO)1 ApplicationMachineService (com.orion.ops.service.api.ApplicationMachineService)1 SessionStore (com.orion.remote.channel.SessionStore)1 IOException (java.io.IOException)1