|
|
@@ -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();
|
|
|
+ }
|
|
|
}
|