Commit 000af11f authored by 陈子龙's avatar 陈子龙

Merge branch 'dev-czl' into dev

parents 4d9c2fca 240b7c46
......@@ -3,14 +3,14 @@ package entity
import "time"
type TaskManage struct {
Id int `json:"id" xorm:"pk autoincr" ` // id
TaskName string `json:"task_name" xorm:"task_name"` // 任务名称
TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述
YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件
YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址
ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数
SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数
FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数
Id int `json:"id" xorm:"pk autoincr" ` // id
TaskName string `json:"task_name" xorm:"task_name"` // 任务名称
TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述
YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件
YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址
//ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数
//SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数
//FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数
HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组ID
IsDelete int `json:"is_delete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除)
CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人
......
......@@ -33,10 +33,10 @@ type ListTaskManageReq struct {
}
type ExecScriptReq struct {
TaskId int `form:"task_id" binding:"required"` //任务id
HostGroupId int `form:"host_group_id" binding:"required"` //主机分组id
Type int `form:"type" binding:"oneof=1 2"` //脚本额外变量类型1yaml 2json
Value string `form:"value"` //脚本额外变量值
Script string `form:"script"` //执行脚本
YmlFileName string `form:"yml_file_name"` //执行脚本url
TaskId int `form:"task_id" binding:"required"` //任务id
//HostGroupId int `form:"host_group_id" binding:"required"` //主机分组id
Type int `form:"type"` //脚本额外变量类型1yaml 2json
Value string `form:"value"` //脚本额外变量值
//Script string `form:"script"` //执行脚本
//YmlFileName string `form:"yml_file_name"` //执行脚本url
}
......@@ -15,6 +15,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"io"
"strings"
)
// AddTaskManage 新增任务
......@@ -125,57 +126,82 @@ func ListTaskManage(c *gin.Context) {
}
func ExecScript(c *gin.Context) {
var req request.ExecScriptReq
if err := c.ShouldBind(&req); err != nil {
var (
req request.ExecScriptReq
err error
script string
)
if err = c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
if req.YmlFileName == "" && req.Script == "" {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("执行脚本为空")), nil)
taskManageSvc := service.TaskManageSvc{}
taskManage, err := taskManageSvc.GetTaskManage(req.TaskId)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
//解析yml文件
if req.YmlFileName != "" && req.Value == "" {
minioClient, err := client.GetMinioConnect()
if err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("执行脚本为空")), nil)
if taskManage.YamlDesc == "" {
//解析文件
ymlUrl := strings.Split(taskManage.YamlUrl, "/")
minioClient, err2 := client.GetMinioConnect()
if err2 != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err2), nil)
return
}
object, err := minioClient.GetObject(c, conf.Options.MinioBucket, req.YmlFileName, minio.GetObjectOptions{})
if err != nil {
SendJsonResponse(c, resp.GetFileStreamError.WithError(err), nil)
object, err2 := minioClient.GetObject(c, conf.Options.MinioBucket, ymlUrl[len(ymlUrl)-1], minio.GetObjectOptions{})
if err2 != nil {
SendJsonResponse(c, resp.GetFileStreamError.WithError(err2), nil)
return
}
obj, err := io.ReadAll(object)
if err != nil {
SendJsonResponse(c, resp.ReadFileError.WithError(err), nil)
obj, err2 := io.ReadAll(object)
if err2 != nil {
SendJsonResponse(c, resp.ReadFileError.WithError(err2), nil)
return
}
req.Script = string(obj)
taskManage.YamlDesc = string(obj)
}
//解析yml文件
//if req.YmlFileName != "" && req.Value == "" {
// minioClient, err := client.GetMinioConnect()
// if err != nil {
// SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("执行脚本为空")), nil)
// return
// }
// object, err := minioClient.GetObject(c, conf.Options.MinioBucket, req.YmlFileName, minio.GetObjectOptions{})
// if err != nil {
// SendJsonResponse(c, resp.GetFileStreamError.WithError(err), nil)
// return
// }
// obj, err := io.ReadAll(object)
// if err != nil {
// SendJsonResponse(c, resp.ReadFileError.WithError(err), nil)
// return
// }
// req.Script = string(obj)
//}
//写入额外yml参数
if req.Type == 1 {
//var scripts []map[string]interface{}
var value map[string]interface{}
j2, err := yaml.YAMLToJSON([]byte(req.Script))
if err != nil {
err = resp.YamlAnalysisError.WithError(err)
j2, err3 := yaml.YAMLToJSON([]byte(req.Value))
if err3 != nil {
err = resp.YamlAnalysisError.WithError(err3)
return
}
err = json.Unmarshal(j2, &value)
if err != nil {
err = resp.InvalidParam.WithError(errors.New("yaml格式错误"))
err3 = json.Unmarshal(j2, &value)
if err3 != nil {
err = resp.InvalidParam.WithError(errors.New("变量配置错误"))
return
}
value["hosts"] = fmt.Sprintf("%s%d", service.AnsibleGroup, taskManage.HostGroupId)
value["hosts"] = fmt.Sprintf("%s%d", service.AnsibleGroup, req.HostGroupId)
j, err := json.Marshal(value)
if err != nil {
err = resp.MarshalError.WithError(err)
j, err3 := json.Marshal(value)
if err3 != nil {
err = resp.MarshalError.WithError(err3)
return
}
req.Value = fmt.Sprintf("%s", j)
......@@ -195,8 +221,7 @@ func ExecScript(c *gin.Context) {
//req.Value = fmt.Sprintf("@/etc/ansible/ansible_extra.yml")
}
taskManageSvc := service.TaskManageSvc{}
id, err := taskManageSvc.ExecScript(req)
id, err := taskManageSvc.ExecScript(req, script)
if err != nil {
SendJsonResponse(c, err, nil)
return
......
......@@ -173,7 +173,7 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
finder.OrderBy("tm.id")
//查询任务
total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+
"where th.task_id = tm.id group by th.task_id) as exec_cnt,tm.create_user,tm.create_time").
"where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").
Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
......@@ -182,7 +182,26 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
return
}
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (id int, err error) {
// DetailsTaskManage 任务详情
func (t *TaskManageSvc) GetTaskManage(id int) (taskManageRes response.TaskManageRes, err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
//查询任务详情
finder := db.Table("task_manage").
Where("is_delete = 0 AND id = ?", id)
_, err = finder.Get(&taskManageRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
return
}
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id int, err error) {
//var scripts []map[string]interface{}
//var script map[string]interface{}
//j2, err := yaml.YAMLToJSON([]byte(req.Script))
......@@ -211,7 +230,7 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (id int, err error
return
}
defer f2.Close()
_, err = f2.Write([]byte(req.Script))
_, err = f2.Write([]byte(script))
if err != nil {
err = resp.FileExecError.WithError(err)
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment