From 8e9246aad1b9031e226492419d584d8821dc793e Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 4 Nov 2024 11:31:01 +0800 Subject: [PATCH] add host.go file and supplementary configuration description and issues --- configmanage/server/internal/hostfile.go | 27 ++++++++++++ configmanage/server/router/router.go | 6 +-- configmanage/server/service/host.go | 55 ++++++++++++++++++++++++ configmanage/server/service/host_test.go | 1 + configmanage/server/service/repo.go | 13 ++++++ 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 configmanage/server/internal/hostfile.go create mode 100644 configmanage/server/service/host.go create mode 100644 configmanage/server/service/host_test.go diff --git a/configmanage/server/internal/hostfile.go b/configmanage/server/internal/hostfile.go new file mode 100644 index 00000000..f5b1f280 --- /dev/null +++ b/configmanage/server/internal/hostfile.go @@ -0,0 +1,27 @@ +package internal + +import ( + "encoding/json" + "time" + + "openeuler.org/PilotGo/configmanage-plugin/db" +) + +type HostFile struct { + ID int `gorm:"unique;autoIncrement:true"` + UUID string `gorm:"primary_key;type:varchar(50)" json:"uuid"` + ConfigInfo ConfigInfo `gorm:"Foreignkey:ConfigInfoUUID"` + ConfigInfoUUID string + Path string `json:"path"` + Name string `json:"name"` + Content json.RawMessage `gorm:"type:json" json:"content"` + Version string `gorm:"type:varchar(50)" json:"version"` + IsActive bool `json:"isactive"` + IsFromHost bool `gorm:"default:false" json:"isfromhost"` + Hostuuid string `gorm:"type:varchar(50)" json:"hostuuid"` + CreatedAt time.Time +} + +func (hf *HostFile) Add() error { + return db.MySQL().Save(&hf).Error +} diff --git a/configmanage/server/router/router.go b/configmanage/server/router/router.go index 70d1a087..049fc4e3 100644 --- a/configmanage/server/router/router.go +++ b/configmanage/server/router/router.go @@ -26,14 +26,14 @@ func RegisterAPIs(router *gin.Engine) { // 提供配置列表 list.GET("/config_infos", controller.ConfigInfosHandler) // 根据列表中的configinfo_uuid获取某一个config信息 - list.GET("/config_info", controller.ConfigInfoHandler) + list.POST("/config_info", controller.ConfigInfoHandler) // 查看某台机器某种类型的的历史配置信息 - list.GET("/config_history", controller.ConfigHistoryHandler) + list.POST("/config_history", controller.ConfigHistoryHandler) } // 添加配置管理 api.POST("/add", controller.AddConfigHandler) // 根据列表中的configinfo_uuid获取某一个具体的正在使用的config信息 - api.GET("/load", controller.LoadConfigHandler) + api.POST("/load", controller.LoadConfigHandler) // 下发配置管理 api.POST("/apply", controller.ApplyConfigHandler) } diff --git a/configmanage/server/service/host.go b/configmanage/server/service/host.go new file mode 100644 index 00000000..aa3ff251 --- /dev/null +++ b/configmanage/server/service/host.go @@ -0,0 +1,55 @@ +package service + +import ( + "encoding/json" + "errors" + "fmt" + "time" + + "openeuler.org/PilotGo/configmanage-plugin/internal" +) + +/* +host: 配置文件 + +一般方法: 1、在/etc/hosts中修改内容 + + 2、执行命令systemctl restart NetworkManager重启网络服务 + +考虑的问题: +*/ +type HostFile = internal.HostFile + +type HostConfig struct { + UUID string `json:"uuid"` + ConfigInfoUUID string `json:"configinfouuid"` + Content json.RawMessage `json:"content"` + Version string `json:"version"` + Path string `json:"path"` + Name string `json:"name"` + //下发改变标志位 + IsActive bool `json:"isactive"` +} + +func (hc *HostConfig) toHostFile() HostFile { + return HostFile{ + UUID: hc.UUID, + ConfigInfoUUID: hc.ConfigInfoUUID, + Path: hc.Path, + Name: hc.Name, + Content: hc.Content, + Version: fmt.Sprintf("v%s", time.Now().Format("2006-01-02-15-04-05")), + IsActive: hc.IsActive, + } +} + +func (hc *HostConfig) Record() error { + //检查info的uuid是否存在 + ci, err := GetInfoByUUID(hc.ConfigInfoUUID) + if err != nil || ci.UUID == "" { + return errors.New("configinfo uuid not exist") + } + + hf := hc.toHostFile() + return hf.Add() +} diff --git a/configmanage/server/service/host_test.go b/configmanage/server/service/host_test.go new file mode 100644 index 00000000..6d43c336 --- /dev/null +++ b/configmanage/server/service/host_test.go @@ -0,0 +1 @@ +package service diff --git a/configmanage/server/service/repo.go b/configmanage/server/service/repo.go index 1b67622d..d5d091ca 100644 --- a/configmanage/server/service/repo.go +++ b/configmanage/server/service/repo.go @@ -16,6 +16,19 @@ import ( "openeuler.org/PilotGo/configmanage-plugin/internal" ) +/* +repo: 配置文件 + +一般方法: 1、在/etc/yum.repo.d/下创建repo文件 + + 2、执行命令使用repo文件dnf clear all 和 dnf makecache + +考虑的问题:1、路径下存在多个repo文件,使用的时候都生效,用文件中某一字段控制某一链接不生效 + + 2、采集和下发的时候都存在多个repo文件,如果下发的文件同名将如何处理? + + 3、下发之前采集还是创建相关配置的时候采集? +*/ type RepoFile = internal.RepoFile type RepoConfig struct { -- Gitee