|
|
@@ -12,17 +12,12 @@ import cn.reghao.autodop.dmaster.app.repository.build.AppCompileRepository;
|
|
|
import cn.reghao.autodop.dmaster.app.repository.build.AppPackRepository;
|
|
|
import cn.reghao.autodop.dmaster.app.repository.build.AppUpdateRepository;
|
|
|
import cn.reghao.autodop.dmaster.app.repository.orchestration.*;
|
|
|
-import cn.reghao.autodop.dmaster.app.vo.BuildConfig;
|
|
|
-import cn.reghao.autodop.dmaster.app.vo.PageList;
|
|
|
-import cn.reghao.autodop.dmaster.app.vo.OrchestrationVO;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import cn.reghao.autodop.dmaster.app.vo.*;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -37,213 +32,154 @@ public class OrchestrateService {
|
|
|
private AppCompileRepository appCompileRepository;
|
|
|
private AppPackRepository appPackRepository;
|
|
|
private NotificationRepository notificationRepository;
|
|
|
- private AppRepository repository;
|
|
|
|
|
|
public OrchestrateService(ProjOrchestrationRepository projRepository,
|
|
|
AppOrchestrationRepository appRepository,
|
|
|
AppUpdateRepository appUpdateRepository,
|
|
|
AppCompileRepository appCompileRepository,
|
|
|
AppPackRepository appPackRepository,
|
|
|
- NotificationRepository notificationRepository,
|
|
|
- AppRepository repository) {
|
|
|
+ NotificationRepository notificationRepository) {
|
|
|
this.projRepository = projRepository;
|
|
|
this.appRepository = appRepository;
|
|
|
this.appUpdateRepository = appUpdateRepository;
|
|
|
this.appCompileRepository = appCompileRepository;
|
|
|
this.appPackRepository = appPackRepository;
|
|
|
this.notificationRepository = notificationRepository;
|
|
|
- this.repository = repository;
|
|
|
}
|
|
|
|
|
|
- public void add(int type, String json) {
|
|
|
- AppBuild appBuild;
|
|
|
+ public void addOrModify(int type, String json, String type1) throws Exception {
|
|
|
switch (type) {
|
|
|
case 1:
|
|
|
// 项目编排
|
|
|
- ProjOrchestration proj = (ProjOrchestration) JsonUtil.jsonToObject(json, ProjOrchestration.class);
|
|
|
- appBuild = proj.getAppBuild();
|
|
|
- checkSharedEntity(appBuild);
|
|
|
+ ProjOrchestrationVO projVO = (ProjOrchestrationVO) JsonUtil.jsonToObject(json, ProjOrchestrationVO.class);
|
|
|
+ AppBuild appBuild = checkSharedBuildConfig(projVO.getAppBuild());
|
|
|
+ ProjOrchestration proj = projVO.to();
|
|
|
+ proj.setAppBuild(appBuild);
|
|
|
projRepository.save(proj);
|
|
|
break;
|
|
|
case 2:
|
|
|
// 应用编排
|
|
|
- // TODO 添加应用的配置文件
|
|
|
- AppOrchestration app = (AppOrchestration) JsonUtil.jsonToObject(json, AppOrchestration.class);
|
|
|
- saveApp(app);
|
|
|
+ AppOrchestrationVO appVO = (AppOrchestrationVO) JsonUtil.jsonToObject(json, AppOrchestrationVO.class);
|
|
|
+ AppOrchestration app = appVO.to();
|
|
|
+ if (appVO.getProjId() == null) {
|
|
|
+ AppBuild appBuild1 = checkSharedBuildConfig(appVO.getAppBuild());
|
|
|
+ app.setAppBuild(appBuild1);
|
|
|
+
|
|
|
+ String notifier = appVO.getNotifier();
|
|
|
+ checkSharedNotification(app, notifier);
|
|
|
+ } else {
|
|
|
+ // TODO
|
|
|
+ ProjOrchestration entity = projRepository.findByProjId(appVO.getProjId());
|
|
|
+ app.setProj(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("add".equals(type1)) {
|
|
|
+ appRepository.save(app);
|
|
|
+ } else {
|
|
|
+ AppOrchestration entity = appRepository.findByAppId(app.getAppId());
|
|
|
+ if (entity == null) {
|
|
|
+ throw new Exception(app.getAppId() + " 不存在...");
|
|
|
+ }
|
|
|
+ app.setId(entity.getId());
|
|
|
+ app.setCreateTime(entity.getCreateTime());
|
|
|
+ app.setUpdateTime(LocalDateTime.now());
|
|
|
+ appRepository.save(app);
|
|
|
+ }
|
|
|
default:
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void saveApp(AppOrchestration app) {
|
|
|
- ProjOrchestration proj = app.getProj();
|
|
|
- if (proj != null) {
|
|
|
- ProjOrchestration projEntity = projRepository.findByProjId(proj.getProjId());
|
|
|
- if (projEntity != null) {
|
|
|
- app.setProj(projEntity);
|
|
|
- } else {
|
|
|
- projRepository.save(proj);
|
|
|
- }
|
|
|
- } else {
|
|
|
- AppBuild appBuild = app.getAppBuild();
|
|
|
- checkSharedEntity(appBuild);
|
|
|
+ private AppBuild checkSharedBuildConfig(AppBuildVO buildVO) throws Exception {
|
|
|
+ AppBuild appBuild = new AppBuild();
|
|
|
+ String updater = buildVO.getUpdater();
|
|
|
+ AppUpdate appUpdate = appUpdateRepository.findByRepoName(updater);
|
|
|
+ if (appUpdate == null) {
|
|
|
+ throw new Exception(updater + " 不存在...");
|
|
|
}
|
|
|
+ appBuild.setAppUpdate(appUpdate);
|
|
|
|
|
|
- Notification notification = app.getNotification();
|
|
|
- Notification notifierEntity = notificationRepository.findByNotifierName(notification.getNotifierName());
|
|
|
- if (notifierEntity != null) {
|
|
|
- app.setNotification(notifierEntity);
|
|
|
- } else {
|
|
|
- notificationRepository.save(notification);
|
|
|
+ String compiler = buildVO.getCompiler();
|
|
|
+ AppCompile appCompile = appCompileRepository.findByCompilerName(compiler);
|
|
|
+ if (appCompile == null) {
|
|
|
+ throw new Exception(compiler + " 不存在...");
|
|
|
}
|
|
|
+ appBuild.setAppCompile(appCompile);
|
|
|
|
|
|
- appRepository.save(app);
|
|
|
+ String packer = buildVO.getPacker();
|
|
|
+ AppPack appPack = appPackRepository.findByPackerName(packer);
|
|
|
+ if (appPack == null) {
|
|
|
+ throw new Exception(packer + " 不存在...");
|
|
|
+ }
|
|
|
+ appBuild.setAppPack(appPack);
|
|
|
+ return appBuild;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkSharedNotification(AppOrchestration app, String notifier) throws Exception {
|
|
|
+ if (notifier != null) {
|
|
|
+ Notification notification = notificationRepository.findByNotifierName(notifier);
|
|
|
+ if (notification == null) {
|
|
|
+ throw new Exception(notification + " 不存在...");
|
|
|
+ }
|
|
|
+ app.setNotification(notification);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public boolean copy(int type, String from, String to) throws Exception {
|
|
|
+ public boolean copy(int type, String from, String to) throws CloneNotSupportedException {
|
|
|
switch (type) {
|
|
|
case 1:
|
|
|
- ProjOrchestration projEntity = projRepository.findByProjId(from);
|
|
|
- ProjOrchestration proj = projRepository.findByProjId(to);
|
|
|
- if (projEntity != null && proj == null) {
|
|
|
- proj = (ProjOrchestration) projEntity.clone();
|
|
|
- proj.setProjId(to);
|
|
|
- proj.setId(null);
|
|
|
- checkSharedEntity(proj.getAppBuild());
|
|
|
- projRepository.save(proj);
|
|
|
- return true;
|
|
|
- }
|
|
|
break;
|
|
|
case 2:
|
|
|
AppOrchestration appEntity = appRepository.findByAppId(from);
|
|
|
- AppOrchestration app = appRepository.findByAppId(to);
|
|
|
- if (appEntity != null && app == null) {
|
|
|
- app = (AppOrchestration) appEntity.clone();
|
|
|
- app.setAppId(to);
|
|
|
- app.setId(null);
|
|
|
- // TODO 复制共享引用
|
|
|
- /*
|
|
|
- app.setDependencyRepos(null);
|
|
|
- app.getAppDeploy().setHosts(null);
|
|
|
- app.getAppDeploy().setLogs(null);*/
|
|
|
- saveApp(app);
|
|
|
- return true;
|
|
|
+ AppOrchestration newApp = appRepository.findByAppId(to);
|
|
|
+ if (appEntity == null || newApp != null) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+ newApp = (AppOrchestration) appEntity.clone();
|
|
|
+ newApp.setAppId(to);
|
|
|
+ newApp.setId(null);
|
|
|
+ newApp.setCreateTime(LocalDateTime.now());
|
|
|
+ newApp.setUpdateTime(LocalDateTime.now());
|
|
|
+ // 处理共享集合
|
|
|
+ newApp.getAppDeploy().setHosts(null);
|
|
|
+ newApp.getAppRunning().setLogs(null);
|
|
|
+ appRepository.save(newApp);
|
|
|
break;
|
|
|
default:
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * TODO 由于 AppBuild 和 ProjBuild 只能二选一,因此需要对 @ManyToOne 约束进行检查
|
|
|
- * TODO 数据库查询异步化
|
|
|
- * 检查构建阶段的共享实体是否存在
|
|
|
- *
|
|
|
- * @date 2020-03-01 下午6:34
|
|
|
- */
|
|
|
- private void checkSharedEntity(AppBuild build) {
|
|
|
- AppUpdate appUpdate = build.getAppUpdate();
|
|
|
- AppUpdate authEntity = appUpdateRepository.findByRepoName(appUpdate.getRepoName());
|
|
|
- if (authEntity != null) {
|
|
|
- build.setAppUpdate(authEntity);
|
|
|
- } else {
|
|
|
- appUpdateRepository.save(appUpdate);
|
|
|
- }
|
|
|
-
|
|
|
- AppCompile appCompile = build.getAppCompile();
|
|
|
- if (appCompile != null) {
|
|
|
- AppCompile compilerEntity = appCompileRepository.findByCompilerName(appCompile.getCompilerName());
|
|
|
- if (compilerEntity != null) {
|
|
|
- build.setAppCompile(compilerEntity);
|
|
|
- } else {
|
|
|
- appCompileRepository.save(appCompile);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- AppPack packer = build.getAppPack();
|
|
|
- if (packer != null) {
|
|
|
- AppPack packerEntity = appPackRepository.findByPackerName(packer.getPackerName());
|
|
|
- if (packerEntity != null) {
|
|
|
- build.setAppPack(packerEntity);
|
|
|
- } else {
|
|
|
- appPackRepository.save(packer);
|
|
|
- }
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public Object get(int type, String id) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- ProjOrchestration proj = projRepository.findByProjId(id);
|
|
|
- return OrchestrationVO.projVO(proj);
|
|
|
- case 2:
|
|
|
- AppOrchestration app = appRepository.findByAppId(id);
|
|
|
- return OrchestrationVO.appVO(app);
|
|
|
- default:
|
|
|
- return "编排类型错误";
|
|
|
- }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
public Object getByPage(int type, String env, PageRequest pageRequest) {
|
|
|
switch (type) {
|
|
|
case 1:
|
|
|
Page<ProjOrchestration> projPages = projRepository.findAll(pageRequest);
|
|
|
- List<ProjOrchestration> projVOs = new ArrayList<>();
|
|
|
- projPages.forEach(proj -> {
|
|
|
- projVOs.add(OrchestrationVO.projVO(proj));
|
|
|
- });
|
|
|
-
|
|
|
- PageList<ProjOrchestration> projs = new PageList<>();
|
|
|
- projs.setList(projVOs);
|
|
|
- projs.setTotalSize(projPages.getTotalElements());
|
|
|
- projs.setTotalPages(projPages.getTotalPages());
|
|
|
- return projs;
|
|
|
+ PageList<ProjOrchestrationVO> projVOs = new PageList<>();
|
|
|
+ projVOs.setTotalSize(projPages.getTotalElements());
|
|
|
+ projVOs.setTotalPages(projPages.getTotalPages());
|
|
|
+ projVOs.setList(projPages.getContent().stream()
|
|
|
+ .map(proj -> new ProjOrchestrationVO().from(proj))
|
|
|
+ .collect(Collectors.toList()));
|
|
|
+ return projVOs;
|
|
|
case 2:
|
|
|
Page<AppOrchestration> appPages = appRepository.findByEnv(env, pageRequest);
|
|
|
- PageList<AppOrchestration> apps = new PageList<>();
|
|
|
- apps.setTotalSize(appPages.getTotalElements());
|
|
|
- apps.setTotalPages(appPages.getTotalPages());
|
|
|
- apps.setList(appPages.getContent().stream().map(OrchestrationVO::appVO).collect(Collectors.toList()));
|
|
|
- return apps;
|
|
|
+ PageList<AppOrchestrationVO> appVOs = new PageList<>();
|
|
|
+ appVOs.setTotalSize(appPages.getTotalElements());
|
|
|
+ appVOs.setTotalPages(appPages.getTotalPages());
|
|
|
+ appVOs.setList(appPages.getContent().stream()
|
|
|
+ .map(app -> new AppOrchestrationVO().from(app))
|
|
|
+ .collect(Collectors.toList()));
|
|
|
+ return appVOs;
|
|
|
default:
|
|
|
return "编排类型错误";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public boolean modify(int type, String json) {
|
|
|
- switch (type) {
|
|
|
- case 1:
|
|
|
- ProjOrchestration proj = (ProjOrchestration) JsonUtil.jsonToObject(json, ProjOrchestration.class);
|
|
|
- ProjOrchestration projEntity = projRepository.findByProjId(proj.getProjId());
|
|
|
- if (projEntity != null) {
|
|
|
- AppBuild appBuild = proj.getAppBuild();
|
|
|
- checkSharedEntity(appBuild);
|
|
|
-
|
|
|
- proj.setId(projEntity.getId());
|
|
|
- proj.setCreateTime(projEntity.getCreateTime());
|
|
|
- proj.setUpdateTime(LocalDateTime.now());
|
|
|
- projRepository.save(proj);
|
|
|
- return true;
|
|
|
- }
|
|
|
- case 2:
|
|
|
- // TODO 若修改了部署的 host,需要额外的处理
|
|
|
- AppOrchestration app = (AppOrchestration) JsonUtil.jsonToObject(json, AppOrchestration.class);
|
|
|
- String appId = app.getAppId();
|
|
|
- AppOrchestration appEntity = appRepository.findByAppId(appId);
|
|
|
- if (appEntity != null) {
|
|
|
- app.setId(appEntity.getId());
|
|
|
- app.setCreateTime(appEntity.getCreateTime());
|
|
|
- app.setUpdateTime(LocalDateTime.now());
|
|
|
- saveApp(app);
|
|
|
- return true;
|
|
|
- }
|
|
|
- default:
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* TODO 除了删除数据库中所有与应用相关的数据,还应删除文件系统和 docker 中与应用相关的数据
|
|
|
*
|
|
|
@@ -258,15 +194,8 @@ public class OrchestrateService {
|
|
|
break;
|
|
|
case 2:
|
|
|
appRepository.deleteByAppId(appId);
|
|
|
+ break;
|
|
|
default:
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public BuildConfig buildConfig(String type) {
|
|
|
-
|
|
|
-
|
|
|
- BuildConfig buildConfig = repository.buildConfig();
|
|
|
- repository.projs(buildConfig);
|
|
|
- return buildConfig;
|
|
|
- }
|
|
|
}
|