|
@@ -2,19 +2,19 @@ package cn.reghao.devops.mgr.ops.build.service.impl;
|
|
|
|
|
|
|
|
import cn.reghao.devops.common.util.Disk;
|
|
import cn.reghao.devops.common.util.Disk;
|
|
|
import cn.reghao.devops.common.util.DiskDetail;
|
|
import cn.reghao.devops.common.util.DiskDetail;
|
|
|
-import cn.reghao.devops.common.util.Machine;
|
|
|
|
|
|
|
+import cn.reghao.devops.mgr.config.AppProperties;
|
|
|
import cn.reghao.devops.mgr.ops.build.model.po.BuildDir;
|
|
import cn.reghao.devops.mgr.ops.build.model.po.BuildDir;
|
|
|
import cn.reghao.devops.mgr.ops.build.service.BuildDirService;
|
|
import cn.reghao.devops.mgr.ops.build.service.BuildDirService;
|
|
|
import cn.reghao.devops.mgr.ops.builder.model.LocalBuildDir;
|
|
import cn.reghao.devops.mgr.ops.builder.model.LocalBuildDir;
|
|
|
import cn.reghao.jutil.jdk.converter.ByteConverter;
|
|
import cn.reghao.jutil.jdk.converter.ByteConverter;
|
|
|
-import cn.reghao.jutil.jdk.web.result.Result;
|
|
|
|
|
-import cn.reghao.jutil.jdk.web.result.ResultStatus;
|
|
|
|
|
|
|
+import jakarta.annotation.PostConstruct;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
@@ -23,104 +23,94 @@ import java.io.IOException;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
public class BuildDirServiceImpl implements BuildDirService {
|
|
public class BuildDirServiceImpl implements BuildDirService {
|
|
|
- // TODO 将 machineId 设置为一个常量
|
|
|
|
|
- private static BuildDir buildDir;
|
|
|
|
|
- private static DiskDetail diskDetail;
|
|
|
|
|
|
|
+ private final String opsDir;
|
|
|
|
|
+ private final BuildDir buildDir;
|
|
|
private final ByteConverter byteConverter;
|
|
private final ByteConverter byteConverter;
|
|
|
private final Disk disk;
|
|
private final Disk disk;
|
|
|
|
|
|
|
|
- public BuildDirServiceImpl(Disk disk, ByteConverter byteConverter) {
|
|
|
|
|
|
|
+ public BuildDirServiceImpl(AppProperties appProperties, Disk disk, ByteConverter byteConverter) {
|
|
|
|
|
+ this.opsDir = appProperties.getOpsDir();
|
|
|
|
|
+ this.buildDir = new BuildDir();
|
|
|
this.disk = disk;
|
|
this.disk = disk;
|
|
|
this.byteConverter = byteConverter;
|
|
this.byteConverter = byteConverter;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @PostConstruct
|
|
|
@Override
|
|
@Override
|
|
|
- public void init(String dirPath) throws Exception {
|
|
|
|
|
- buildDir = createLocalBuildDir(dirPath);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private BuildDir createLocalBuildDir(String localBuildDir) throws Exception {
|
|
|
|
|
- diskDetail = disk.diskDetail(localBuildDir);
|
|
|
|
|
- String machineId = Machine.ID;
|
|
|
|
|
- String machineIpv4 = Machine.IPV4;
|
|
|
|
|
- String mountedOn = diskDetail.getMountedOn();
|
|
|
|
|
- long total = diskDetail.getTotal();
|
|
|
|
|
- String totalStr = byteConverter.convert(total);
|
|
|
|
|
- long used = diskDetail.getUsed();
|
|
|
|
|
- String usedStr = byteConverter.convert(used);
|
|
|
|
|
- long avail = diskDetail.getAvail();
|
|
|
|
|
- String availStr = byteConverter.convert(avail);
|
|
|
|
|
-
|
|
|
|
|
- BuildDir buildDir = new BuildDir(machineId, machineIpv4, localBuildDir, mountedOn,
|
|
|
|
|
- total, totalStr, used, usedStr, avail, availStr);
|
|
|
|
|
- createLocalBuildDir(buildDir);
|
|
|
|
|
- return buildDir;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void createLocalBuildDir(BuildDir buildDir) throws Exception {
|
|
|
|
|
- setLocalBuildDir(buildDir);
|
|
|
|
|
- File localRepo = new File(LocalBuildDir.localRepo);
|
|
|
|
|
- if (!localRepo.exists() && !localRepo.mkdirs()) {
|
|
|
|
|
- throw new Exception(localRepo.getAbsolutePath() + " 不存在且创建失败");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- File compileDir = new File(LocalBuildDir.compileDir);
|
|
|
|
|
- if (!compileDir.exists() && !compileDir.mkdirs()) {
|
|
|
|
|
- throw new Exception(compileDir.getAbsolutePath() + " 不存在且创建失败");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- File packDir = new File(LocalBuildDir.packDir);
|
|
|
|
|
- if (!packDir.exists() && !packDir.mkdirs()) {
|
|
|
|
|
- throw new Exception(packDir.getAbsolutePath() + " 不存在且创建失败");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- File cacheDir = new File(LocalBuildDir.cacheDir);
|
|
|
|
|
- if (!cacheDir.exists() && !cacheDir.mkdirs()) {
|
|
|
|
|
- throw new Exception(cacheDir.getAbsolutePath() + " 不存在且创建失败");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- File logDir = new File(LocalBuildDir.logDir);
|
|
|
|
|
- if (!logDir.exists() && !logDir.mkdirs()) {
|
|
|
|
|
- throw new Exception(logDir.getAbsolutePath() + " 不存在且创建失败");
|
|
|
|
|
|
|
+ public void init() {
|
|
|
|
|
+ File dir = new File(opsDir);
|
|
|
|
|
+ if (!dir.exists() && !dir.mkdirs()) {
|
|
|
|
|
+ log.error("创建本地构建目录 {} 失败,devops-mgr 结束运行", opsDir);
|
|
|
|
|
+ System.exit(1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ try {
|
|
|
|
|
+ createLocalBuildDir(opsDir);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("初始化本地构建目录失败,devops-mgr 结束运行: {}", e.getMessage());
|
|
|
|
|
+ System.exit(1);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void setLocalBuildDir(BuildDir buildDir) {
|
|
|
|
|
- String dirPath = buildDir.getDirPath();
|
|
|
|
|
- LocalBuildDir.localRepo = String.format("%s/local-repo", dirPath);
|
|
|
|
|
- LocalBuildDir.compileDir = String.format("%s/compile-dir", dirPath);
|
|
|
|
|
- LocalBuildDir.packDir = String.format("%s/pack-dir", dirPath);
|
|
|
|
|
- LocalBuildDir.cacheDir = String.format("%s/cache", dirPath);
|
|
|
|
|
- LocalBuildDir.logDir = String.format("%s/log", dirPath);
|
|
|
|
|
|
|
+ private void createLocalBuildDir(String opsDir) throws Exception {
|
|
|
|
|
+ LocalBuildDir.localRepo = String.format("%s/local-repo", opsDir);
|
|
|
|
|
+ LocalBuildDir.compileDir = String.format("%s/compile-dir", opsDir);
|
|
|
|
|
+ LocalBuildDir.packDir = String.format("%s/pack-dir", opsDir);
|
|
|
|
|
+ LocalBuildDir.cacheDir = String.format("%s/cache", opsDir);
|
|
|
|
|
+ LocalBuildDir.logDir = String.format("%s/log", opsDir);
|
|
|
|
|
+ List<String> dirPathList = List.of(
|
|
|
|
|
+ LocalBuildDir.localRepo,
|
|
|
|
|
+ LocalBuildDir.compileDir,
|
|
|
|
|
+ LocalBuildDir.packDir,
|
|
|
|
|
+ LocalBuildDir.cacheDir,
|
|
|
|
|
+ LocalBuildDir.logDir
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ for (String dirPath : dirPathList) {
|
|
|
|
|
+ File dir = new File(dirPath);
|
|
|
|
|
+ if (!dir.exists() && !dir.mkdirs()) {
|
|
|
|
|
+ throw new Exception(dir.getAbsolutePath() + " 目录不存在且创建失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public synchronized Result erase() {
|
|
|
|
|
|
|
+ public synchronized void erase() {
|
|
|
// TODO 保证没有 app 处在构建状态
|
|
// TODO 保证没有 app 处在构建状态
|
|
|
- String localRepo = LocalBuildDir.localRepo;
|
|
|
|
|
- erase(localRepo);
|
|
|
|
|
- String compileDir = LocalBuildDir.compileDir;
|
|
|
|
|
- erase(compileDir);
|
|
|
|
|
- String packDir = LocalBuildDir.packDir;
|
|
|
|
|
- erase(packDir);
|
|
|
|
|
- return Result.result(ResultStatus.SUCCESS);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ List<String> dirPathList = List.of(
|
|
|
|
|
+ LocalBuildDir.localRepo,
|
|
|
|
|
+ LocalBuildDir.compileDir,
|
|
|
|
|
+ LocalBuildDir.packDir
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
- private void erase(String dirPath) {
|
|
|
|
|
try {
|
|
try {
|
|
|
- File dir = new File(dirPath);
|
|
|
|
|
- FileUtils.cleanDirectory(dir);
|
|
|
|
|
|
|
+ for (String dirPath : dirPathList) {
|
|
|
|
|
+ File dir = new File(dirPath);
|
|
|
|
|
+ FileUtils.cleanDirectory(dir);
|
|
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ log.error("{}", e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public BuildDir get() {
|
|
|
|
|
- buildDir.setUsed(diskDetail.getUsed());
|
|
|
|
|
- buildDir.setAvail(diskDetail.getAvail());
|
|
|
|
|
- buildDir.setUsedStr(byteConverter.convert(diskDetail.getUsed()));
|
|
|
|
|
- buildDir.setAvailStr(byteConverter.convert(diskDetail.getAvail()));
|
|
|
|
|
|
|
+ public BuildDir getBuildDir() {
|
|
|
|
|
+ DiskDetail diskDetail = disk.diskDetail(opsDir);
|
|
|
|
|
+ String mountedOn = diskDetail.getMountedOn();
|
|
|
|
|
+ long total = diskDetail.getTotal();
|
|
|
|
|
+ String totalStr = byteConverter.convert(total);
|
|
|
|
|
+ long used = diskDetail.getUsed();
|
|
|
|
|
+ String usedStr = byteConverter.convert(used);
|
|
|
|
|
+ long avail = diskDetail.getAvail();
|
|
|
|
|
+ String availStr = byteConverter.convert(avail);
|
|
|
|
|
+
|
|
|
|
|
+ buildDir.setMountedOn(mountedOn);
|
|
|
|
|
+ buildDir.setTotal(total);
|
|
|
|
|
+ buildDir.setTotalStr(totalStr);
|
|
|
|
|
+ buildDir.setUsed(used);
|
|
|
|
|
+ buildDir.setUsedStr(usedStr);
|
|
|
|
|
+ buildDir.setAvail(avail);
|
|
|
|
|
+ buildDir.setAvailStr(availStr);
|
|
|
return buildDir;
|
|
return buildDir;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|