|
@@ -6,6 +6,7 @@ import com.usky.common.core.bean.ApiResult;
|
|
|
import com.usky.common.core.bean.CommonPage;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.util.List;
|
|
@@ -14,6 +15,16 @@ import java.util.List;
|
|
|
@RequestMapping("/api/update")
|
|
|
public class FileUpdateInfoController {
|
|
|
|
|
|
+ // 从配置文件中读取脚本名称
|
|
|
+ @Value("${file.linuxrestart}")
|
|
|
+ private String linuxContralScript;
|
|
|
+
|
|
|
+ @Value("${file.windowsstop}")
|
|
|
+ private String windowsStopScript;
|
|
|
+
|
|
|
+ @Value("${file.windowsstart}")
|
|
|
+ private String windowsStartScript;
|
|
|
+
|
|
|
@Autowired
|
|
|
private FileUpdateInfoService fileUpdateInfoService;
|
|
|
|
|
@@ -82,4 +93,52 @@ public class FileUpdateInfoController {
|
|
|
return ApiResult.error("删除文件信息失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @PostMapping("/controlById")
|
|
|
+ @ApiOperation(value = "控制服务", notes = "根据文件 ID 和操作类型控制服务(启动、停止、重启)")
|
|
|
+ public ApiResult<String> controlServiceById(@RequestParam Long id, @RequestParam String action) {
|
|
|
+ try {
|
|
|
+ FileUpdateInfo fileUpdateInfo = fileUpdateInfoService.getFileById(id);
|
|
|
+ if (fileUpdateInfo == null) {
|
|
|
+ return ApiResult.error("未找到对应的文件信息,ID: " + id);
|
|
|
+ }
|
|
|
+ String fileName = fileUpdateInfo.getFileName();
|
|
|
+ controlApplication(fileName, action);
|
|
|
+ return ApiResult.success("服务控制命令已发送: " + action);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return ApiResult.error("控制服务失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void controlApplication(String fileName, String action) throws Exception {
|
|
|
+ String osName = System.getProperty("os.name").toLowerCase();
|
|
|
+
|
|
|
+ if (osName.contains("linux") || osName.contains("unix")) {
|
|
|
+ // Linux 系统
|
|
|
+ String command = "sh " + linuxContralScript + " " + action + " " + fileName;
|
|
|
+ Runtime.getRuntime().exec(command);
|
|
|
+ } else if (osName.contains("windows")) {
|
|
|
+ // Windows 系统
|
|
|
+ String command;
|
|
|
+ switch (action.toLowerCase()) {
|
|
|
+ case "start":
|
|
|
+ command = windowsStartScript + " " + fileName;
|
|
|
+ break;
|
|
|
+ case "stop":
|
|
|
+ command = windowsStopScript + " " + fileName;
|
|
|
+ break;
|
|
|
+ case "restart":
|
|
|
+ // Windows 系统没有单独的重启脚本,需要先停止再启动
|
|
|
+ Runtime.getRuntime().exec(windowsStopScript + " " + fileName);
|
|
|
+ Thread.sleep(5000); // 等待 5 秒,确保服务停止
|
|
|
+ Runtime.getRuntime().exec(windowsStartScript + " " + fileName);
|
|
|
+ return;
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException("不支持的操作类型: " + action);
|
|
|
+ }
|
|
|
+ Runtime.getRuntime().exec(command);
|
|
|
+ } else {
|
|
|
+ throw new Exception("不支持的操作系统: " + osName);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|