Skip to content

Commit

Permalink
feat: Add supports of task display for node upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu committed Nov 25, 2024
1 parent 5a532b3 commit 65e6927
Show file tree
Hide file tree
Showing 28 changed files with 768 additions and 61 deletions.
4 changes: 2 additions & 2 deletions agent/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
if err := u.handleBackup(backupDir, fileOP); err != nil {
return err
}
if err := fileOP.CopyFile(path.Join(req.UpgradePath, "1panel-agent.service"), "/etc/systemd/system/1panel-agent.service"); err != nil {
if err := fileOP.CopyFile(path.Join(req.UpgradePath, "1panel-agent.service"), "/etc/systemd/system"); err != nil {
_ = u.handleRollback(backupDir, 1, fileOP)
global.LOG.Errorf("handle upgrade 1panel service file failed, err: %v", err)
return err
Expand Down Expand Up @@ -83,7 +83,7 @@ func (u *UpgradeService) handleBackup(backupDir string, fileOP files.FileOp) err

func (u *UpgradeService) handleRollback(backupDir string, errStep int, fileOP files.FileOp) error {
if errStep == 1 {
if err := fileOP.CopyFile(path.Join(backupDir, "1panel-agent.service"), "/etc/systemd/system/1panel-agent.service"); err != nil {
if err := fileOP.CopyFile(path.Join(backupDir, "1panel-agent.service"), "/etc/systemd/system"); err != nil {
global.LOG.Errorf("handle recover 1panel service file failed, err: %v", err)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions agent/init/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func Init() {
global.LOG.Fatalf("load base dir before start failed, err: %v", err)
}
global.CONF.System.BaseDir = baseDir.Value
version, err := settingRepo.Get(settingRepo.WithByKey("Version"))
version, err := settingRepo.Get(settingRepo.WithByKey("SystemVersion"))
if err != nil {
global.LOG.Fatalf("load base dir before start failed, err: %v", err)
global.LOG.Fatalf("load system version before start failed, err: %v", err)
}
global.CONF.System.Version = version.Value

Expand Down
2 changes: 1 addition & 1 deletion agent/init/migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func Init() {
migrations.InitImageRepo,
migrations.InitDefaultCA,
migrations.InitPHPExtensions,
migrations.AddTask,
migrations.UpdateWebsite,
migrations.UpdateWebsiteDomain,
migrations.UpdateApp,
migrations.AddTaskDB,
migrations.UpdateAppInstall,
migrations.UpdateSnapshot,
migrations.UpdateCronjob,
migrations.InitBaseDir,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
Expand Down
19 changes: 10 additions & 9 deletions agent/init/migration/migrations/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var AddTable = &gormigrate.Migration{
&model.WebsiteDnsAccount{},
&model.WebsiteDomain{},
&model.WebsiteSSL{},
&model.Task{},
)
},
}
Expand Down Expand Up @@ -217,14 +216,6 @@ var InitPHPExtensions = &gormigrate.Migration{
},
}

var AddTask = &gormigrate.Migration{
ID: "20240802-add-task",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&model.Task{})
},
}

var UpdateWebsite = &gormigrate.Migration{
ID: "20240812-update-website",
Migrate: func(tx *gorm.DB) error {
Expand Down Expand Up @@ -279,3 +270,13 @@ var UpdateCronjob = &gormigrate.Migration{
return tx.AutoMigrate(&model.Cronjob{}, &model.JobRecords{})
},
}

var InitBaseDir = &gormigrate.Migration{
ID: "20241122-init-setting",
Migrate: func(tx *gorm.DB) error {
if err := tx.Create(&model.Setting{Key: "BaseDir", Value: global.CONF.System.BaseDir}).Error; err != nil {
return err
}
return nil
},
}
1 change: 0 additions & 1 deletion agent/router/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ func commonGroups() []CommonRouter {
&RuntimeRouter{},
&ProcessRouter{},
&WebsiteCARouter{},
&UpgradeRouter{},
}
}
3 changes: 3 additions & 0 deletions agent/router/ro_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ func (s *SettingRouter) InitRouter(Router *gin.RouterGroup) {
settingRouter.POST("/snapshot/description/update", baseApi.UpdateSnapDescription)

settingRouter.GET("/basedir", baseApi.LoadBaseDir)

settingRouter.POST("/upgrade", baseApi.Upgrade)
settingRouter.POST("/rollback", baseApi.Rollback)
}
}
17 changes: 0 additions & 17 deletions agent/router/ro_upgrade.go

This file was deleted.

13 changes: 13 additions & 0 deletions core/app/dto/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dto

import "github.com/1Panel-dev/1Panel/core/app/model"

type SearchTaskLogReq struct {
Status string `json:"status"`
Type string `json:"type"`
PageInfo
}

type TaskDTO struct {
model.Task
}
18 changes: 18 additions & 0 deletions core/app/model/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package model

import "time"

type Task struct {
ID string `gorm:"primarykey;" json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Operate string `json:"operate"`
LogFile string `json:"logFile"`
Status string `json:"status"`
ErrorMsg string `json:"errorMsg"`
OperationLogID uint `json:"operationLogID"`
ResourceID uint `json:"resourceID"`
CurrentStep string `json:"currentStep"`
EndAt time.Time `json:"endAt"`
CreatedAt time.Time `json:"createdAt"`
}
9 changes: 9 additions & 0 deletions core/app/model/upgrade_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

type UpgradeLog struct {
BaseModel
NodeID uint `json:"nodeID"`
OldVersion string `json:"oldVersion"`
NewVersion string `json:"newVersion"`
BackupFile string `json:"backupFile"`
}
6 changes: 6 additions & 0 deletions core/app/repo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ICommonRepo interface {
WithByType(ty string) DBOption
WithByKey(key string) DBOption
WithOrderBy(orderStr string) DBOption
WithByStatus(status string) DBOption

WithOrderRuleBy(orderBy, order string) DBOption
}
Expand Down Expand Up @@ -66,6 +67,11 @@ func (c *CommonRepo) WithByKey(key string) DBOption {
return g.Where("key = ?", key)
}
}
func (c *CommonRepo) WithByStatus(status string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("status = ?", status)
}
}
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Order(orderStr)
Expand Down
90 changes: 90 additions & 0 deletions core/app/repo/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package repo

import (
"context"

"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/global"
"gorm.io/gorm"
)

type TaskRepo struct {
}

type ITaskRepo interface {
Save(ctx context.Context, task *model.Task) error
GetFirst(opts ...DBOption) (model.Task, error)
Page(page, size int, opts ...DBOption) (int64, []model.Task, error)
Update(ctx context.Context, task *model.Task) error

WithByID(id string) DBOption
WithResourceID(id uint) DBOption
WithOperate(taskOperate string) DBOption
}

func NewITaskRepo() ITaskRepo {
return &TaskRepo{}
}

func getTaskDb(opts ...DBOption) *gorm.DB {
db := global.TaskDB
for _, opt := range opts {
db = opt(db)
}
return db
}

func getTaskTx(ctx context.Context, opts ...DBOption) *gorm.DB {
tx, ok := ctx.Value("db").(*gorm.DB)
if ok {
for _, opt := range opts {
tx = opt(tx)
}
return tx
}
return getTaskDb(opts...)
}

func (t TaskRepo) WithByID(id string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}

func (t TaskRepo) WithOperate(taskOperate string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("operate = ?", taskOperate)
}
}

func (t TaskRepo) WithResourceID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("resource_id = ?", id)
}
}

func (t TaskRepo) Save(ctx context.Context, task *model.Task) error {
return getTaskTx(ctx).Save(&task).Error
}

func (t TaskRepo) GetFirst(opts ...DBOption) (model.Task, error) {
var task model.Task
db := getTaskDb(opts...).Model(&model.Task{})
if err := db.First(&task).Error; err != nil {
return task, err
}
return task, nil
}

func (t TaskRepo) Page(page, size int, opts ...DBOption) (int64, []model.Task, error) {
var tasks []model.Task
db := getTaskDb(opts...).Model(&model.Task{})
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&tasks).Error
return count, tasks, err
}

func (t TaskRepo) Update(ctx context.Context, task *model.Task) error {
return getTaskTx(ctx).Save(&task).Error
}
93 changes: 93 additions & 0 deletions core/app/repo/upgrade_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package repo

import (
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/global"
"gorm.io/gorm"
)

type UpgradeLogRepo struct{}

type IUpgradeLogRepo interface {
Get(opts ...DBOption) (model.UpgradeLog, error)
List(opts ...DBOption) ([]model.UpgradeLog, error)
Create(log *model.UpgradeLog) error
Page(limit, offset int, opts ...DBOption) (int64, []model.UpgradeLog, error)
Delete(opts ...DBOption) error

WithByNodeID(nodeID uint) DBOption
WithByIDs(ids []uint) DBOption
WithByID(id uint) DBOption
}

func NewIUpgradeLogRepo() IUpgradeLogRepo {
return &UpgradeLogRepo{}
}

func (u *UpgradeLogRepo) Get(opts ...DBOption) (model.UpgradeLog, error) {
var log model.UpgradeLog
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&log).Error
return log, err
}

func (u *UpgradeLogRepo) List(opts ...DBOption) ([]model.UpgradeLog, error) {
var logs []model.UpgradeLog
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&logs).Error
return logs, err
}

func (u *UpgradeLogRepo) Clean() error {
return global.DB.Exec("delete from upgrade_logs;").Error
}

func (u *UpgradeLogRepo) Create(log *model.UpgradeLog) error {
return global.DB.Create(log).Error
}

func (u *UpgradeLogRepo) Save(log *model.UpgradeLog) error {
return global.DB.Save(log).Error
}

func (u *UpgradeLogRepo) Delete(opts ...DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.UpgradeLog{}).Error
}

func (u *UpgradeLogRepo) Page(page, size int, opts ...DBOption) (int64, []model.UpgradeLog, error) {
var ops []model.UpgradeLog
db := global.DB.Model(&model.UpgradeLog{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&ops).Error
return count, ops, err
}

func (c *UpgradeLogRepo) WithByNodeID(nodeID uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("node_id = ?", nodeID)
}
}
func (c *UpgradeLogRepo) WithByID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}
func (c *UpgradeLogRepo) WithByIDs(ids []uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}
2 changes: 2 additions & 0 deletions core/app/service/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ var (
logRepo = repo.NewILogRepo()
groupRepo = repo.NewIGroupRepo()
launcherRepo = repo.NewILauncherRepo()

taskRepo = repo.NewITaskRepo()
)
Loading

0 comments on commit 65e6927

Please sign in to comment.