From 47f5999992f5ba9c4720472164c005d53dbf6a38 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Tue, 16 Sep 2025 16:45:26 +0800 Subject: [PATCH] Add script execution method handler --- automation/server/go.mod | 2 +- automation/server/go.sum | 4 +- .../module/script_library/controller/exec.go | 36 +++++++++++++ .../internal/module/script_library/router.go | 5 ++ .../PilotGo/sdk/plugin/client/scripts.go | 52 +++++++++++++++++++ vendor/modules.txt | 2 +- 6 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 automation/server/internal/module/script_library/controller/exec.go create mode 100644 vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/scripts.go diff --git a/automation/server/go.mod b/automation/server/go.mod index ae27020c..558711c7 100644 --- a/automation/server/go.mod +++ b/automation/server/go.mod @@ -5,7 +5,7 @@ go 1.23.0 toolchain go1.23.7 require ( - gitee.com/openeuler/PilotGo/sdk v0.0.0-20250903062443-96565885a85b + gitee.com/openeuler/PilotGo/sdk v0.0.0-20250916064441-cba49a792605 github.com/gin-gonic/gin v1.9.1 github.com/go-redis/redis/v8 v8.11.5 github.com/go-sql-driver/mysql v1.8.1 diff --git a/automation/server/go.sum b/automation/server/go.sum index 19fcadd4..a9acac23 100644 --- a/automation/server/go.sum +++ b/automation/server/go.sum @@ -1,7 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -gitee.com/openeuler/PilotGo/sdk v0.0.0-20250903062443-96565885a85b h1:+MzK9x0x455Ut+rgOpIZNMllKvxCYz9/pOJ4BliGiKo= -gitee.com/openeuler/PilotGo/sdk v0.0.0-20250903062443-96565885a85b/go.mod h1:MjGXxy1zfF0eQjT932SohfrkgnWGvozgB9sNQX/f/Gs= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20250916064441-cba49a792605 h1:LAtXnGrcKzsuQRVHrrWKD2V7N53stdNSNGaEZgegTTU= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20250916064441-cba49a792605/go.mod h1:MjGXxy1zfF0eQjT932SohfrkgnWGvozgB9sNQX/f/Gs= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= diff --git a/automation/server/internal/module/script_library/controller/exec.go b/automation/server/internal/module/script_library/controller/exec.go new file mode 100644 index 00000000..1315551d --- /dev/null +++ b/automation/server/internal/module/script_library/controller/exec.go @@ -0,0 +1,36 @@ +package controller + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" + "openeuler.org/PilotGo/PilotGo-plugin-automation/pkg/utils" +) + +func ExecScriptHandler(c *gin.Context) { + var sr struct { + UUIDS []string `json:"UUIDS"` + ScriptType string `json:"script_type"` + ScriptContent string `json:"script_content"` + Params string `json:"params"` + } + if err := c.ShouldBindJSON(&sr); err != nil { + response.Fail(c, nil, err.Error()) + return + } + hosts, err := global.App.Client.MachineList() + if err != nil { + response.Fail(c, nil, err.Error()) + return + } + result, err := global.App.Client.AgentRunScripts(&common.Batch{MachineUUIDs: sr.UUIDS}, sr.ScriptType, utils.EncodeScriptContent(sr.ScriptContent), sr.Params) + if err != nil { + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, gin.H{ + "hosts": hosts, + "result": result, + }, "success") +} diff --git a/automation/server/internal/module/script_library/router.go b/automation/server/internal/module/script_library/router.go index 0f7bffcd..ce4987e2 100644 --- a/automation/server/internal/module/script_library/router.go +++ b/automation/server/internal/module/script_library/router.go @@ -29,4 +29,9 @@ func ScriptLibraryHandler(router *gin.RouterGroup) { tag.PUT("/update", controller.UpdateTagHandler) tag.DELETE("/delete", controller.DeleteTagHandler) } + + exec := router.Group("/exec") + { + exec.POST("/script", controller.ExecScriptHandler) + } } diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/scripts.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/scripts.go new file mode 100644 index 00000000..fbcd4913 --- /dev/null +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/scripts.go @@ -0,0 +1,52 @@ +package client + +import ( + "encoding/json" + "fmt" + + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/plugin/jwt" + "gitee.com/openeuler/PilotGo/sdk/utils/httputils" +) + +type ScriptsRun struct { + Batch *common.Batch `json:"batch"` + ScriptType string `json:"script_type"` + ScriptContent string `json:"script_content"` + Params string `json:"params"` +} + +func (c *Client) AgentRunScripts(batch *common.Batch, scriptType string, script string, params string) ([]*common.CmdResult, error) { + serverInfo, err := c.Registry.Get("pilotgo-server") + if err != nil { + return nil, err + } + url := fmt.Sprintf("http://%s:%s/api/v1/pluginapi/runScripts", serverInfo.Address, serverInfo.Port) + + scriptRun := &ScriptsRun{ + Batch: batch, + ScriptType: scriptType, + ScriptContent: script, + Params: params, + } + r, err := httputils.Post(url, &httputils.Params{ + Body: scriptRun, + Cookie: map[string]string{ + jwt.TokenCookie: c.token, + }, + }) + if err != nil { + return nil, err + } + + res := &struct { + Code int `json:"code"` + Message string `json:"msg"` + Data []*common.CmdResult `json:"data"` + }{} + if err := json.Unmarshal(r.Body, res); err != nil { + return nil, err + } + + return res.Data, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 85e78148..40b8bb54 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -15,7 +15,7 @@ filippo.io/edwards25519/field ## explicit; go 1.23.0 # gitee.com/openeuler/PilotGo/sdk v0.0.0-20250820124645-b6480a0f3109 ## explicit; go 1.23.0 -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20250903062443-96565885a85b +# gitee.com/openeuler/PilotGo/sdk v0.0.0-20250916064441-cba49a792605 ## explicit; go 1.23.0 gitee.com/openeuler/PilotGo/sdk/common gitee.com/openeuler/PilotGo/sdk/go-micro/registry -- Gitee