Browse Source

处理机器额外的配置

reghao 4 years ago
parent
commit
5c4e484235

+ 26 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/controller/MachineController.java

@@ -1,13 +1,17 @@
 package cn.reghao.autodop.dmaster.machine.controller;
 
 import cn.reghao.autodop.dmaster.machine.service.MachineService;
+import cn.reghao.autodop.dmaster.notification.entity.NotifyGroup;
 import cn.reghao.autodop.dmaster.utils.WebBody;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 /**
  * @author reghao
  * @date 2019-08-30 18:49:15
@@ -23,8 +27,29 @@ public class MachineController {
         this.machineService = machineService;
     }
 
+    @ApiOperation(value = "设置机器通知")
+    @PostMapping(value = "/notify/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseBody
+    public ResponseEntity<String> setMonitorNotify(@PathVariable("machineId") String machineId,
+                                                   @RequestParam("groupId") List<NotifyGroup> notifyGroups) {
+        //notifyGroups.forEach(notifyGroup -> monitorService.setNotify(jobId, notifyGroup));
+        return ResponseEntity.ok().body(WebBody.success());
+    }
+
+    @ApiOperation(value = "编辑 SSH 认证信息")
+    @PostMapping(value = "/sshauth", produces = MediaType.APPLICATION_JSON_VALUE)
+    public ResponseEntity<String> editSshAuth() {
+        return ResponseEntity.ok().body(WebBody.success());
+    }
+
+    @ApiOperation(value = "编辑机器额外信息")
+    @PostMapping(value = "/host", produces = MediaType.APPLICATION_JSON_VALUE)
+    public ResponseEntity<String> editExtraMachineInfo() {
+        return ResponseEntity.ok().body(WebBody.success());
+    }
+
     @ApiOperation(value = "删除机器")
-    @DeleteMapping("/host")
+    @DeleteMapping(value = "/host", produces = MediaType.APPLICATION_JSON_VALUE)
     public ResponseEntity<String> machine() {
         return ResponseEntity.ok().body(WebBody.success());
     }

+ 20 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/controller/MachinePageController.java

@@ -3,8 +3,10 @@ package cn.reghao.autodop.dmaster.machine.controller;
 import cn.reghao.autodop.dmaster.machine.db.crud.MachineHostCrudService;
 import cn.reghao.autodop.dmaster.machine.db.crud.MachineInfoCrudService;
 import cn.reghao.autodop.dmaster.machine.db.crud.MachineStatCrudService;
+import cn.reghao.autodop.dmaster.machine.entity.SshConnData;
 import cn.reghao.autodop.dmaster.machine.entity.po.MachineHost;
 import cn.reghao.autodop.dmaster.machine.entity.po.MachineStat;
+import cn.reghao.autodop.dmaster.machine.entity.po.SshAuth;
 import cn.reghao.autodop.dmaster.machine.entity.po.info.MachineInfo;
 import cn.reghao.autodop.dmaster.machine.service.MachineService;
 import cn.reghao.autodop.dmaster.machine.entity.vo.HostInfo;
@@ -118,9 +120,27 @@ public class MachinePageController {
         return "/machine/status";
     }
 
+    @ApiOperation(value = "机器通知设置页面")
+    @GetMapping("/notify/{machineId}")
+    public String machineNotifyPage(@PathVariable("machineId") String machineId, Model model) {
+        return "/machine/machinenotify";
+    }
+
+    @ApiOperation(value = "SSH 认证设置页面")
+    @GetMapping("/sshauth/{machineId}")
+    public String sshAuthPage(@PathVariable("machineId") String machineId, Model model) {
+        SshAuth sshAuth = new SshAuth();
+
+        model.addAttribute("machineId", machineId);
+        model.addAttribute("sshAuth", sshAuth);
+        return "/machine/sshauth";
+    }
+
     @ApiOperation(value = "SSH 控制台页面")
     @GetMapping("/webssh/{machineId}")
     public String webSshPage(@PathVariable("machineId") String machineId, Model model) {
+        SshConnData sshConnData = new SshConnData();
+        model.addAttribute("sshConnData", sshConnData);
         return "/machine/webssh";
     }
 }

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/entity/SshConnData.java

@@ -10,7 +10,7 @@ import lombok.Data;
 public class SshConnData {
     private String ops;
     private String host;
-    private int port = 22;
+    private int port;
     private String username;
     private String password;
     private String command;

+ 15 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/entity/po/SshAuth.java

@@ -0,0 +1,15 @@
+package cn.reghao.autodop.dmaster.machine.entity.po;
+
+import lombok.Data;
+
+/**
+ * @author reghao
+ * @date 2021-07-07 17:30:12
+ */
+@Data
+public class SshAuth {
+    private Integer port;
+    private String username;
+    private String password;
+    private String rsaPrikey;
+}

+ 6 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/service/MachineService.java

