From d28040ec53ab1fed660d8868b0d16514c79a5a8e Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Wed, 10 Jan 2024 16:23:18 +0800 Subject: [PATCH] update vendor and add extentions;fix init prometheus yml --- server/config.yml.templete | 2 +- server/go.mod | 2 +- server/go.sum | 2 + server/httphandler/runresult.go | 11 ++- server/main.go | 3 +- server/service/extentions.go | 27 +++++++ .../init_prometheus/init_prometheus.go | 6 +- server/service/init_prometheus/ymlhash.go | 9 ++- server/service/runresult.go | 23 +++--- server/service/tags.go | 4 +- .../openeuler/PilotGo/sdk/common/extention.go | 77 ++++++++++++++++++- .../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 | 31 ++++++-- .../PilotGo/sdk/plugin/client/config.go | 3 + .../PilotGo/sdk/plugin/client/extention.go | 4 +- .../PilotGo/sdk/plugin/client/handler.go | 24 ++++-- .../PilotGo/sdk/plugin/client/heartbeat.go | 62 +++++++++++++++ .../PilotGo/sdk/plugin/client/machine.go | 4 + .../PilotGo/sdk/plugin/client/plugin.go | 6 +- .../PilotGo/sdk/plugin/client/script.go | 13 +++- .../PilotGo/sdk/plugin/client/service.go | 10 +++ .../PilotGo/sdk/utils/httputils/request.go | 1 + server/vendor/modules.txt | 2 +- 26 files changed, 309 insertions(+), 67 deletions(-) create mode 100644 server/service/extentions.go create mode 100644 server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go create mode 100644 server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go diff --git a/server/config.yml.templete b/server/config.yml.templete index 6fc471b..a6848aa 100644 --- a/server/config.yml.templete +++ b/server/config.yml.templete @@ -1,5 +1,5 @@ plugin_prometheus: - url: "http://localhost:8090/plugin/prometheus" + url: "http://localhost:8090" plugin_type: "iframe" # iframe micro-app prometheus_server: addr: "localhost:9090" diff --git a/server/go.mod b/server/go.mod index d7bc47d..5449132 100644 --- a/server/go.mod +++ b/server/go.mod @@ -16,7 +16,7 @@ require ( ) require ( - gitee.com/openeuler/PilotGo/sdk v0.0.0-20231114113407-d762078e13db + gitee.com/openeuler/PilotGo/sdk v0.0.0-20240110060359-307cb097716f github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/server/go.sum b/server/go.sum index 67b79e0..f8ea20e 100644 --- a/server/go.sum +++ b/server/go.sum @@ -4,6 +4,8 @@ gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110131016-eda5d89f4a35 h1:Lcp6rAHmRu gitee.com/openeuler/PilotGo/sdk v0.0.0-20231110131016-eda5d89f4a35/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231114113407-d762078e13db h1:Mh5VnWRGJFI33V5Acx0tilefUKvLhjbXU2QBnBRjgFY= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231114113407-d762078e13db/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20240110060359-307cb097716f h1:DGwrH8ikENblWnxpTdF21OuQKcN3I4uLkp4oKi2nbuw= +gitee.com/openeuler/PilotGo/sdk v0.0.0-20240110060359-307cb097716f/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/server/httphandler/runresult.go b/server/httphandler/runresult.go index ad760f5..5c669b4 100644 --- a/server/httphandler/runresult.go +++ b/server/httphandler/runresult.go @@ -11,7 +11,9 @@ import ( // 运行远程命令安装、卸载exporter func RunCommand(c *gin.Context) { - d := &common.CmdStruct{} + d := &struct { + MachineUUIDs []string `json:"machine_uuids"` + }{} if err := c.ShouldBind(d); err != nil { logger.Debug("绑定批次参数失败:%s", err) response.Fail(c, nil, "parameter error") @@ -29,14 +31,17 @@ func RunCommand(c *gin.Context) { return } - run_result := func(result []*common.RunResult) { + run_result := func(result []*common.CmdResult) { for _, res := range result { if err := service.ProcessResult(res, command_type); err != nil { logger.Error("处理结果失败:%v", err.Error()) } } } - err := plugin.Client.RunCommandAsync(d.Batch, command, run_result) + dd := &common.Batch{ + MachineUUIDs: d.MachineUUIDs, + } + err := plugin.Client.RunCommandAsync(dd, command, run_result) if err != nil { logger.Error("远程调用失败:%v", err.Error()) response.Fail(c, nil, err.Error()) diff --git a/server/main.go b/server/main.go index 92f2887..d94daef 100644 --- a/server/main.go +++ b/server/main.go @@ -39,7 +39,8 @@ func main() { plugin.Client = client.DefaultClient(plugin.Init(config.Config().PluginPrometheus, config.Config().PrometheusServer)) router.RegisterAPIs(server) router.StaticRouter(server) - service.GetTags() //pilotgo机器列表tag标签 + service.GetTags() // pilotgo机器列表tag标签 + service.AddExtentions() // 添加扩展点 if err := server.Run(config.Config().HttpServer.Addr); err != nil { logger.Fatal("failed to run server: %v", err) diff --git a/server/service/extentions.go b/server/service/extentions.go new file mode 100644 index 0000000..ce33d7a --- /dev/null +++ b/server/service/extentions.go @@ -0,0 +1,27 @@ +package service + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "openeuler.org/PilotGo/prometheus-plugin/plugin" +) + +func AddExtentions() { + var ex []common.Extention + me1 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "安装exporter", + URL: "/plugin/prometheus/run?type=" + CommandInstall_Type, + } + me2 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "卸载exporter", + URL: "/plugin/prometheus/run?type=" + CommandRemove_Type, + } + pe := &common.PageExtention{ + Type: common.ExtentionPage, + Name: "plugin-prometheus", + IsIndex: true, + } + ex = append(ex, me1, me2, pe) + plugin.Client.RegisterExtention(ex) +} diff --git a/server/service/init_prometheus/init_prometheus.go b/server/service/init_prometheus/init_prometheus.go index 764144b..db868bc 100644 --- a/server/service/init_prometheus/init_prometheus.go +++ b/server/service/init_prometheus/init_prometheus.go @@ -10,7 +10,7 @@ import ( ) const ( - GlobalPrometheusYmlInit = "../../scripts/init_prometheus_yml.sh" + GlobalPrometheusYmlInit = "scripts/init_prometheus_yml.sh" GlobalPrometheusYml = "/etc/prometheus/prometheus.yml" ) @@ -22,7 +22,7 @@ func InitPrometheus(httpaddr string) error { return errors.New(`please install prometheus greater than 2.28`) } - ok, err := CheckYMLHash() + ok, err := CheckYMLHash(httpaddr) if ok && err == nil { err = initPrometheusYML(httpaddr) if err != nil { @@ -60,5 +60,5 @@ func initYML(httaddr string) error { if exitcode == 0 && stderr == "" && err == nil { return nil } - return err + return errors.New(stderr) } diff --git a/server/service/init_prometheus/ymlhash.go b/server/service/init_prometheus/ymlhash.go index 7938d1d..a150acf 100644 --- a/server/service/init_prometheus/ymlhash.go +++ b/server/service/init_prometheus/ymlhash.go @@ -6,10 +6,10 @@ import ( const ymlfile = "./.prometheus-yml.data" -func CheckYMLHash() (bool, error) { +func CheckYMLHash(httpaddr string) (bool, error) { if !utils.IsFileExist(ymlfile) { - err := reset() + err := reset(httpaddr) if err != nil { return false, err } @@ -27,7 +27,10 @@ func CheckYMLHash() (bool, error) { return currentContent != savedContent, nil } -func reset() error { +func reset(httpaddr string) error { + if err := initYML(httpaddr); err != nil { + return err + } bs, err := utils.FileReadString(GlobalPrometheusYml) if err != nil { return err diff --git a/server/service/runresult.go b/server/service/runresult.go index 21d304f..b83c20b 100644 --- a/server/service/runresult.go +++ b/server/service/runresult.go @@ -19,36 +19,33 @@ const ( CommandRemove_Cmd = "yum remove -y golang-github-prometheus-node_exporter && echo '卸载成功' || echo '卸载失败'" ) -func ProcessResult(res *common.RunResult, command_type string) error { - if res.Error != nil { - return errors.New(res.Error.(string)) - } +func ProcessResult(res *common.CmdResult, command_type string) error { result := &model.PrometheusTarget{ - UUID: res.CmdResult.MachineUUID, - TargetIP: res.CmdResult.MachineIP, + UUID: res.MachineUUID, + TargetIP: res.MachineIP, Port: "9100", } - ok, err := dao.IsExistTargetUUID(res.CmdResult.MachineUUID) + ok, err := dao.IsExistTargetUUID(res.MachineUUID) if err != nil { return err } if !ok && command_type == CommandInstall_Type && ResultOptStdout(res) { - if Err := dao.AddPrometheusTarget(result); Err != nil { - return errors.New("保存结果失败:" + Err.Error()) + if err := dao.AddPrometheusTarget(result); err != nil { + return errors.New("保存结果失败:" + err.Error()) } } if ok && command_type == CommandRemove_Type && ResultOptStdout(res) { - if Err := dao.DeletePrometheusTarget(result); Err != nil { - return errors.New("删除prometheus target失败: " + Err.Error()) + if err := dao.DeletePrometheusTarget(result); err != nil { + return errors.New("删除prometheus target失败: " + err.Error()) } } return nil } -func ResultOptStdout(res *common.RunResult) bool { - stdout := res.CmdResult.Stdout +func ResultOptStdout(res *common.CmdResult) bool { + stdout := res.Stdout for _, msg := range ResultOptMsg { if strings.Contains(stdout, msg) { return true diff --git a/server/service/tags.go b/server/service/tags.go index 52cbd7d..5910b9d 100644 --- a/server/service/tags.go +++ b/server/service/tags.go @@ -15,14 +15,14 @@ func GetTags() { tag := common.Tag{ UUID: uuid, Type: common.TypeOk, - Data: "已安装agent", + Data: "exporter", } tags = append(tags, tag) } else { tag := common.Tag{ UUID: uuid, Type: common.TypeError, - Data: "未安装agent", + Data: "", } tags = append(tags, tag) } diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go index 8f6d040..fa182ca 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go @@ -5,11 +5,84 @@ const ( ExtentionMachine = "machine" // 批处理扩展,增加选择批次时对批次的操作 ExtentionBatch = "batch" + // 主页面扩展,增加侧边栏入口及主页面 + ExtentionPage = "page" ) -type Extention struct { - PluginName string `json:"plugin_name"` +type Extention interface { + Clone() Extention +} + +type MachineExtention struct { + Type string `json:"type"` Name string `json:"name"` + URL string `json:"url"` + Permission string `json:"permission"` +} + +type BatchExtention struct { + Type string `json:"type"` + Name string `json:"name"` + URL string `json:"url"` + Permission string `json:"permission"` +} + +type PageExtention struct { Type string `json:"type"` + Name string `json:"name"` + IsIndex bool `json:"is_index"` URL string `json:"url"` + Permission string `json:"permission"` +} + +func (me *MachineExtention) Clone() Extention { + result := *me + return &result +} + +func (be *BatchExtention) Clone() Extention { + result := *be + return &result +} + +func (pe *PageExtention) Clone() Extention { + result := *pe + return &result +} + +// 解析extentions参数 +func ParseParameters(data []map[string]interface{}) []Extention { + var extentions []Extention + for _, v := range data { + switch v["type"] { + case ExtentionMachine: + me := &MachineExtention{ + Name: v["name"].(string), + URL: v["url"].(string), + Permission: v["permission"].(string), + Type: v["type"].(string), + } + extentions = append(extentions, me) + + case ExtentionBatch: + be := &BatchExtention{ + Name: v["name"].(string), + URL: v["url"].(string), + Permission: v["permission"].(string), + Type: v["type"].(string), + } + extentions = append(extentions, be) + + case ExtentionPage: + pe := &PageExtention{ + Name: v["name"].(string), + URL: v["url"].(string), + Permission: v["permission"].(string), + Type: v["type"].(string), + IsIndex: v["is_index"].(bool), + } + extentions = append(extentions, pe) + } + } + return extentions } diff --git a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go index e194752..33ef060 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/machine.go +++ b/server/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/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go index 46a5312..8c61445 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/script.go +++ b/server/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/server/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go index e9cb796..32cc99b 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/logger/logger.go +++ b/server/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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go new file mode 100644 index 0000000..f73f97c --- /dev/null +++ b/server/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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go index c35c460..fb02875 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 @@ -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 @@ -25,7 +29,11 @@ type Client struct { getTagsCallback GetTagsCallback // 用于平台扩展点功能 - extentions []*common.Extention + 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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go index 35dcdfd..a23122e 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,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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go index 4460e21..a0d233e 100644 --- a/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go +++ b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go @@ -4,6 +4,6 @@ import ( "gitee.com/openeuler/PilotGo/sdk/common" ) -func (c *Client) RegisterExtention(exts []*common.Extention) { - c.extentions = exts +func (c *Client) RegisterExtention(exts []common.Extention) { + c.extentions = append(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 b72388d..1438def 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 @@ -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,9 @@ 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") if !ok { response.Fail(c, gin.H{"status": false}, "未获取到client值信息") @@ -64,12 +66,18 @@ func BindHandler(c *gin.Context) { response.Fail(c, gin.H{"status": false}, "client信息错误") return } - client.server = c.Request.RemoteAddr - + 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()) @@ -93,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值信息") @@ -108,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()) @@ -135,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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/heartbeat.go new file mode 100644 index 0000000..b0e1697 --- /dev/null +++ b/server/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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go index 6e7fce9..34acd4b 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 @@ -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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go index b3a9f50..c009d55 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 @@ -2,6 +2,7 @@ package client import ( "encoding/json" + "errors" "gitee.com/openeuler/PilotGo/sdk/common" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" @@ -22,10 +23,13 @@ type PluginInfo struct { // 用于插件与PilotGo server通讯 type PluginFullInfo struct { PluginInfo - Extentions []*common.Extention + Extentions []common.Extention } 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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go index 1477090..5809e5c 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 @@ -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/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go index b9a9cbf..8790c9a 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 @@ -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/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go b/server/vendor/gitee.com/openeuler/PilotGo/sdk/utils/httputils/request.go index 1093f48..3c371e3 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 b142087..0a27cfd 100644 --- a/server/vendor/modules.txt +++ b/server/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231114113407-d762078e13db +# gitee.com/openeuler/PilotGo/sdk v0.0.0-20240110060359-307cb097716f ## explicit; go 1.17 gitee.com/openeuler/PilotGo/sdk/common gitee.com/openeuler/PilotGo/sdk/logger -- Gitee