Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 优化快照功能 #6583

Merged
merged 12 commits into from
Sep 25, 2024
49 changes: 32 additions & 17 deletions agent/app/api/v2/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"github.com/gin-gonic/gin"
)

// @Tags System Setting
// @Summary Load system snapshot data
// @Description 获取系统快照数据
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/snapshot/load [get]
func (b *BaseApi) LoadSnapshotData(c *gin.Context) {
data, err := snapshotService.LoadSnapshotData()
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, data)
}

// @Tags System Setting
// @Summary Create system snapshot
// @Description 创建系统快照
Expand All @@ -30,47 +45,47 @@ func (b *BaseApi) CreateSnapshot(c *gin.Context) {
}

// @Tags System Setting
// @Summary Import system snapshot
// @Description 导入已有快照
// @Summary Recreate system snapshot
// @Description 创建系统快照重试
// @Accept json
// @Param request body dto.SnapshotImport true "request"
// @Param request body dto.OperateByID true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/snapshot/import [post]
// @x-panel-log {"bodyKeys":["from", "names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [from] 同步系统快照 [names]","formatEN":"Sync system snapshots [names] from [from]"}
func (b *BaseApi) ImportSnapshot(c *gin.Context) {
var req dto.SnapshotImport
// @Router /settings/snapshot/recrete [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"重试创建快照 [name]","formatEN":recrete the snapshot [name]"}
func (b *BaseApi) RecreateSnapshot(c *gin.Context) {
var req dto.OperateByID
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

if err := snapshotService.SnapshotImport(req); err != nil {
if err := snapshotService.SnapshotReCreate(req.ID); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}

// @Tags System Setting
// @Summary Load Snapshot status
// @Description 获取快照状态
// @Summary Import system snapshot
// @Description 导入已有快照
// @Accept json
// @Param request body dto.OperateByID true "request"
// @Param request body dto.SnapshotImport true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/snapshot/status [post]
func (b *BaseApi) LoadSnapShotStatus(c *gin.Context) {
var req dto.OperateByID
// @Router /settings/snapshot/import [post]
// @x-panel-log {"bodyKeys":["from", "names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [from] 同步系统快照 [names]","formatEN":"Sync system snapshots [names] from [from]"}
func (b *BaseApi) ImportSnapshot(c *gin.Context) {
var req dto.SnapshotImport
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

data, err := snapshotService.LoadSnapShotStatus(req.ID)
if err != nil {
if err := snapshotService.SnapshotImport(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, data)
helper.SuccessWithData(c, nil)
}

// @Tags System Setting
Expand Down
60 changes: 0 additions & 60 deletions agent/app/dto/setting.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dto

import "time"

type SettingInfo struct {
SystemIP string `json:"systemIP"`
DockerSockPath string `json:"dockerSockPath"`
Expand Down Expand Up @@ -34,64 +32,6 @@ type SettingUpdate struct {
Value string `json:"value"`
}

type SnapshotStatus struct {
Panel string `json:"panel"`
PanelInfo string `json:"panelInfo"`
DaemonJson string `json:"daemonJson"`
AppData string `json:"appData"`
PanelData string `json:"panelData"`
BackupData string `json:"backupData"`

Compress string `json:"compress"`
Size string `json:"size"`
Upload string `json:"upload"`
}

type SnapshotCreate struct {
ID uint `json:"id"`
SourceAccountIDs string `json:"sourceAccountIDs" validate:"required"`
DownloadAccountID uint `json:"downloadAccountID" validate:"required"`
Description string `json:"description" validate:"max=256"`
Secret string `json:"secret"`
}
type SnapshotRecover struct {
IsNew bool `json:"isNew"`
ReDownload bool `json:"reDownload"`
ID uint `json:"id" validate:"required"`
Secret string `json:"secret"`
}
type SnapshotBatchDelete struct {
DeleteWithFile bool `json:"deleteWithFile"`
Ids []uint `json:"ids" validate:"required"`
}

type SnapshotImport struct {
BackupAccountID uint `json:"backupAccountID"`
Names []string `json:"names"`
Description string `json:"description" validate:"max=256"`
}

type SnapshotInfo struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description" validate:"max=256"`
From string `json:"from"`
DefaultDownload string `json:"defaultDownload"`
Status string `json:"status"`
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt"`
Version string `json:"version"`
Size int64 `json:"size"`

InterruptStep string `json:"interruptStep"`
RecoverStatus string `json:"recoverStatus"`
RecoverMessage string `json:"recoverMessage"`
LastRecoveredAt string `json:"lastRecoveredAt"`
RollbackStatus string `json:"rollbackStatus"`
RollbackMessage string `json:"rollbackMessage"`
LastRollbackedAt string `json:"lastRollbackedAt"`
}

type SyncTime struct {
NtpSite string `json:"ntpSite" validate:"required"`
}
Expand Down
103 changes: 103 additions & 0 deletions agent/app/dto/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package dto

import "time"

type SnapshotStatus struct {
BaseData string `json:"baseData"`
AppImage string `json:"appImage"`
PanelData string `json:"panelData"`
BackupData string `json:"backupData"`

Compress string `json:"compress"`
Size string `json:"size"`
Upload string `json:"upload"`
}

type SnapshotCreate struct {
ID uint `json:"id"`
Name string `json:"name"`
TaskID string `json:"taskID"`
SourceAccountIDs string `json:"sourceAccountIDs" validate:"required"`
DownloadAccountID uint `json:"downloadAccountID" validate:"required"`
Description string `json:"description" validate:"max=256"`
Secret string `json:"secret"`
InterruptStep string `json:"interruptStep"`

AppData []DataTree `json:"appData"`
BackupData []DataTree `json:"backupData"`
PanelData []DataTree `json:"panelData"`

WithMonitorData bool `json:"withMonitorData"`
WithLoginLog bool `json:"withLoginLog"`
WithOperationLog bool `json:"withOperationLog"`
WithSystemLog bool `json:"withSystemLog"`
WithTaskLog bool `json:"withTaskLog"`
}

type SnapshotData struct {
AppData []DataTree `json:"appData"`
BackupData []DataTree `json:"backupData"`
PanelData []DataTree `json:"panelData"`

WithMonitorData bool `json:"withMonitorData"`
WithLoginLog bool `json:"withLoginLog"`
WithOperationLog bool `json:"withOperationLog"`
WithSystemLog bool `json:"withSystemLog"`
WithTaskLog bool `json:"withTaskLog"`
}
type DataTree struct {
ID string `json:"id"`
Label string `json:"label"`
Key string `json:"key"`
Name string `json:"name"`
Size uint64 `json:"size"`
IsCheck bool `json:"isCheck"`
IsDisable bool `json:"isDisable"`

Path string `json:"path"`

RelationItemID string `json:"relationItemID"`
Children []DataTree `json:"children"`
}
type SnapshotRecover struct {
IsNew bool `json:"isNew"`
ReDownload bool `json:"reDownload"`
ID uint `json:"id" validate:"required"`
TaskID string `json:"taskID"`
Secret string `json:"secret"`
}
type SnapshotBatchDelete struct {
DeleteWithFile bool `json:"deleteWithFile"`
Ids []uint `json:"ids" validate:"required"`
}

type SnapshotImport struct {
BackupAccountID uint `json:"backupAccountID"`
Names []string `json:"names"`
Description string `json:"description" validate:"max=256"`
}

type SnapshotInfo struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description" validate:"max=256"`
From string `json:"from"`
DefaultDownload string `json:"defaultDownload"`
Status string `json:"status"`
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt"`
Version string `json:"version"`
Size int64 `json:"size"`

TaskID string `json:"taskID"`
TaskRecoverID string `json:"taskRecoverID"`
TaskRollbackID string `json:"taskRollbackID"`

InterruptStep string `json:"interruptStep"`
RecoverStatus string `json:"recoverStatus"`
RecoverMessage string `json:"recoverMessage"`
LastRecoveredAt string `json:"lastRecoveredAt"`
RollbackStatus string `json:"rollbackStatus"`
RollbackMessage string `json:"rollbackMessage"`
LastRollbackedAt string `json:"lastRollbackedAt"`
}
31 changes: 14 additions & 17 deletions agent/app/model/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ package model
type Snapshot struct {
BaseModel
Name string `json:"name" gorm:"not null;unique"`
Secret string `json:"secret"`
Description string `json:"description"`
SourceAccountIDs string `json:"sourceAccountIDs"`
DownloadAccountID uint `json:"downloadAccountID"`
Status string `json:"status"`
Message string `json:"message"`
Version string `json:"version"`

TaskID string `json:"taskID"`
TaskRecoverID string `json:"taskRecoverID"`
TaskRollbackID string `json:"taskRollbackID"`

AppData string `json:"appData"`
PanelData string `json:"panelData"`
BackupData string `json:"backupData"`
WithMonitorData bool `json:"withMonitorData"`
WithLoginLog bool `json:"withLoginLog"`
WithOperationLog bool `json:"withOperationLog"`
WithSystemLog bool `json:"withSystemLog"`
WithTaskLog bool `json:"withTaskLog"`

InterruptStep string `json:"interruptStep"`
RecoverStatus string `json:"recoverStatus"`
RecoverMessage string `json:"recoverMessage"`
LastRecoveredAt string `json:"lastRecoveredAt"`
RollbackStatus string `json:"rollbackStatus"`
RollbackMessage string `json:"rollbackMessage"`
LastRollbackAt string `json:"lastRollbackAt"`
}

type SnapshotStatus struct {
BaseModel
SnapID uint `json:"snapID"`
Panel string `json:"panel" gorm:"default:Running"`
PanelInfo string `json:"panelInfo" gorm:"default:Running"`
DaemonJson string `json:"daemonJson" gorm:"default:Running"`
AppData string `json:"appData" gorm:"default:Running"`
PanelData string `json:"panelData" gorm:"default:Running"`
BackupData string `json:"backupData" gorm:"default:Running"`

Compress string `json:"compress" gorm:"default:Waiting"`
Size string `json:"size" `
Upload string `json:"upload" gorm:"default:Waiting"`
}
36 changes: 0 additions & 36 deletions agent/app/repo/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ type ISnapshotRepo interface {
Update(id uint, vars map[string]interface{}) error
Page(limit, offset int, opts ...DBOption) (int64, []model.Snapshot, error)
Delete(opts ...DBOption) error

GetStatus(snapID uint) (model.SnapshotStatus, error)
GetStatusList(opts ...DBOption) ([]model.SnapshotStatus, error)
CreateStatus(snap *model.SnapshotStatus) error
DeleteStatus(snapID uint) error
UpdateStatus(id uint, vars map[string]interface{}) error
}

func NewISnapshotRepo() ISnapshotRepo {
Expand Down Expand Up @@ -73,33 +67,3 @@ func (u *SnapshotRepo) Delete(opts ...DBOption) error {
}
return db.Delete(&model.Snapshot{}).Error
}

func (u *SnapshotRepo) GetStatus(snapID uint) (model.SnapshotStatus, error) {
var data model.SnapshotStatus
if err := global.DB.Where("snap_id = ?", snapID).First(&data).Error; err != nil {
return data, err
}
return data, nil
}

func (u *SnapshotRepo) GetStatusList(opts ...DBOption) ([]model.SnapshotStatus, error) {
var status []model.SnapshotStatus
db := global.DB.Model(&model.SnapshotStatus{})
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&status).Error
return status, err
}

func (u *SnapshotRepo) CreateStatus(snap *model.SnapshotStatus) error {
return global.DB.Create(snap).Error
}

func (u *SnapshotRepo) DeleteStatus(snapID uint) error {
return global.DB.Where("snap_id = ?", snapID).Delete(&model.SnapshotStatus{}).Error
}

func (u *SnapshotRepo) UpdateStatus(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.SnapshotStatus{}).Where("id = ?", id).Updates(vars).Error
}
7 changes: 4 additions & 3 deletions agent/app/repo/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"context"

"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"

Expand All @@ -13,7 +14,7 @@ type TaskRepo struct {
}

type ITaskRepo interface {
Create(ctx context.Context, task *model.Task) error
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
Expand Down Expand Up @@ -64,8 +65,8 @@ func (t TaskRepo) WithResourceID(id uint) DBOption {
}
}

func (t TaskRepo) Create(ctx context.Context, task *model.Task) error {
return getTaskTx(ctx).Create(&task).Error
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) {
Expand Down
2 changes: 1 addition & 1 deletion agent/app/service/container_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (u *ContainerService) TestCompose(req dto.ComposeCreate) (bool, error) {
if err := u.loadPath(&req); err != nil {
return false, err
}
cmd := exec.Command("docker compose", "-f", req.Path, "config")
cmd := exec.Command("docker", "compose", "-f", req.Path, "config")
stdout, err := cmd.CombinedOutput()
if err != nil {
return false, errors.New(string(stdout))
Expand Down
Loading
Loading