瀏覽代碼

后台管理首页:查看扩展程序状态,重启/启动按钮,调用脚本管理程序

jyyw 1 年之前
父節點
當前提交
e8adbe72d0

+ 9 - 11
ipfcst-common/ipfcst-common-data/src/main/java/com/jiayue/ipfcst/common/data/dto/ProcessResult.java

@@ -1,23 +1,21 @@
 package com.jiayue.ipfcst.common.data.dto;
 
 import lombok.Getter;
+import lombok.Setter;
 
 import java.util.List;
 
 @Getter
+@Setter
 public class ProcessResult {
 
+    //输入
+    //public List<String> cmds;
+    //public Map<String, String> envs;
+    //public File workspace;
 
-  public List<String> output;
-
-  int exitCode;
-
-  public void setExitCode(int exitCode) {
-    this.exitCode = exitCode;
-  }
-
-  public void setOutput(List<String> output) {
-    this.output = output;
-  }
+    //输出
+    public List<String> output;
+    Integer exitCode;
 
 }

+ 95 - 0
ipfcst-common/ipfcst-common-data/src/main/java/com/jiayue/ipfcst/common/data/util/ShellUtil.java

@@ -0,0 +1,95 @@
+package com.jiayue.ipfcst.common.data.util;
+
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.RuntimeUtil;
+import cn.hutool.core.util.StrUtil;
+import com.jiayue.ipfcst.common.data.dto.ProcessResult;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+public class ShellUtil {
+
+    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+
+    /**
+     * 执行sh命令
+     *
+     * @param cmds
+     * @return
+     */
+    public static ProcessResult runRuntime(List<String> cmds) {
+        log.info("开始执行");
+        ProcessResult processResult = new ProcessResult();
+        if (null == cmds || cmds.isEmpty()) {
+            processResult.setExitCode(1);
+            log.error("命令行为空!");
+        } else {
+            List<String> result = RuntimeUtil.execForLines(StandardCharsets.UTF_8, ArrayUtil.toArray(cmds, String.class));
+            processResult.setOutput(result);
+            processResult.setExitCode(0);
+            result.forEach(log::info);
+        }
+        log.info("执行结束");
+        return processResult;
+    }
+
+    /**
+     * 执行sh命令,可配置环境变量及工作路径
+     *
+     * @param cmds
+     * @param envs
+     * @param workspace
+     * @return
+     */
+    public static ProcessResult runProcess(List<String> cmds, Map<String, String> envs, File workspace) {
+        log.info("开始执行");
+        Process process = null;
+        InputStream inputStream = null;
+        ProcessResult result = new ProcessResult();
+        List<String> message;
+        try {
+            ProcessBuilder processBuilder = new ProcessBuilder();
+            processBuilder.command(cmds);
+            //命令工作空间
+            if (null != workspace && workspace.exists() && workspace.isDirectory()) {
+                processBuilder.directory(workspace);
+            }
+            //环境变量
+            if (null != envs) {
+                Map<String, String> enviroment = processBuilder.environment();
+                for (String key : envs.keySet()) {
+                    if (enviroment.containsKey(key)) {
+                        if (!StrUtil.equals(enviroment.get(key), envs.get(key))) {
+                            enviroment.put(key, envs.get(key));
+                        }
+                    } else {
+                        enviroment.put(key, envs.get(key));
+                    }
+                }
+            }
+            log.info(String.join(" ", processBuilder.command()));
+            //执行命令
+            process = processBuilder.start();
+            inputStream = process.getInputStream();
+            message = IoUtil.readUtf8Lines(inputStream, new ArrayList<>());
+            result.setOutput(message);
+            result.setExitCode(0);
+        } catch (Exception ex) {
+            result.setExitCode(1);
+        } finally {
+            IoUtil.closeIfPosible(inputStream);
+            RuntimeUtil.destroy(process);
+            log.info("执行结束");
+        }
+        return result;
+    }
+}

+ 58 - 4
ipfcst-console/src/main/frontend/views/dashboard/index.vue

@@ -2,10 +2,18 @@
   <div id="tunnelinfo">
     <div class="crumbs">
       <el-breadcrumb separator="/">
