From 4ede94edd7c269737c6fcbc6fa83b30664ac13cf Mon Sep 17 00:00:00 2001 From: Gzx1999 Date: Tue, 2 Jan 2024 18:13:49 +0800 Subject: [PATCH] update sdk to support latest platform functions --- go.mod | 2 +- go.sum | 2 + .../openeuler/PilotGo/sdk/common/machine.go | 13 ++-- .../openeuler/PilotGo/sdk/common/script.go | 6 +- .../openeuler/PilotGo/sdk/logger/logger.go | 16 ++--- .../PilotGo/sdk/plugin/client/bindsync.go | 15 +++++ .../PilotGo/sdk/plugin/client/client.go | 29 +++++++-- .../PilotGo/sdk/plugin/client/config.go | 3 + .../PilotGo/sdk/plugin/client/handler.go | 22 ++++--- .../PilotGo/sdk/plugin/client/heartbeat.go | 62 +++++++++++++++++++ .../PilotGo/sdk/plugin/client/machine.go | 4 ++ .../PilotGo/sdk/plugin/client/plugin.go | 4 ++ .../PilotGo/sdk/plugin/client/script.go | 13 +++- .../PilotGo/sdk/plugin/client/service.go | 10 +++ vendor/modules.txt | 2 +- 15 files changed, 168 insertions(+), 35 deletions(-) create mode 100644 vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go create mode 100644 vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go diff --git a/go.mod b/go.mod index b2153e9..8af55fe 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module openeuler.org/PilotGo/grafana-plugin go 1.17 require ( - gitee.com/openeuler/PilotGo/sdk v0.0.0-20231208073829-a1e2ae2fa48f + gitee.com/openeuler/PilotGo/sdk v0.0.0-20231229071059-61698c219eb3 github.com/gin-gonic/gin v1.9.1 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 8d4488c..745a2e6 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ gitee.com/openeuler/PilotGo/sdk v0.0.0-20231208073829-a1e2ae2fa48f h1:SSV11E2fBoKPdJH2SmCMtFLKjnW126lGtEknljmfBpo= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231208073829-a1e2ae2fa48f/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231229071059-61698c219eb3 h1:J3frP8uNIwxjTzbaP00tNmJVtQGtcwL0K6HRjZaxvQM= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20231229071059-61698c219eb3/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= 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/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go b/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go index e194752..33ef060 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go @@ -1,12 +1,13 @@ package common type MachineNode struct { - UUID string `json:"uuid"` - Department string `json:"department"` - IP string `json:"ip"` - CPUArch string `json:"cpu_arch"` - OS string `json:"os"` - State int `json:"state"` + UUID string `json:"uuid"` + Department string `json:"department"` + IP string `json:"ip"` + CPUArch string `json:"cpu_arch"` + OS string `json:"os"` + RunStatus string `json:"runstatus"` + MaintStatus string `json:"maintatatus"` } type Batch struct { diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go b/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go index 46a5312..8c61445 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go @@ -2,13 +2,9 @@ package common type AsyncCmdResult struct { TaskID string `json:"task_id"` - Result []*RunResult `json:"result"` + Result []*CmdResult `json:"result"` } -type RunResult struct { - CmdResult CmdResult `json:"cmd_result"` - Error interface{} `json:"error"` -} type CmdResult struct { MachineUUID string `json:"machine_uuid"` MachineIP string `json:"machine_ip"` diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go b/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go index e9cb796..32cc99b 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go @@ -143,12 +143,14 @@ func RequestLogger() gin.HandlerFunc { clientIP := c.ClientIP() // 日志格式 - Debug("status_code:%d latency_time:%s client_ip:%s req_method:%s req_uri:%s", - statusCode, - latencyTime, - clientIP, - reqMethod, - reqUri, - ) + if reqUri != "/api/v1/pluginapi/heartbeat" { + Debug("status_code:%d latency_time:%s client_ip:%s req_method:%s req_uri:%s", + statusCode, + latencyTime, + clientIP, + reqMethod, + reqUri, + ) + } } } diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go new file mode 100644 index 0000000..f73f97c --- /dev/null +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go @@ -0,0 +1,15 @@ +package client + +import ( + "gitee.com/openeuler/PilotGo/sdk/logger" +) + +func (c *Client) Wait4Bind() { + c.cond.L.Lock() + logger.Debug("等待bind中...") + for c.server == "" { + c.cond.Wait() // 等待条件变量被通知 + } + c.cond.L.Unlock() + logger.Debug("bind 成功!") +} diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go index c35c460..ab58665 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go @@ -1,6 +1,8 @@ package client import ( + "sync" + "gitee.com/openeuler/PilotGo/sdk/common" "github.com/gin-gonic/gin" ) @@ -9,6 +11,8 @@ type GetTagsCallback func([]string) []common.Tag type Client struct { PluginInfo *PluginInfo + // 并发锁 + l sync.Mutex // 远程PilotGo server地址 server string @@ -26,6 +30,10 @@ type Client struct { // 用于平台扩展点功能 extentions []*common.Extention + + //bind阻塞功能支持 + mu sync.Mutex + cond *sync.Cond } var global_client *Client @@ -40,6 +48,7 @@ func DefaultClient(desc *PluginInfo) *Client { asyncCmdResultChan: make(chan *common.AsyncCmdResult, 20), cmdProcessorCallbackMap: make(map[string]CallbackHandler), } + global_client.cond = sync.NewCond(&global_client.mu) return global_client } @@ -60,28 +69,28 @@ func (client *Client) RegisterHandlers(router *gin.Engine) { c.Set("__internal__client_instance", client) }) { - mg.GET("/info", InfoHandler) + mg.GET("/info", infoHandler) // 绑定PilotGo server - mg.PUT("/bind", BindHandler) + mg.PUT("/bind", bindHandler) } api := router.Group("/plugin_manage/api/v1/") { api.GET("/extentions", func(c *gin.Context) { c.Set("__internal__client_instance", client) - }, ExtentionsHandler) + }, extentionsHandler) api.GET("/gettags", func(c *gin.Context) { c.Set("__internal__client_instance", client) - }, TagsHandler) + }, tagsHandler) api.POST("/event", func(c *gin.Context) { c.Set("__internal__client_instance", client) - }, EventHandler) + }, eventHandler) api.PUT("/command_result", func(c *gin.Context) { c.Set("__internal__client_instance", client) - }, CommandResultHandler) + }, commandResultHandler) } // pg := router.Group("/plugin/" + desc.Name) @@ -100,3 +109,11 @@ func (client *Client) RegisterHandlers(router *gin.Engine) { func (client *Client) OnGetTags(callback GetTagsCallback) { client.getTagsCallback = callback } + +// client是否bind PilotGo server +func (client *Client) IsBind() bool { + client.l.Lock() + defer client.l.Unlock() + + return !(client.server == "") +} diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go index 35dcdfd..a23122e 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go @@ -9,6 +9,9 @@ import ( ) func (c *Client) ApplyConfig(batch *common.Batch, path, content string) error { + if !c.IsBind() { + return errors.New("unbind PilotGo-server platform") + } url := c.Server() + "/api/v1/pluginapi/apply_config" r, err := httputils.Put(url, nil) if err != nil { diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go index 2ad545b..1438def 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go @@ -33,7 +33,7 @@ func ReverseProxyHandler(c *gin.Context) { proxy.ServeHTTP(c.Writer, c.Request) } -func InfoHandler(c *gin.Context) { +func infoHandler(c *gin.Context) { v, ok := c.Get("__internal__client_instance") if !ok { response.Fail(c, gin.H{"status": false}, "未获取到client值信息") @@ -53,7 +53,7 @@ func InfoHandler(c *gin.Context) { c.JSON(http.StatusOK, info) } -func BindHandler(c *gin.Context) { +func bindHandler(c *gin.Context) { port := c.Query("port") v, ok := c.Get("__internal__client_instance") @@ -66,12 +66,18 @@ func BindHandler(c *gin.Context) { response.Fail(c, gin.H{"status": false}, "client信息错误") return } - client.server = strings.Split(c.Request.RemoteAddr, ":")[0] + ":" + port - + server := strings.Split(c.Request.RemoteAddr, ":")[0] + ":" + port + if client.server == "" { + client.server = server + } else if client.server != "" && client.server != server { + logger.Error("已有PilotGo-server与此插件绑定") + } + client.cond.Broadcast() + client.sendHeartBeat() response.Success(c, nil, "bind server success") } -func EventHandler(c *gin.Context) { +func eventHandler(c *gin.Context) { j, err := io.ReadAll(c.Request.Body) // 接收数据 if err != nil { logger.Error("没获取到:%s", err.Error()) @@ -95,7 +101,7 @@ func EventHandler(c *gin.Context) { client.ProcessEvent(&msg) } -func ExtentionsHandler(c *gin.Context) { +func extentionsHandler(c *gin.Context) { v, ok := c.Get("__internal__client_instance") if !ok { response.Fail(c, gin.H{"status": false}, "未获取到client值信息") @@ -110,7 +116,7 @@ func ExtentionsHandler(c *gin.Context) { response.Success(c, client.extentions, "") } -func CommandResultHandler(c *gin.Context) { +func commandResultHandler(c *gin.Context) { j, err := io.ReadAll(c.Request.Body) // 接收数据 if err != nil { logger.Error("没获取到:%s", err.Error()) @@ -137,7 +143,7 @@ func CommandResultHandler(c *gin.Context) { } -func TagsHandler(c *gin.Context) { +func tagsHandler(c *gin.Context) { j, err := io.ReadAll(c.Request.Body) // 接收数据 if err != nil { logger.Error("没获取到:%s", err.Error()) diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go new file mode 100644 index 0000000..b0e1697 --- /dev/null +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go @@ -0,0 +1,62 @@ +package client + +import ( + "encoding/json" + "fmt" + "net/http" + "time" + + "gitee.com/openeuler/PilotGo/sdk/logger" + "gitee.com/openeuler/PilotGo/sdk/utils/httputils" +) + +const ( + HeartbeatInterval = 30 * time.Second + HeartbeatKey = "heartbeat:" +) + +// 插件连接状态 +type PluginStatus struct { + Connected bool + LastConnect time.Time +} + +func (client *Client) sendHeartBeat() { + clientID := client.PluginInfo.Url + go func() { + for { + err := client.sendHeartbeat(clientID) + if err != nil { + logger.Error("Heartbeat failed:%v", err) + } + time.Sleep(HeartbeatInterval) + } + }() +} + +func (client *Client) sendHeartbeat(clientID string) error { + p := &struct { + PluginUrl string `json:"clientID"` + }{ + PluginUrl: clientID, + } + + ServerUrl := "http://" + client.Server() + "/api/v1/pluginapi/heartbeat" + resp, err := httputils.Post(ServerUrl, &httputils.Params{ + Body: p, + }) + if err != nil { + return err + } + res := &struct { + Code int `json:"code"` + Message string `json:"msg"` + }{} + if err := json.Unmarshal(resp.Body, res); err != nil { + return err + } + if res.Code != http.StatusOK { + return fmt.Errorf("heartbeat failed with status: %v", res.Code) + } + return nil +} diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go index 6e7fce9..34acd4b 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go @@ -2,12 +2,16 @@ package client import ( "encoding/json" + "errors" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" ) func (c *Client) MachineList() ([]*common.MachineNode, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := "http://" + c.Server() + "/api/v1/pluginapi/machine_list" r, err := httputils.Get(url, nil) if err != nil { diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go index b3a9f50..f63c429 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go @@ -2,6 +2,7 @@ package client import ( "encoding/json" + "errors" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" @@ -26,6 +27,9 @@ type PluginFullInfo struct { } func (c *Client) GetPluginInfo(name string) (*PluginInfo, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := c.Server() + "/api/v1/pluginapi/plugins" r, err := httputils.Get(url, nil) if err != nil { diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go index 1477090..5809e5c 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go @@ -3,6 +3,7 @@ package client import ( "encoding/base64" "encoding/json" + "errors" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" @@ -13,9 +14,13 @@ type CallbackHandler struct { TaskLen int } -type RunCommandCallback func([]*common.RunResult) +type RunCommandCallback func([]*common.CmdResult) func (c *Client) RunCommand(batch *common.Batch, cmd string) ([]*common.CmdResult, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } + url := "http://" + c.Server() + "/api/v1/pluginapi/run_command" p := &common.CmdStruct{ @@ -49,6 +54,9 @@ type ScriptStruct struct { } func (c *Client) RunScript(batch *common.Batch, script string, params []string) ([]*common.CmdResult, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := "http://" + c.Server() + "/api/v1/pluginapi/run_script" p := &ScriptStruct{ @@ -77,6 +85,9 @@ func (c *Client) RunScript(batch *common.Batch, script string, params []string) } func (c *Client) RunCommandAsync(batch *common.Batch, cmd string, callback RunCommandCallback) error { + if !c.IsBind() { + return errors.New("unbind PilotGo-server platform") + } url := "http://" + c.Server() + "/api/v1/pluginapi/run_command_async?plugin_name=" + c.PluginInfo.Name p := &common.CmdStruct{ diff --git a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go index b9a9cbf..8790c9a 100644 --- a/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go +++ b/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go @@ -2,12 +2,16 @@ package client import ( "encoding/json" + "errors" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" ) func (c *Client) ServiceStatus(batch *common.Batch, servicename string) ([]*common.ServiceResult, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := c.Server() + "/api/v1/pluginapi/service/:name" p := &common.ServiceStruct{ @@ -31,6 +35,9 @@ func (c *Client) ServiceStatus(batch *common.Batch, servicename string) ([]*comm } func (c *Client) StartService(batch *common.Batch, serviceName string) ([]*common.ServiceResult, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := c.Server() + "/api/v1/pluginapi/start_service" p := &common.ServiceStruct{ @@ -54,6 +61,9 @@ func (c *Client) StartService(batch *common.Batch, serviceName string) ([]*commo } func (c *Client) StopService(batch *common.Batch, serviceName string) ([]*common.ServiceResult, error) { + if !c.IsBind() { + return nil, errors.New("unbind PilotGo-server platform") + } url := c.Server() + "/api/v1/pluginapi/stop_service" p := &common.ServiceStruct{ diff --git a/vendor/modules.txt b/vendor/modules.txt index ad94d63..ded544f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231208073829-a1e2ae2fa48f +# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231229071059-61698c219eb3 ## explicit; go 1.17 gitee.com/openeuler/PilotGo/sdk/common gitee.com/openeuler/PilotGo/sdk/logger -- Gitee