2024-07-23 14:48:37 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
2024-08-02 16:49:43 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
|
2024-07-23 14:48:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
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-08-09 18:30:39 +08:00
|
|
|
node, err := settingRepo.Get(settingRepo.WithByKey("CurrentNode"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Fatalf("load current node before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CurrentNode = node.Value
|
2024-07-23 14:48:37 +08:00
|
|
|
|
|
|
|
handleCronjobStatus()
|
|
|
|
handleSnapStatus()
|
|
|
|
loadLocalDir()
|
|
|
|
initDir()
|
2024-08-02 16:49:43 +08:00
|
|
|
_ = initSSL()
|
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
|
|
|
|
|
|
|
|
snapRepo := repo.NewISnapshotRepo()
|
|
|
|
|
|
|
|
status, _ := snapRepo.GetStatusList()
|
|
|
|
for _, item := range status {
|
|
|
|
updates := make(map[string]interface{})
|
|
|
|
if item.Panel == constant.StatusRunning {
|
|
|
|
updates["panel"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.PanelInfo == constant.StatusRunning {
|
|
|
|
updates["panel_info"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.DaemonJson == constant.StatusRunning {
|
|
|
|
updates["daemon_json"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.AppData == constant.StatusRunning {
|
|
|
|
updates["app_data"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.PanelData == constant.StatusRunning {
|
|
|
|
updates["panel_data"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.BackupData == constant.StatusRunning {
|
|
|
|
updates["backup_data"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.Compress == constant.StatusRunning {
|
|
|
|
updates["compress"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if item.Upload == constant.StatusUploading {
|
|
|
|
updates["upload"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
if len(updates) != 0 {
|
|
|
|
_ = snapRepo.UpdateStatus(item.ID, updates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
var backup model.BackupAccount
|
|
|
|
_ = global.DB.Where("type = ?", "LOCAL").First(&backup).Error
|
|
|
|
if backup.ID == 0 {
|
|
|
|
global.LOG.Errorf("no such backup account `%s` in db", "LOCAL")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
global.LOG.Errorf("json unmarshal backup.Vars: %v failed, err: %v", backup.Vars, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := varMap["dir"]; !ok {
|
|
|
|
global.LOG.Error("load local backup dir failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
baseDir, ok := varMap["dir"].(string)
|
|
|
|
if ok {
|
|
|
|
if _, err := os.Stat(baseDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
|
|
|
|
global.LOG.Errorf("mkdir %s failed, err: %v", baseDir, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
global.CONF.System.Backup = baseDir
|
|
|
|
return
|
|
|
|
}
|
|
|
|
global.LOG.Errorf("error type dir: %T", varMap["dir"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDir() {
|
|
|
|
composePath := path.Join(global.CONF.System.BaseDir, "1panel/docker/compose/")
|
|
|
|
if _, err := os.Stat(composePath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(composePath, os.ModePerm); err != nil {
|
|
|
|
global.LOG.Errorf("mkdir %s failed, err: %v", composePath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-02 16:49:43 +08:00
|
|
|
|
|
|
|
func initSSL() error {
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
if _, err := os.Stat("/opt/1panel/nodeJson"); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
type nodeInfo struct {
|
|
|
|
ServerCrt string `json:"serverCrt"`
|
|
|
|
ServerKey string `json:"serverKey"`
|
|
|
|
CurrentNode string `json:"currentNode"`
|
|
|
|
}
|
|
|
|
nodeJson, err := os.ReadFile("/opt/1panel/nodeJson")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var node nodeInfo
|
|
|
|
if err := json.Unmarshal(nodeJson, &node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
itemKey, _ := encrypt.StringEncrypt(node.ServerKey)
|
|
|
|
if err := settingRepo.Update("ServerKey", itemKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
itemCrt, _ := encrypt.StringEncrypt(node.ServerCrt)
|
|
|
|
if err := settingRepo.Update("ServerCrt", itemCrt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("CurrentNode", node.CurrentNode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.CurrentNode = node.CurrentNode
|
|
|
|
_ = os.Remove(("/opt/1panel/nodeJson"))
|
|
|
|
return nil
|
|
|
|
}
|