diff --git a/automation/server/go.mod b/automation/server/go.mod index ae27020cad6453ee53ef3d8b4a7be473570f9286..558711c79dcb3d421e6313c5c96567420e9225bf 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 19fcadd40007bf15342c227144acb5f514b2522d..a9acac23e0b17de85856745349b7423f9fc3cac4 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 0000000000000000000000000000000000000000..1315551df6b6550a791ff5b8d4ed2d5481f0f03f --- /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 0f7bffcd690e97bd9065d9ade992ad9042a0c554..ce4987e25e49a8ffe1caecec71a5753b13d2ab46 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 0000000000000000000000000000000000000000..fbcd4913c3f812ee43f3921e3b7c3a397e193d90 --- /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 85e781484b8489deeb0015aa1009c7501cc31a13..40b8bb5411cfba546e4438f4801e8266865f262f 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