mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-09 01:10:26 +08:00
feat: 目录及代码优化调整
This commit is contained in:
108
devops/controllers/machine.go
Normal file
108
devops/controllers/machine.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"mayfly-go/base"
|
||||
"mayfly-go/base/biz"
|
||||
"mayfly-go/base/ctx"
|
||||
"mayfly-go/devops/machine"
|
||||
"mayfly-go/devops/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type MachineController struct {
|
||||
base.Controller
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024 * 1024 * 10,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func (c *MachineController) Machines() {
|
||||
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
|
||||
return models.GetMachineList(c.GetPageParam())
|
||||
})
|
||||
}
|
||||
|
||||
func (c *MachineController) Run() {
|
||||
rc := ctx.NewReqCtx(true, "执行机器命令")
|
||||
c.ReturnData(rc, func(account *ctx.LoginAccount) interface{} {
|
||||
cmd := c.GetString("cmd")
|
||||
biz.NotEmpty(cmd, "cmd不能为空")
|
||||
|
||||
rc.ReqParam = cmd
|
||||
|
||||
res, err := c.getCli().Run(cmd)
|
||||
biz.BizErrIsNil(err, "执行命令失败")
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
// 系统基本信息
|
||||
func (c *MachineController) SysInfo() {
|
||||
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
|
||||
res, err := c.getCli().GetSystemInfo()
|
||||
biz.BizErrIsNil(err, "获取系统基本信息失败")
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
// top命令信息
|
||||
func (c *MachineController) Top() {
|
||||
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
|
||||
return c.getCli().GetTop()
|
||||
})
|
||||
}
|
||||
|
||||
func (c *MachineController) GetProcessByName() {
|
||||
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
|
||||
name := c.GetString("name")
|
||||
biz.NotEmpty(name, "name不能为空")
|
||||
res, err := c.getCli().GetProcessByName(name)
|
||||
biz.BizErrIsNil(err, "获取失败")
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
func (c *MachineController) WsSSH() {
|
||||
wsConn, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
|
||||
if err != nil {
|
||||
panic(biz.NewBizErr("获取requst responsewirte错误"))
|
||||
}
|
||||
|
||||
cols, _ := c.GetInt("cols", 80)
|
||||
rows, _ := c.GetInt("rows", 40)
|
||||
|
||||
sws, err := machine.NewLogicSshWsSession(cols, rows, c.getCli(), wsConn)
|
||||
if sws == nil {
|
||||
panic(biz.NewBizErr("连接失败"))
|
||||
}
|
||||
//if wshandleError(wsConn, err) {
|
||||
// return
|
||||
//}
|
||||
defer sws.Close()
|
||||
|
||||
quitChan := make(chan bool, 3)
|
||||
sws.Start(quitChan)
|
||||
go sws.Wait(quitChan)
|
||||
|
||||
<-quitChan
|
||||
}
|
||||
|
||||
func (c *MachineController) GetMachineId() uint64 {
|
||||
machineId, _ := strconv.Atoi(c.Ctx.Input.Param(":machineId"))
|
||||
biz.IsTrue(machineId > 0, "machineId错误")
|
||||
return uint64(machineId)
|
||||
}
|
||||
|
||||
func (c *MachineController) getCli() *machine.Cli {
|
||||
cli, err := machine.GetCli(c.GetMachineId())
|
||||
biz.BizErrIsNil(err, "获取客户端错误")
|
||||
return cli
|
||||
}
|
||||
Reference in New Issue
Block a user