From 62879697131d377a1ac4689de9d8f47141142caa Mon Sep 17 00:00:00 2001 From: wubijie Date: Fri, 10 Nov 2023 09:54:41 +0800 Subject: [PATCH] add gettags test --- server/go.mod | 2 +- server/go.sum | 8 +++ server/main.go | 15 ++++++ .../openeuler/PilotGo/sdk/common/extention.go | 15 ++++++ .../openeuler/PilotGo/sdk/common/tag.go | 14 +++++ .../PilotGo/sdk/plugin/client/client.go | 20 +++++++ .../PilotGo/sdk/plugin/client/extention.go | 9 ++++ .../PilotGo/sdk/plugin/client/handler.go | 54 +++++++++++++++++++ .../PilotGo/sdk/plugin/client/machine.go | 9 ++-- .../PilotGo/sdk/plugin/client/script.go | 2 +- server/vendor/modules.txt | 2 +- 11 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go create mode 100644 server/vendor/gitee.com/openeuler/PilotGo/sdk/common/tag.go create mode 100644 server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go diff --git a/server/go.mod b/server/go.mod index 4c735a4..acd1d6b 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,7 +3,7 @@ module openeuler.org/PilotGo/redis-plugin go 1.20 require ( - gitee.com/openeuler/PilotGo/sdk v0.0.0-20231019072242-b911c9979e39 + gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110012917-3eceb3662098 github.com/gin-gonic/gin v1.9.1 gopkg.in/yaml.v2 v2.4.0 gorm.io/driver/mysql v1.5.2 diff --git a/server/go.sum b/server/go.sum index b5c7b31..57367f5 100644 --- a/server/go.sum +++ b/server/go.sum @@ -38,6 +38,14 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231019072242-b911c9979e39 h1:UV9/VLrs4pvuu0nVVelFXSrd/75U1+0bZ/SCqxMfL2Y= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231019072242-b911c9979e39/go.mod h1:iPlzp8pa9YCXF8oPwFw/QfqP8btwunhv+dnMd8y8KeU= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231106070541-1f96eb7967c2 h1:MWXqe0NFzw55TGRTl9E8fm4OeTCzoCd/W1IEJOT4lQ8= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231106070541-1f96eb7967c2/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231107034041-5710927e351e h1:Opeo2MOq6+MSL0lCwQkKpBtmsgfPgywtcYyJTIDeEns= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231107034041-5710927e351e/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231109024612-b4f4c12d9f24 h1:kqNxXxF3a4DJNqsp20WGTQN7VUIRtlL3aLUIuspk0sI= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231109024612-b4f4c12d9f24/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110012917-3eceb3662098 h1:OYoBm/IlipXyqzRztzOq9YNsLbuV8AsY0viV2xpPsik= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110012917-3eceb3662098/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= diff --git a/server/main.go b/server/main.go index 50d18b6..742b014 100644 --- a/server/main.go +++ b/server/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/plugin/client" "openeuler.org/PilotGo/redis-plugin/config" @@ -42,6 +43,20 @@ func main() { //可用通信的方式获取服务端地址 global.GlobalClient.Server = config.Config().PilotGoServer.Addr + + global.GlobalClient.OnGetTags(func(uuids []string) []common.Tag { + var tags []common.Tag + for _, uuid := range uuids { + tag := common.Tag{ + UUID: uuid, + Type: "1", + Data: "lable", + } + tags = append(tags, tag) + } + return tags + }) + go router.RegisterAPIs(server) if err := server.Run(config.Config().HttpServer.Addr); err != nil { logger.Error("failed to run server: %s", err) diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go new file mode 100644 index 0000000..8f6d040 --- /dev/null +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go @@ -0,0 +1,15 @@ +package common + +const ( + // 单机/多机操作扩展,增加选择单机/多机时对机器的操作功能 + ExtentionMachine = "machine" + // 批处理扩展,增加选择批次时对批次的操作 + ExtentionBatch = "batch" +) + +type Extention struct { + PluginName string `json:"plugin_name"` + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` +} diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/tag.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/tag.go new file mode 100644 index 0000000..dab5e54 --- /dev/null +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/tag.go @@ -0,0 +1,14 @@ +package common + +const ( + TypeOk = "ok" + TypeWarn = "warn" + TypeError = "error" +) + +type Tag struct { + UUID string `json:"machineuuid"` + PluginName string `json:"plugin_name"` + Type string `json:"type"` + Data string `json:"data"` +} diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go index fc3a573..60c8a9d 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go @@ -5,6 +5,8 @@ import ( "github.com/gin-gonic/gin" ) +type GetTagsCallback func([]string) []common.Tag + type Client struct { Server string PluginInfo *PluginInfo @@ -16,6 +18,12 @@ type Client struct { // 用于异步command及script执行结果处理机 asyncCmdResultChan chan *common.AsyncCmdResult cmdProcessorCallbackMap map[string]CallbackHandler + + // 用于处理主机标签 + getTagsCallback GetTagsCallback + + // 用于平台扩展点功能 + extentions []*common.Extention } var global_client *Client @@ -52,6 +60,14 @@ func (client *Client) RegisterHandlers(router *gin.Engine) { api := router.Group("/plugin_manage/api/v1/") { + api.GET("/extentions", func(c *gin.Context) { + c.Set("__internal__client_instance", client) + }, ExtentionsHandler) + + api.GET("/gettags", func(c *gin.Context) { + c.Set("__internal__client_instance", client) + }, TagsHandler) + api.POST("/event", func(c *gin.Context) { c.Set("__internal__client_instance", client) }, EventHandler) @@ -73,3 +89,7 @@ func (client *Client) RegisterHandlers(router *gin.Engine) { client.startEventProcessor() client.startCommandResultProcessor() } + +func (client *Client) OnGetTags(callback GetTagsCallback) { + client.getTagsCallback = callback +} diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go new file mode 100644 index 0000000..4460e21 --- /dev/null +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go @@ -0,0 +1,9 @@ +package client + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" +) + +func (c *Client) RegisterExtention(exts []*common.Extention) { + c.extentions = exts +} diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go index 34b796a..e90a705 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go @@ -11,6 +11,7 @@ import ( "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/logger" + "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" ) @@ -60,6 +61,21 @@ func EventHandler(c *gin.Context) { client.ProcessEvent(&msg) } +func ExtentionsHandler(c *gin.Context) { + v, ok := c.Get("__internal__client_instance") + if !ok { + response.Fail(c, gin.H{"status": false}, "未获取到client值信息") + return + } + client, ok := v.(*Client) + if !ok { + response.Fail(c, gin.H{"status": false}, "client获取失败") + return + } + + response.Success(c, client.extentions, "") +} + func CommandResultHandler(c *gin.Context) { j, err := io.ReadAll(c.Request.Body) // 接收数据 if err != nil { @@ -86,3 +102,41 @@ func CommandResultHandler(c *gin.Context) { client.ProcessCommandResult(&result) } + +func TagsHandler(c *gin.Context) { + j, err := io.ReadAll(c.Request.Body) // 接收数据 + if err != nil { + logger.Error("没获取到:%s", err.Error()) + response.Fail(c, gin.H{"status": false}, "没获取到:"+err.Error()) + return + } + uuidTags := &struct { + UUIDS []string `json:"uuids"` + }{} + if err := json.Unmarshal(j, &uuidTags); err != nil { + logger.Error("反序列化结果失败%s", err.Error()) + response.Fail(c, gin.H{"status": false}, "反序列化结果失败:"+err.Error()) + return + } + + v, ok := c.Get("__internal__client_instance") + if !ok { + logger.Error("%v", "未获取到client值信息") + response.Fail(c, gin.H{"status": false}, "未获取到client值信息") + return + } + client, ok := v.(*Client) + if !ok { + logger.Error("%v", "client获取失败") + response.Fail(c, gin.H{"status": false}, "client获取失败") + return + } + + if client.getTagsCallback != nil { + result := client.getTagsCallback(uuidTags.UUIDS) + response.Success(c, result, "") + } else { + logger.Error("get tags callback not set") + response.Fail(c, gin.H{"status": false}, "get tags callback not set") + } +} diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go index 5d218f8..64bcdb5 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go @@ -8,15 +8,18 @@ import ( ) func (c *Client) MachineList() ([]*common.MachineNode, error) { - url := c.Server + "/api/v1/pluginapi/machine_list" + url := "http://" + c.Server + "/api/v1/pluginapi/machine_list" r, err := httputils.Get(url, nil) if err != nil { return nil, err } - result := []*common.MachineNode{} + result := struct { + Code int `json:"code"` + Data []*common.MachineNode `json:"data"` + }{} if err := json.Unmarshal(r.Body, &result); err != nil { return nil, err } - return result, nil + return result.Data, nil } diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go index 7a08662..2b1038f 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go @@ -81,7 +81,7 @@ func (c *Client) RunCommandAsync(batch *common.Batch, cmd string, callback RunCo p := &common.CmdStruct{ Batch: batch, - Command: cmd, + Command: base64.StdEncoding.EncodeToString([]byte(cmd)), } r, err := httputils.Post(url, &httputils.Params{ diff --git a/server/vendor/modules.txt b/server/vendor/modules.txt index 8e70718..f1c9f18 100644 --- a/server/vendor/modules.txt +++ b/server/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231019072242-b911c9979e39 +# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110012917-3eceb3662098 ## explicit; go 1.17 gitee.com/openeuler/PilotGo/sdk/common gitee.com/openeuler/PilotGo/sdk/logger -- Gitee