Przeglądaj źródła

将 shell 工具类移到 jdkutil 包

reghao 4 lat temu
rodzic
commit
691fa600bf

+ 0 - 76
common/src/main/java/cn/reghao/autodop/common/util/shell/ShellExecutor.java

@@ -1,76 +0,0 @@
-package cn.reghao.autodop.common.util.shell;
-
-import java.io.*;
-import java.util.UUID;
-
-/**
- * @author reghao
- * @date 2019-08-20 23:45:06
- */
-public class ShellExecutor {
-    private ProcessBuilder pb;
-
-    public ShellExecutor() {
-        this.pb = new ProcessBuilder();
-    }
-
-    /**
-     * 执行 shell 命令/脚本
-     *
-     * @param cmd shell 命令或脚本
-     * @param dir 工作目录
-     * @return
-     * @date 2020-11-09 下午11:22
-     */
-    public ShellResult exec(String cmd, String dir) {
-        String[] cmdarray = cmd.split("\\s+");
-        String output = System.getProperty("java.io.tmpdir") + "/" + UUID.randomUUID().toString() + ".out";
-        File ofile = new File(output);
-
-        ShellResult shellResult;
-        try {
-            ofile.createNewFile();
-            pb.command(cmdarray)
-                    // 将标准错误合并到标准输出
-                    .redirectErrorStream(true)
-                    // 将所有输出重定向到文件
-                    .redirectOutput(ofile);
-            if (dir != null) {
-                pb.directory(new File(dir));
-            }
-
-            shellResult = exec(pb, ofile);
-            ofile.delete();
-        } catch (IOException | InterruptedException e) {
-            shellResult = new ShellResult(1);
-            shellResult.setOutput(e.getMessage());
-        }
-
-        return shellResult;
-    }
-
-    private ShellResult exec(ProcessBuilder pb, File ofile) throws IOException, InterruptedException {
-        Process newProcess = pb.start();
-        ProcessHandle handle = newProcess.toHandle();
-        // 子进程 PID
-        long pid = handle.pid();
-
-        // 父进程等待子进程结束
-        int exitCode = newProcess.waitFor();
-        ShellResult shellResult = new ShellResult(exitCode);
-        shellResult.setExitCode(exitCode);
-        shellResult.setOutput(output(ofile));
-        return shellResult;
-    }
-
-    private String output(File ofile) throws IOException {
-        StringBuilder sb = new StringBuilder();
-        BufferedReader in =  new BufferedReader(new InputStreamReader(new FileInputStream(ofile)));
-        String line;
-        while ((line = in.readLine()) != null) {
-            sb.append(line).append(System.lineSeparator());
-        }
-        in.close();
-        return sb.toString();
-    }
-}

+ 0 - 38
common/src/main/java/cn/reghao/autodop/common/util/shell/ShellResult.java

@@ -1,38 +0,0 @@
-package cn.reghao.autodop.common.util.shell;
-
-/**
- * @author reghao
- * @date 2019-08-24 14:47:57
- */
-public class ShellResult {
-    private int exitCode;
-    // 包含 stdout 和/或 stderr
-    private String output;
-
-    public ShellResult(int exitCode) {
-        this.exitCode = exitCode;
-    }
-
-    /**
-     * 是否成功执行
-     *
-     * @param
-     * @return
-     * @date 2021-05-22 上午11:11
-     */
-    public boolean isSuccess() {
-        return exitCode == 0;
-    }
-
-    public void setExitCode(int exitCode) {
-        this.exitCode = exitCode;
-    }
-
-    public String getOutput() {
-        return output;
-    }
-
-    public void setOutput(String output) {
-        this.output = output;
-    }
-}

+ 6 - 6
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/util/buildtool/compiler/ShellCompiler.java

@@ -1,7 +1,7 @@
 package cn.reghao.autodop.dmaster.app.util.buildtool.compiler;
 
-import cn.reghao.autodop.common.util.shell.ShellExecutor;
-import cn.reghao.autodop.common.util.shell.ShellResult;
+import cn.reghao.jdkutil.shell.ShellExecutor;
+import cn.reghao.jdkutil.shell.ShellResult;
 import cn.reghao.jdkutil.serializer.JsonConverter;
 import cn.reghao.autodop.dmaster.app.model.po.config.build.CompilerConfig;
 
