diff --git a/atune/server/controller/atunemanage.go b/atune/server/controller/atunemanage.go new file mode 100644 index 0000000000000000000000000000000000000000..ed1b2adacfea8614f718af8180eac395761fa0a2 --- /dev/null +++ b/atune/server/controller/atunemanage.go @@ -0,0 +1,71 @@ +package controller + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/logger" + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/atune-plugin/plugin" + "openeuler.org/PilotGo/atune-plugin/service" +) + +func AtuneClientInstall(c *gin.Context) { + d := &struct { + MachineUUIDs []string `json:"uuids"` + }{} + if err := c.ShouldBind(d); err != nil { + logger.Debug("绑定批次参数失败:%s", err) + response.Fail(c, nil, "parameter error") + return + } + + run_result := func(result []*common.CmdResult) { + for _, res := range result { + logger.Info("结果:%v", *res) + if err := service.AtuneManage(res, service.CommandInstall_Type); err != nil { + logger.Error("处理结果失败:%v", err.Error()) + } + } + } + + dd := &common.Batch{ + MachineUUIDs: d.MachineUUIDs, + } + err := plugin.GlobalClient.RunCommandAsync(dd, service.CommandInstall_Cmd, run_result) + if err != nil { + logger.Error("远程调用失败:%v", err.Error()) + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "指令下发完成") +} +func AtuneClientRemove(c *gin.Context) { + d := &struct { + MachineUUIDs []string `json:"uuids"` + }{} + if err := c.ShouldBind(d); err != nil { + logger.Debug("绑定批次参数失败:%s", err) + response.Fail(c, nil, "parameter error") + return + } + + run_result := func(result []*common.CmdResult) { + for _, res := range result { + logger.Info("结果:%v", *res) + if err := service.AtuneManage(res, service.CommandUninstall_Type); err != nil { + logger.Error("处理结果失败:%v", err.Error()) + } + } + } + + dd := &common.Batch{ + MachineUUIDs: d.MachineUUIDs, + } + err := plugin.GlobalClient.RunCommandAsync(dd, service.CommandUninstall_Cmd, run_result) + if err != nil { + logger.Error("远程调用失败:%v", err.Error()) + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "指令下发完成") +} diff --git a/atune/server/controller/runresult.go b/atune/server/controller/runresult.go index 093420f021e6079a9e0dde87f54ad92e2e9ec72f..c54c0f19eeeea8bb215c5fc950b443d3db1712f8 100644 --- a/atune/server/controller/runresult.go +++ b/atune/server/controller/runresult.go @@ -1,53 +1,11 @@ package controller import ( - "gitee.com/openeuler/PilotGo/sdk/common" - "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" - "openeuler.org/PilotGo/atune-plugin/plugin" "openeuler.org/PilotGo/atune-plugin/service" ) -func RunCommand(c *gin.Context) { - d := &struct { - MachineUUIDs []string `json:"machine_uuids"` - Command string `json:"command"` - TuneID int `json:"tune_id"` - TaskName string `json:"task_name"` - }{} - if err := c.ShouldBind(d); err != nil { - logger.Debug("绑定批次参数失败:%s", err) - response.Fail(c, nil, "parameter error") - return - } - dbtaskid, err := service.SaveTask(d.Command, d.TaskName, d.MachineUUIDs, d.TuneID) - if err != nil { - response.Fail(c, nil, err.Error()) - return - } - - run_result := func(result []*common.CmdResult) { - for _, res := range result { - logger.Info("结果:%v", *res) - if err := service.ProcessResult(res, d.Command, dbtaskid); err != nil { - logger.Error("处理结果失败:%v", err.Error()) - } - } - } - - dd := &common.Batch{ - MachineUUIDs: d.MachineUUIDs, - } - err = plugin.GlobalClient.RunCommandAsync(dd, d.Command, run_result) - if err != nil { - logger.Error("远程调用失败:%v", err.Error()) - response.Fail(c, nil, err.Error()) - return - } - response.Success(c, nil, "指令下发完成") -} - func DeleteResult(c *gin.Context) { resultdel := struct { ResultID []int `json:"ids"` diff --git a/atune/server/controller/task.go b/atune/server/controller/task.go index f8a6cb4e29f2165fb1b88af5d629c5378c44c479..48755f825b8a64c809f78ccac13472dfdab8ef68 100644 --- a/atune/server/controller/task.go +++ b/atune/server/controller/task.go @@ -1,11 +1,53 @@ package controller import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/atune-plugin/plugin" "openeuler.org/PilotGo/atune-plugin/service" ) +func CreatTask(c *gin.Context) { + d := &struct { + MachineUUIDs []string `json:"machine_uuids"` + Command string `json:"command"` + TuneID int `json:"tune_id"` + TaskName string `json:"task_name"` + }{} + if err := c.ShouldBind(d); err != nil { + logger.Debug("绑定批次参数失败:%s", err) + response.Fail(c, nil, "parameter error") + return + } + dbtaskid, err := service.SaveTask(d.Command, d.TaskName, d.MachineUUIDs, d.TuneID) + if err != nil { + response.Fail(c, nil, err.Error()) + return + } + + run_result := func(result []*common.CmdResult) { + for _, res := range result { + logger.Info("结果:%v", *res) + if err := service.ProcessResult(res, d.Command, dbtaskid); err != nil { + logger.Error("处理结果失败:%v", err.Error()) + } + } + } + + dd := &common.Batch{ + MachineUUIDs: d.MachineUUIDs, + } + err = plugin.GlobalClient.RunCommandAsync(dd, d.Command, run_result) + if err != nil { + logger.Error("远程调用失败:%v", err.Error()) + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "指令下发完成") +} + func TaskLists(c *gin.Context) { query := &response.PaginationQ{} err := c.ShouldBindQuery(query) diff --git a/atune/server/dao/atunemanagedao.go b/atune/server/dao/atunemanagedao.go new file mode 100644 index 0000000000000000000000000000000000000000..ed06308ce6e3e35bbfef87bad84ed7d556df4c22 --- /dev/null +++ b/atune/server/dao/atunemanagedao.go @@ -0,0 +1,38 @@ +package dao + +import ( + "errors" + + "openeuler.org/PilotGo/atune-plugin/db" + "openeuler.org/PilotGo/atune-plugin/model" +) + +func IsExist(uuid string) (bool, error) { + var ac model.AtuneClient + err := db.MySQL().Where("machine_uuid = ?", uuid).Find(&ac).Error + if err != nil { + return false, errors.New("查询数据库失败:" + err.Error()) + } + return ac.ID != 0, nil +} + +func AddAtuneClientList(ac *model.AtuneClient) error { + a := model.AtuneClient{ + MachineUUID: ac.MachineUUID, + MachineIP: ac.MachineIP, + } + err := db.MySQL().Save(&a).Error + if err != nil { + return err + } + return nil +} + +func DeleteAtuneClientList(ac *model.AtuneClient) error { + var a model.AtuneClient + err := db.MySQL().Where("machine_uuid = ?", ac.MachineUUID).Unscoped().Delete(a).Error + if err != nil { + return err + } + return nil +} diff --git a/atune/server/dao/taskdao.go b/atune/server/dao/taskdao.go index 96906519426d58f9b56db6e537c4a6e0fa01f6cd..a8325169d043d16782d3634bb23f427ce6151e7c 100644 --- a/atune/server/dao/taskdao.go +++ b/atune/server/dao/taskdao.go @@ -5,10 +5,15 @@ import ( "gitee.com/openeuler/PilotGo/sdk/response" "openeuler.org/PilotGo/atune-plugin/db" - "openeuler.org/PilotGo/atune-plugin/global" "openeuler.org/PilotGo/atune-plugin/model" ) +const ( + // 任务状态 + Completed = "已完成" + Executing = "执行中" +) + func QueryTaskLists(query *response.PaginationQ) ([]*model.Tasks, int64, error) { var tasks []*model.Tasks if err := db.MySQL().Preload("Tune").Preload("RunResults").Order("id desc").Limit(query.PageSize).Offset((query.Page - 1) * query.PageSize).Find(&tasks).Error; err != nil { @@ -31,7 +36,7 @@ func SaveTask(task *model.Tasks) (int, error) { func UpdateTask(dbtaskid int) error { var t model.Tasks task := model.Tasks{ - TaskStatus: global.Completed, + TaskStatus: Completed, UpdateTime: time.Now().Format("2006-01-02 15:04:05"), } if err := db.MySQL().Model(&t).Where("id = ?", dbtaskid).Updates(&task).Error; err != nil { diff --git a/atune/server/db/db.go b/atune/server/db/db.go index ff4482f3154261c1de04e02f68ae61f078224046..3a145d0d966a1363999db594420e5cae607d9630 100644 --- a/atune/server/db/db.go +++ b/atune/server/db/db.go @@ -44,6 +44,7 @@ func MysqldbInit(conf *config.MysqlDBInfo) error { MySQL().AutoMigrate(&model.Tunes{}) MySQL().AutoMigrate(&model.RunResult{}) MySQL().AutoMigrate(&model.Tasks{}) + MySQL().AutoMigrate(&model.AtuneClient{}) return nil } diff --git a/atune/server/global/global.go b/atune/server/global/global.go deleted file mode 100644 index d7636f8b7434f31f4a2b3e00273fb65da48fbd31..0000000000000000000000000000000000000000 --- a/atune/server/global/global.go +++ /dev/null @@ -1,7 +0,0 @@ -package global - -const ( - // 任务状态 - Completed = "已完成" - Executing = "执行中" -) diff --git a/atune/server/go.mod b/atune/server/go.mod index 9a4026bfd3c0204af31759307f948b89713364ba..c25a5c6a3cbdb7a9bccc6b8b6a6c059257ecb424 100644 --- a/atune/server/go.mod +++ b/atune/server/go.mod @@ -3,7 +3,7 @@ module openeuler.org/PilotGo/atune-plugin go 1.18 require ( - gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227075111-fc150174f73a + gitee.com/openeuler/PilotGo/sdk v0.0.0-20240110060359-307cb097716f github.com/gin-gonic/gin v1.9.1 gopkg.in/yaml.v2 v2.4.0 gorm.io/driver/mysql v1.5.1 diff --git a/atune/server/go.sum b/atune/server/go.sum index 7c7a7c5fe1ade9522390617df9d04b85ffdb08a7..98e89cb3f67fc5b4d0e0feca690a4b46cf4ad295 100644 --- a/atune/server/go.sum +++ b/atune/server/go.sum @@ -12,6 +12,8 @@ gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227070920-c289614a2e10 h1:RUbvPYH43b gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227070920-c289614a2e10/go.mod h1:zEVmY3l48/CvR2kIM3DJpD/jWTG1qkOxRoHJii/D0us= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227075111-fc150174f73a h1:vCMqSAe5nX8dX1tGH1MwTlMWljcziEV6Gw83Gu6KYqE= gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227075111-fc150174f73a/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/atune/server/main.go b/atune/server/main.go index 8cd555f1be5ae11234a5e9d046992af1eebd2d15..315e392e768fd7d991a0968774a67e63704340b0 100644 --- a/atune/server/main.go +++ b/atune/server/main.go @@ -12,6 +12,7 @@ import ( "openeuler.org/PilotGo/atune-plugin/db" "openeuler.org/PilotGo/atune-plugin/plugin" "openeuler.org/PilotGo/atune-plugin/router" + "openeuler.org/PilotGo/atune-plugin/service" ) func main() { @@ -30,6 +31,8 @@ func main() { } plugin.GlobalClient = client.DefaultClient(plugin.Init(config.Config().PluginAtune)) + service.GetTags() + service.AddExtentions() err := router.HttpServerInit(config.Config().HttpServer) if err != nil { diff --git a/atune/server/model/common.go b/atune/server/model/common.go index f04696defbd9d5cef624245bca4687f2c3e91286..13e3da95e40c7a778c883f0f6b240b9f86382007 100644 --- a/atune/server/model/common.go +++ b/atune/server/model/common.go @@ -37,3 +37,9 @@ type Tasks struct { RunResults []RunResult `gorm:"foreignKey:TaskID;constraint:OnDelete:CASCADE;" json:"results"` Tune Tunes `gorm:"foreignKey:TuneID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"tune"` } + +type AtuneClient struct { + ID int `gorm:"primary_key;AUTO_INCREMENT" json:"id"` + MachineUUID string `json:"machine_uuid"` + MachineIP string `json:"machine_ip"` +} diff --git a/atune/server/router/router.go b/atune/server/router/router.go index 2b977e087eb76862b42193f2404b8b89a3a2911b..01f9c0b02f19465d1511e2c4d3971fc63538c140 100644 --- a/atune/server/router/router.go +++ b/atune/server/router/router.go @@ -42,6 +42,8 @@ func registerAPIs(router *gin.Engine) { atune := router.Group("/plugin/" + plugin.GlobalClient.PluginInfo.Name) { + atune.POST("atune_install", controller.AtuneClientInstall) + atune.POST("atune_uninstall", controller.AtuneClientRemove) atune.GET("all", controller.GetAtuneAll) atune.GET("info", controller.GetAtuneInfo) } @@ -57,15 +59,14 @@ func registerAPIs(router *gin.Engine) { task := router.Group("/plugin/" + plugin.GlobalClient.PluginInfo.Name) { + task.POST("task_new", controller.CreatTask) task.GET("tasks", controller.TaskLists) - task.DELETE("task_delete", controller.DeleteTask) task.GET("task_search", controller.SearchTask) } restune := router.Group("/plugin/" + plugin.GlobalClient.PluginInfo.Name) { - restune.POST("run", controller.RunCommand) restune.DELETE("result_delete", controller.DeleteResult) restune.GET("result_search", controller.SearchResult) } diff --git a/atune/server/service/atunemanageservice.go b/atune/server/service/atunemanageservice.go new file mode 100644 index 0000000000000000000000000000000000000000..09e04047cf44d24001647408bd197ad854f28893 --- /dev/null +++ b/atune/server/service/atunemanageservice.go @@ -0,0 +1,55 @@ +package service + +import ( + "errors" + "strings" + + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/logger" + "openeuler.org/PilotGo/atune-plugin/dao" + "openeuler.org/PilotGo/atune-plugin/model" +) + +var ResultOptMsg = []string{"安装成功", "卸载成功"} + +const ( + CommandInstall_Type = "install" + CommandUninstall_Type = "uninstall" + + CommandInstall_Cmd = "yum install -y golang-github-prometheus-node_exporter && (echo '安装成功'; systemctl start node_exporter) || echo '安装失败'" + CommandUninstall_Cmd = "yum remove -y golang-github-prometheus-node_exporter && echo '卸载成功' || echo '卸载失败'" +) + +func AtuneManage(res *common.CmdResult, command_type string) error { + result := &model.AtuneClient{ + MachineUUID: res.MachineUUID, + MachineIP: res.MachineIP, + } + logger.Info("A-Tune客户端安装状态:\n%v", res) + ok, err := dao.IsExist(res.MachineUUID) + if err != nil { + return err + } + if !ok && command_type == CommandInstall_Type && resultOptStdout(res) { + if err := dao.AddAtuneClientList(result); err != nil { + return errors.New("保存结果失败:" + err.Error()) + } + } + if ok && command_type == CommandUninstall_Type && resultOptStdout(res) { + if err := dao.DeleteAtuneClientList(result); err != nil { + return errors.New("删除prometheus target失败: " + err.Error()) + } + } + + return nil +} + +func resultOptStdout(res *common.CmdResult) bool { + stdout := res.Stdout + for _, msg := range ResultOptMsg { + if strings.Contains(stdout, msg) { + return true + } + } + return false +} diff --git a/atune/server/service/extentions.go b/atune/server/service/extentions.go new file mode 100644 index 0000000000000000000000000000000000000000..430b09adbfff10f88c436ada9cc1c1b8e014a760 --- /dev/null +++ b/atune/server/service/extentions.go @@ -0,0 +1,36 @@ +package service + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "openeuler.org/PilotGo/atune-plugin/plugin" +) + +func AddExtentions() { + var ex []common.Extention + me1 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "安装a-tune", + URL: "/plugin/atune/atune_install", + Permission: "plugin.atune.agent/install", + } + me2 := &common.MachineExtention{ + Type: common.ExtentionMachine, + Name: "卸载a-tune", + URL: "/plugin/atune/atune_uninstall", + Permission: "plugin.atune.agent/uninstall", + } + pe1 := &common.PageExtention{ + Type: common.ExtentionPage, + Name: "plugin-atune", + URL: "/plugin/atune/task", + Permission: "plugin.prometheus.page/menu", + } + pe2 := &common.PageExtention{ + Type: common.ExtentionPage, + Name: "plugin-atune", + URL: "/plugin/atune/template", + Permission: "plugin.prometheus.page/menu", + } + ex = append(ex, me1, me2, pe1, pe2) + plugin.GlobalClient.RegisterExtention(ex) +} diff --git a/atune/server/service/tags.go b/atune/server/service/tags.go new file mode 100644 index 0000000000000000000000000000000000000000..a65f421be7db5fbe4422b6a4063ca6619ed0e0f5 --- /dev/null +++ b/atune/server/service/tags.go @@ -0,0 +1,33 @@ +package service + +import ( + "gitee.com/openeuler/PilotGo/sdk/common" + "openeuler.org/PilotGo/atune-plugin/dao" + "openeuler.org/PilotGo/atune-plugin/plugin" +) + +func GetTags() { + tag_cb := func(uuids []string) []common.Tag { + var tags []common.Tag + for _, uuid := range uuids { + ok, _ := dao.IsExist(uuid) + if ok { + tag := common.Tag{ + UUID: uuid, + Type: common.TypeOk, + Data: "atune", + } + tags = append(tags, tag) + } else { + tag := common.Tag{ + UUID: uuid, + Type: common.TypeError, + Data: "", + } + tags = append(tags, tag) + } + } + return tags + } + plugin.GlobalClient.OnGetTags(tag_cb) +} diff --git a/atune/server/service/taskservice.go b/atune/server/service/taskservice.go index 660bdd197a27f6826b7625c12f39d6b5dcf0b204..19d700173c761153cccea7042a6fcd8460837f9b 100644 --- a/atune/server/service/taskservice.go +++ b/atune/server/service/taskservice.go @@ -8,7 +8,6 @@ import ( "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/response" "openeuler.org/PilotGo/atune-plugin/dao" - "openeuler.org/PilotGo/atune-plugin/global" "openeuler.org/PilotGo/atune-plugin/model" ) @@ -25,7 +24,7 @@ func SaveTask(cmd string, task_name string, uuids []string, tuneId int) (int, er TaskName: task_name, TuneID: tuneId, Script: cmd, - TaskStatus: global.Executing, + TaskStatus: dao.Executing, CreateTime: time.Now().Format("2006-01-02 15:04:05"), } diff --git a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go index 8f6d040e4bea996789d685ef17340550565f4d28..fa182cacfb5678aa7e68818b026985986932094e 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/common/extention.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/bindsync.go new file mode 100644 index 0000000000000000000000000000000000000000..f73f97cd488d06e9d837aedd7f3b631182352508 --- /dev/null +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go index c35c46054a03d0853a519bd3ed67893c24133717..fb028757e39034efe31fc19f23213ae0249500b1 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/client.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go index 35dcdfd5ebb4cb8f6d99b63ec377f666f046c377..a23122edad4c3c2d276450ee1ec76d5caf62dd65 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/config.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go index 4460e210ba75263fd5c29d68b3b925ac7d7ea388..a0d233efcc79d4531f37b35a1270315f67882f53 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/extention.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go index a720319fcc8a12aa348347468a76c1c4481af824..1438deff2f444385baf73d2da24383fa21246243 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/handler.go +++ b/atune/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,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") @@ -72,11 +72,12 @@ func BindHandler(c *gin.Context) { } 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()) @@ -100,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值信息") @@ -115,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()) @@ -142,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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go index 6e7fce91f67b5a977bfdd1e396f4b8e42eed6a08..34acd4bf7bee29a4b5758444d48827d415ce3061 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/machine.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go index b3a9f50d7c7edd93ecae05fd333549f2cb9f0792..c009d55213537167c3ac8da5997aa011e67fe3b4 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/plugin.go +++ b/atune/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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go index e5fa25985118f7a57593fd144f20b1a5a9d2425b..5809e5c07ad6e08f1819690ee8057808a992b9b7 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/script.go +++ b/atune/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" @@ -16,6 +17,10 @@ type CallbackHandler struct { 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/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go b/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go index b9a9cbfe1ada9ebcc11adda2da5e4f5538fadaf9..8790c9a96715a5e43606be4f24ee1c591d1f15f3 100644 --- a/atune/server/vendor/gitee.com/openeuler/PilotGo/sdk/plugin/client/service.go +++ b/atune/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/atune/server/vendor/modules.txt b/atune/server/vendor/modules.txt index 127ad19d5cf3e688c3cd74154457f6eedcd578bf..45cc5ee3e94e2a14482ea00dd72872eec49af11a 100644 --- a/atune/server/vendor/modules.txt +++ b/atune/server/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/PilotGo/sdk v0.0.0-20231227075111-fc150174f73a +# 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