fix: 调整 playbook 执行时serial 参数获取

This commit is contained in:
zhangshuai
2024-10-31 17:02:52 +08:00
parent 1d6b710f63
commit 97adfa39cd
2 changed files with 220 additions and 126 deletions

View File

@@ -12,10 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -62,7 +61,7 @@ public class APIController {
File tempFile = T.FileUtil.file(Constant.TEMP_PATH, fileName);
try {
AdbUtil.CommandResult result = adbUtil.pull(filePath, tempFile.getAbsolutePath());
AdbUtil.CommandResult result = adbUtil.pull(filePath, tempFile.getAbsolutePath(), false);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -105,7 +104,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);
AdbUtil.CommandResult result = adbUtil.install(tempFile.getAbsolutePath(), true, true, false);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -116,7 +115,7 @@ public class APIController {
}
if (T.StrUtil.isNotEmpty(path)) {
AdbUtil.CommandResult result = adbUtil.install(path, true, true);
AdbUtil.CommandResult result = adbUtil.install(path, true, true, false);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -141,7 +140,7 @@ public class APIController {
@PostMapping("/pcap")
public R startTcpdump(@RequestParam(required = false, defaultValue = "") String packageName) {
AdbUtil.CommandResult result = adbUtil.startTcpdump(packageName);
AdbUtil.CommandResult result = adbUtil.startTcpdump(packageName, false);
if (0 != result.exitCode()) {
throw new APIException("exec tcpdump error");
}
@@ -150,9 +149,9 @@ public class APIController {
@DeleteMapping("/pcap")
public synchronized void stopTcpdump(@RequestParam String id,
@RequestParam(required = false, defaultValue = "false") Boolean returnFile,
HttpServletResponse response) throws IOException {
AdbUtil.CommandResult result = adbUtil.stopTcpdump(id);
@RequestParam(required = false, defaultValue = "false") Boolean returnFile,
HttpServletResponse response) throws IOException {
AdbUtil.CommandResult result = adbUtil.stopTcpdump(id, false);
if (0 != result.exitCode()) {
throw new APIException(result.output());
}
@@ -166,7 +165,7 @@ public class APIController {
if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS);
}
AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath());
AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath(), false);
if (0 != pulled.exitCode()) {
throw new APIException(pulled.output());
}
@@ -181,7 +180,7 @@ public class APIController {
} finally {
if (T.StrUtil.isNotEmpty(filePath)) {
// remove pcap file
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath));
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath), false);
}
}
}
@@ -254,53 +253,112 @@ public class APIController {
}
@PostMapping("/playbook")
public R execPlaybook(@RequestParam("files") MultipartFile[] files, @RequestParam("packageName") String packageName) throws IOException {
// save zip and apk
String tid = T.StrUtil.uuid();
File appFile = null;
public R execPlaybook(@RequestParam("files") MultipartFile file,
@RequestParam("packageName") String packageName,
@RequestParam("id") String id) {
File apkFile = null;
File playbookAirDir = null;
for (MultipartFile file : files) {
if (T.FileUtil.extName(file.getOriginalFilename()).equals("zip")) {
File playbookFile = T.FileUtil.file(Constant.TEMP_PATH, tid, file.getOriginalFilename());
T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), playbookFile);
File playbookDir = T.FileUtil.file(Constant.TEMP_PATH, tid);
T.ZipUtil.unzip(playbookFile, playbookDir);
playbookAirDir = Arrays.stream(playbookDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".air");
}
})).findFirst().get();
} else {
appFile = T.FileUtil.file(Constant.TEMP_PATH, tid, file.getOriginalFilename());
T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), appFile);
}
File destination = null;
try {
File playbookDir = T.FileUtil.file(Constant.TEMP_PATH, id);
destination = T.FileUtil.file(Constant.TEMP_PATH, id, file.getName());
T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), destination);
// unzip file
T.ZipUtil.unzip(destination, playbookDir);
// apk
apkFile = Arrays.stream(playbookDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".apk");
}
})).findFirst().get();
// playbook zip
File playbook = Arrays.stream(playbookDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".zip") && !name.equals(file.getName());
}
})).findFirst().get();
// unzip playbook zip
T.ZipUtil.unzip(playbook, playbookDir);
playbookAirDir = Arrays.stream(playbookDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".air");
}
})).findFirst().get();
} catch (Exception e) {
log.error(e.getMessage());
Map resultMap = T.MapUtil.builder()
.put("status", "error")
.build();
Constant.PLAYBOOK_RUN_RESULT.put(id, resultMap);
return R.ok();
} finally {
T.FileUtil.del(destination);
}
PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, appFile, playbookAirDir, tid, packageName);
PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, apkFile, playbookAirDir, id, packageName);
ThreadUtil.execAsync(playbookRunnable);
return R.ok().putData("tid", tid);
return R.ok();
}
@GetMapping("/playbook/{id}")
public void getExecPlaybookResult(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
public R checkJobResult(@PathVariable("id") String id){
if (T.StrUtil.isEmpty(id)) {
throw new APIException(RCode.BAD_REQUEST);
}
Map result = Constant.PLAYBOOK_RUN_RESULT.get(id);
if (result != null) {
String status = T.MapUtil.getStr(result, "status");
if (T.MapUtil.getStr(result, "status").equals("done")) {
String artifact = T.MapUtil.getStr(result, "artifact");
File pcapFile = T.FileUtil.file(artifact);
Constant.PLAYBOOK_RUN_RESULT.remove(id);
T.ResponseUtil.downloadFile(response, pcapFile.getName(), T.FileUtil.readBytes(pcapFile));
} else if (status.equals("error")) {
Constant.PLAYBOOK_RUN_RESULT.remove(id);
response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result)));
} else {
response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result)));
}
return R.ok().putData(result);
}
@GetMapping("/playbook/{id}/log")
public R getJobResultLog(@PathVariable("id") String id,
@RequestParam("offset") Integer offset){
if (T.StrUtil.isEmpty(id)) {
throw new APIException(RCode.BAD_REQUEST);
}
// log file
File logFile = T.FileUtil.file(Constant.TEMP_PATH, id, "result.log");
HashMap<Object, Object> result = T.MapUtil.newHashMap(false);
try (RandomAccessFile raf = new RandomAccessFile(logFile, "r")) {
raf.seek(offset);
byte[] bytes = new byte[(int)raf.length() - offset];
raf.readFully(bytes);
String content = new String(bytes);
result.put("content", content);
result.put("length", bytes.length);
result.put("offset", offset + bytes.length);
} catch (IOException e) {
log.error("getJobResultLog error", e);
throw new APIException(RCode.ERROR);
}
return R.ok().putData(result);
}
@GetMapping("/playbook/{id}/artifact")
public void getJobResultArtifact(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
if (T.StrUtil.isEmpty(id)) {
throw new APIException(RCode.BAD_REQUEST);
}
// 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[] files = jobResult.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".log") || name.endsWith(".pcap");
}
});
T.ZipUtil.zip(zipFile, true, files);
T.ResponseUtil.downloadFile(response,zipFile.getName(), T.FileUtil.readBytes(zipFile));
}
public class PlaybookRunnable extends Thread {
@@ -321,37 +379,47 @@ public class APIController {
@Override
public void run() {
File logFile = FileUtil.file(Constant.TEMP_PATH, tid, "result.log");
try {
adbUtil.setLogFile(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);
AdbUtil.CommandResult install = adbUtil.install(apkFile.getAbsolutePath(), true, 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);
AdbUtil.CommandResult startTcpdump = adbUtil.startTcpdump(packageName, true);
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());
T.FileUtil.writeString(execResult.output(), FileUtil.file(Constant.TEMP_PATH, tid, "log", "log.txt"), "UTF-8");
AdbUtil.CommandResult execResult = adbUtil.execPlaybook(playbookDir.getPath(), true);
if (0 != execResult.exitCode()) {
// exec playbook error, stop tcpdump and delete pcap
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output());
adbUtil.execShellCommand(String.format("shell rm -rf %s", stopTcpdump.output()));
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);
throw new APIException("exec playbook error");
}
// 4. stop tcpdump
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output());
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output(), true);
T.FileUtil.appendString(T.StrUtil.concat(true, stopTcpdump.output(), "\n"), logFile, "UTF-8");
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());
}
@@ -361,29 +429,30 @@ public class APIController {
if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS);
}
AdbUtil.CommandResult pull = adbUtil.pull(filePath, localPcapFile.getAbsolutePath());
AdbUtil.CommandResult pull = adbUtil.pull(filePath, localPcapFile.getAbsolutePath(), true);
T.FileUtil.appendString(T.StrUtil.concat(true, pull.output(), "\n"), logFile, "UTF-8");
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));
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath), true);
resultMap = T.MapUtil.builder()
.put("status", "done")
.put("artifact", localPcapFile.getAbsolutePath())
.build();
Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap);
}catch (Exception e) {
} catch (Exception e) {
log.error(e);
Map resultMap = T.MapUtil.builder()
.put("status", "error")
.build();
Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap);
} finally {
adbUtil.stopApp(packageName);
//T.FileUtil.del(apkFile);
//T.FileUtil.clean(playbookDir);
adbUtil.stopApp(packageName, true);
T.FileUtil.appendString(String.format("Job succeeded"), logFile, "UTF-8");
}
}
}