From d2e4661d5ab4f51b8ceb316f2c3101dafd6df441 Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 4 Nov 2024 15:22:27 +0800 Subject: [PATCH] add hostconfig apply func --- configmanage/server/service/host.go | 89 +++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/configmanage/server/service/host.go b/configmanage/server/service/host.go index 7e420389..d3f73ca0 100644 --- a/configmanage/server/service/host.go +++ b/configmanage/server/service/host.go @@ -4,13 +4,18 @@ import ( "encoding/json" "errors" "fmt" + "net/http" + "strconv" "time" + "gitee.com/openeuler/PilotGo/sdk/common" + "gitee.com/openeuler/PilotGo/sdk/plugin/client" + "gitee.com/openeuler/PilotGo/sdk/utils/httputils" "openeuler.org/PilotGo/configmanage-plugin/internal" ) /* -host: 配置文件 +host: 配置文件,hosts文件只有一个 一般方法: 1、在/etc/hosts中修改内容 @@ -69,12 +74,86 @@ func (hc *HostConfig) Load() error { return nil } -// TODO: -func (rc *HostConfig) Apply() (json.RawMessage, error) { - return nil, errors.New("failed to apply host config") +func (hc *HostConfig) Apply() (json.RawMessage, error) { + //从数据库获取下发的信息 + hf, err := internal.GetHostFileByUUID(hc.UUID) + if err != nil { + return nil, err + } + if hf.ConfigInfoUUID != hc.ConfigInfoUUID || hf.UUID != hc.UUID { + return nil, errors.New("数据库不存在此配置") + } + + batchids, err := internal.GetConfigBatchByUUID(hc.ConfigInfoUUID) + if err != nil { + return nil, err + } + departids, err := internal.GetConfigDepartByUUID(hc.ConfigInfoUUID) + if err != nil { + return nil, err + } + nodes, err := internal.GetConfigNodesByUUID(hc.ConfigInfoUUID) + if err != nil { + return nil, err + } + + //从hc中解析下发的文件内容,逐一进行下发 + Repofile := common.File{} + err = json.Unmarshal([]byte(hf.Content), &Repofile) + if err != nil { + return nil, err + } + result := "" + de := Deploy{ + DeployBatch: common.Batch{ + BatchIds: batchids, + DepartmentIDs: departids, + MachineUUIDs: nodes, + }, + DeployPath: Repofile.Path, + DeployFileName: Repofile.Name, + DeployText: Repofile.Content, + } + url := "http://" + client.GetClient().Server() + "/api/v1/pluginapi/file_deploy" + r, err := httputils.Post(url, &httputils.Params{ + Body: de, + }) + if err != nil { + return nil, err + } + if r.StatusCode != http.StatusOK { + return nil, errors.New("server process error:" + strconv.Itoa(r.StatusCode)) + } + + resp := &common.CommonResult{} + if err := json.Unmarshal(r.Body, resp); err != nil { + return nil, err + } + if resp.Code != http.StatusOK { + return nil, errors.New(resp.Message) + } + + data := []common.NodeResult{} + if err := resp.ParseData(&data); err != nil { + return nil, err + } + // 将执行失败的文件、机器信息和原因添加到结果字符串中 + for _, d := range data { + if d.Error != "" { + result = result + Repofile.Content + "文件" + d.UUID + ":" + d.Error + "\n" + } + } + + //TODO:部分成功如何修改数据库 + if result == "" { + //下发成功修改数据库应用版本 + err = hf.UpdateByuuid() + return nil, err + } + return nil, errors.New(result + "failed to apply host config") } // TODO: -func (rc *HostConfig) Collect() error { +func (hc *HostConfig) Collect() error { return nil } -- Gitee