From 1954332dfbe8672f1b35c717ec75ae566bce9d4d Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Wed, 7 Jun 2023 14:32:11 +0800 Subject: [PATCH] Refactor gala-ops plugin based on Grafana plugin --- gala-ops/server/client/client.go | 30 ---------- gala-ops/server/config.yml.templete | 2 +- gala-ops/server/httphandler/agent.go | 16 +++--- gala-ops/server/httphandler/metricdata.go | 39 +++++++------ gala-ops/server/httphandler/pluginmanager.go | 15 ----- gala-ops/server/main.go | 60 +++++++++++++++----- gala-ops/server/plugin/prometheus.go | 14 ++--- 7 files changed, 80 insertions(+), 96 deletions(-) delete mode 100644 gala-ops/server/client/client.go delete mode 100644 gala-ops/server/httphandler/pluginmanager.go diff --git a/gala-ops/server/client/client.go b/gala-ops/server/client/client.go deleted file mode 100644 index 99a410da..00000000 --- a/gala-ops/server/client/client.go +++ /dev/null @@ -1,30 +0,0 @@ -package client - -import ( - "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" - "openeuler.org/PilotGo/gala-ops-plugin/config" -) - -const Version = "0.0.1" - -var globalClient *client.Client - -func init() { - globalClient = client.DefaultClient(&client.PluginInfo{ - Name: "gala-ops", - Version: Version, - Description: "gala-ops智能运维工具", - Author: "guozhengxin", - Email: "guozhengxin@kylinos.cn", - Url: "http://192.168.75.100:9999/plugin/gala-ops", - // ReverseDest: "http://192.168.48.163:3000/", - }, config.Config().Logopts) -} - -func Client() *client.Client { - return globalClient -} - -func StartClient(conf *config.HttpConf) { - globalClient.Serve(conf.Addr) -} diff --git a/gala-ops/server/config.yml.templete b/gala-ops/server/config.yml.templete index ff099e25..0a6da584 100644 --- a/gala-ops/server/config.yml.templete +++ b/gala-ops/server/config.yml.templete @@ -3,7 +3,7 @@ grafana: user: "" passwd: "" http: - addr: "0.0.0.0:7888" + addr: "0.0.0.0:9999" PilotGo: addr: "localhost:8889" mysql: diff --git a/gala-ops/server/httphandler/agent.go b/gala-ops/server/httphandler/agent.go index 7d165dc4..17fe8ef3 100644 --- a/gala-ops/server/httphandler/agent.go +++ b/gala-ops/server/httphandler/agent.go @@ -4,12 +4,12 @@ import ( "fmt" "net/http" + "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" - "openeuler.org/PilotGo/gala-ops-plugin/client" "openeuler.org/PilotGo/gala-ops-plugin/plugin" ) -func InstallGopher(ctx *gin.Context) { +func InstallGopher(ctx *gin.Context, client *client.Client) { // TODO param := &struct { Batch []string @@ -22,7 +22,7 @@ func InstallGopher(ctx *gin.Context) { } cmd := "yum install -y gala-gopher && systemctl start gala-gopher" - cmdResults, err := client.Client().RunScript(param.Batch, cmd) + cmdResults, err := client.RunScript(param.Batch, cmd) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": -1, @@ -55,7 +55,7 @@ func InstallGopher(ctx *gin.Context) { ret = append(ret, d) } - err = plugin.MonitorTargets(monitorTargets) + err = plugin.MonitorTargets(monitorTargets, client) if err != nil { fmt.Println("error: failed to add gala-gopher to prometheus monitor targets") } @@ -67,7 +67,7 @@ func InstallGopher(ctx *gin.Context) { }) } -func UpgradeGopher(ctx *gin.Context) { +func UpgradeGopher(ctx *gin.Context, client *client.Client) { // TODO param := &struct { Batch []string @@ -80,7 +80,7 @@ func UpgradeGopher(ctx *gin.Context) { } cmd := "systemctl stop gala-gopher && yum upgrade -y gala-gopher && systemctl start gala-gopher" - cmdResults, err := client.Client().RunScript(param.Batch, cmd) + cmdResults, err := client.RunScript(param.Batch, cmd) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": -1, @@ -115,7 +115,7 @@ func UpgradeGopher(ctx *gin.Context) { }) } -func UninstallGopher(ctx *gin.Context) { +func UninstallGopher(ctx *gin.Context, client *client.Client) { // TODO param := &struct { Batch []string @@ -128,7 +128,7 @@ func UninstallGopher(ctx *gin.Context) { } cmd := "systemctl stop gala-gopher && yum autoremove -y gala-gopher" - cmdResults, err := client.Client().RunScript(param.Batch, cmd) + cmdResults, err := client.RunScript(param.Batch, cmd) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "code": -1, diff --git a/gala-ops/server/httphandler/metricdata.go b/gala-ops/server/httphandler/metricdata.go index a15c4e84..a3b87ab3 100644 --- a/gala-ops/server/httphandler/metricdata.go +++ b/gala-ops/server/httphandler/metricdata.go @@ -1,17 +1,15 @@ package httphandler import ( - "encoding/json" "fmt" "strings" "time" "gitee.com/openeuler/PilotGo-plugins/sdk/logger" - "gitee.com/openeuler/PilotGo-plugins/sdk/utils" + "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/api" v1 "github.com/prometheus/client_golang/api/prometheus/v1" - "openeuler.org/PilotGo/gala-ops-plugin/client" ) type Plugin struct { @@ -41,24 +39,29 @@ func PrometheusAPI(URL string) (v1.API, error) { return v1api, nil } -func PrometheusMetrics(ctx *gin.Context) { - bs, err := utils.Request("GET", client.Client().Server+"plugins") - if err != nil { - logger.Error("faild to get plugin list: ", err) - } - plugins := &[]*Plugin{} - err = json.Unmarshal(bs, plugins) +func PrometheusMetrics(ctx *gin.Context, client *client.Client) { + // bs, err := utils.Request("GET", client.Server+"plugins") + // if err != nil { + // logger.Error("faild to get plugin list: ", err) + // } + // plugins := &[]*Plugin{} + // err = json.Unmarshal(bs, plugins) + // if err != nil { + // logger.Error("unmarshal request plugin info error:%s", err.Error()) + // } + // var Prometheus_addr string + // for _, p := range *plugins { + // if p.Name == "gala-ops" { + // Prometheus_addr = p.Url + // } + // } + plugin, err := client.GetPluginInfo("prometheus") if err != nil { - logger.Error("unmarshal request plugin info error:%s", err.Error()) - } - var Prometheus_addr string - for _, p := range *plugins { - if p.Name == "gala-ops" { - Prometheus_addr = p.Url - } + logger.Error("failed to get plugin info from pilotgoserver: ", err) + return } - promAPI, err := PrometheusAPI(strings.Split(Prometheus_addr, "/")[2]) + promAPI, err := PrometheusAPI(strings.Split(plugin.Url, "/")[2]) if err != nil { logger.Error("failed to create prometheus api: ", err) } diff --git a/gala-ops/server/httphandler/pluginmanager.go b/gala-ops/server/httphandler/pluginmanager.go deleted file mode 100644 index 589aa0d6..00000000 --- a/gala-ops/server/httphandler/pluginmanager.go +++ /dev/null @@ -1,15 +0,0 @@ -package httphandler - -import ( - "net/http" - - "github.com/gin-gonic/gin" - "openeuler.org/PilotGo/gala-ops-plugin/client" -) - -func PluginInfo(ctx *gin.Context) { - plugin_info := client.Client().Plugin - client.Client().Server = "http://" + ctx.Request.RemoteAddr - - ctx.JSON(http.StatusOK, plugin_info) -} diff --git a/gala-ops/server/main.go b/gala-ops/server/main.go index 1ae48773..23e0085c 100644 --- a/gala-ops/server/main.go +++ b/gala-ops/server/main.go @@ -4,43 +4,73 @@ import ( "fmt" "os" + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" + "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" - - "openeuler.org/PilotGo/gala-ops-plugin/client" "openeuler.org/PilotGo/gala-ops-plugin/config" "openeuler.org/PilotGo/gala-ops-plugin/database" "openeuler.org/PilotGo/gala-ops-plugin/httphandler" ) +const Version = "0.0.1" + +var GlobalClient *client.Client + +var PluginInfo = &client.PluginInfo{ + Name: "gala-ops", + Version: Version, + Description: "gala-ops智能运维工具", + Author: "guozhengxin", + Email: "guozhengxin@kylinos.cn", + Url: "http://192.168.75.100:9999/plugin/gala-ops", + // ReverseDest: "http://192.168.48.163:3000/", +} + func main() { fmt.Println("hello gala-ops") - // config.Init() - if err := database.MysqlInit(config.Config().Mysql); err != nil { fmt.Println("failed to initialize database") os.Exit(-1) } - engine := client.Client().HttpEngine - registerHandlers(engine) - client.StartClient(config.Config().Http) + InitLogger() + + server := gin.Default() + + GlobalClient := client.DefaultClient(PluginInfo) + // 临时给server赋值 + GlobalClient.Server = "http://192.168.75.100:8888" + GlobalClient.RegisterHandlers(server) + InitRouter(server) + + if err := server.Run(config.Config().Http.Addr); err != nil { + logger.Fatal("failed to run server") + } } -func registerHandlers(engine *gin.Engine) { - manager := engine.Group("/plugin_manage") - { - manager.GET("/info", httphandler.PluginInfo) +func InitLogger() { + if err := logger.Init(config.Config().Logopts); err != nil { + fmt.Printf("logger init failed, please check the config file: %s", err) + os.Exit(-1) } +} - api := engine.Group("/plugin/gala-ops/api") +func InitRouter(router *gin.Engine) { + api := router.Group("/plugin/gala-ops/api") { // 脚本执行结果接口 // api.PUT("/run_script_result", httphandler.RunScriptResult) // 安装/升级/卸载gala-gopher监控终端 - api.PUT("/install_gopher", httphandler.InstallGopher) - api.PUT("/upgrade_gopher", httphandler.UpgradeGopher) - api.DELETE("/uninstall_gopher", httphandler.UninstallGopher) + api.PUT("/install_gopher", func(ctx *gin.Context) { + httphandler.InstallGopher(ctx, GlobalClient) + }) + api.PUT("/upgrade_gopher", func(ctx *gin.Context) { + httphandler.UpgradeGopher(ctx, GlobalClient) + }) + api.DELETE("/uninstall_gopher", func(ctx *gin.Context) { + httphandler.UninstallGopher(ctx, GlobalClient) + }) } } diff --git a/gala-ops/server/plugin/prometheus.go b/gala-ops/server/plugin/prometheus.go index 1215aa0b..bb9cf8fb 100644 --- a/gala-ops/server/plugin/prometheus.go +++ b/gala-ops/server/plugin/prometheus.go @@ -1,8 +1,6 @@ package plugin -import ( - "openeuler.org/PilotGo/gala-ops-plugin/client" -) +import "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" // 请求prometheus插件接口,将gala-ops targets添加到监控清单当中 func addTargets(targets []string, url string) error { @@ -13,9 +11,8 @@ func addTargets(targets []string, url string) error { return nil } -func MonitorTargets(targets []string) error { - c := client.Client() - plugin, err := c.GetPluginInfo("prometheus") +func MonitorTargets(targets []string, client *client.Client) error { + plugin, err := client.GetPluginInfo("prometheus") if err != nil { return err } @@ -33,9 +30,8 @@ func deleteTargets(targets []string, url string) error { return nil } -func DeleteTargets(targets []string) error { - c := client.Client() - plugin, err := c.GetPluginInfo("prometheus") +func DeleteTargets(targets []string, client *client.Client) error { + plugin, err := client.GetPluginInfo("prometheus") if err != nil { return err } -- Gitee