瀏覽代碼

添加数据备份和还原模块

reghao 5 年之前
父節點
當前提交
5ca8bf3b2d

+ 1 - 1
common/src/main/java/cn/reghao/autodop/common/notification/DingNotify.java

@@ -18,7 +18,7 @@ public class DingNotify implements Notify {
         DingText dingText = new DingText(content);
         WebResponse webResponse = webRequest.postJson(destination, JsonUtil.objectToJson(dingText));
         if (webResponse.getStatusCode() == 200) {
-            throw new Exception("发送通知失败");
+            throw new Exception("通知发送失败...");
         }
     }
 }

+ 74 - 4
common/src/main/java/cn/reghao/autodop/common/shell/ShellExecutor.java

@@ -1,29 +1,58 @@
 package cn.reghao.autodop.common.shell;
 
-import lombok.extern.slf4j.Slf4j;
+import cn.reghao.autodop.common.utils.text.TextFile;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
- * TODO: 改造 shell 执行器,提升执行效率
- *
  * @author reghao
  * @date 2019-08-20 23:45:06
  */
-@Slf4j
 public class ShellExecutor {
+    @Deprecated
     private InputStreamReader outstream;
+    @Deprecated
     private InputStreamReader errstream;
 
+    /**
+     * @param scriptPath shell 脚本绝对路径
+     * @param args 日志文件以 .log 为后缀,且必须为数组的第一个元素(若存在日志文件)
+     * @return
+     * @date 2020-11-09 下午7:22
+     */
+    public ShellResult execute(String scriptPath, String... args) throws Exception {
+        List<String> commands = new ArrayList<>();
+        commands.add("sh");
+        commands.add(scriptPath);
+        commands.addAll(Arrays.asList(args));
+        ProcessBuilder pb = new ProcessBuilder(commands);
+        ShellResult shellResult = exec(pb);
+        if (args.length != 0 && args[0].endsWith(".log")) {
+            // 读取日志文件的内容
+            TextFile textFile = new TextFile();
+            if (shellResult.hasError()) {
+                shellResult.setStderr(textFile.readFile(args[0]));
+            } else {
+                shellResult.setStdout(textFile.readFile(args[0]));
+            }
+        }
+
+        return shellResult;
+    }
+
+    @Deprecated
     public ShellResult execute(String script) {
         String dir = System.getProperty("user.home");
         return execute0(dir, script);
     }
 
+    @Deprecated
     public ShellResult execute(String dir, String script) {
         if (dir == null) {
             return null;
@@ -33,6 +62,7 @@ public class ShellExecutor {
     }
 
     // TODO: 输出流有大量数据时程序会阻塞
+    @Deprecated
     private ShellResult execute0(String dir, String script) {
         String cmd = "cd " + dir + " && ";
         cmd += script;
@@ -75,6 +105,7 @@ public class ShellExecutor {
         return result;
     }
 
+    @Deprecated
     static class ShellOutput extends Thread {
         private BufferedReader in;
         private StringBuilder output = new StringBuilder();
@@ -110,4 +141,43 @@ public class ShellExecutor {
             }
         }
     }
+
+    private ShellResult exec(ProcessBuilder pb) throws Exception {
+        Process p = pb.start();
+        // 等待子进程结束
+        int exitCode = p.waitFor();
+        ShellResult shellResult = new ShellResult();
+        shellResult.setExitCode(exitCode);
+        if (exitCode == 0) {
+            shellResult.setStdout(output(p.getInputStream()));
+        } else {
+            shellResult.setStderr(output(p.getErrorStream()));
+        }
+        return shellResult;
+    }
+
+    private String output(InputStream in) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+        StringBuilder out = new StringBuilder();
+        String line;
+        while ((line = reader.readLine()) != null) {
+            out.append(line).append(System.lineSeparator());
+        }
+        reader.close();
+
+        return out.toString();
+    }
+
+    public static void main(String[] args) throws Exception {
+        String script = "/home/reghao/scripts/cron/cron_rsync.sh";
+        script = "/home/reghao/work/azy/svn/test/4-运维管理/构建部署/自动化脚本/build_app.sh";
+        String[] arr = new String[2];
+        //arr[0] = "/tmp/content1.log";
+        arr[0] = "content";
+        arr[1] = "/home/reghao/opt/data/autodop/compile-dir/content/ContentService/4-ContentPresentation/Com.IQuizoo.ContentService";
+
+        ShellExecutor shellExecutor = new ShellExecutor();
+        ShellResult shellResult = shellExecutor.execute(script, arr);
+        System.out.println();
+    }
 }

+ 7 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/ShellCompiler.java

@@ -26,6 +26,13 @@ public class ShellCompiler implements CodeCompiler {
 
     @Override
     public void compile(String appName, String appDir, String env) throws Exception {
+        String logfile = System.getProperty("java.io.tmpdir") + "/" + appName;
+        String[] arr = new String[3];
+        arr[0] = logfile;
+        arr[1] = appName;
+        arr[2] = appDir;
+        //ShellResult result1 = shell.execute(script, arr);
+
         ShellResult result = shell.execute(appDir, script);
         if (result.hasError()) {
             Map<String, String> map = new HashMap<>();

+ 33 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/backup/BackupController.java

@@ -0,0 +1,33 @@
+package cn.reghao.autodop.dmaster.backup;
+
+import cn.reghao.autodop.common.result.WebResult;
+import io.swagger.annotations.Api;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author reghao
+ * @date 2019-11-15 08:44:50
+ */
+@Api(tags = "备份接口")
+@RestController
+@RequestMapping("/api/backup")
+public class BackupController {
+    private BackupService backupService;
+
+    public BackupController(BackupService backupService) {
+        this.backupService = backupService;
+    }
+
+    @PostMapping("/service")
+    public String addService() {
+        return WebResult.success("ok");
+    }
+
+    @GetMapping("/service")
+    public String serviceList() {
+        return WebResult.success("ok");
+    }
+}

+ 11 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/backup/BackupService.java

@@ -0,0 +1,11 @@
+package cn.reghao.autodop.dmaster.backup;
+
+import org.springframework.stereotype.Service;
+
+/**
+ * @author reghao
+ * @date 2020-10-22 17:51:56
+ */
+@Service
+public class BackupService {
+}