diff --git a/server/go.mod b/server/go.mod index acd1d6b2ecdd0a0fdfc9944ada730197935a2108..a28809d935aa3b9b74094957954f13c7ec9bd1a9 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-20231110012917-3eceb3662098 + gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130032403-02364be090ab 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 57367f57e89c704297010908a67d0fb7025dea25..2e864f16401e7ba8d903fac7fa61a2a90e96e961 100644 --- a/server/go.sum +++ b/server/go.sum @@ -46,6 +46,14 @@ gitee.com/openeuler/PilotGo/sdk v0.0.0-20231109024612-b4f4c12d9f24 h1:kqNxXxF3a4 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= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110025008-81b873fcc950 h1:QhlDZUlfuazNlKFnRdG861oNn+sM3Xts/jEyrpeLeyI= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110025008-81b873fcc950/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110031525-bf462264e481 h1:ed0p10IBLpakMIi4POcoRY3uI3OlUTRfcAUHLJxCjUA= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110031525-bf462264e481/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130015721-7961ff6790fc h1:37ad5xTtVIQ5aR6JWMuxL0mJD+/EIeB9RWmcjV7B+f8= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130015721-7961ff6790fc/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130032403-02364be090ab h1:0IqUG+S8pNDjMRnhtxAjuSu2yyotEWc+mKhLpPWSJLU= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130032403-02364be090ab/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 742b01402d7d685e79386b3cf7093060cc4b69b4..3f0178914988a70c4dad024d3fe0197f7989c4af 100644 --- a/server/main.go +++ b/server/main.go @@ -41,9 +41,6 @@ func main() { server := router.InitRouter() global.GlobalClient = client.DefaultClient(plugin.Init(config.Config().PluginRedis, config.Config().RedisServer)) - //可用通信的方式获取服务端地址 - global.GlobalClient.Server = config.Config().PilotGoServer.Addr - global.GlobalClient.OnGetTags(func(uuids []string) []common.Tag { var tags []common.Tag for _, uuid := range uuids { 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 60c8a9d0d7812d9ae305b8b84211b4e942c71aea..c35c46054a03d0853a519bd3ed67893c24133717 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 @@ -8,9 +8,11 @@ import ( type GetTagsCallback func([]string) []common.Tag type Client struct { - Server string PluginInfo *PluginInfo + // 远程PilotGo server地址 + server string + // 用于event消息处理 eventChan chan *common.EventMessage eventCallbackMap map[int]EventCallback @@ -27,11 +29,8 @@ type Client struct { } var global_client *Client -var BaseInfo *PluginInfo func DefaultClient(desc *PluginInfo) *Client { - BaseInfo = desc - global_client = &Client{ PluginInfo: desc, @@ -49,13 +48,21 @@ func GetClient() *Client { return global_client } +func (client *Client) Server() string { + return client.server +} + // RegisterHandlers 注册一些插件标准的API接口,清单如下: // GET /plugin_manage/info func (client *Client) RegisterHandlers(router *gin.Engine) { // 提供插件基本信息 - mg := router.Group("/plugin_manage/") + mg := router.Group("/plugin_manage/", func(c *gin.Context) { + c.Set("__internal__client_instance", client) + }) { mg.GET("/info", InfoHandler) + // 绑定PilotGo server + mg.PUT("/bind", BindHandler) } api := router.Group("/plugin_manage/api/v1/") diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go index cf87dbce49a9644e47e9824f23b3e0dda1770d9f..35dcdfd5ebb4cb8f6d99b63ec377f666f046c377 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go @@ -9,7 +9,7 @@ import ( ) func (c *Client) ApplyConfig(batch *common.Batch, path, content string) error { - url := c.Server + "/api/v1/pluginapi/apply_config" + url := c.Server() + "/api/v1/pluginapi/apply_config" r, err := httputils.Put(url, nil) if err != nil { return err diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/event.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/event.go index 6ad1ef727c08e7f925a4e6593b480c058a3593a4..4ec5f1b73fb3fbccd27662547302449161743400 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/event.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/event.go @@ -20,7 +20,7 @@ func (c *Client) ListenEvent(eventTypes []int, callbacks []EventCallback) error eventtypes = append(eventtypes, strconv.Itoa(i)) } - url := c.Server + "/api/v1/pluginapi/listener?eventTypes=" + strings.Join(eventtypes, ",") + url := c.Server() + "/api/v1/pluginapi/listener?eventTypes=" + strings.Join(eventtypes, ",") r, err := httputils.Put(url, &httputils.Params{ Body: c.PluginInfo, }) @@ -59,7 +59,7 @@ func (c *Client) UnListenEvent(eventTypes []int) error { eventtypes = append(eventtypes, strconv.Itoa(i)) } - url := c.Server + "/api/v1/pluginapi/listener?eventTypes=" + strings.Join(eventtypes, ",") + url := c.Server() + "/api/v1/pluginapi/listener?eventTypes=" + strings.Join(eventtypes, ",") r, err := httputils.Delete(url, &httputils.Params{ Body: c.PluginInfo, }) @@ -94,7 +94,7 @@ func (c *Client) UnListenEvent(eventTypes []int) error { // 发布event事件 func (c *Client) PublishEvent(msg common.EventMessage) error { - url := c.Server + "/api/v1/pluginapi/publish_event" + url := c.Server() + "/api/v1/pluginapi/publish_event" r, err := httputils.Put(url, &httputils.Params{ Body: &msg, }) 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 e90a705b2c4e17bcae6b332ed2c34ff4c0d0a2a7..2ad545b170e80a832edf221383ccaf87543ec7bd 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 @@ -34,7 +34,41 @@ func ReverseProxyHandler(c *gin.Context) { } func InfoHandler(c *gin.Context) { - c.JSON(http.StatusOK, BaseInfo) + 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 + } + + info := &PluginFullInfo{ + PluginInfo: *client.PluginInfo, + Extentions: client.extentions, + } + + c.JSON(http.StatusOK, info) +} + +func BindHandler(c *gin.Context) { + port := c.Query("port") + + 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 + } + client.server = strings.Split(c.Request.RemoteAddr, ":")[0] + ":" + port + + response.Success(c, nil, "bind server success") } func EventHandler(c *gin.Context) { @@ -69,7 +103,7 @@ func ExtentionsHandler(c *gin.Context) { } client, ok := v.(*Client) if !ok { - response.Fail(c, gin.H{"status": false}, "client获取失败") + response.Fail(c, gin.H{"status": false}, "client信息错误") return } 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 64bcdb54831935212e24b5cabba52cdd00d991ac..6e7fce91f67b5a977bfdd1e396f4b8e42eed6a08 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,7 +8,7 @@ import ( ) func (c *Client) MachineList() ([]*common.MachineNode, error) { - url := "http://" + 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 diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go index edee734f53118098e3dfeea38797fff37be11a60..b3a9f50d7c7edd93ecae05fd333549f2cb9f0792 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go @@ -3,9 +3,11 @@ package client import ( "encoding/json" + "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" ) +// 用于初始化Client type PluginInfo struct { Name string `json:"name"` Version string `json:"version"` @@ -17,8 +19,14 @@ type PluginInfo struct { ReverseDest string `json:"reverse_dest"` } +// 用于插件与PilotGo server通讯 +type PluginFullInfo struct { + PluginInfo + Extentions []*common.Extention +} + func (c *Client) GetPluginInfo(name string) (*PluginInfo, error) { - url := c.Server + "/api/v1/pluginapi/plugins" + url := c.Server() + "/api/v1/pluginapi/plugins" r, err := httputils.Get(url, nil) if err != nil { return nil, err 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 2b1038f9d304b856ef9430b6426fbce87a7b11f5..1477090acc7a6aa4514b0a0b8f9a785ed431e972 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 @@ -16,7 +16,7 @@ type CallbackHandler struct { type RunCommandCallback func([]*common.RunResult) func (c *Client) RunCommand(batch *common.Batch, cmd string) ([]*common.CmdResult, error) { - url := "http://" + c.Server + "/api/v1/pluginapi/run_command" + url := "http://" + c.Server() + "/api/v1/pluginapi/run_command" p := &common.CmdStruct{ Batch: batch, @@ -49,7 +49,7 @@ type ScriptStruct struct { } func (c *Client) RunScript(batch *common.Batch, script string, params []string) ([]*common.CmdResult, error) { - url := "http://" + c.Server + "/api/v1/pluginapi/run_script" + url := "http://" + c.Server() + "/api/v1/pluginapi/run_script" p := &ScriptStruct{ Batch: batch, @@ -77,7 +77,7 @@ func (c *Client) RunScript(batch *common.Batch, script string, params []string) } func (c *Client) RunCommandAsync(batch *common.Batch, cmd string, callback RunCommandCallback) error { - url := "http://" + c.Server + "/api/v1/pluginapi/run_command_async?plugin_name=" + c.PluginInfo.Name + url := "http://" + c.Server() + "/api/v1/pluginapi/run_command_async?plugin_name=" + c.PluginInfo.Name p := &common.CmdStruct{ Batch: batch, diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go index 2913507352d7ecb825ee2deab1f3f2cbbc426988..b9a9cbfe1ada9ebcc11adda2da5e4f5538fadaf9 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go @@ -8,7 +8,7 @@ import ( ) func (c *Client) ServiceStatus(batch *common.Batch, servicename string) ([]*common.ServiceResult, error) { - url := c.Server + "/api/v1/pluginapi/service/:name" + url := c.Server() + "/api/v1/pluginapi/service/:name" p := &common.ServiceStruct{ Batch: batch, @@ -31,7 +31,7 @@ func (c *Client) ServiceStatus(batch *common.Batch, servicename string) ([]*comm } func (c *Client) StartService(batch *common.Batch, serviceName string) ([]*common.ServiceResult, error) { - url := c.Server + "/api/v1/pluginapi/start_service" + url := c.Server() + "/api/v1/pluginapi/start_service" p := &common.ServiceStruct{ Batch: batch, @@ -54,7 +54,7 @@ func (c *Client) StartService(batch *common.Batch, serviceName string) ([]*commo } func (c *Client) StopService(batch *common.Batch, serviceName string) ([]*common.ServiceResult, error) { - url := c.Server + "/api/v1/pluginapi/stop_service" + url := c.Server() + "/api/v1/pluginapi/stop_service" p := &common.ServiceStruct{ Batch: batch, diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go index 1093f4857760886f1c87e7751d94dab10f5058cb..3c371e3f18b4a94345498293a0e8936c7e2caf5b 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go @@ -57,6 +57,7 @@ func request(method, url string, param *Params) (*Response, error) { } hc := &http.Client{Transport: &http.Transport{ + DisableKeepAlives: true, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, diff --git a/server/vendor/modules.txt b/server/vendor/modules.txt index f1c9f1882776965b90909a7e78e9b8bfc2825556..c3314efdeda56efe860213c6a73d48a307fbd228 100644 --- a/server/vendor/modules.txt +++ b/server/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110012917-3eceb3662098 +# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231130032403-02364be090ab ## explicit; go 1.17 gitee.com/openeuler/PilotGo/sdk/common gitee.com/openeuler/PilotGo/sdk/logger