From 4e83cfdf46b10dd5c0929ae2190055acdb2e5924 Mon Sep 17 00:00:00 2001 From: Gzx1999 Date: Wed, 14 Jun 2023 16:45:06 +0800 Subject: [PATCH] implement client run command and script --- sdk/common/machine.go | 18 +++++++------- sdk/plugin/client/script.go | 47 ++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/sdk/common/machine.go b/sdk/common/machine.go index 64e86af2..dbb61531 100644 --- a/sdk/common/machine.go +++ b/sdk/common/machine.go @@ -1,16 +1,16 @@ package common type MachineNode struct { - UUID string - Department string - IP string - CPUArch string - OSInfo string - State int + 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"` } type Batch struct { - BatchUUID string - DepartmentIDs []string - MachineUUIDs []string + BatchUUID string `json:"batch_id"` + DepartmentIDs []string `json:"department_ids"` + MachineUUIDs []string `json:"machine_uuids"` } diff --git a/sdk/plugin/client/script.go b/sdk/plugin/client/script.go index dc3d923d..51b0056e 100644 --- a/sdk/plugin/client/script.go +++ b/sdk/plugin/client/script.go @@ -1,36 +1,61 @@ package client import ( + "encoding/base64" "encoding/json" - "io" - "net/http" "gitee.com/openeuler/PilotGo-plugins/sdk/common" + "gitee.com/openeuler/PilotGo-plugins/sdk/utils/httputils" ) type CmdResult struct { MachineUUID string MachineIP string - Code int + RetCode int Stdout string Stderr string } -func (c *Client) RunScript(batch *common.Batch, cmd string) ([]*CmdResult, error) { - url := c.Server + "/api/v1/pluginapi/run_script" - req, err := http.NewRequest("POST", url, nil) +func (c *Client) RunCommand(batch *common.Batch, cmd string) ([]*CmdResult, error) { + url := c.Server + "/api/v1/pluginapi/run_command" + + p := &struct { + Batch *common.Batch `json:"batch"` + Command string `json:"command"` + }{ + Batch: batch, + Command: base64.StdEncoding.EncodeToString([]byte(cmd)), + } + + bs, err := httputils.Post(url, &httputils.Params{ + Body: p, + }) if err != nil { return nil, err } - hc := &http.Client{} - resp, err := hc.Do(req) - if err != nil { + res := []*CmdResult{} + if err := json.Unmarshal(bs, &res); err != nil { return nil, err } - defer resp.Body.Close() - bs, err := io.ReadAll(resp.Body) + return res, nil +} + +func (c *Client) RunScript(batch *common.Batch, script string) ([]*CmdResult, error) { + url := c.Server + "/api/v1/pluginapi/run_script" + + p := &struct { + Batch *common.Batch `json:"batch"` + Script string `json:"script"` + }{ + Batch: batch, + Script: base64.StdEncoding.EncodeToString([]byte(script)), + } + + bs, err := httputils.Post(url, &httputils.Params{ + Body: p, + }) if err != nil { return nil, err } -- Gitee