|
|
@@ -2,7 +2,6 @@ package cn.reghao.devops.manager.app.service.config.impl;
|
|
|
|
|
|
import cn.reghao.devops.common.machine.Disk;
|
|
|
import cn.reghao.devops.common.machine.Machine;
|
|
|
-import cn.reghao.devops.manager.app.db.repository.config.build.BuildDirRepository;
|
|
|
import cn.reghao.devops.manager.app.model.po.config.build.BuildDir;
|
|
|
import cn.reghao.devops.common.build.model.LocalBuildDir;
|
|
|
import cn.reghao.devops.manager.app.service.config.BuildDirService;
|
|
|
@@ -18,7 +17,6 @@ import oshi.SystemInfo;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -28,44 +26,35 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class BuildDirServiceImpl implements BuildDirService {
|
|
|
// TODO 将 machineId 设置为一个常量
|
|
|
- private final BuildDirRepository buildDirRepository;
|
|
|
+ private static BuildDir buildDir;
|
|
|
+ private static DiskDetail diskDetail;
|
|
|
private final ByteConverter converter;
|
|
|
private final Disk disk;
|
|
|
|
|
|
- public BuildDirServiceImpl(SystemInfo si, BuildDirRepository buildDirRepository) {
|
|
|
+ public BuildDirServiceImpl(SystemInfo si) {
|
|
|
this.disk = new Disk(si);
|
|
|
- this.buildDirRepository = buildDirRepository;
|
|
|
this.converter = new ByteConverter();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void createAndSave() {
|
|
|
- try {
|
|
|
- BuildDir buildDir = defaultLocalBuildDir();
|
|
|
- buildDirRepository.save(buildDir);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("创建本地构建目录失败: {},devops-manager 结束运行", e.getMessage());
|
|
|
- System.exit(1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private BuildDir defaultLocalBuildDir() throws Exception {
|
|
|
- String home = System.getProperty("user.home");
|
|
|
- String localBuildDir = String.format("%s/.devops", home);
|
|
|
- return createLocalBuildDir(localBuildDir);
|
|
|
+ public void init(String dirPath) throws Exception {
|
|
|
+ buildDir = createLocalBuildDir(dirPath);
|
|
|
}
|
|
|
|
|
|
private BuildDir createLocalBuildDir(String localBuildDir) throws Exception {
|
|
|
- DiskDetail diskDetail = disk.diskDetail(localBuildDir);
|
|
|
+ diskDetail = disk.diskDetail(localBuildDir);
|
|
|
String machineId = Machine.ID;
|
|
|
String machineIpv4 = Machine.IPV4;
|
|
|
String mountedOn = diskDetail.getMountedOn();
|
|
|
- String total = converter.convert(ByteType.Bytes, diskDetail.getTotal());
|
|
|
- String used = converter.convertStr(ByteType.Bytes, ByteType.MiB, diskDetail.getUsed());
|
|
|
- String avail = converter.convertStr(ByteType.Bytes, ByteType.MiB, diskDetail.getAvail());
|
|
|
-
|
|
|
- BuildDir buildDir = new BuildDir(machineId, machineIpv4, localBuildDir, mountedOn, total, used, avail);
|
|
|
+ long total = diskDetail.getTotal();
|
|
|
+ String totalStr = converter.convert(ByteType.Bytes, total);
|
|
|
+ long used = diskDetail.getUsed();
|
|
|
+ String usedStr = converter.convertStr(ByteType.Bytes, ByteType.MiB, used);
|
|
|
+ long avail = diskDetail.getAvail();
|
|
|
+ String availStr = converter.convertStr(ByteType.Bytes, ByteType.MiB, avail);
|
|
|
+
|
|
|
+ BuildDir buildDir = new BuildDir(machineId, machineIpv4, localBuildDir, mountedOn,
|
|
|
+ total, totalStr, used, usedStr, avail, availStr);
|
|
|
createLocalBuildDir(buildDir);
|
|
|
return buildDir;
|
|
|
}
|
|
|
@@ -89,83 +78,39 @@ public class BuildDirServiceImpl implements BuildDirService {
|
|
|
}
|
|
|
|
|
|
private void setLocalBuildDir(BuildDir buildDir) {
|
|
|
- LocalBuildDir.localRepo = buildDir.getDirPath() + "/local-repo";
|
|
|
- LocalBuildDir.compileDir = buildDir.getDirPath() + "/compile-dir";
|
|
|
- LocalBuildDir.packDir = buildDir.getDirPath() + "/pack-dir";
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void refresh(BuildDir buildDir) {
|
|
|
- setLocalBuildDir(buildDir);
|
|
|
String dirPath = buildDir.getDirPath();
|
|
|
- DiskDetail diskDetail = disk.diskDetail(dirPath);
|
|
|
- if (diskDetail != null) {
|
|
|
- buildDir.setUsed(converter.convertStr(ByteType.Bytes, ByteType.MiB, diskDetail.getUsed()));
|
|
|
- buildDir.setAvail(converter.convertStr(ByteType.Bytes, ByteType.MiB, diskDetail.getAvail()));
|
|
|
- } else {
|
|
|
- log.error("本地目录 {} 不合法,devops-manager 结束运行", dirPath);
|
|
|
- System.exit(1);
|
|
|
- }
|
|
|
+ LocalBuildDir.localRepo = String.format("%s/local-repo", dirPath);
|
|
|
+ LocalBuildDir.compileDir = String.format("%s/compile-dir", dirPath);
|
|
|
+ LocalBuildDir.packDir = String.format("%s/pack-dir", dirPath);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public synchronized Result modify(BuildDir buildDir) {
|
|
|
- if (buildDir.getId() == null) {
|
|
|
- return Result.result(ResultStatus.FAIL, "构建目录不存在");
|
|
|
- }
|
|
|
-
|
|
|
- BuildDir oldBuildDir = buildDirRepository.findByMachineId(buildDir.getMachineId());
|
|
|
- String oldDirPath = oldBuildDir.getDirPath();
|
|
|
- String oldMountedOn = oldBuildDir.getMountedOn();
|
|
|
- String newDirPath = buildDir.getDirPath();
|
|
|
- String newMountedOn = disk.diskDetail(newDirPath).getMountedOn();
|
|
|
-
|
|
|
- if (newDirPath.equals(oldDirPath)) {
|
|
|
- return Result.result(ResultStatus.FAIL, "目录没有改变");
|
|
|
- } else if (new File(newDirPath).exists()) {
|
|
|
- return Result.result(ResultStatus.FAIL, "新目录已存在,请另外指定一个不存在的目录");
|
|
|
- } else if (newMountedOn.equals(oldMountedOn)) {
|
|
|
- return Result.result(ResultStatus.FAIL, "新目录和当前目录同属一个分区");
|
|
|
- }
|
|
|
-
|
|
|
- String oldPackDir = LocalBuildDir.packDir;
|
|
|
- // TODO 目录迁移时不能有应用处于构建过程中
|
|
|
- try {
|
|
|
- BuildDir newBuildDir = createLocalBuildDir(newDirPath);
|
|
|
- File file = new File(oldPackDir);
|
|
|
- if (Objects.requireNonNull(file.list()).length != 0) {
|
|
|
- try {
|
|
|
- copyDirContentToDir(oldPackDir, LocalBuildDir.packDir);
|
|
|
- } catch (IOException e) {
|
|
|
- FileUtils.deleteQuietly(new File(newDirPath));
|
|
|
- return Result.result(ResultStatus.ERROR, e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- buildDirRepository.save(newBuildDir);
|
|
|
- return Result.result(ResultStatus.SUCCESS);
|
|
|
- } catch (Exception e) {
|
|
|
- return Result.result(ResultStatus.FAIL, e.getMessage());
|
|
|
- }
|
|
|
+ public synchronized Result erase() {
|
|
|
+ // 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);
|
|
|
}
|
|
|
|
|
|
- // TODO 和 BuilderUtil 中的代码重复
|
|
|
- private void copyDirContentToDir(String src, String dst) throws IOException {
|
|
|
- File srcDir = new File(src);
|
|
|
- File dstDir = new File(dst);
|
|
|
- for (File file : Objects.requireNonNull(srcDir.listFiles())) {
|
|
|
- if (file.isDirectory()) {
|
|
|
- String dirname = file.getName();
|
|
|
- File dstdir = new File(dst + File.separator + dirname);
|
|
|
- FileUtils.copyDirectory(file, dstdir);
|
|
|
- } else {
|
|
|
- FileUtils.copyFileToDirectory(file, dstDir);
|
|
|
- }
|
|
|
+ private void erase(String dirPath) {
|
|
|
+ try {
|
|
|
+ File dir = new File(dirPath);
|
|
|
+ FileUtils.cleanDirectory(dir);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public BuildDir get(String machineId) {
|
|
|
- return buildDirRepository.findByMachineId(machineId);
|
|
|
+ public BuildDir get() {
|
|
|
+ buildDir.setUsed(diskDetail.getUsed());
|
|
|
+ buildDir.setAvail(diskDetail.getAvail());
|
|
|
+ buildDir.setUsedStr(converter.convert(ByteType.Bytes, diskDetail.getUsed()));
|
|
|
+ buildDir.setAvailStr(converter.convert(ByteType.Bytes, diskDetail.getAvail()));
|
|
|
+ return buildDir;
|
|
|
}
|
|
|
}
|