Commit 8e9a9af5 authored by 魏灿's avatar 魏灿

脚本执行fix

parent 3a56f2f8
......@@ -37,4 +37,5 @@ type ExecScriptReq struct {
Type int `form:"type"` //脚本额外变量类型1yaml 2json
Value string `form:"value"` //脚本额外变量值
Script string `form:"script"` //执行脚本
ScriptUrl string `form:"script_url"` //执行脚本url
}
package service
import (
"context"
"errors"
"fmt"
"github.com/ghodss/yaml"
json "github.com/json-iterator/go"
"github.com/minio/minio-go/v7"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"io"
"os"
"os/exec"
"time"
......@@ -174,6 +178,21 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
}
func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{}, err error) {
if req.ScriptUrl != "" {
minioClient, err := client.GetMinioConnect()
if err != nil {
return nil, resp.DbConnectError.WithError(err)
}
object, err := minioClient.GetObject(context.Background(), conf.Options.MinioBucket, req.ScriptUrl, minio.GetObjectOptions{})
if err != nil {
return nil, resp.FileExecError.WithError(err)
}
obj, err := io.ReadAll(object)
if err != nil {
return nil, resp.FileExecError.WithError(err)
}
req.Script = string(obj)
}
var script map[string]interface{}
j2, err := yaml.YAMLToJSON([]byte(req.Script))
if err != nil {
......@@ -181,7 +200,7 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq) (data interface{},
}
err = json.Unmarshal(j2, &script)
if err != nil {
return nil, resp.MarshalError.WithError(err)
return nil, resp.MarshalError.WithError(errors.New("yaml格式错误"))
}
if script["host"] == "all" {
script["host"] = fmt.Sprintf("%s%d", AnsibleGroup, req.HostGroupId)
......
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