2021-09-11 14:04:09 +08:00
|
|
|
package api
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"mayfly-go/base/biz"
|
|
|
|
|
"mayfly-go/base/ctx"
|
|
|
|
|
"mayfly-go/base/ginx"
|
|
|
|
|
"mayfly-go/base/utils"
|
2021-11-11 15:56:02 +08:00
|
|
|
"mayfly-go/base/ws"
|
2021-09-11 14:04:09 +08:00
|
|
|
"mayfly-go/server/devops/api/form"
|
|
|
|
|
"mayfly-go/server/devops/api/vo"
|
2021-06-07 17:22:07 +08:00
|
|
|
"mayfly-go/server/devops/application"
|
|
|
|
|
"mayfly-go/server/devops/domain/entity"
|
|
|
|
|
"mayfly-go/server/devops/infrastructure/machine"
|
2021-05-08 18:00:33 +08:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Machine struct {
|
2021-07-28 18:03:19 +08:00
|
|
|
MachineApp application.Machine
|
2021-05-08 18:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Machine) Machines(rc *ctx.ReqCtx) {
|
2021-09-08 17:55:57 +08:00
|
|
|
res := m.MachineApp.GetMachineList(new(entity.Machine), ginx.GetPageParam(rc.GinCtx), new([]*vo.MachineVO))
|
|
|
|
|
list := res.List.(*[]*vo.MachineVO)
|
|
|
|
|
for _, mv := range *list {
|
|
|
|
|
mv.HasCli = machine.HasCli(*mv.Id)
|
|
|
|
|
}
|
|
|
|
|
rc.ResData = res
|
2021-05-08 18:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Machine) SaveMachine(rc *ctx.ReqCtx) {
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
machineForm := new(form.MachineForm)
|
|
|
|
|
ginx.BindJsonAndValid(g, machineForm)
|
|
|
|
|
|
|
|
|
|
entity := new(entity.Machine)
|
|
|
|
|
utils.Copy(entity, machineForm)
|
|
|
|
|
|
|
|
|
|
entity.SetBaseInfo(rc.LoginAccount)
|
|
|
|
|
m.MachineApp.Save(entity)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
func (m *Machine) DeleteMachine(rc *ctx.ReqCtx) {
|
2021-09-08 17:55:57 +08:00
|
|
|
id := uint64(ginx.PathParamInt(rc.GinCtx, "machineId"))
|
2021-07-28 18:03:19 +08:00
|
|
|
rc.ReqParam = id
|
|
|
|
|
m.MachineApp.Delete(id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 17:55:57 +08:00
|
|
|
// 关闭机器客户端
|
|
|
|
|
func (m *Machine) CloseCli(rc *ctx.ReqCtx) {
|
|
|
|
|
machine.DeleteCli(GetMachineId(rc.GinCtx))
|
2021-05-08 18:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Machine) WsSSH(g *gin.Context) {
|
2021-11-11 15:56:02 +08:00
|
|
|
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
|
2021-05-08 18:00:33 +08:00
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
|
|
|
|
|
wsConn.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(biz.NewBizErr("升级websocket失败"))
|
|
|
|
|
}
|
|
|
|
|
// 权限校验
|
2021-06-09 16:58:57 +08:00
|
|
|
rc := ctx.NewReqCtxWithGin(g).WithRequiredPermission(ctx.NewPermission("machine:terminal"))
|
|
|
|
|
if err = ctx.PermissionHandler(rc); err != nil {
|
2021-05-08 18:00:33 +08:00
|
|
|
panic(biz.NewBizErr("没有权限"))
|
|
|
|
|
}
|
2021-09-11 14:04:09 +08:00
|
|
|
// 演示环境禁止非admin用户执行
|
2021-11-11 15:56:02 +08:00
|
|
|
// if rc.LoginAccount.Username != "admin" {
|
|
|
|
|
// panic(biz.NewBizErrCode(401, "非admin用户无权该操作"))
|
|
|
|
|
// }
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
cols := ginx.QueryInt(g, "cols", 80)
|
|
|
|
|
rows := ginx.QueryInt(g, "rows", 40)
|
|
|
|
|
|
|
|
|
|
sws, err := machine.NewLogicSshWsSession(cols, rows, m.MachineApp.GetCli(GetMachineId(g)), wsConn)
|
2021-09-08 17:55:57 +08:00
|
|
|
biz.ErrIsNilAppendErr(err, "连接失败:%s")
|
2021-05-08 18:00:33 +08:00
|
|
|
defer sws.Close()
|
|
|
|
|
|
|
|
|
|
quitChan := make(chan bool, 3)
|
|
|
|
|
sws.Start(quitChan)
|
|
|
|
|
go sws.Wait(quitChan)
|
|
|
|
|
|
|
|
|
|
<-quitChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetMachineId(g *gin.Context) uint64 {
|
|
|
|
|
machineId, _ := strconv.Atoi(g.Param("machineId"))
|
|
|
|
|
biz.IsTrue(machineId != 0, "machineId错误")
|
|
|
|
|
return uint64(machineId)
|
|
|
|
|
}
|