From 7fca2c538cd70aefc0e740d96f001917194d0d8e Mon Sep 17 00:00:00 2001 From: wubijie Date: Fri, 8 Nov 2024 09:47:53 +0800 Subject: [PATCH] add collect for host config --- configmanage/server/service/host.go | 68 ++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/configmanage/server/service/host.go b/configmanage/server/service/host.go index f09b6d9f..c9f8a478 100644 --- a/configmanage/server/service/host.go +++ b/configmanage/server/service/host.go @@ -9,8 +9,10 @@ import ( "time" "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/plugin/client" "gitee.com/openeuler/PilotGo/sdk/utils/httputils" + "github.com/google/uuid" "openeuler.org/PilotGo/configmanage-plugin/internal" ) @@ -165,8 +167,72 @@ func (hc *HostConfig) Apply() (json.RawMessage, error) { return nil, errors.New(result + "failed to apply host config") } -// TODO: func (hc *HostConfig) Collect() error { + ci, err := GetConfigByUUID(hc.ConfigInfoUUID) + if err != nil { + return err + } + + //发请求获取配置详情 + url := "http://" + client.GetClient().Server() + "/api/v1/pluginapi/getnodefiles" + p := struct { + DeployBatch common.Batch `json:"deploybatch"` + Path string `json:"path"` + FileName string `json:"filename"` + }{ + DeployBatch: common.Batch{ + BatchIds: ci.BatchIds, + DepartmentIDs: ci.DepartIds, + MachineUUIDs: ci.Nodes, + }, + Path: hc.Path, + FileName: hc.Name, + } + r, err := httputils.Post(url, &httputils.Params{ + Body: p, + }) + if err != nil { + return err + } + if r.StatusCode != http.StatusOK { + return errors.New("server process error:" + strconv.Itoa(r.StatusCode)) + } + + resp := &common.CommonResult{} + if err := json.Unmarshal(r.Body, resp); err != nil { + return err + } + if resp.Code != http.StatusOK { + return errors.New(resp.Message) + } + + data := []common.NodeResult{} + if err := resp.ParseData(&data); err != nil { + return err + } + result := "" + for _, v := range data { + if v.Error == "" { + file, _ := json.Marshal(v.Data) + rf := RepoFile{ + UUID: uuid.New().String(), + ConfigInfoUUID: hc.ConfigInfoUUID, + Content: file, + Version: fmt.Sprintf("v%s", time.Now().Format("2006-01-02-15-04-05")), + IsFromHost: true, + Hostuuid: v.UUID, + } + err = rf.Add() + if err != nil { + logger.Error("failed to add hostconfig: %s", err.Error()) + } + } else { + result = result + v.UUID + ":" + v.Error + "\n" + } + } + if result != "" { + return errors.New(result + "failed to collect host config") + } return nil } -- Gitee