|
|
@@ -1,16 +1,14 @@
|
|
|
package cn.reghao.autodop.dmaster.app.service.build;
|
|
|
|
|
|
import cn.reghao.autodop.common.utils.text.TextFile;
|
|
|
-import cn.reghao.autodop.dmaster.app.constant.AppType;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.build.AppBuild;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.build.AppCompile;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.build.AppPack;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.build.AppUpdate;
|
|
|
-import cn.reghao.autodop.dmaster.app.constant.BuildStage;
|
|
|
+import cn.reghao.autodop.dmaster.app.constant.AppStage;
|
|
|
import cn.reghao.autodop.dmaster.app.constant.CompilerType;
|
|
|
import cn.reghao.autodop.common.deploy.PackerType;
|
|
|
import cn.reghao.autodop.dmaster.app.constant.RepoType;
|
|
|
-import cn.reghao.autodop.dmaster.app.vo.log.BuildDeployLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
|
|
|
@@ -34,9 +32,7 @@ import java.time.LocalDateTime;
|
|
|
import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
- * TODO 统计构建各阶段耗时
|
|
|
- *
|
|
|
- * 应用构建部署,根据 AppOrchestration 的值完成应用构建和部署的配置
|
|
|
+ * 应用构建部署
|
|
|
*
|
|
|
* @author reghao
|
|
|
* @date 2019-10-09 10:54:43
|
|
|
@@ -50,11 +46,8 @@ public class AppIntegrate {
|
|
|
private AppOrchestration app;
|
|
|
// 远程仓库最近一次提交的信息
|
|
|
private CommitLog lastCommitLog;
|
|
|
- private BuildLog buildLog;
|
|
|
- private DeployLog deployLog;
|
|
|
|
|
|
private CodeUpdater codeUpdater;
|
|
|
- private PreCompile preCompile;
|
|
|
private CodeCompiler codeCompiler;
|
|
|
private CodePacker codePacker;
|
|
|
private AppDeployer appDeployer;
|
|
|
@@ -82,7 +75,6 @@ public class AppIntegrate {
|
|
|
|
|
|
private void init() {
|
|
|
initLocalDir();
|
|
|
- initBuildLog();
|
|
|
initBuildDeployConfig();
|
|
|
}
|
|
|
|
|
|
@@ -92,11 +84,6 @@ public class AppIntegrate {
|
|
|
appPackDir = SysConfig.packDir + "/" + app.getAppId();
|
|
|
}
|
|
|
|
|
|
- private void initBuildLog() {
|
|
|
- buildLog = new BuildLog();
|
|
|
- buildLog.setApp(app);
|
|
|
- }
|
|
|
-
|
|
|
private void initBuildDeployConfig() {
|
|
|
AppBuild appBuild;
|
|
|
ProjOrchestration proj = app.getProj();
|
|
|
@@ -129,16 +116,11 @@ public class AppIntegrate {
|
|
|
AppCompile appCompile = appBuild.getAppCompile();
|
|
|
if (appCompile.getCompilerType().equals(CompilerType.shell.name())) {
|
|
|
codeCompiler = new ShellCompiler(appCompile);
|
|
|
- if (app.getAppType().equals(AppType.dotnetCore.name())) {
|
|
|
- preCompile = new DotnetPreCompile();
|
|
|
- } else {
|
|
|
- preCompile = null;
|
|
|
- }
|
|
|
} else if (appCompile.getCompilerType().equals(CompilerType.maven.name())) {
|
|
|
codeCompiler = new MavenCompiler("");
|
|
|
} else if (appCompile.getCompilerType().equals(CompilerType.none.name())) {
|
|
|
// 静态应用,不需要编译器
|
|
|
- codeCompiler = new DefaultCompiler();
|
|
|
+ codeCompiler = new NullCompiler();
|
|
|
}
|
|
|
|
|
|
// 初始化打包配置
|
|
|
@@ -150,53 +132,89 @@ public class AppIntegrate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 编译源码前的操作(一般是调整配置)
|
|
|
- *
|
|
|
- * @date 2019-11-07 下午2:03
|
|
|
- */
|
|
|
- public void preCompile(String appEntryDir) throws Exception {
|
|
|
- preCompile.preCompile(app, appEntryDir);
|
|
|
- }
|
|
|
+ /* 构建:更新 -> 编译 -> 打包 */
|
|
|
+ public BuildLog build() {
|
|
|
+ BuildLog buildLog = new BuildLog();
|
|
|
+ buildLog.setApp(app);
|
|
|
|
|
|
- /* 更新 -> 编译 -> 打包 -> 部署 */
|
|
|
- public boolean update() {
|
|
|
+ boolean isUpdate;
|
|
|
try {
|
|
|
- String remote;
|
|
|
- String branch;
|
|
|
- String local = SysConfig.localRepo + "/" + app.getAppId();
|
|
|
- ProjOrchestration proj = app.getProj();
|
|
|
- if (proj != null) {
|
|
|
- remote = proj.getProjRepo();
|
|
|
- branch = proj.getBranch();
|
|
|
- } else {
|
|
|
- remote = app.getAppRepo();
|
|
|
- branch = app.getBranch();
|
|
|
- }
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ isUpdate = update(buildLog);
|
|
|
+ buildLog.setUpdateTotalTime(System.currentTimeMillis() - start);
|
|
|
+ log.info("{} 更新完成...", app.getAppId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ buildException(buildLog, e);
|
|
|
+ return buildLog;
|
|
|
+ }
|
|
|
|
|
|
- if (lastCommitLog == null) {
|
|
|
- lastCommitLog = codeUpdater.lastCommitInfo(local + "/" + app.getDirname(), branch);
|
|
|
+ if (isUpdate) {
|
|
|
+ try {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ compile();
|
|
|
+ buildLog.setCompileTotalTime(System.currentTimeMillis() - start);
|
|
|
+ log.info("{} 编译完成...", app.getAppId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ buildException(buildLog, e);
|
|
|
+ return buildLog;
|
|
|
}
|
|
|
|
|
|
- UpdateStatus updateStatus = update0(remote, branch, local);
|
|
|
- if (updateStatus.isUpdated) {
|
|
|
- lastCommitLog = updateStatus.getLastCommitLog();
|
|
|
- buildLog.setCommitLog(lastCommitLog);
|
|
|
- BuilderUtil.copyToCompileDir(local, app);
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- buildLog.setStage(BuildStage.update.name());
|
|
|
- buildLog.setCode(0);
|
|
|
- buildLog.setMsg("应用代码未更新");
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
- return false;
|
|
|
+ try {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ pack(buildLog);
|
|
|
+ buildLog.setPackTotalTime(System.currentTimeMillis() - start);
|
|
|
+ log.info("{} 打包完成...", app.getAppId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ buildException(buildLog, e);
|
|
|
+ return buildLog;
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- buildLog.setStage(BuildStage.update.name());
|
|
|
- buildLog.setCode(1);
|
|
|
- buildLog.setMsg(ExceptionUtil.errorMsg(e));
|
|
|
+ } else {
|
|
|
+ buildLog.setCompileTotalTime(0);
|
|
|
+ buildLog.setPackTotalTime(0);
|
|
|
+ buildLog.setMsg("代码未变,无需构建");
|
|
|
+ buildLog.setCode(2);
|
|
|
buildLog.setBuildTime(LocalDateTime.now());
|
|
|
+ return buildLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ buildLog.setMsg("构建成功");
|
|
|
+ buildLog.setCode(0);
|
|
|
+ buildLog.setBuildTime(LocalDateTime.now());
|
|
|
+ return buildLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void buildException(BuildLog buildLog, Exception e) {
|
|
|
+ buildLog.setCode(1);
|
|
|
+ buildLog.setMsg("构建失败");
|
|
|
+ buildLog.setErrDetail(ExceptionUtil.errorMsg(e));
|
|
|
+ buildLog.setBuildTime(LocalDateTime.now());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean update(BuildLog buildLog) throws Exception {
|
|
|
+ String remote;
|
|
|
+ String branch;
|
|
|
+ String local = SysConfig.localRepo + "/" + app.getAppId();
|
|
|
+ ProjOrchestration proj = app.getProj();
|
|
|
+ if (proj != null) {
|
|
|
+ remote = proj.getProjRepo();
|
|
|
+ branch = proj.getBranch();
|
|
|
+ } else {
|
|
|
+ remote = app.getAppRepo();
|
|
|
+ branch = app.getBranch();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (lastCommitLog == null) {
|
|
|
+ lastCommitLog = codeUpdater.lastCommitInfo(local + "/" + app.getDirname(), branch);
|
|
|
+ }
|
|
|
+
|
|
|
+ UpdateStatus updateStatus = update0(remote, branch, local);
|
|
|
+ // TODO 代码版本未更改的情况下,是否应该构建
|
|
|
+ if (updateStatus.isUpdated || app.isAlwaysBuild()) {
|
|
|
+ lastCommitLog = updateStatus.getLastCommitLog();
|
|
|
+ buildLog.setCommitLog(lastCommitLog);
|
|
|
+ BuilderUtil.copyToCompileDir(local, app);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -224,54 +242,24 @@ public class AppIntegrate {
|
|
|
return new UpdateStatus(current, true);
|
|
|
}
|
|
|
|
|
|
- public boolean compile() {
|
|
|
- try {
|
|
|
- if (preCompile != null) {
|
|
|
- preCompile(appCompileDir + app.getCompileDir());
|
|
|
- }
|
|
|
- // TODO 通过脚本调用编译器时会卡住
|
|
|
- codeCompiler.compile(app.getDirname(), appCompileDir + app.getCompileDir(), app.getEnv());
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- buildLog.setStage(BuildStage.compile.name());
|
|
|
- buildLog.setCode(1);
|
|
|
- buildLog.setMsg(ExceptionUtil.errorMsg(e));
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
- return false;
|
|
|
- }
|
|
|
+ private void compile() throws Exception {
|
|
|
+ // TODO 通过脚本调用编译器时会卡住
|
|
|
+ codeCompiler.compile(app.getDirname(), appCompileDir + app.getCompileDir(), app.getEnv());
|
|
|
}
|
|
|
|
|
|
- public boolean pack() {
|
|
|
- try {
|
|
|
- String appPath = codePacker.pack(app.getAppId(), lastCommitLog.getCommitId(),
|
|
|
- appCompileDir + app.getCompileDir());
|
|
|
- buildLog.setAppPath(appPath);
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- buildLog.setStage(BuildStage.pack.name());
|
|
|
- buildLog.setCode(1);
|
|
|
- buildLog.setMsg(ExceptionUtil.errorMsg(e));
|
|
|
- buildLog.setBuildTime(LocalDateTime.now());
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void deploy() {
|
|
|
- deployLog = appDeployer.deploy(app, buildLog);
|
|
|
+ private void pack(BuildLog buildLog) throws Exception {
|
|
|
+ String appEntryDir = appCompileDir + app.getCompileDir();
|
|
|
+ String appPath = codePacker.pack(app.getAppId(), lastCommitLog.getCommitId(), appEntryDir);
|
|
|
+ buildLog.setAppPath(appPath);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 构建部署日志
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2020-05-15 上午11:31
|
|
|
- */
|
|
|
- public BuildDeployLog log() {
|
|
|
- return new BuildDeployLog(buildLog, deployLog);
|
|
|
+ /* 部署 */
|
|
|
+ public DeployLog deploy(BuildLog buildLog) {
|
|
|
+ long totalTime = System.currentTimeMillis();
|
|
|
+ DeployLog deployLog = appDeployer.deploy(buildLog);
|
|
|
+ deployLog.setDeployTotalTime(System.currentTimeMillis() - totalTime);
|
|
|
+ log.info("{} 部署完成...", app.getAppId());
|
|
|
+ return deployLog;
|
|
|
}
|
|
|
|
|
|
@Data
|