@@ -13,18 +13,18 @@ import cn.reghao.autodop.dmaster.app.model.po.config.build.CompilerConfig;
  */
 public class ShellCompiler implements CodeCompiler {
     private final ShellExecutor shell;
-    private final String compileCmd;
+    private final String[] compileCmd;
 
     public ShellCompiler(CompilerConfig compilerConfig) {
-        this.compileCmd = compilerConfig.getCompileCmd();
+        this.compileCmd = compilerConfig.getCompileCmd().split("\\s+");
         this.shell = new ShellExecutor();
     }
 
     @Override
     public void compile(String appId, String appCompileHome) throws Exception {
-        ShellResult result = shell.exec(compileCmd, appCompileHome);
+        ShellResult result = shell.exec(appCompileHome, compileCmd);
         if (!result.isSuccess()) {
-            throw new Exception(JsonConverter.objectToJson(result.getOutput()));
+            throw new Exception(JsonConverter.objectToJson(result.getResult()));
         }
     }
 }

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/util/ssh/ws/WebSocketHandlerImpl.java

@@ -13,7 +13,7 @@ import org.springframework.web.socket.*;
 @Slf4j
 @Component
 public class WebSocketHandlerImpl implements WebSocketHandler {
-    private WebSsh webSsh;
+    private final WebSsh webSsh;
 
     public WebSocketHandlerImpl(WebSsh webSsh) {
         this.webSsh = webSsh;

+ 2 - 27
scripts/test.json

@@ -1,29 +1,4 @@
 {
-  "appId": "dnkt",
-  "env": "dev",
-  "description": "大脑课堂",
-  "alwaysBuild": true,
-  "enable": true,
-  "appType": "npm",
-  "appRepo": "git@codeup.aliyun.com:5f1f8daf6207a1a8b17f6742/FrontEnd/IQuizoo.Game.git",
-  "branch": "develop",
-  "dirname": "IQuizoo.Game",
-  "compileHome": "/IQuizoo.Game",
-  "buildConfigVO": {
-    "repo": "git-codeup",
-    "compiler": "none",
-    "packer": "docker-dev",
-    "dockerfile": "FROM debian10/nginx\n\nRUN sed -i 's/8080/8081/' /etc/nginx/conf.d/http.conf\nCOPY ./dist-test /opt/webroot/\n"
-  },
-  "deployConfigs": [
-    {
-      "machineId": "5d1a727991f34d3a9c1220a1899e6ebd",
-      "packerType": "docker",
-      "dockerConfig": "{\n  \"env\":[\n    \"appName=dnkt\",\n    \"ASPNETCORE_ENVIRONMENT=Production\",\n    \"ASPNETCORE_URLS=http://172.16.45.67:8002;https://172.16.45.67:8102\"\n  ],\n  \"hostConfig\":{\n    \"networkMode\":\"host\",\n    \"restartPolicy\":{\n      \"name\":\"on-failure\",\n      \"maximumRetryCount\":5\n    }\n  }\n}"
-    }
-  ],
-  "runningConfig": {
-    "logs": []
-  },
-  "notifier": "ding-dev"
+  "pid": "9097",
+  "startScript": "/root/start.sh"
 }

+ 0 - 25
scripts/test1.json

@@ -1,25 +0,0 @@
-{
-  "appId": "dnkt-bplus",
-  "env": "test",
-  "description": "大脑课堂",
-  "alwaysBuild": true,
-  "enable": true,
-  "appType": "npm",
-  "appRepo": "git@codeup.aliyun.com:5f1f8daf6207a1a8b17f6742/FrontEnd/IQuizoo.Game.git",
-  "branch": "bplus",
-  "dirname": "IQuizoo.Game",
-  "compileHome": "/",
-  "dockerfile": "FROM debian10/nginx\n\nRUN sed -i 's/8080/8081/' /etc/nginx/conf.d/http.conf\nCOPY ./dist-test /opt/webroot/\n",
-  "buildConfig": {
-    "repo": "git-codeup",
-    "compiler": "none",
-    "packer": "docker-test"
-  },
-  "deployConfigs": [],
-  "runningConfig": {
-    "httpPort": 8081,
-    "healthCheck": "/",
-    "logConfigs": []
-  },
-  "notifier": "ding-dev"
-}