use of com.orion.ops.handler.app.machine.BuildMachineProcessor in project orion-ops by lijiahangmax.
the class ApplicationBuildServiceImpl method submitBuildTask.
@Override
public Long submitBuildTask(ApplicationBuildRequest request) {
Long appId = request.getAppId();
Long profileId = request.getProfileId();
// 查询应用
ApplicationInfoDO app = applicationInfoDAO.selectById(appId);
Valid.notNull(app, MessageConst.APP_ABSENT);
// 查询环境
ApplicationProfileDO profile = applicationProfileDAO.selectById(profileId);
Valid.notNull(profile, MessageConst.PROFILE_ABSENT);
// 查询应用执行块
List<ApplicationActionDO> actions = applicationActionService.getAppProfileActions(appId, profileId, StageType.BUILD.getType());
Valid.notEmpty(actions, MessageConst.APP_PROFILE_NOT_CONFIGURED);
UserDTO user = Currents.getUser();
// 获取构建序列
Integer buildSeq = applicationEnvService.getBuildSeqAndIncrement(appId, profileId);
// 设置构建参数
ApplicationBuildDO buildTask = new ApplicationBuildDO();
buildTask.setAppId(appId);
buildTask.setAppName(app.getAppName());
buildTask.setAppTag(app.getAppTag());
buildTask.setProfileId(profileId);
buildTask.setProfileName(profile.getProfileName());
buildTask.setProfileTag(profile.getProfileTag());
buildTask.setBuildSeq(buildSeq);
buildTask.setBranchName(request.getBranchName());
buildTask.setCommitId(request.getCommitId());
buildTask.setVcsId(app.getVcsId());
buildTask.setBuildStatus(BuildStatus.WAIT.getStatus());
buildTask.setDescription(request.getDescription());
buildTask.setCreateUserId(user.getId());
buildTask.setCreateUserName(user.getUsername());
applicationBuildDAO.insert(buildTask);
Long buildId = buildTask.getId();
// 设置目录信息
buildTask.setLogPath(PathBuilders.getBuildLogPath(buildId));
String bundlePath = applicationEnvService.getAppEnvValue(appId, profileId, ApplicationEnvAttr.BUNDLE_PATH.getKey());
buildTask.setBundlePath(PathBuilders.getBuildBundlePath(buildId) + "/" + Files1.getFileName(bundlePath));
// 更新构建信息
applicationBuildDAO.updateById(buildTask);
// 检查是否包含环境变量命令
final boolean hasEnvCommand = actions.stream().filter(s -> ActionType.BUILD_COMMAND.getType().equals(s.getActionType())).map(ApplicationActionDO::getActionCommand).filter(Strings::isNotBlank).anyMatch(s -> s.contains(EnvConst.SYMBOL));
Map<String, String> env = Maps.newMap();
if (hasEnvCommand) {
// 查询应用环境变量
env.putAll(applicationEnvService.getAppProfileFullEnv(appId, profileId));
// 查询机器环境变量
env.putAll(machineEnvService.getFullMachineEnv(Const.HOST_MACHINE_ID));
// 查询系统环境变量
env.putAll(systemEnvService.getFullSystemEnv());
// 添加构建环境变量
env.putAll(this.getBuildEnv(buildId, buildSeq, app.getVcsId(), request));
}
// 设置action
for (ApplicationActionDO action : actions) {
ApplicationActionLogDO actionLog = new ApplicationActionLogDO();
actionLog.setRelId(buildId);
actionLog.setStageType(StageType.BUILD.getType());
actionLog.setMachineId(Const.HOST_MACHINE_ID);
actionLog.setActionId(action.getAppId());
actionLog.setActionName(action.getActionName());
actionLog.setActionType(action.getActionType());
if (ActionType.BUILD_COMMAND.equals(ActionType.of(action.getActionType()))) {
actionLog.setActionCommand(Strings.format(action.getActionCommand(), EnvConst.SYMBOL, env));
}
actionLog.setRunStatus(ActionStatus.WAIT.getStatus());
applicationActionLogDAO.insert(actionLog);
// 设置日志路径
actionLog.setLogPath(PathBuilders.getBuildActionLogPath(buildId, actionLog.getId()));
// 更新
applicationActionLogDAO.updateById(actionLog);
}
// 提交构建任务
log.info("提交应用构建任务 buildId: {}", buildId);
BuildMachineProcessor buildProcessor = new BuildMachineProcessor(buildId);
Threads.start(buildProcessor, SchedulerPools.APP_BUILD_SCHEDULER);
// 设置日志参数
EventParamsHolder.addParams(buildTask);
return buildId;
}
Aggregations