|
|
@@ -1,329 +0,0 @@
|
|
|
-package cn.reghao.autodop.dmaster.orchestrate.service;
|
|
|
-
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.tools.AppPacker;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.tools.CodeCompiler;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.tools.ReposAuth;
|
|
|
-import cn.reghao.autodop.dmaster.common.result.ResultCode;
|
|
|
-import cn.reghao.autodop.dmaster.common.result.WebResult;
|
|
|
-import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.caching.OrchestrationCaching;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.dao.AppDAO;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.tools.Notifier;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.build.*;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.pojo.orchestration.*;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.NotifierRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.build.CodeCompilerRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.build.AppPackerRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.build.ReposAuthRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.orchestration.AppOrchestrationRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.repository.orchestration.ProjectOrchestrationRepository;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.vo.AppVO;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.vo.PageList;
|
|
|
-import cn.reghao.autodop.dmaster.orchestrate.vo.ProjVO;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.PageRequest;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author reghao
|
|
|
- * @date 2020-02-28 17:10:37
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class OrchestrateService {
|
|
|
- private ProjectOrchestrationRepository projOrchestrationRepository;
|
|
|
- private AppOrchestrationRepository appOrchestrationRepository;
|
|
|
- private AppDAO appDAO;
|
|
|
-
|
|
|
- private NotifierRepository notifierRepository;
|
|
|
- private ReposAuthRepository reposAuthRepository;
|
|
|
- private CodeCompilerRepository codeCompilerRepository;
|
|
|
- private AppPackerRepository appPackerRepository;
|
|
|
- private OrchestrationCaching orchestrationCaching;
|
|
|
-
|
|
|
- public OrchestrateService(ProjectOrchestrationRepository projOrchestrationRepository,
|
|
|
- AppOrchestrationRepository appOrchestrationRepository,
|
|
|
- AppDAO appDAO,
|
|
|
- NotifierRepository notifierRepository,
|
|
|
- ReposAuthRepository reposAuthRepository,
|
|
|
- CodeCompilerRepository codeCompilerRepository,
|
|
|
- AppPackerRepository appPackerRepository,
|
|
|
- OrchestrationCaching orchestrationCaching) {
|
|
|
-
|
|
|
- this.projOrchestrationRepository = projOrchestrationRepository;
|
|
|
- this.appOrchestrationRepository = appOrchestrationRepository;
|
|
|
- this.appDAO = appDAO;
|
|
|
-
|
|
|
- this.notifierRepository = notifierRepository;
|
|
|
- this.reposAuthRepository = reposAuthRepository;
|
|
|
- this.codeCompilerRepository = codeCompilerRepository;
|
|
|
- this.appPackerRepository = appPackerRepository;
|
|
|
-
|
|
|
- this.orchestrationCaching = orchestrationCaching;
|
|
|
- }
|
|
|
-
|
|
|
- public String getBuild(int type) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- List<ReposAuth> auths = reposAuthRepository.findAll();
|
|
|
- Map<String, List<ReposAuth>> authMap = new HashMap<>();
|
|
|
- auths.forEach(auth -> {
|
|
|
- auth.setId(null);
|
|
|
- auth.setCreateTime(null);
|
|
|
- List<ReposAuth> list = authMap.get(auth.getReposType());
|
|
|
- if (list != null) {
|
|
|
- list.add(auth);
|
|
|
- } else {
|
|
|
- list = new ArrayList<>();
|
|
|
- list.add(auth);
|
|
|
- authMap.put(auth.getReposType(), list);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return new WebResult<Map<String, List<ReposAuth>>>(ResultCode.SUCCESS).jsonResult(authMap);
|
|
|
- case 2:
|
|
|
- List<CodeCompiler> compilers = codeCompilerRepository.findAll();
|
|
|
- Map<String, List<CodeCompiler>> compilerMap = new HashMap<>();
|
|
|
- compilers.forEach(compiler -> {
|
|
|
- compiler.setId(null);
|
|
|
- compiler.setCreateTime(null);
|
|
|
- List<CodeCompiler> list = compilerMap.get(compiler.getCompilerType());
|
|
|
- if (list != null) {
|
|
|
- list.add(compiler);
|
|
|
- } else {
|
|
|
- list = new ArrayList<>();
|
|
|
- list.add(compiler);
|
|
|
- compilerMap.put(compiler.getCompilerType(), list);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return new WebResult<Map<String, List<CodeCompiler>> >(ResultCode.SUCCESS).jsonResult(compilerMap);
|
|
|
- case 3:
|
|
|
- List<AppPacker> packers = appPackerRepository.findAll();
|
|
|
- Map<String, List<AppPacker>> packerMap = new HashMap<>();
|
|
|
- packers.forEach(packer -> {
|
|
|
- packer.setId(null);
|
|
|
- packer.setCreateTime(null);
|
|
|
- List<AppPacker> list = packerMap.get(packer.getPackerType());
|
|
|
- if (list != null) {
|
|
|
- list.add(packer);
|
|
|
- } else {
|
|
|
- list = new ArrayList<>();
|
|
|
- list.add(packer);
|
|
|
- packerMap.put(packer.getPackerType(), list);
|
|
|
- }
|
|
|
- });
|
|
|
- return new WebResult<Map<String, List<AppPacker>>>(ResultCode.SUCCESS).jsonResult(packerMap);
|
|
|
- default:
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * TODO 由于 AppBuild 和 ProjBuild 只能二选一,因此需要对 @ManyToOne 约束进行检查
|
|
|
- * 检查构建阶段的共享实体是否存在
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2020-03-01 下午6:34
|
|
|
- */
|
|
|
- private void checkSharedEntity(Orchestration orchestration) throws Exception {
|
|
|
- if (orchestration instanceof ProjOrchestration) {
|
|
|
- AppBuild build = ((ProjOrchestration) orchestration).getBuild();
|
|
|
- if (build != null) {
|
|
|
- checkManyToOne(build);
|
|
|
- } else {
|
|
|
- throw new Exception("配置出错咯~");
|
|
|
- }
|
|
|
- } else if (orchestration instanceof AppOrchestration) {
|
|
|
- AppOrchestration app = (AppOrchestration) orchestration;
|
|
|
- AppBuild build = app.getBuild();
|
|
|
- ProjBuild projBuild = app.getProjBuild();
|
|
|
- if (build == null && projBuild == null) {
|
|
|
- throw new Exception("配置出错咯~");
|
|
|
- } else if (build == null) {
|
|
|
- String projId = projBuild.getProjId();
|
|
|
- ProjOrchestration proj = projOrchestrationRepository.findByIdentifier(projId);
|
|
|
- if (proj == null) {
|
|
|
- throw new Exception("配置出错咯~");
|
|
|
- }
|
|
|
- } else if (projBuild == null) {
|
|
|
- checkManyToOne(build);
|
|
|
- } else {
|
|
|
- // AppBuild 和 ProjBuild 均存在时,使用 AppBuild
|
|
|
- app.setProjBuild(null);
|
|
|
- checkManyToOne(build);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 数据库查询异步化
|
|
|
- private void checkManyToOne(AppBuild build) {
|
|
|
- Notifier notifier = build.getNotifier();
|
|
|
- Notifier notifierEntity = notifierRepository.findByNotifierName(notifier.getNotifierName());
|
|
|
- if (notifierEntity != null) {
|
|
|
- build.setNotifier(notifierEntity);
|
|
|
- } else {
|
|
|
- notifierRepository.save(notifier);
|
|
|
- }
|
|
|
-
|
|
|
- ReposAuth reposAuth = build.getCodeRepos().getReposAuth();
|
|
|
- ReposAuth authEntity = reposAuthRepository.findByReposName(reposAuth.getReposName());
|
|
|
- if (authEntity != null) {
|
|
|
- build.getCodeRepos().setReposAuth(authEntity);
|
|
|
- } else {
|
|
|
- reposAuthRepository.save(reposAuth);
|
|
|
- }
|
|
|
-
|
|
|
- CodeCompiler compiler = build.getCodeCompiler();
|
|
|
- if (compiler != null) {
|
|
|
- CodeCompiler compilerEntity = codeCompilerRepository.findByCompilerName(compiler.getCompilerName());
|
|
|
- if (compilerEntity != null) {
|
|
|
- build.setCodeCompiler(compilerEntity);
|
|
|
- } else {
|
|
|
- codeCompilerRepository.save(compiler);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- AppPacker packer = build.getAppPacker();
|
|
|
- if (packer != null) {
|
|
|
- AppPacker packerEntity = appPackerRepository.findByPackerName(packer.getPackerName());
|
|
|
- if (packerEntity != null) {
|
|
|
- build.setAppPacker(packerEntity);
|
|
|
- } else {
|
|
|
- appPackerRepository.save(packer);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 后期将 batchAdd 合并到此方法
|
|
|
- public void add(int type, String json) throws Exception {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- // 项目编排
|
|
|
- ProjOrchestration proj = (ProjOrchestration) JsonUtil.jsonToObject(json, ProjOrchestration.class);
|
|
|
- checkSharedEntity(proj);
|
|
|
- projOrchestrationRepository.save(proj);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- AppOrchestration app = (AppOrchestration) JsonUtil.jsonToObject(json, AppOrchestration.class);
|
|
|
- checkSharedEntity(app);
|
|
|
- appOrchestrationRepository.save(app);
|
|
|
- default:
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void batchAdd(int type, String json) throws Exception {
|
|
|
- // TODO 优化代码
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- List<Object> objs = JsonUtil.jsonToObjectArray(json, ProjOrchestration.class);
|
|
|
- for (Object obj : objs) {
|
|
|
- ProjOrchestration proj = (ProjOrchestration) obj;
|
|
|
- checkSharedEntity(proj);
|
|
|
- // TODO 使用 saveAll 时若多个编排中具有相同的 UNIQUE 约束列值,则会引发 UNIQUE 约束异常
|
|
|
- projOrchestrationRepository.save(proj);
|
|
|
- }
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- List<Object> objs1 = JsonUtil.jsonToObjectArray(json, AppOrchestration.class);
|
|
|
- for (Object obj : objs1) {
|
|
|
- AppOrchestration app = (AppOrchestration) obj;
|
|
|
- checkSharedEntity(app);
|
|
|
- // TODO 使用 saveAll 时若多个编排中具有相同的 UNIQUE 约束列值,则会引发 UNIQUE 约束异常
|
|
|
- appOrchestrationRepository.save(app);
|
|
|
- }
|
|
|
- default:
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String get(int type, String identifier) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- ProjOrchestration proj = projOrchestrationRepository.findByIdentifier(identifier);
|
|
|
- return new WebResult<ProjOrchestration>(ResultCode.SUCCESS).jsonResult(proj);
|
|
|
- case 2:
|
|
|
- //AppOrchestration app = orchestrationCaching.findAppByIdentifier(identifier);
|
|
|
- //AppOrchestration app = appOrchestrationRepository.findByIdentifier(identifier);
|
|
|
- //return new WebResult<AppOrchestration>(ResultCode.SUCCESS).jsonResult(app);
|
|
|
- default:
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String getByPage(int type, PageRequest pageRequest) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- Page<ProjOrchestration> projPages = projOrchestrationRepository.findAll(pageRequest);
|
|
|
- List<ProjVO> vos = new ArrayList<>();
|
|
|
- projPages.forEach(proj -> {
|
|
|
- List<String> appIds = appDAO.findAppsByProjId(proj.getIdentifier());
|
|
|
- ProjVO vo = ProjVO.of(proj, appIds.toString()
|
|
|
- .replace("[", "").replace("]", ""));
|
|
|
- vos.add(vo);
|
|
|
- });
|
|
|
-
|
|
|
- PageList<ProjVO> list = new PageList<>();
|
|
|
- list.setList(vos);
|
|
|
- list.setSize(vos.size());
|
|
|
-
|
|
|
- return new WebResult<PageList>(ResultCode.SUCCESS).jsonResult(list);
|
|
|
- case 2:
|
|
|
- Page<AppOrchestration> appPages = appOrchestrationRepository.findAll(pageRequest);
|
|
|
- List<AppVO> appVOS = new ArrayList<>();
|
|
|
- appPages.getContent().forEach(app -> {
|
|
|
- appVOS.add(AppVO.of(app));
|
|
|
- });
|
|
|
-
|
|
|
- PageList<AppVO> list1 = new PageList<>();
|
|
|
- list1.setList(appVOS);
|
|
|
- list1.setSize(appVOS.size());
|
|
|
-
|
|
|
- return new WebResult<PageList>(ResultCode.SUCCESS).jsonResult(list1);
|
|
|
- default:
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 注意对编排修改时产生的数据不一致
|
|
|
- public void modify(int type, String json) throws Exception {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- // TODO 修改 projEntity 中与 proj 不同的地方
|
|
|
- ProjOrchestration proj = (ProjOrchestration) JsonUtil.jsonToObject(json, ProjOrchestration.class);
|
|
|
- ProjOrchestration projEntity = projOrchestrationRepository.findByIdentifier(proj.getIdentifier());
|
|
|
- projEntity.setDescription(proj.getDescription());
|
|
|
-
|
|
|
- checkSharedEntity(proj);
|
|
|
- projOrchestrationRepository.save(projEntity);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- AppOrchestration app = (AppOrchestration) JsonUtil.jsonToObject(json, AppOrchestration.class);
|
|
|
- AppOrchestration appEntity = appOrchestrationRepository.findByIdentifier(app.getIdentifier());
|
|
|
-
|
|
|
- app.setId(appEntity.getId());
|
|
|
- app.setCreateTime(appEntity.getCreateTime());
|
|
|
- checkSharedEntity(app);
|
|
|
- appOrchestrationRepository.save(app);
|
|
|
- break;
|
|
|
- default:
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void delete(int type, String identifier) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- // TODO 删除项目时,所有与项目关联的应用都会失效
|
|
|
- projOrchestrationRepository.deleteByIdentifier(identifier);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- appOrchestrationRepository.deleteByIdentifier(identifier);
|
|
|
- break;
|
|
|
- default:
|
|
|
- }
|
|
|
- }
|
|
|
-}
|