Skip to content

Commit

Permalink
重构app目录
Browse files Browse the repository at this point in the history
  • Loading branch information
cgw committed Sep 19, 2023
1 parent a0371c9 commit 5ec41ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -801,21 +801,16 @@ private void buildNodejsImage(DeploymentContext context) {
if(!TechTypeEnum.NODEJS.getCode().equals(context.getApp().getTechType())) {
return;
}
File branchFile = new File(context.getLocalPathOfBranch());
File targetFile = new File(branchFile.getParent() + "/" + context.getApp().getAppName());
rename(branchFile, targetFile);
File targetFile = new File(context.getLocalPathOfBranch());
doBuildImage(context, context.getApp().getBaseImage(), null, Arrays.asList(targetFile.toPath()));
}

private void buildNuxtImage(DeploymentContext context) {
if(!TechTypeEnum.NUXT.getCode().equals(context.getApp().getTechType())) {
return;
}

AppExtendNuxt appExted = context.getApp().getAppExtend();
File branchFile = new File(context.getLocalPathOfBranch());
File targetFile = new File(branchFile.getParent() + "/" + context.getApp().getAppName());
rename(branchFile, targetFile);
File targetFile = new File(context.getLocalPathOfBranch());
String baseImage = Constants.NODE_IMAGE_BASE_URL + appExted.getNodeVersion().substring(1);
doBuildImage(context, baseImage, null, Arrays.asList(targetFile.toPath()));
}
Expand All @@ -824,9 +819,7 @@ private void buildHtmlImage(DeploymentContext context) {
if(!TechTypeEnum.HTML.getCode().equals(context.getApp().getTechType())) {
return;
}
File branchFile = new File(context.getLocalPathOfBranch());
File targetFile = new File(branchFile.getParent() + "/" + context.getApp().getAppName());
rename(branchFile, targetFile);
File targetFile = new File(context.getLocalPathOfBranch());
doBuildImage(context, context.getApp().getBaseImage(), null, Arrays.asList(targetFile.toPath()));
}

Expand Down Expand Up @@ -868,34 +861,12 @@ private void buildDjangoImage(DeploymentContext context) {

private void buildPythonImage(DeploymentContext context, List<String> entrypoint) {
App app = context.getApp();
File branchFile = new File(context.getLocalPathOfBranch());
File targetFile = new File(branchFile.getParent() + "/" + app.getAppName());
rename(branchFile, targetFile);
File targetFile = new File(context.getLocalPathOfBranch());
String version = ((AppExtendPython)app.getAppExtend()).getPythonVersion();
String baseImage = Constants.PYTHON_IMAGE_BASE_URL + version.substring(1);
doBuildImage(context, baseImage, entrypoint, Arrays.asList(targetFile.toPath()));
}

private boolean rename(File source, File target) {
boolean success = false;
//如果30秒还没成功,则报错
for(int i = 0; i < 300; i++) {
if(source.renameTo(target)) {
success = true;
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
//ignore
}
}
if(!success) {
LogUtils.throwException(logger, MessageCodeEnum.RENAME_FILE_FAILURE);
}
return success;
}

private void doBuildImage(DeploymentContext context, String baseImageName, List<String> entrypoint, List<Path> targetFiles) {
ImageRepo imageRepo = context.getGlobalConfigAgg().getImageRepo();
String imageUrl = imageRepo.getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.List;

import org.dhorse.api.enums.MessageCodeEnum;
import org.dhorse.api.response.PageData;
import org.dhorse.api.response.model.AppBranch;
import org.dhorse.api.response.model.AppTag;
Expand All @@ -13,17 +14,19 @@
import org.dhorse.infrastructure.utils.DeploymentContext;
import org.dhorse.infrastructure.utils.FileUtils;
import org.dhorse.infrastructure.utils.K8sUtils;
import org.dhorse.infrastructure.utils.LogUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class CodeRepoStrategy {

public static final Logger logger = LoggerFactory.getLogger(CodeRepoStrategy.class);

public void clearHistoryBranch(DeploymentContext context) {
void clearLocalHistoryCode(DeploymentContext context) {
String localPathOfBranch = localPathOfBranch(context);
File[] hisBranchs = new File(localPathOfBranch(context)).listFiles();
for(File h : hisBranchs) {
//为了提高Node类应用install过程的性能,不删除安装文件
//为了提高Node类应用install过程的性能,不删除node_modules文件
if("node_modules".equals(h.getName())) {
continue;
}
Expand All @@ -34,14 +37,16 @@ public void clearHistoryBranch(DeploymentContext context) {
h.delete();
}
} catch (IOException e) {
logger.error("Failed to clear local history branch", e);
logger.error("Failed to clear local history code, please delete it"
+ " manually, path is: " + localPathOfBranch, e);
LogUtils.throwException(logger, MessageCodeEnum.DELETE_FAILURE);
}
}
}

public boolean downloadBranch(DeploymentContext context) {
checkLocalPathOfBranch(context);
clearHistoryBranch(context);
clearLocalHistoryCode(context);
return doDownloadBranch(context);
}

Expand Down
2 changes: 1 addition & 1 deletion dhorse-rest/src/main/resources/dhorse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#====================================================================================================
#如果启用,则优先使用Mysql
#mysql.enable: true
#需要手动创建dhorse库,DHorse在启动时会自动创建表
#需要手动创建dhorse库,dhorse在启动时会自动创建表
#mysql.url: jdbc:mysql://localhost:3306/dhorse?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true
#该账号需要具有建表权限
#mysql.user: username
Expand Down

0 comments on commit 5ec41ae

Please sign in to comment.