1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

fix: Modify the node initialization logic (#8124)

This commit is contained in:
ssongliu 2025-03-11 23:08:32 +08:00 committed by GitHub
parent 6ae1eea7b1
commit 99d3e65df5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 27 additions and 91 deletions

View File

@ -63,11 +63,3 @@ func (b *BaseApi) UpdateSetting(c *gin.Context) {
func (b *BaseApi) LoadBaseDir(c *gin.Context) {
helper.SuccessWithData(c, global.Dir.DataDir)
}
func (b *BaseApi) ReloadConn(c *gin.Context) {
if err := settingService.ReloadConn(); err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
}

View File

@ -62,6 +62,10 @@ func (u *LauncherRepo) SyncAll(data []model.AppLauncher) error {
tx.Rollback()
return err
}
if len(data) == 0 {
tx.Commit()
return nil
}
if err := tx.Model(model.AppLauncher{}).Save(&data).Error; err != nil {
tx.Rollback()
return err

View File

@ -2,14 +2,10 @@ package service
import (
"encoding/json"
"fmt"
"time"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
)
type SettingService struct{}
@ -17,8 +13,6 @@ type SettingService struct{}
type ISettingService interface {
GetSettingInfo() (*dto.SettingInfo, error)
Update(key, value string) error
ReloadConn() error
}
func NewISettingService() ISettingService {
@ -50,55 +44,3 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
func (u *SettingService) Update(key, value string) error {
return settingRepo.UpdateOrCreate(key, value)
}
func (u *SettingService) ReloadConn() error {
if global.IsMaster {
return nil
}
isLocal, nodeInfo, err := xpack.LoadNodeInfo()
if err != nil {
global.LOG.Errorf("load new node info failed, err: %v", err)
return nil
}
if isLocal {
global.IsMaster = true
return nil
}
itemKey, _ := encrypt.StringEncrypt(nodeInfo.ServerKey)
if err := settingRepo.Update("ServerKey", itemKey); err != nil {
global.LOG.Errorf("update server key failed, err: %v", err)
return nil
}
itemCrt, _ := encrypt.StringEncrypt(nodeInfo.ServerCrt)
if err := settingRepo.Update("ServerCrt", itemCrt); err != nil {
global.LOG.Errorf("update server crt failed, err: %v", err)
return nil
}
if err := settingRepo.Update("NodeScope", nodeInfo.Scope); err != nil {
global.LOG.Errorf("update current node failed, err: %v", err)
return nil
}
if err := settingRepo.Update("SystemVersion", nodeInfo.Version); err != nil {
global.LOG.Errorf("update system version failed, err: %v", err)
return nil
}
if err := settingRepo.Update("BaseDir", nodeInfo.BaseDir); err != nil {
global.LOG.Errorf("update base dir failed, err: %v", err)
return nil
}
if err := settingRepo.Update("NodePort", fmt.Sprintf("%v", nodeInfo.NodePort)); err != nil {
global.LOG.Errorf("update node port failed, err: %v", err)
return nil
}
global.Dir.BaseDir = nodeInfo.BaseDir
global.CONF.Base.Version = nodeInfo.Version
global.CONF.Base.Port = fmt.Sprintf("%v", nodeInfo.NodePort)
global.CONF.Base.InstallDir = nodeInfo.BaseDir
if global.CONF.Base.Port == "0" {
global.CONF.Base.Port = "9999"
}
global.IsMaster = nodeInfo.Scope == "master"
return nil
}

View File

@ -11,4 +11,8 @@ func Init() {
global.DB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "agent.db"), "agent")
global.TaskDB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "task.db"), "task")
global.MonitorDB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "monitor.db"), "monitor")
if global.IsMaster {
global.CoreDB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "core.db"), "core")
}
}

View File

