From 8fe40b7a97cd984c0a0d3299c71645036c8432f1 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 26 Dec 2024 15:38:56 +0800 Subject: [PATCH] feat: Complete the node initialization logic (#7571) --- agent/app/model/setting.go | 11 +++++------ agent/app/repo/app_launcher.go | 16 ++++++++++++++++ agent/app/repo/backup.go | 16 ++++++++++++++++ agent/app/service/setting.go | 1 - agent/init/hook/hook.go | 2 +- agent/init/migration/migrations/init.go | 8 ++++---- agent/utils/xpack/xpack.go | 2 -- core/app/service/backup.go | 15 ++++++++------- frontend/src/api/modules/backup.ts | 6 +++--- 9 files changed, 53 insertions(+), 24 deletions(-) diff --git a/agent/app/model/setting.go b/agent/app/model/setting.go index 092557c9a..462a3bd27 100644 --- a/agent/app/model/setting.go +++ b/agent/app/model/setting.go @@ -8,10 +8,9 @@ type Setting struct { } type NodeInfo struct { - Scope string `json:"scope"` - BaseDir string `json:"baseDir"` - Version string `json:"version"` - EncryptKey string `json:"encryptKey"` - ServerCrt string `json:"serverCrt"` - ServerKey string `json:"serverKey"` + Scope string `json:"scope"` + BaseDir string `json:"baseDir"` + Version string `json:"version"` + ServerCrt string `json:"serverCrt"` + ServerKey string `json:"serverKey"` } diff --git a/agent/app/repo/app_launcher.go b/agent/app/repo/app_launcher.go index b597e20bf..41290834e 100644 --- a/agent/app/repo/app_launcher.go +++ b/agent/app/repo/app_launcher.go @@ -13,6 +13,8 @@ type ILauncherRepo interface { Create(launcher *model.AppLauncher) error Save(launcher *model.AppLauncher) error Delete(opts ...DBOption) error + + SyncAll(data []model.AppLauncher) error } func NewILauncherRepo() ILauncherRepo { @@ -53,3 +55,17 @@ func (u *LauncherRepo) Delete(opts ...DBOption) error { } return db.Delete(&model.AppLauncher{}).Error } + +func (u *LauncherRepo) SyncAll(data []model.AppLauncher) error { + tx := global.DB.Begin() + if err := tx.Where("1 = 1").Delete(&model.AppLauncher{}).Error; err != nil { + tx.Rollback() + return err + } + if err := tx.Model(model.AppLauncher{}).Save(&data).Error; err != nil { + tx.Rollback() + return err + } + tx.Commit() + return nil +} diff --git a/agent/app/repo/backup.go b/agent/app/repo/backup.go index 26f3fca58..cfa307ad0 100644 --- a/agent/app/repo/backup.go +++ b/agent/app/repo/backup.go @@ -26,6 +26,8 @@ type IBackupRepo interface { WithByFileName(fileName string) DBOption WithByCronID(cronjobID uint) DBOption WithFileNameStartWith(filePrefix string) DBOption + + SyncAll(data []model.BackupAccount) error } func NewIBackupRepo() IBackupRepo { @@ -153,3 +155,17 @@ func (u *BackupRepo) GetRecord(opts ...DBOption) (*model.BackupRecord, error) { err := db.Find(record).Error return record, err } + +func (u *BackupRepo) SyncAll(data []model.BackupAccount) error { + tx := global.DB.Begin() + if err := tx.Where("1 = 1").Delete(&model.BackupAccount{}).Error; err != nil { + tx.Rollback() + return err + } + if err := tx.Model(model.BackupAccount{}).Save(&data).Error; err != nil { + tx.Rollback() + return err + } + tx.Commit() + return nil +} diff --git a/agent/app/service/setting.go b/agent/app/service/setting.go index 6347d587d..e1c7dd184 100644 --- a/agent/app/service/setting.go +++ b/agent/app/service/setting.go @@ -106,7 +106,6 @@ func (u *SettingService) ReloadConn() error { global.CONF.System.BaseDir = nodeInfo.BaseDir global.CONF.System.Version = nodeInfo.Version - global.CONF.System.EncryptKey = nodeInfo.EncryptKey global.IsMaster = nodeInfo.Scope == "master" return nil } diff --git a/agent/init/hook/hook.go b/agent/init/hook/hook.go index f2c528684..a030de6dd 100644 --- a/agent/init/hook/hook.go +++ b/agent/init/hook/hook.go @@ -26,7 +26,7 @@ func initGlobalData() { if err := settingRepo.Update("SystemStatus", "Free"); err != nil { global.LOG.Fatalf("init service before start failed, err: %v", err) } - + global.CONF.System.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey") _ = service.NewISettingService().ReloadConn() if global.IsMaster { global.CoreDB = common.LoadDBConnByPath(path.Join(global.CONF.System.DbPath, "core.db"), "core") diff --git a/agent/init/migration/migrations/init.go b/agent/init/migration/migrations/init.go index f27f1b08d..490f72951 100644 --- a/agent/init/migration/migrations/init.go +++ b/agent/init/migration/migrations/init.go @@ -15,7 +15,7 @@ import ( ) var AddTable = &gormigrate.Migration{ - ID: "20241009-add-table", + ID: "20241226-add-table", Migrate: func(tx *gorm.DB) error { return tx.AutoMigrate( &model.AppDetail{}, @@ -81,9 +81,6 @@ var InitSetting = &gormigrate.Migration{ if err := tx.Create(&model.Setting{Key: "BaseDir", Value: nodeInfo.BaseDir}).Error; err != nil { return err } - if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: nodeInfo.EncryptKey}).Error; err != nil { - return err - } itemKey, _ := encrypt.StringEncrypt(nodeInfo.ServerKey) if err := tx.Create(&model.Setting{Key: "ServerKey", Value: itemKey}).Error; err != nil { return err @@ -99,6 +96,9 @@ var InitSetting = &gormigrate.Migration{ return err } + if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: common.RandStr(16)}).Error; err != nil { + return err + } if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil { return err } diff --git a/agent/utils/xpack/xpack.go b/agent/utils/xpack/xpack.go index d359ef1e6..478e62894 100644 --- a/agent/utils/xpack/xpack.go +++ b/agent/utils/xpack/xpack.go @@ -14,7 +14,6 @@ import ( "github.com/1Panel-dev/1Panel/agent/buserr" "github.com/1Panel-dev/1Panel/agent/constant" "github.com/1Panel-dev/1Panel/agent/utils/cmd" - "github.com/1Panel-dev/1Panel/agent/utils/common" ) func RemoveTamper(website string) {} @@ -48,7 +47,6 @@ func LoadNodeInfo() (bool, model.NodeInfo, error) { var info model.NodeInfo info.BaseDir = loadParams("BASE_DIR") info.Version = loadParams("ORIGINAL_VERSION") - info.EncryptKey = common.RandStr(16) info.Scope = "master" return false, info, nil } diff --git a/core/app/service/backup.go b/core/app/service/backup.go index c9ad84d04..e9de78575 100644 --- a/core/app/service/backup.go +++ b/core/app/service/backup.go @@ -107,13 +107,14 @@ func (u *BackupService) GetLocalDir() (string, error) { } func (u *BackupService) SearchWithPage(req dto.SearchPageWithType) (int64, interface{}, error) { - count, accounts, err := backupRepo.Page( - req.Page, - req.PageSize, - repo.WithByType(req.Type), - repo.WithByName(req.Info), - repo.WithOrderBy("created_at desc"), - ) + options := []global.DBOption{repo.WithOrderBy("created_at desc")} + if len(req.Type) != 0 { + options = append(options, repo.WithByType(req.Type)) + } + if len(req.Info) != 0 { + options = append(options, repo.WithByType(req.Info)) + } + count, accounts, err := backupRepo.Page(req.Page, req.PageSize, options...) if err != nil { return 0, nil, err } diff --git a/frontend/src/api/modules/backup.ts b/frontend/src/api/modules/backup.ts index b9024337c..0a5dc7998 100644 --- a/frontend/src/api/modules/backup.ts +++ b/frontend/src/api/modules/backup.ts @@ -6,12 +6,12 @@ import { Backup } from '../interface/backup'; import { TimeoutEnum } from '@/enums/http-enum'; // backup-agent -export const listBackupOptions = () => { - return http.get>(`/backups/options`); -}; export const handleBackup = (params: Backup.Backup) => { return http.post(`/backups/backup`, params, TimeoutEnum.T_1H); }; +export const listBackupOptions = () => { + return http.get>(`/backups/options`); +}; export const handleRecover = (params: Backup.Recover) => { return http.post(`/backups/recover`, params, TimeoutEnum.T_1D); };