Jelajahi Sumber

更新 BuildDirService, 获取构建目录所在磁盘分区的当前容量

reghao 4 hari lalu
induk
melakukan
5c449e2e51

+ 3 - 4
mgr/src/main/java/cn/reghao/devops/mgr/ops/build/controller/BuildDirController.java

@@ -4,7 +4,6 @@ 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.util.DefaultSetting;
 import cn.reghao.devops.mgr.util.SelectOption;
-import cn.reghao.jutil.jdk.web.result.Result;
 import cn.reghao.jutil.web.WebResult;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
@@ -37,7 +36,7 @@ public class BuildDirController {
     @Operation(summary = "构建目录页面", description = "N")
     @GetMapping(value = "/build/dir", produces = MediaType.APPLICATION_JSON_VALUE)
     public String buildDirPage() {
-        BuildDir buildDir = buildDirService.get();
+        BuildDir buildDir = buildDirService.getBuildDir();
         List<BuildDir> list = List.of(buildDir);
         return WebResult.success(list);
     }
@@ -45,8 +44,8 @@ public class BuildDirController {
     @Operation(summary = "清空构建目录", description = "N")
     @PostMapping(value = "/build/dir/erase", produces = MediaType.APPLICATION_JSON_VALUE)
     public String emptyBuildDir() {
-        Result result = buildDirService.erase();
-        return WebResult.result(result);
+        buildDirService.erase();
+        return WebResult.success();
     }
 
     @Operation(summary = "环境和应用类型列表", description = "N")

+ 3 - 3
mgr/src/main/java/cn/reghao/devops/mgr/ops/build/service/BuildDirService.java

@@ -8,7 +8,7 @@ import cn.reghao.jutil.jdk.web.result.Result;
  * @date 2021-09-17 15:56:49
  */
 public interface BuildDirService {
-    void init(String dirPath) throws Exception;
-    Result erase();
-    BuildDir get();
+    void init();
+    void erase();
+    BuildDir getBuildDir();
 }

+ 69 - 79
mgr/src/main/java/cn/reghao/devops/mgr/ops/build/service/impl/BuildDirServiceImpl.java

@@ -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.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.service.BuildDirService;
 import cn.reghao.devops.mgr.ops.builder.model.LocalBuildDir;
 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 org.apache.commons.io.FileUtils;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.List;
 
 /**
  * @author reghao
@@ -23,104 +23,94 @@ import java.io.IOException;
 @Slf4j
 @Service
 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 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.byteConverter = byteConverter;
     }
 
+    @PostConstruct
     @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
-    public synchronized Result erase() {
+    public synchronized void 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);
-    }
+        List<String> dirPathList = List.of(
+                LocalBuildDir.localRepo,
+                LocalBuildDir.compileDir,
+                LocalBuildDir.packDir
+        );
 
-    private void erase(String dirPath) {
         try {
-            File dir = new File(dirPath);
-            FileUtils.cleanDirectory(dir);
+            for (String dirPath : dirPathList) {
+                File dir = new File(dirPath);
+                FileUtils.cleanDirectory(dir);
+            }
         } catch (IOException e) {
-            e.printStackTrace();
+            log.error("{}", e.getMessage());
         }
     }
 
     @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;
     }
 }