2024-07-23 14:48:37 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
2024-08-13 15:33:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/service"
|
2024-07-23 14:48:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
2024-11-28 18:42:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/common"
|
2024-08-02 16:49:43 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
|
2024-11-28 18:42:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
|
2024-07-23 14:48:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
2024-11-28 18:42:40 +08:00
|
|
|
initWithNodeJson()
|
|
|
|
initGlobalData()
|
|
|
|
handleCronjobStatus()
|
|
|
|
handleSnapStatus()
|
|
|
|
loadLocalDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGlobalData() {
|
2024-07-23 14:48:37 +08:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil {
|
|
|
|
_ = settingRepo.Create("SystemStatus", "Free")
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
|
|
|
|
global.LOG.Fatalf("init service before start failed, err: %v", err)
|
|
|
|
}
|
2024-11-26 14:34:47 +08:00
|
|
|
|
2024-11-28 18:42:40 +08:00
|
|
|
global.CONF.System.BaseDir, _ = settingRepo.GetValueByKey("BaseDir")
|
|
|
|
global.CONF.System.Version, _ = settingRepo.GetValueByKey("SystemVersion")
|
|
|
|
global.CONF.System.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey")
|
2024-12-10 13:57:38 +08:00
|
|
|
global.CONF.System.CurrentNode, _ = settingRepo.GetValueByKey("CurrentNode")
|
2024-11-28 18:42:40 +08:00
|
|
|
|
2024-12-10 13:57:38 +08:00
|
|
|
global.IsMaster = global.CONF.System.CurrentNode == "127.0.0.1" || len(global.CONF.System.CurrentNode) == 0
|
2024-08-14 10:15:55 +08:00
|
|
|
if global.IsMaster {
|
2024-11-28 18:42:40 +08:00
|
|
|
global.CoreDB = common.LoadDBConnByPath(path.Join(global.CONF.System.DbPath, "core.db"), "core")
|
2024-08-14 10:15:55 +08:00
|
|
|
} else {
|
2024-11-28 18:42:40 +08:00
|
|
|
global.CONF.System.MasterAddr, _ = settingRepo.GetValueByKey("MasterAddr")
|
2024-08-14 10:15:55 +08:00
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSnapStatus() {
|
|
|
|
msgFailed := "the task was interrupted due to the restart of the 1panel service"
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", "OnSaveData").
|
|
|
|
Updates(map[string]interface{}{"status": constant.StatusSuccess}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("recover_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"recover_status": constant.StatusFailed,
|
|
|
|
"recover_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("rollback_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"rollback_status": constant.StatusFailed,
|
|
|
|
"rollback_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCronjobStatus() {
|
|
|
|
_ = global.DB.Model(&model.JobRecords{}).Where("status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": "the task was interrupted due to the restart of the 1panel service",
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLocalDir() {
|
2024-08-14 10:15:55 +08:00
|
|
|
var vars string
|
|
|
|
if global.IsMaster {
|
|
|
|
var account model.BackupAccount
|
|
|
|
if err := global.CoreDB.Where("id = 1").First(&account).Error; err != nil {
|
|
|
|
global.LOG.Errorf("load local backup account info failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars = account.Vars
|
|
|
|
} else {
|
|
|
|
account, _, err := service.NewBackupClientWithID(1)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load local backup account info failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars = account.Vars
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
2024-08-14 10:15:55 +08:00
|
|
|
localDir, err := service.LoadLocalDirByStr(vars)
|
2024-08-13 15:33:34 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load local backup dir failed, err: %v", err)
|
2024-08-14 10:15:55 +08:00
|
|
|
return
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
2024-08-14 10:15:55 +08:00
|
|
|
global.CONF.System.Backup = localDir
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
|
2024-11-28 18:42:40 +08:00
|
|
|
func initWithNodeJson() {
|
|
|
|
if global.IsMaster {
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-11-28 18:42:40 +08:00
|
|
|
isLocal, nodeInfo, err := xpack.LoadNodeInfo()
|
2024-08-02 16:49:43 +08:00
|
|
|
if err != nil {
|
2024-11-28 18:42:40 +08:00
|
|
|
global.LOG.Errorf("load new node info failed, err: %v", err)
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-11-28 18:42:40 +08:00
|
|
|
if isLocal {
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-11-28 18:42:40 +08:00
|
|
|
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
itemKey, _ := encrypt.StringEncrypt(nodeInfo.ServerKey)
|
2024-08-02 16:49:43 +08:00
|
|
|
if err := settingRepo.Update("ServerKey", itemKey); err != nil {
|
2024-11-28 18:42:40 +08:00
|
|
|
global.LOG.Errorf("update server key failed, err: %v", err)
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-11-28 18:42:40 +08:00
|
|
|
itemCrt, _ := encrypt.StringEncrypt(nodeInfo.ServerCrt)
|
2024-08-02 16:49:43 +08:00
|
|
|
if err := settingRepo.Update("ServerCrt", itemCrt); err != nil {
|
2024-11-28 18:42:40 +08:00
|
|
|
global.LOG.Errorf("update server crt failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("CurrentNode", nodeInfo.CurrentNode); err != nil {
|
|
|
|
global.LOG.Errorf("update current node failed, err: %v", err)
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-11-28 18:42:40 +08:00
|
|
|
if err := settingRepo.Update("SystemVersion", nodeInfo.Version); err != nil {
|
|
|
|
global.LOG.Errorf("update system version failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("BaseDir", nodeInfo.BaseDir); err != nil {
|
|
|
|
global.LOG.Errorf("update base dir failed, err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("MasterAddr", nodeInfo.MasterAddr); err != nil {
|
|
|
|
global.LOG.Errorf("update master addr failed, err: %v", err)
|
|
|
|
return
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
|
|
|
}
|