Search in sources :

Example 1 with CommandTaskSubmitVO

use of com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO 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)

Aggregations

CommandExecDO (com.orion.ops.entity.domain.CommandExecDO)1 MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)1 UserDTO (com.orion.ops.entity.dto.UserDTO)1 CommandTaskSubmitVO (com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO)1