2021-09-11 14:04:09 +08:00
|
|
|
|
package api
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/api/form"
|
|
|
|
|
|
"mayfly-go/internal/machine/api/vo"
|
|
|
|
|
|
"mayfly-go/internal/machine/application"
|
|
|
|
|
|
"mayfly-go/internal/machine/domain/entity"
|
2022-10-26 20:49:29 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
|
"mayfly-go/pkg/ctx"
|
|
|
|
|
|
"mayfly-go/pkg/ginx"
|
|
|
|
|
|
"mayfly-go/pkg/utils"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type MachineScript struct {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
MachineScriptApp application.MachineScript
|
|
|
|
|
|
MachineApp application.Machine
|
2022-10-26 20:49:29 +08:00
|
|
|
|
TagApp tagapp.TagTree
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineScript) MachineScripts(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
condition := &entity.MachineScript{MachineId: GetMachineId(g)}
|
|
|
|
|
|
rc.ResData = m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineScript) SaveMachineScript(rc *ctx.ReqCtx) {
|
|
|
|
|
|
form := new(form.MachineScriptForm)
|
|
|
|
|
|
ginx.BindJsonAndValid(rc.GinCtx, form)
|
|
|
|
|
|
rc.ReqParam = form
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为entity,并设置基本信息
|
|
|
|
|
|
machineScript := new(entity.MachineScript)
|
|
|
|
|
|
utils.Copy(machineScript, form)
|
|
|
|
|
|
machineScript.SetBaseInfo(rc.LoginAccount)
|
|
|
|
|
|
|
|
|
|
|
|
m.MachineScriptApp.Save(machineScript)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineScript) DeleteMachineScript(rc *ctx.ReqCtx) {
|
|
|
|
|
|
msa := m.MachineScriptApp
|
|
|
|
|
|
sid := GetMachineScriptId(rc.GinCtx)
|
|
|
|
|
|
ms := msa.GetById(sid)
|
|
|
|
|
|
biz.NotNil(ms, "该脚本不存在")
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("[scriptId: %d, name: %s, desc: %s, script: %s]", sid, ms.Name, ms.Description, ms.Script)
|
|
|
|
|
|
msa.Delete(sid)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *MachineScript) RunMachineScript(rc *ctx.ReqCtx) {
|
|
|
|
|
|
g := rc.GinCtx
|
|
|
|
|
|
|
|
|
|
|
|
scriptId := GetMachineScriptId(g)
|
|
|
|
|
|
machineId := GetMachineId(g)
|
|
|
|
|
|
ms := m.MachineScriptApp.GetById(scriptId, "MachineId", "Name", "Script")
|
|
|
|
|
|
biz.NotNil(ms, "该脚本不存在")
|
|
|
|
|
|
biz.IsTrue(ms.MachineId == application.Common_Script_Machine_Id || ms.MachineId == machineId, "该脚本不属于该机器")
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
script := ms.Script
|
|
|
|
|
|
// 如果有脚本参数,则用脚本参数替换脚本中的模板占位符参数
|
|
|
|
|
|
if params := g.Query("params"); params != "" {
|
|
|
|
|
|
script = utils.TemplateParse(ms.Script, utils.Json2Map(params))
|
|
|
|
|
|
}
|
2022-04-22 17:49:21 +08:00
|
|
|
|
cli := m.MachineApp.GetCli(machineId)
|
2022-10-26 20:49:29 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
|
2022-04-22 17:49:21 +08:00
|
|
|
|
|
|
|
|
|
|
res, err := cli.Run(script)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 记录请求参数
|
|
|
|
|
|
rc.ReqParam = fmt.Sprintf("[machineId: %d, scriptId: %d, name: %s]", machineId, scriptId, ms.Name)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(biz.NewBizErr(fmt.Sprintf("执行命令失败:%s", err.Error())))
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.ResData = res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetMachineScriptId(g *gin.Context) uint64 {
|
|
|
|
|
|
scriptId, _ := strconv.Atoi(g.Param("scriptId"))
|
|
|
|
|
|
biz.IsTrue(scriptId > 0, "scriptId错误")
|
|
|
|
|
|
return uint64(scriptId)
|
|
|
|
|
|
}
|