fix: 调整 playbook 执行日志记录

This commit is contained in:
zhangshuai
2024-10-31 18:07:04 +08:00
parent 97adfa39cd
commit ad63b414bf
4 changed files with 127 additions and 122 deletions

View File

@@ -7,6 +7,7 @@ import cn.hutool.log.Log;
import jakarta.servlet.http.HttpServletResponse;
import net.geedge.api.entity.EnvApiYml;
import net.geedge.api.util.AdbUtil;
import net.geedge.api.util.CommandExec;
import net.geedge.common.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -28,9 +29,12 @@ public class APIController {
@Autowired
public APIController(EnvApiYml envApiYml) {
this.adbUtil = AdbUtil.getInstance(envApiYml.getAdb());
this.adbUtil = AdbUtil.getInstance(envApiYml.getAdb(), new CommandExec(null));
}
@Autowired
private EnvApiYml apiYml;
@GetMapping("/status")
public R status() {
return R.ok(adbUtil.status());
@@ -61,7 +65,7 @@ public class APIController {
File tempFile = T.FileUtil.file(Constant.TEMP_PATH, fileName);
try {
AdbUtil.CommandResult result = adbUtil.pull(filePath, tempFile.getAbsolutePath(), false);
AdbUtil.CommandResult result = adbUtil.pull(filePath, tempFile.getAbsolutePath());
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -104,7 +108,7 @@ public class APIController {
tempFile = T.FileUtil.file(Constant.TEMP_PATH, file.getOriginalFilename());
file.transferTo(tempFile);
AdbUtil.CommandResult result = adbUtil.install(tempFile.getAbsolutePath(), true, true, false);
AdbUtil.CommandResult result = adbUtil.install(tempFile.getAbsolutePath(), true, true);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -115,7 +119,7 @@ public class APIController {
}
if (T.StrUtil.isNotEmpty(path)) {
AdbUtil.CommandResult result = adbUtil.install(path, true, true, false);
AdbUtil.CommandResult result = adbUtil.install(path, true, true);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -140,7 +144,7 @@ public class APIController {
@PostMapping("/pcap")
public R startTcpdump(@RequestParam(required = false, defaultValue = "") String packageName) {
AdbUtil.CommandResult result = adbUtil.startTcpdump(packageName, false);
AdbUtil.CommandResult result = adbUtil.startTcpdump(packageName);
if (0 != result.exitCode()) {
throw new APIException("exec tcpdump error");
}
@@ -151,7 +155,7 @@ public class APIController {
public synchronized void stopTcpdump(@RequestParam String id,
@RequestParam(required = false, defaultValue = "false") Boolean returnFile,
HttpServletResponse response) throws IOException {
AdbUtil.CommandResult result = adbUtil.stopTcpdump(id, false);
AdbUtil.CommandResult result = adbUtil.stopTcpdump(id);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -165,7 +169,7 @@ public class APIController {
if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS);
}
AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath(), false);
AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath());
if (0 != pulled.exitCode()) {
throw new APIException(pulled.output());
}
@@ -180,7 +184,7 @@ public class APIController {
} finally {
if (T.StrUtil.isNotEmpty(filePath)) {
// remove pcap file
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath), false);
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath));
}
}
}
@@ -302,7 +306,7 @@ public class APIController {
} finally {
T.FileUtil.del(destination);
}
PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, apkFile, playbookAirDir, id, packageName);
PlaybookRunnable playbookRunnable = new PlaybookRunnable(apiYml, apkFile, playbookAirDir, id, packageName);
ThreadUtil.execAsync(playbookRunnable);
return R.ok();
}
@@ -349,7 +353,7 @@ public class APIController {
}
// job dir
File jobResult = T.FileUtil.file(Constant.TEMP_PATH, id);
File zipFile = T.FileUtil.file(Constant.TEMP_PATH, T.StrUtil.concat(true, id, ".zip"));
File zipFile = T.FileUtil.file(Constant.TEMP_PATH, id, T.StrUtil.concat(true, id, ".zip"));
File[] files = jobResult.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
@@ -364,13 +368,14 @@ public class APIController {
public class PlaybookRunnable extends Thread {
private AdbUtil adbUtil;
private EnvApiYml envApiYml;
private String tid;
private File apkFile;
private String packageName;
private File playbookDir;
public PlaybookRunnable(AdbUtil adbUtil, File apkFile, File playbookDir, String tid, String packageName) {
this.adbUtil = adbUtil;
public PlaybookRunnable(EnvApiYml envApiYml, File apkFile, File playbookDir, String tid, String packageName) {
this.envApiYml = envApiYml;
this.tid = tid;
this.apkFile = apkFile;
this.packageName = packageName;
@@ -382,42 +387,40 @@ public class APIController {
File logFile = FileUtil.file(Constant.TEMP_PATH, tid, "result.log");
try {
adbUtil.setLogFile(logFile);
T.FileUtil.appendString(String.format("Running with %s:%s Android Simulator \n", envApiYml.getAdb().getHost(), envApiYml.getAdb().getPort()), logFile, "UTF-8");
adbUtil = new AdbUtil(envApiYml.getAdb(), new CommandExec(logFile));
Map resultMap = T.MapUtil.builder()
.put("status", "running")
.build();
Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap);
T.FileUtil.appendString(String.format("Running with %s Android Simulator \n", adbUtil.getSerial()), logFile, "UTF-8");
// 1. install apk
AdbUtil.CommandResult install = adbUtil.install(apkFile.getAbsolutePath(), true, true, true);
AdbUtil.CommandResult install = adbUtil.install(apkFile.getAbsolutePath(), true, true);
if (0 != install.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Install apk failed: exit code %s \n", install.exitCode()), logFile, "UTF-8");
throw new APIException(install.output());
}
// 2. star tcpdump
AdbUtil.CommandResult startTcpdump = adbUtil.startTcpdump(packageName, true);
AdbUtil.CommandResult startTcpdump = adbUtil.startTcpdump(packageName);
if (0 != startTcpdump.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Start tcpdump failed: exit code %s \n", startTcpdump.exitCode()), logFile, "UTF-8");
throw new APIException("exec tcpdump error");
}
// 3. exec playbook
AdbUtil.CommandResult execResult = adbUtil.execPlaybook(playbookDir.getPath(), true);
AdbUtil.CommandResult execResult = adbUtil.execPlaybook(playbookDir.getPath(), logFile);
if (0 != execResult.exitCode()) {
// exec playbook error, stop tcpdump and delete pcap
T.FileUtil.appendString(String.format("ERROR: Exec playbook failed: exit code %s \n", execResult.exitCode()), logFile, "UTF-8");
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output(), true);
adbUtil.execShellCommand(String.format("shell rm -rf %s", stopTcpdump.output()), true);
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output());
adbUtil.execShellCommand(String.format("shell rm -rf %s", stopTcpdump.output()));
throw new APIException("exec playbook error");
}
// 4. stop tcpdump
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output(), true);
T.FileUtil.appendString(T.StrUtil.concat(true, stopTcpdump.output(), "\n"), logFile, "UTF-8");
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output());
if (0 != stopTcpdump.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Stop tcpdump failed: exit code %s \n", stopTcpdump.exitCode()), logFile, "UTF-8");
throw new APIException(stopTcpdump.output());
@@ -430,15 +433,14 @@ public class APIController {
throw new APIException(RCode.NOT_EXISTS);
}
AdbUtil.CommandResult pull = adbUtil.pull(filePath, localPcapFile.getAbsolutePath(), true);
T.FileUtil.appendString(T.StrUtil.concat(true, pull.output(), "\n"), logFile, "UTF-8");
AdbUtil.CommandResult pull = adbUtil.pull(filePath, localPcapFile.getAbsolutePath());
if (0 != pull.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Pull pcap file failed: exit code %s \n", pull.exitCode()), logFile, "UTF-8");
throw new APIException(pull.output());
}
// 6. delete android pcap
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath), true);
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath));
resultMap = T.MapUtil.builder()
.put("status", "done")
@@ -451,7 +453,7 @@ public class APIController {
.build();
Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap);
} finally {
adbUtil.stopApp(packageName, true);
adbUtil.stopApp(packageName);
T.FileUtil.appendString(String.format("Job succeeded"), logFile, "UTF-8");
}
}