@@ -1,5 +1,6 @@
 package cn.reghao.autodop.dmaster.machine.service;
 
+import cn.reghao.autodop.dmaster.machine.db.query.MachineHostQuery;
 import org.springframework.stereotype.Service;
 
 /**
@@ -8,4 +9,9 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class MachineService {
+    private MachineHostQuery hostQuery;
+
+    public MachineService(MachineHostQuery hostQuery) {
+        this.hostQuery = hostQuery;
+    }
 }

+ 15 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/service/ssh/WebSsh.java

@@ -10,7 +10,21 @@ import java.io.IOException;
  */
 public interface WebSsh {
     void init(WebSocketSession session);
-    void sendMessage(WebSocketSession session, byte[] buffer) throws IOException;
+    /**
+     * 处理客户端发送的消息
+     *
+     * @param
+     * @return
+     * @date 2021-07-07 下午4:45
+     */
     void handleMessage(String buffer, WebSocketSession session);
+    /**
+     * 返回消息给客户端
+     *
+     * @param
+     * @return
+     * @date 2021-07-07 下午4:45
+     */
+    void sendBack(WebSocketSession session, byte[] buffer) throws IOException;
     void close(WebSocketSession session);
 }

+ 2 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/machine/service/ssh/WebSshImpl.java

@@ -44,7 +44,7 @@ public class WebSshImpl implements WebSsh {
     }
 
     @Override
