diff --git a/gala-ops/server/httphandler/agent.go b/gala-ops/server/httphandler/agent.go new file mode 100644 index 0000000000000000000000000000000000000000..a39723f96bd271886c05b08c8d6d7691bcd0fd77 --- /dev/null +++ b/gala-ops/server/httphandler/agent.go @@ -0,0 +1,65 @@ +package httphandler + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/gala-ops-plugin/pluginclient" +) + +func InstallGopher(ctx *gin.Context) { + // TODO + param := &struct { + Batch []string + }{} + if err := ctx.BindJSON(param); err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{ + "code": -1, + "status": "parameter error", + }) + } + + cmd := "yum install -y gala-gopher" + cmdResults, err := pluginclient.Client().RunScript(param.Batch, cmd) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{ + "code": -1, + "status": fmt.Sprintf("run remote script error:%s", err), + }) + } + + ret := []interface{}{} + for _, result := range cmdResults { + d := struct { + MachineUUID string + InstallStatus string + Error string + }{ + MachineUUID: result.MachineUUID, + InstallStatus: "ok", + Error: "", + } + + if result.Code != 0 { + d.InstallStatus = "error" + d.Error = result.Stderr + } + + ret = append(ret, d) + } + + ctx.JSON(http.StatusOK, gin.H{ + "code": 0, + "status": fmt.Sprintf("run remote script error:%s", err), + "data": ret, + }) +} + +func UpgradeGopher(ctx *gin.Context) { + // TODO +} + +func UninstallGopher(ctx *gin.Context) { + // TODO +} diff --git a/gala-ops/server/main.go b/gala-ops/server/main.go index 02b7b4908b0f43b821c00133a47ee5597840c3d6..34fd20aca349b944bea98508a4b57aff9c9969b9 100644 --- a/gala-ops/server/main.go +++ b/gala-ops/server/main.go @@ -3,7 +3,9 @@ package main import ( "fmt" + "github.com/gin-gonic/gin" "openeuler.org/PilotGo/gala-ops-plugin/config" + "openeuler.org/PilotGo/gala-ops-plugin/httphandler" "openeuler.org/PilotGo/plugin-sdk/plugin" ) @@ -25,4 +27,17 @@ func main() { }) client.Serve(":8888") + + registerHandlers(client.HttpEngine) + +} + +func registerHandlers(engine *gin.Engine) { + api := engine.Group("/plugin/gala-ops/api") + { + // 安装/升级/卸载gala-gopher监控终端 + api.PUT("/install_gopher", httphandler.InstallGopher) + api.PUT("/upgrade_gopher", httphandler.UpgradeGopher) + api.DELETE("/uninstall_gopher", httphandler.UninstallGopher) + } } diff --git a/gala-ops/server/pluginclient/client.go b/gala-ops/server/pluginclient/client.go new file mode 100644 index 0000000000000000000000000000000000000000..c63a8ae824d7ddf7e95971608b590da372146f86 --- /dev/null +++ b/gala-ops/server/pluginclient/client.go @@ -0,0 +1,21 @@ +package pluginclient + +import ( + "sync" + + "openeuler.org/PilotGo/plugin-sdk/plugin" +) + +var once sync.Once +var global_client *plugin.Client + +func NewClient(desc *plugin.PluginInfo) { + once.Do(func() { + // client init + global_client = plugin.DefaultClient(desc) + }) +} + +func Client() *plugin.Client { + return global_client +} diff --git a/sdk/plugin/client.go b/sdk/plugin/client.go index 2167035a7ad00b1d7cb0d446e9caccbb5a535dad..6af43bee73e06f87c6cf767e5f67a47fc870f4ba 100644 --- a/sdk/plugin/client.go +++ b/sdk/plugin/client.go @@ -73,35 +73,38 @@ func (c *Client) Serve(url ...string) { c.HttpEngine.Run(url...) } -func (c *Client) RunScript(batch []string, cmd string) (int, string, string, error) { +type CmdResult struct { + MachineUUID string + Code int + Stdout string + Stderr string +} + +func (c *Client) RunScript(batch []string, cmd string) ([]*CmdResult, error) { url := c.Server + "/api/v1/pluginapi/run_script" req, err := http.NewRequest("POST", url, nil) if err != nil { - return 0, "", "", err + return nil, err } hc := &http.Client{} resp, err := hc.Do(req) if err != nil { - return 0, "", "", err + return nil, err } defer resp.Body.Close() bs, err := io.ReadAll(resp.Body) if err != nil { - return 0, "", "", err + return nil, err } - res := &struct { - Code int - Stdout string - Stderr string - }{} - if err := json.Unmarshal(bs, res); err != nil { - return 0, "", "", err + res := []*CmdResult{} + if err := json.Unmarshal(bs, &res); err != nil { + return nil, err } - return res.Code, res.Stdout, res.Stderr, nil + return res, nil } type MachineNode struct {