-        <el-breadcrumb-item><h3><i class="el-icon-tickets"/>&nbsp;综合通道信息
-          <el-button size="mini" type="primary" round @click="check" style="width: 150px;margin-left: 100px" :loading="btnLonding">查看网卡信息</el-button>
-          <el-button size="mini" type="primary" round @click="check1" style="width: 150px;margin-left: 100px" :loading="btnLonding">查看通道对端ip状态</el-button>
-          <el-button size="mini" type="primary" round @click="check2" style="width: 150px;margin-left: 100px" :loading="btnLonding">查看通道本机端口状态</el-button>
+        <el-breadcrumb-item>
+        <h3>
+        <i class="el-icon-tickets"/>&nbsp;综合通道信息
+          <el-button size="mini" type="primary" round style="width: 150px;margin-left: 40px" :loading="btnLonding" @click="check">查看网卡信息</el-button>
+          <el-button size="mini" type="primary" round style="width: 150px;margin-left: 40px" :loading="btnLonding" @click="check1">查看通道对端ip状态</el-button>
+          <el-button size="mini" type="primary" round style="width: 150px;margin-left: 40px" :loading="btnLonding" @click="check2">查看通道本机端口状态</el-button>
+          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spare2程序:
+          <el-button size="mini" type="primary" round style="width: 60px;margin-left: 10px" :loading="btnLonding" @click="check3">状态</el-button>
+          <el-button size="mini" type="primary" round style="width: 60px;margin-left: 10px" :loading="btnLonding" @click="check4">重启</el-button>
+          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;upload程序:
+          <el-button size="mini" type="primary" round style="width: 60px;margin-left: 10px" :loading="btnLonding" @click="check5">状态</el-button>
+          <el-button size="mini" type="primary" round style="width: 60px;margin-left: 10px" :loading="btnLonding" @click="check6">重启</el-button>
         </h3>
         </el-breadcrumb-item>
       </el-breadcrumb>
@@ -1084,6 +1092,52 @@ export default {
       // this.ping2()
       // this.pingData.push("完毕!!!")
     },