@ -2,15 +2,12 @@ package hook
import (
"os"
"path"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/app/service"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
)
@ -31,11 +28,8 @@ func initGlobalData() {
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
global.LOG.Fatalf("init service before start failed, err: %v", err)
}
_, _ = xpack.LoadNodeInfo(false)
global.CONF.Base.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey")
_ = service.NewISettingService().ReloadConn()
if global.IsMaster {
global.CoreDB = common.LoadDBConnByPath(path.Join(global.Dir.DbDir, "core.db"), "core")
}
}
func handleSnapStatus() {

View File

@ -78,7 +78,7 @@ var InitSetting = &gormigrate.Migration{
ID: "20240722-init-setting",
Migrate: func(tx *gorm.DB) error {
global.CONF.Base.EncryptKey = common.RandStr(16)
_, nodeInfo, err := xpack.LoadNodeInfo()
nodeInfo, err := xpack.LoadNodeInfo(true)
if err != nil {
return err
}

View File

@ -52,12 +52,12 @@ func Init() {
global.CONF = serverConfig
global.CONF.Base.IsDemo = v.GetBool("system.is_demo")
initInstallDir()
initBaseInfo()
global.Viper = v
}
func initInstallDir() {
_, nodeInfo, err := xpack.LoadNodeInfo()
func initBaseInfo() {
nodeInfo, err := xpack.LoadNodeInfo(true)
if err != nil {
panic(err)
}

View File

@ -14,7 +14,6 @@ func (s *SettingRouter) InitRouter(Router *gin.RouterGroup) {
settingRouter.POST("/search", baseApi.GetSettingInfo)
settingRouter.GET("/search/available", baseApi.GetSystemAvailable)
settingRouter.POST("/update", baseApi.UpdateSetting)
settingRouter.POST("/conn/reload", baseApi.ReloadConn)
settingRouter.GET("/snapshot/load", baseApi.LoadSnapshotData)
settingRouter.POST("/snapshot", baseApi.CreateSnapshot)

View File

@ -71,7 +71,6 @@ func (f FileOp) CreateDirWithPath(isDir bool, pathItem string) (string, error) {
}
if !f.Stat(checkPath) {
if err := f.CreateDir(checkPath, os.ModePerm); err != nil {
global.LOG.Errorf("mkdir %s failed, err: %v", checkPath, err)
return pathItem, err
}
}

View File

@ -9,6 +9,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
)
@ -18,12 +19,13 @@ func StartClam(startClam model.Clam, isUpdate bool) (int, error) {
return 0, buserr.New("ErrXpackNotFound")
}
func LoadNodeInfo() (bool, model.NodeInfo, error) {
func LoadNodeInfo(isBase bool) (model.NodeInfo, error) {
var info model.NodeInfo
info.BaseDir = loadParams("BASE_DIR")
info.Version = loadParams("ORIGINAL_VERSION")
info.Scope = "master"
return false, info, nil
global.IsMaster = true
return info, nil
}
func loadParams(param string) string {

View File

@ -9,7 +9,7 @@ import (
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
"github.com/1Panel-dev/1Panel/core/utils/req_helper/proxy_local"
"github.com/1Panel-dev/1Panel/core/utils/xpack"
)
@ -55,6 +55,6 @@ func (u *LauncherService) ChangeShow(req dto.SettingUpdate) error {
func syncLauncherToAgent() {
launchers, _ := launcherRepo.List()
itemData, _ := json.Marshal(launchers)
_, _ = req_helper.NewLocalClient("/api/v2/dashboard/app/launcher/sync", http.MethodPost, bytes.NewReader((itemData)))
_, _ = proxy_local.NewLocalClient("/api/v2/dashboard/app/launcher/sync", http.MethodPost, bytes.NewReader((itemData)))
_ = xpack.RequestToAllAgent("/api/v2/dashboard/app/launcher/sync", http.MethodPost, bytes.NewReader((itemData)))
}

View File

@ -21,7 +21,7 @@ import (
"github.com/1Panel-dev/1Panel/core/utils/cloud_storage"
"github.com/1Panel-dev/1Panel/core/utils/cloud_storage/client"
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
"github.com/1Panel-dev/1Panel/core/utils/req_helper/proxy_local"
"github.com/1Panel-dev/1Panel/core/utils/xpack"
"github.com/jinzhu/copier"
)
@ -167,7 +167,7 @@ func (u *BackupService) Delete(name string) error {
if backup.Type == constant.Local {
return buserr.New("ErrBackupLocal")
}
if _, err := req_helper.NewLocalClient(fmt.Sprintf("/api/v2/backups/check/%s", name), http.MethodGet, nil); err != nil {
if _, err := proxy_local.NewLocalClient(fmt.Sprintf("/api/v2/backups/check/%s", name), http.MethodGet, nil); err != nil {
global.LOG.Errorf("check used of local cronjob failed, err: %v", err)
return buserr.New("ErrBackupInUsed")
}
@ -376,5 +376,5 @@ func syncAccountToAgent(backup model.BackupAccount, operation string) {
itemJson := dto.SyncToAgent{Name: backup.Name, Operation: operation, Data: string(itemData)}
bodyItem, _ := json.Marshal(itemJson)
_ = xpack.RequestToAllAgent("/api/v2/backups/sync", http.MethodPost, bytes.NewReader((bodyItem)))
_, _ = req_helper.NewLocalClient("/api/v2/backups/sync", http.MethodPost, bytes.NewReader((bodyItem)))
_, _ = proxy_local.NewLocalClient("/api/v2/backups/sync", http.MethodPost, bytes.NewReader((bodyItem)))
}

View File

@ -12,7 +12,7 @@ import (
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/buserr"
"github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
"github.com/1Panel-dev/1Panel/core/utils/req_helper/proxy_local"
"github.com/1Panel-dev/1Panel/core/utils/xpack"
"github.com/jinzhu/copier"
)
@ -110,7 +110,7 @@ func (u *GroupService) Delete(id uint) error {
err = xpack.UpdateGroup("node", id, defaultGroup.ID)
case "website":
bodyItem := []byte(fmt.Sprintf(`{"Group":%v, "NewGroup":%v}`, id, defaultGroup.ID))
if _, err := req_helper.NewLocalClient("/api/v2/websites/group/change", http.MethodPost, bytes.NewReader(bodyItem)); err != nil {
if _, err := proxy_local.NewLocalClient("/api/v2/websites/group/change", http.MethodPost, bytes.NewReader(bodyItem)); err != nil {
return err
}
if err := xpack.UpdateGroup("node", id, defaultGroup.ID); err != nil {

View File

@ -18,7 +18,6 @@ import (
"time"
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
"github.com/1Panel-dev/1Panel/core/app/dto"
"github.com/1Panel-dev/1Panel/core/app/repo"
@ -29,6 +28,7 @@ import (
"github.com/1Panel-dev/1Panel/core/utils/common"
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
"github.com/1Panel-dev/1Panel/core/utils/firewall"
"github.com/1Panel-dev/1Panel/core/utils/req_helper/proxy_local"
"github.com/gin-gonic/gin"
)
@ -295,7 +295,7 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error {
if err != nil {
return err
}
res, err := req_helper.NewLocalClient("/api/v2/websites/ca/obtain", http.MethodPost, bytes.NewReader(jsonData))
res, err := proxy_local.NewLocalClient("/api/v2/websites/ca/obtain", http.MethodPost, bytes.NewReader(jsonData))
if err != nil {
return err
}

View File

@ -1,4 +1,4 @@
package req_helper
package proxy_local
import (
"context"