Search in sources :

Example 1 with CommandExecDO

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

the class CommandExecServiceImpl method execDetail.

@Override
public CommandExecVO execDetail(Long id) {
    CommandExecDO execDO = this.selectById(id);
    Valid.notNull(execDO, MessageConst.EXEC_TASK_ABSENT);
    CommandExecVO execVO = Converts.to(execDO, CommandExecVO.class);
    this.assembleExecData(Collections.singletonList(execVO), false);
    return execVO;
}
Also used : CommandExecVO(com.orion.ops.entity.vo.CommandExecVO) CommandExecDO(com.orion.ops.entity.domain.CommandExecDO)

Example 2 with CommandExecDO

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

the class CommandExecServiceImpl method terminatedExec.

@Override
public void terminatedExec(Long id) {
    CommandExecDO execDO = this.selectById(id);
    Valid.notNull(execDO, MessageConst.EXEC_TASK_ABSENT);
    Valid.isTrue(ExecStatus.RUNNABLE.getStatus().equals(execDO.getExecStatus()), MessageConst.ILLEGAL_STATUS);
    // 获取任务并停止
    IExecHandler session = execSessionHolder.getSession(id);
    Valid.notNull(session, MessageConst.SESSION_PRESENT);
    session.terminated();
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.ID, id);
}
Also used : IExecHandler(com.orion.ops.handler.exec.IExecHandler) CommandExecDO(com.orion.ops.entity.domain.CommandExecDO)

Example 3 with CommandExecDO

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

the class CommandExecServiceImpl method writeCommand.

@Override
public void writeCommand(Long id, String command) {
    CommandExecDO execDO = this.selectById(id);
    Valid.notNull(execDO, MessageConst.EXEC_TASK_ABSENT);
    if (!execDO.getExecStatus().equals(ExecStatus.RUNNABLE.getStatus())) {
        return;
    }
    // 获取任务信息
    IExecHandler session = execSessionHolder.getSession(id);
    Valid.notNull(session, MessageConst.EXEC_TASK_THREAD_ABSENT);
    session.write(command + Const.LF);
}
Also used : IExecHandler(com.orion.ops.handler.exec.IExecHandler) CommandExecDO(com.orion.ops.entity.domain.CommandExecDO)

Example 4 with CommandExecDO

use of com.orion.ops.entity.domain.CommandExecDO 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 5 with CommandExecDO

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

the class CommandExecHandler method updateStatus.

/**
 * 更新状态
 *
 * @param status status
 */
private void updateStatus(ExecStatus status) {
    Date now = new Date();
    // 更新
    CommandExecDO update = new CommandExecDO();
    update.setId(execId);
    update.setExecStatus(status.getStatus());
    update.setUpdateTime(now);
    switch(status) {
        case RUNNABLE:
            record.setStartDate(now);
            update.setStartDate(now);
            break;
        case COMPLETE:
            update.setEndDate(now);
            update.setExitCode(exitCode);
            break;
        case EXCEPTION:
        case TERMINATED:
            update.setEndDate(now);
            break;
        default:
    }
    int effect = commandExecDAO.updateById(update);
    log.info("execHandler-更新状态 id: {}, status: {}, effect: {}", execId, status, effect);
}
Also used : CommandExecDO(com.orion.ops.entity.domain.CommandExecDO) Date(java.util.Date)

Aggregations

CommandExecDO (com.orion.ops.entity.domain.CommandExecDO)6 IExecHandler (com.orion.ops.handler.exec.IExecHandler)2 Date (java.util.Date)2 MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)1 UserDTO (com.orion.ops.entity.dto.UserDTO)1 CommandExecVO (com.orion.ops.entity.vo.CommandExecVO)1 CommandTaskSubmitVO (com.orion.ops.entity.vo.sftp.CommandTaskSubmitVO)1