-    public void sendMessage(WebSocketSession session, byte[] buffer) throws IOException {
+    public void sendBack(WebSocketSession session, byte[] buffer) throws IOException {
         session.sendMessage(new TextMessage(buffer));
     }
 
@@ -103,7 +103,7 @@ public class WebSshImpl implements WebSsh {
             byte[] buffer = new byte[1024];
             int i;
             while ((i = input.read(buffer)) != -1) {
-                sendMessage(session, Arrays.copyOfRange(buffer, 0, i));
+                sendBack(session, Arrays.copyOfRange(buffer, 0, i));
             }
         } catch (IOException e) {
             e.printStackTrace();

+ 0 - 9
dmaster/src/main/java/cn/reghao/autodop/dmaster/monitor/controller/MonitorController.java

@@ -29,15 +29,6 @@ public class MonitorController {
         this.monitorService = monitorService;
     }
 
-    /*@ApiOperation(value = "设置监控通知")
-    @PostMapping(value = "/notify/{jobId}", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
-    public ResponseEntity<String> setMonitorNotify(@PathVariable("jobId") String jobId,
-                                                   @RequestParam("groupId") List<NotifyGroup> notifyGroups) {
-        notifyGroups.forEach(notifyGroup -> monitorService.setNotify(jobId, notifyGroup));
-        return ResponseEntity.ok().body(WebBody.success());
-    }*/
-
     @ApiOperation(value = "编辑监控任务")
     @PostMapping(value = "/job", produces = MediaType.APPLICATION_JSON_VALUE)
     public ResponseEntity<String> editMonitorJob(@Valid MonitorJob monitorJob) throws Exception {

+ 0 - 6
dmaster/src/main/java/cn/reghao/autodop/dmaster/monitor/controller/MonitorPageController.java

@@ -43,12 +43,6 @@ public class MonitorPageController {
         return "/monitor/index";
     }
 
-    @ApiOperation(value = "监控任务通知设置页面")
-    @GetMapping("/notify/{id}")
-    public String monitorNotifyPage(@PathVariable("id") Integer id, Model model) {
-        return "/monitor/monitornotify";
-    }
-
     @ApiOperation(value = "监控任务编辑页面")
     @GetMapping("/job/edit/{id}")
     public String monitorJobEditPage(@PathVariable("id") MonitorJob monitorJob, Model model) {

+ 16 - 5
dmaster/src/main/resources/static/js/webssh.js

@@ -2,14 +2,25 @@ function WSSHClient() {
 }
 
 WSSHClient.prototype._generateEndpoint = function () {
+    let protocol = location.protocol
+    let hostname = location.hostname
+    let port = location.port
+
     let prefix;
-    if (window.location.protocol === 'https:') {
-        prefix = 'wss://';
+    if (protocol === 'https') {
+        if (port === 443) {
+            prefix = 'wss://' + hostname;
+        } else {
+            prefix = 'wss://' + hostname + ':' + port;
+        }
     } else {
-        prefix = 'ws://';
+        if (port === 80) {
+            prefix = 'ws://' + hostname;
+        } else {
+            prefix = 'ws://' + hostname + ':' + port;
+        }
     }
-    // TODO 根据地址栏设置地址
-    return  prefix + '127.0.0.1:4020/webssh';
+    return  prefix + '/webssh';
 };
 
 WSSHClient.prototype.connect = function (options) {

+ 21 - 3
dmaster/src/main/resources/templates/machine/edit.html

@@ -3,9 +3,27 @@
 <head th:replace="/common/template :: header(~{::title},~{::link},~{::style})"></head>
 
 <body>
-    <div class="timo-detail-page">
-        <div class="timo-detail-title">编辑信息</div>
-    </div>
+<div class="layui-form timo-compile">
+    <form th:action="@{'/api/monitor/job'}">
+        <!--<input type="hidden" name="jobId" th:value="${monitorJob.jobId}"/>-->
+        <div class="layui-form-item">
+            <label class="layui-form-label required">任务类名</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="jobClassName" readonly="true" th:value="${monitorJob.jobClassName}">
+            </div>
+        </div>
+        <div class="layui-form-item">
+            <label class="layui-form-label required">CRON 表达式</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="cronExp" placeholder="请输入 CRON 表达式" required th:value="${monitorJob.cronExp}">
+            </div>
+        </div>
+        <div class="layui-form-item timo-finally">
+            <button class="layui-btn ajax-submit"><i class="fa fa-check-circle"></i> 保存</button>
+            <button class="layui-btn btn-secondary close-popup"><i class="fa fa-times-circle"></i> 关闭</button>
+        </div>
+    </form>
+</div>
 <script th:replace="/common/template :: script"></script>
 </body>
 </html>

+ 8 - 2
dmaster/src/main/resources/templates/machine/host.html

@@ -52,6 +52,7 @@
                     <th class="sortable" data-field="osName">系统名字</th>
                     <th class="sortable" data-field="osVersion">系统版本</th>
                     <th>通知组</th>
+                    <th>SSH 认证</th>
                     <th>SSH 控制台</th>
                     <th>操作</th>
                 </tr>
@@ -69,11 +70,16 @@
                     <td th:text="${item.osVersion}">系统版本</td>
                     <td>
                         <a class="open-popup" data-title="设置通知组"
-                           th:attr="data-url=@{'/machine/host/status/disk/'+${item.machineId}}" data-size="1200,600"
+                           th:attr="data-url=@{'/machine/notify/'+${item.machineId}}" data-size="800,600"
                            href="#">设置</a>
                     </td>
                     <td>
-                        <a class="open-popup" data-title="WebSSH"
+                        <a class="open-popup" data-title="设置 SSH 帐号"
+                           th:attr="data-url=@{'/machine/sshauth/'+${item.machineId}}" data-size="800,600"
+                           href="#">设置</a>
+                    </td>
+                    <td>
+                        <a class="open-popup" data-title="WebTerminal"
                            th:attr="data-url=@{'/machine/webssh/'+${item.machineId}}" data-size="1200,600"
                            href="#">SSH</a>
                     </td>

+ 0 - 0
dmaster/src/main/resources/templates/monitor/monitornotify.html → dmaster/src/main/resources/templates/machine/machinenotify.html


+ 41 - 0
dmaster/src/main/resources/templates/machine/sshauth.html

@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})"></head>
+
+<body>
+<div class="layui-form timo-compile">
+    <form th:action="@{'/api/machine/sshauth'}">
+        <input type="hidden" name="machineId" th:value="${machineId}"/>
+        <div class="layui-form-item">
+            <label class="layui-form-label required">SSH 端口</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="port" readonly="true" th:value="${sshAuth.port}">
+            </div>
+        </div>
+        <div class="layui-form-item">
+            <label class="layui-form-label required">用户名</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="username" placeholder="请输入用户名" required th:value="${sshAuth.username}">
+            </div>
+        </div>
+        <div class="layui-form-item">
+            <label class="layui-form-label required">密码</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="password" placeholder="请输入用户密码" required th:value="${sshAuth.password}">
+            </div>
+        </div>
+        <div class="layui-form-item">
+            <label class="layui-form-label required">RSA 私钥</label>
+            <div class="layui-input-inline">
+                <input class="layui-input" type="text" name="rsaPrikey" placeholder="请输入 RSA 私钥" required th:value="${sshAuth.rsaPrikey}">
+            </div>
+        </div>
+        <div class="layui-form-item timo-finally">
+            <button class="layui-btn ajax-submit"><i class="fa fa-check-circle"></i> 保存</button>
+            <button class="layui-btn btn-secondary close-popup"><i class="fa fa-times-circle"></i> 关闭</button>
+        </div>
+    </form>
+</div>
+<script th:replace="/common/template :: script"></script>
+</body>
+</html>

+ 1 - 0
dmaster/src/main/resources/templates/machine/webssh.html

@@ -12,6 +12,7 @@
 <script type="text/javascript" th:src="@{/js/webssh.js}"></script>
 <script type="text/javascript" th:src="@{/js/xterm.js}"></script>
 <script type="text/javascript">
+    // TODO 在 controller 的 model 中指定数据
     var options = {
         ops:'connect',
         host: '192.168.0.171',//IP