diff --git a/configmanage/server/global/plugin_configmanage.go b/configmanage/server/global/plugin_configmanage.go index 8e8d138661427e44e09ccda51a274d492df99815..9e71c785960983dbc79816689fd1df55a531531c 100644 --- a/configmanage/server/global/plugin_configmanage.go +++ b/configmanage/server/global/plugin_configmanage.go @@ -27,4 +27,5 @@ const ( SSHD = "sshd" Sysctl = "sysctl" DNS = "dns" + PATH = "path" ) diff --git a/configmanage/server/internal/pathfile.go b/configmanage/server/internal/pathfile.go new file mode 100644 index 0000000000000000000000000000000000000000..e4a6519f3ffd913cd947e108f6219d3570ea9834 --- /dev/null +++ b/configmanage/server/internal/pathfile.go @@ -0,0 +1,51 @@ +package internal + +import ( + "encoding/json" + "time" + + "openeuler.org/PilotGo/configmanage-plugin/db" +) + +type PathFile struct { + ID int `gorm:"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 `gorm:"default:false" json:"isactive"` + IsFromHost bool `gorm:"default:false" json:"isfromhost"` + Hostuuid string `gorm:"type:varchar(50)" json:"hostuuid"` + CreatedAt time.Time +} + +func (pf *PathFile) Add() error { + sql := ` + INSERT INTO path_file (uuid,config_info_uuid,path,name,content,version,is_active,is_from_host,hostuuid) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + ON DUPLICATE KEY UPDATE + uuid = VALUES(uuid), + config_info_uuid = VALUES(config_info_uuid), + path = VALUES(path), + name = VALUES(name), + content = VALUES(content), + version = VALUES(version), + is_active = VALUES(is_active), + is_from_host = VALUES(is_from_host), + hostuuid = VALUES(hostuuid); + ` + return db.MySQL().Exec(sql, + pf.UUID, + pf.ConfigInfoUUID, + pf.Path, + pf.Name, + pf.Content, + pf.Version, + pf.IsActive, + pf.IsFromHost, + pf.Hostuuid, + ).Error +} diff --git a/configmanage/server/service/configinstance.go b/configmanage/server/service/configinstance.go index 8a77e3d0c8600a81643819b0287fa1849810bd0f..0d8dd1bd54c48fbd06cb4c0d22e689073b59334d 100644 --- a/configmanage/server/service/configinstance.go +++ b/configmanage/server/service/configinstance.go @@ -52,6 +52,10 @@ func Init() error { if err != nil { return err } + err = db.MySQL().Set("gorm:table_options", "ENGINE=InnoDB CHARACTER SET utf8mb4").AutoMigrate(&internal.PathFile{}) + if err != nil { + return err + } return nil } diff --git a/configmanage/server/service/path.go b/configmanage/server/service/path.go new file mode 100644 index 0000000000000000000000000000000000000000..59f20e859b9a068237bceabd423516378f7a7b1e --- /dev/null +++ b/configmanage/server/service/path.go @@ -0,0 +1,48 @@ +package service + +import ( + "encoding/json" + "fmt" + "time" + + "openeuler.org/PilotGo/configmanage-plugin/internal" +) + +/* +path: 环境变量文件只有一个 + +一般方法: 1、在/etc/profile中修改内容 + +考虑的问题: +*/ + +type PathFile = internal.PathFile + +type PathConfig 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 (pc *PathConfig) toPathFile() PathFile { + return PathFile{ + UUID: pc.UUID, + ConfigInfoUUID: pc.ConfigInfoUUID, + Path: pc.Path, + Name: pc.Name, + Content: pc.Content, + Version: fmt.Sprintf("v%s", time.Now().Format("2006-01-02-15-04-05")), + IsActive: pc.IsActive, + IsFromHost: false, + } +} + +func (pc *PathConfig) Record() error { + pf := pc.toPathFile() + return pf.Add() +} diff --git a/configmanage/server/service/path_test.go b/configmanage/server/service/path_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7d41a52cda1972e8b22693e39fe939d038b77b8a --- /dev/null +++ b/configmanage/server/service/path_test.go @@ -0,0 +1,29 @@ +package service + +import ( + "encoding/json" + "fmt" + "os" + "testing" + + "github.com/google/uuid" +) + +func TestPathConfig_Record(t *testing.T) { + // 设置测试数据 + hc := &PathConfig{ + UUID: uuid.New().String(), + ConfigInfoUUID: "158e0acf-159b-4876-83b1-fa5f3d6460b1", + Content: json.RawMessage(`{"test": "test"}`), + Path: "/path", + Name: "path.txt", + IsActive: false, + } + + // 调用被测试的函数 + err := hc.Record() + if err != nil { + fmt.Printf("record error: %s\n", err) + os.Exit(-1) + } +}