fix: ASW-176 exec playbook 接口增加reInstall,clearCache,unInstall参数

This commit is contained in:
zhangshuai
2024-11-18 14:50:34 +08:00
parent 7eb847d7bf
commit 63f4e6ae4a
3 changed files with 138 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ package net.geedge.api.controller;
import cn.hutool.core.codec.Base32Codec;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.Log;
@@ -20,7 +21,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
@RestController
@RequestMapping("/api/v1/env")
@@ -264,7 +264,10 @@ public class APIController {
@PostMapping("/playbook")
public R execPlaybook(@RequestParam("file") MultipartFile file,
@RequestParam("packageName") String packageName,
@RequestParam("id") String id) {
@RequestParam("id") String id,
@RequestParam("reInstall") Boolean reInstall,
@RequestParam("clearCache") Boolean clearCache,
@RequestParam("unInstall") Boolean unInstall) {
File apkFile = null;
File playbookAirDir = null;
File destination = null;
@@ -307,7 +310,8 @@ public class APIController {
} finally {
T.FileUtil.del(destination);
}
PlaybookRunnable playbookRunnable = new PlaybookRunnable(apiYml, apkFile, playbookAirDir, id, packageName);
PlaybookRunnable playbookRunnable = new PlaybookRunnable(apiYml, apkFile, playbookAirDir, id, packageName, reInstall, clearCache, unInstall);
playbookRunnable.setName(T.StrUtil.concat(true, id, "-", apkFile.getName()));
ThreadUtil.execAsync(playbookRunnable);
return R.ok();
@@ -406,14 +410,20 @@ public class APIController {
private File apkFile;
private String packageName;
private File playbookDir;
private boolean reInstall;
private boolean clearCache;
private boolean unInstall;
private boolean interrupt;
public PlaybookRunnable(EnvApiYml envApiYml, File apkFile, File playbookDir, String tid, String packageName) {
public PlaybookRunnable(EnvApiYml envApiYml, File apkFile, File playbookDir, String tid, String packageName, Boolean reInstall, Boolean clearCache, Boolean unInstall) {
this.envApiYml = envApiYml;
this.tid = tid;
this.apkFile = apkFile;
this.packageName = packageName;
this.playbookDir = playbookDir;
this.reInstall = reInstall;
this.clearCache = clearCache;
this.unInstall = unInstall;
this.interrupt = false;
}
@@ -425,31 +435,57 @@ public class APIController {
AdbUtil.CommandResult tcpdumpPackage = null;
AdbUtil.CommandResult tcpdumpAll = null;
try {
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();
T.FileUtil.writeString(T.JSONUtil.toJsonStr(resultMap), statusFile, "UTF-8");
// install apk
if (interrupt) return;
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());
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));
// Check if the package is installed
if (interrupt) return;
boolean packageIsInstall = adbUtil.findPackageInstall(packageName);
if (packageIsInstall){
if (!reInstall){
// install apk
if (interrupt) return;
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());
}
}
}else {
// install apk
if (interrupt) return;
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());
}
}
//Close other apps
if (interrupt) return;
List<String> packageNameList = adbUtil.findPackageNameList();
this.closeApp(packageNameList, packageName);
// clear app data
if (interrupt) return;
AdbUtil.CommandResult clearData = adbUtil.clearAppData(packageName);
if (0 != clearData.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Clear %s data error: exit code %s \n", packageName, install.exitCode()), logFile, "UTF-8");
throw new APIException(clearData.output());
if (clearCache){
AdbUtil.CommandResult clearData = adbUtil.clearAppData(packageName);
if (0 != clearData.exitCode()) {
T.FileUtil.appendString(String.format("ERROR: Clear %s data error: exit code %s \n", packageName, clearData.exitCode()), logFile, "UTF-8");
throw new APIException(clearData.output());
}
}
// Launch the app
if (interrupt) return;
adbUtil.startApp(packageName);
// star tcpdump: package name
if (interrupt) return;
tcpdumpPackage = adbUtil.startTcpdump(packageName);
@@ -501,12 +537,24 @@ public class APIController {
AdbUtil.CommandResult allTcpdump = adbUtil.stopTcpdump(tcpdumpAll.output());
adbUtil.execShellCommand(String.format("shell rm -rf %s", allTcpdump.output()));
}
adbUtil.stopApp(packageName);
T.FileUtil.appendString(String.format("Job succeeded"), logFile, "UTF-8");
this.closeApp(ListUtil.empty(), packageName);
if (unInstall) {
adbUtil.uninstall(packageName);
}
T.FileUtil.appendString(String.format("Job execution ends"), logFile, "UTF-8");
ACTIVE_TASKS.remove(this);
}
}
private void closeApp(List<String> packageNameList, String packageName) {
if (CollUtil.isNotEmpty(packageNameList)){
for (String name : packageNameList) {
adbUtil.stopApp(name);
}
}
adbUtil.stopApp(packageName);
}
@Override
public void interrupt() {
super.interrupt();