+        //
+        check3() {
+          this.visible2 = true
+          this.lsofData = ['查看程序运行状态', '------------------']
+          this.$axios.get('/exteranls/spare2/status').then(res => {
+            if (res.data != null) {
+              for (let j = 0; j < res.data.length; j++) {
+                this.lsofData.push(res.data[j])
+              }
+            }
+          })
+        },
+        check4() {
+          this.visible2 = true
+          this.lsofData = ['重启程序', '------------------']
+          this.$axios.get('/exteranls/spare2/restart').then(res => {
+            if (res.data != null) {
+              for (let j = 0; j < res.data.length; j++) {
+                this.lsofData.push(res.data[j])
+              }
+            }
+          })
+        },
+        //
+        check5() {
+          this.visible2 = true
+          this.lsofData = ['查看程序运行状态', '------------------']
+          this.$axios.get('/exteranls/upload/status').then(res => {
+            if (res.data != null) {
+              for (let j = 0; j < res.data.length; j++) {
+                this.lsofData.push(res.data[j])
+              }
+            }
+          })
+        },
+        check6() {
+          this.visible2 = true
+          this.lsofData = ['重启程序', '------------------']
+          this.$axios.get('/exteranls/upload/restart').then(res => {
+            if (res.data != null) {
+              for (let j = 0; j < res.data.length; j++) {
+                this.lsofData.push(res.data[j])
+              }
+            }
+          })
+        },
     lsof1(i){
       if (this.tableData.length>i){
         if(this.tableData[i].selfPort!=undefined&&this.tableData[i].selfPort!=null){

+ 76 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/console/controller/ExteranlApplicationsController.java

@@ -0,0 +1,76 @@
+package com.jiayue.ipfcst.console.controller;
+
+import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
+import com.jiayue.ipfcst.common.data.dto.ProcessResult;
+import com.jiayue.ipfcst.console.service.ExteranlApplicationsService;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/exteranls")
+@Slf4j
+public class ExteranlApplicationsController {
+
+  @Autowired
+  ExteranlApplicationsService exteranlApplicationsService;
+
+  @SneakyThrows
+  @GetMapping(value = "/spare2/status")
+  public ResponseVO spare2Status() {
+    ProcessResult processResult = exteranlApplicationsService.status("spare2");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/spare2/restart")
+  public ResponseVO spare2Restart() {
+    ProcessResult processResult = exteranlApplicationsService.restart("spare2");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/spare2/start")
+  public ResponseVO spare2Start() {
+    ProcessResult processResult = exteranlApplicationsService.start("spare2");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/spare2/stop")
+  public ResponseVO spare2Stop() {
+    ProcessResult processResult = exteranlApplicationsService.stop("spare2");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/upload/status")
+  public ResponseVO uploadStatus() {
+    ProcessResult processResult = exteranlApplicationsService.status("upload");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/upload/restart")
+  public ResponseVO uploadRestart() {
+    ProcessResult processResult = exteranlApplicationsService.restart("upload");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/upload/start")
+  public ResponseVO uploadStart() {
+    ProcessResult processResult = exteranlApplicationsService.start("upload");
+    return ResponseVO.success(processResult.output);
+  }
+
+  @SneakyThrows
+  @GetMapping(value = "/upload/stop")
+  public ResponseVO uploadStop() {
+    ProcessResult processResult = exteranlApplicationsService.stop("upload");
+    return ResponseVO.success(processResult.output);
+  }
+}

+ 90 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/console/service/ExteranlApplicationsService.java

@@ -0,0 +1,90 @@
+package com.jiayue.ipfcst.console.service;
+
+import cn.hutool.core.collection.CollUtil;
+import com.jiayue.ipfcst.common.data.dto.ProcessResult;
+import com.jiayue.ipfcst.common.data.util.ShellUtil;
+import com.jiayue.ipfcst.fileupload.util.FileUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class ExteranlApplicationsService {
+
+  public ProcessResult status(String iden) {
+    String script = FileUtil.getBinPath() + File.separator + "status-" + iden + ".sh";
+    File file = new File(script);
+    ProcessResult processResult = new ProcessResult();
+    if (file.exists() && file.isFile()) {
+      processResult = runScript(script);
+    } else {
+      processResult.setExitCode(1);
+      processResult.output = CollUtil.newArrayList("文件" + file.getName() + "不存在!");
+    }
+    return processResult;
+  }
+
+  public ProcessResult restart(String iden) {
+    String script = FileUtil.getBinPath() + File.separator + "restart-" + iden + ".sh";
+    File file = new File(script);
+    ProcessResult processResult = new ProcessResult();
+    if (file.exists() && file.isFile()) {
+      processResult = runScript(script);
+    } else {
+      processResult.setExitCode(1);
+      processResult.output = CollUtil.newArrayList("文件" + file.getName() + "不存在!");
+    }
+    return processResult;
+  }
+
+  public ProcessResult start(String iden) {
+    String script = FileUtil.getBinPath() + File.separator + "start-" + iden + ".sh";
+    File file = new File(script);
+    ProcessResult processResult = new ProcessResult();
+    if (file.exists() && file.isFile()) {
+      processResult = runScript(script);
+    } else {
+      processResult.setExitCode(1);
+      processResult.output = CollUtil.newArrayList("文件" + file.getName() + "不存在!");
+    }
+    return processResult;
+  }
+
+  public ProcessResult stop(String iden) {
+    String script = FileUtil.getBinPath() + File.separator + "stop-" + iden + ".sh";
+    File file = new File(script);
+    ProcessResult processResult = new ProcessResult();
+    if (file.exists() && file.isFile()) {
+      processResult = runScript(script);
+    } else {
+      processResult.setExitCode(1);
+      processResult.output = CollUtil.newArrayList("文件" + file.getName() + "不存在!");
+    }
+    return processResult;
+  }
+
+  private ProcessResult runScript(String script) {
+    List<String> cmds = new ArrayList<>();
+    cmds.add("sh");
+    cmds.add("-c");
+    cmds.add(script);
+    return ShellUtil.runRuntime(cmds);
+  }
+
+  private ProcessResult getPid(String iden) {
+    List<String> cmds = new ArrayList<>();
+    Map<String, String> envs = new HashMap<>();
+    envs.put("LC_ALL", "zh_CN.UTF-8");
+    envs.put("LANG", "zh_CN.UTF-8");
+    cmds.add("sh");
+    cmds.add("-c");
+    cmds.add("pgrep -fl " + iden);
+    return ShellUtil.runProcess(cmds, envs, new File(FileUtil.getBinPath()));
+  }
+}

+ 1 - 2
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/console/job/UploadAppUpJob.java

@@ -1,7 +1,7 @@
 package com.jiayue.ipfcst.spare2.console.job;
 
 import cn.hutool.core.collection.CollectionUtil;
-import com.jiayue.ipfcst.common.data.entity.ProcessResult;
+import com.jiayue.ipfcst.common.data.dto.ProcessResult;
 import com.jiayue.ipfcst.spare2.util.FileUtil;
 import com.jiayue.ipfcst.spare2.util.ShellUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -34,7 +34,6 @@ public class UploadAppUpJob {
         log.info(CollectionUtil.join(processResult.output, " "));
     }
 
-
     private ProcessResult getPid() {
         List<String> cmds = new ArrayList<>();
         Map<String, String> envs = new HashMap<>();

+ 1 - 1
ipfcst-spare2/src/main/java/com/jiayue/ipfcst/spare2/util/ShellUtil.java

@@ -3,7 +3,7 @@ package com.jiayue.ipfcst.spare2.util;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.RuntimeUtil;
-import com.jiayue.ipfcst.common.data.entity.ProcessResult;
+import com.jiayue.ipfcst.common.data.dto.ProcessResult;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;