diff --git a/core/app/api/v2/upgrade.go b/core/app/api/v2/upgrade.go index 17802aac8..b99b07365 100644 --- a/core/app/api/v2/upgrade.go +++ b/core/app/api/v2/upgrade.go @@ -65,3 +65,25 @@ func (b *BaseApi) Upgrade(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +// @Tags System Setting +// @Summary Upgrade +// @Description 系统回滚 +// @Accept json +// @Param request body dto.OperateByID true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /core/settings/rollback [post] +// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"upgrade_logs","output_column":"old_version","output_value":"version"}],"formatZH":"回滚系统 => [version]","formatEN":"rollback system => [version]"} +func (b *BaseApi) Rollback(c *gin.Context) { + var req dto.OperateByID + if err := helper.CheckBindAndValidate(&req, c); err != nil { + return + } + + if err := upgradeService.Rollback(req); err != nil { + helper.InternalServer(c, err) + return + } + helper.SuccessWithData(c, nil) +} diff --git a/core/app/repo/app_launcher.go b/core/app/repo/app_launcher.go index adf8ced98..2647051f0 100644 --- a/core/app/repo/app_launcher.go +++ b/core/app/repo/app_launcher.go @@ -8,17 +8,17 @@ import ( type LauncherRepo struct{} type ILauncherRepo interface { - Get(opts ...DBOption) (model.AppLauncher, error) - List(opts ...DBOption) ([]model.AppLauncher, error) + Get(opts ...global.DBOption) (model.AppLauncher, error) + List(opts ...global.DBOption) ([]model.AppLauncher, error) Create(launcher *model.AppLauncher) error - Delete(opts ...DBOption) error + Delete(opts ...global.DBOption) error } func NewILauncherRepo() ILauncherRepo { return &LauncherRepo{} } -func (u *LauncherRepo) Get(opts ...DBOption) (model.AppLauncher, error) { +func (u *LauncherRepo) Get(opts ...global.DBOption) (model.AppLauncher, error) { var launcher model.AppLauncher db := global.DB for _, opt := range opts { @@ -27,7 +27,7 @@ func (u *LauncherRepo) Get(opts ...DBOption) (model.AppLauncher, error) { err := db.First(&launcher).Error return launcher, err } -func (u *LauncherRepo) List(opts ...DBOption) ([]model.AppLauncher, error) { +func (u *LauncherRepo) List(opts ...global.DBOption) ([]model.AppLauncher, error) { var ops []model.AppLauncher db := global.DB.Model(&model.AppLauncher{}) for _, opt := range opts { @@ -41,7 +41,7 @@ func (u *LauncherRepo) Create(launcher *model.AppLauncher) error { return global.DB.Create(launcher).Error } -func (u *LauncherRepo) Delete(opts ...DBOption) error { +func (u *LauncherRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) diff --git a/core/app/repo/backup.go b/core/app/repo/backup.go index 76ef2b7d4..937b2c00a 100644 --- a/core/app/repo/backup.go +++ b/core/app/repo/backup.go @@ -8,19 +8,19 @@ import ( type BackupRepo struct{} type IBackupRepo interface { - Get(opts ...DBOption) (model.BackupAccount, error) - List(opts ...DBOption) ([]model.BackupAccount, error) - Page(limit, offset int, opts ...DBOption) (int64, []model.BackupAccount, error) + Get(opts ...global.DBOption) (model.BackupAccount, error) + List(opts ...global.DBOption) ([]model.BackupAccount, error) + Page(limit, offset int, opts ...global.DBOption) (int64, []model.BackupAccount, error) Create(backup *model.BackupAccount) error Save(backup *model.BackupAccount) error - Delete(opts ...DBOption) error + Delete(opts ...global.DBOption) error } func NewIBackupRepo() IBackupRepo { return &BackupRepo{} } -func (u *BackupRepo) Get(opts ...DBOption) (model.BackupAccount, error) { +func (u *BackupRepo) Get(opts ...global.DBOption) (model.BackupAccount, error) { var backup model.BackupAccount db := global.DB for _, opt := range opts { @@ -30,7 +30,7 @@ func (u *BackupRepo) Get(opts ...DBOption) (model.BackupAccount, error) { return backup, err } -func (u *BackupRepo) Page(page, size int, opts ...DBOption) (int64, []model.BackupAccount, error) { +func (u *BackupRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.BackupAccount, error) { var ops []model.BackupAccount db := global.DB.Model(&model.BackupAccount{}) for _, opt := range opts { @@ -42,7 +42,7 @@ func (u *BackupRepo) Page(page, size int, opts ...DBOption) (int64, []model.Back return count, ops, err } -func (u *BackupRepo) List(opts ...DBOption) ([]model.BackupAccount, error) { +func (u *BackupRepo) List(opts ...global.DBOption) ([]model.BackupAccount, error) { var ops []model.BackupAccount db := global.DB.Model(&model.BackupAccount{}) for _, opt := range opts { @@ -60,7 +60,7 @@ func (u *BackupRepo) Save(backup *model.BackupAccount) error { return global.DB.Save(backup).Error } -func (u *BackupRepo) Delete(opts ...DBOption) error { +func (u *BackupRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) diff --git a/core/app/repo/command.go b/core/app/repo/command.go index 6212ed050..807df054f 100644 --- a/core/app/repo/command.go +++ b/core/app/repo/command.go @@ -3,25 +3,28 @@ package repo import ( "github.com/1Panel-dev/1Panel/core/app/model" "github.com/1Panel-dev/1Panel/core/global" + "gorm.io/gorm" ) type CommandRepo struct{} type ICommandRepo interface { - List(opts ...DBOption) ([]model.Command, error) - Page(limit, offset int, opts ...DBOption) (int64, []model.Command, error) + List(opts ...global.DBOption) ([]model.Command, error) + Page(limit, offset int, opts ...global.DBOption) (int64, []model.Command, error) Create(command *model.Command) error Update(id uint, vars map[string]interface{}) error UpdateGroup(group, newGroup uint) error - Delete(opts ...DBOption) error - Get(opts ...DBOption) (model.Command, error) + Delete(opts ...global.DBOption) error + Get(opts ...global.DBOption) (model.Command, error) + + WithByInfo(info string) global.DBOption } func NewICommandRepo() ICommandRepo { return &CommandRepo{} } -func (u *CommandRepo) Get(opts ...DBOption) (model.Command, error) { +func (u *CommandRepo) Get(opts ...global.DBOption) (model.Command, error) { var command model.Command db := global.DB for _, opt := range opts { @@ -31,7 +34,7 @@ func (u *CommandRepo) Get(opts ...DBOption) (model.Command, error) { return command, err } -func (u *CommandRepo) Page(page, size int, opts ...DBOption) (int64, []model.Command, error) { +func (u *CommandRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.Command, error) { var users []model.Command db := global.DB.Model(&model.Command{}) for _, opt := range opts { @@ -43,7 +46,7 @@ func (u *CommandRepo) Page(page, size int, opts ...DBOption) (int64, []model.Com return count, users, err } -func (u *CommandRepo) List(opts ...DBOption) ([]model.Command, error) { +func (u *CommandRepo) List(opts ...global.DBOption) ([]model.Command, error) { var commands []model.Command db := global.DB.Model(&model.Command{}) for _, opt := range opts { @@ -64,10 +67,19 @@ func (h *CommandRepo) UpdateGroup(group, newGroup uint) error { return global.DB.Model(&model.Command{}).Where("group_id = ?", group).Updates(map[string]interface{}{"group_id": newGroup}).Error } -func (u *CommandRepo) Delete(opts ...DBOption) error { +func (u *CommandRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) } return db.Delete(&model.Command{}).Error } + +func (c *CommandRepo) WithByInfo(info string) global.DBOption { + return func(g *gorm.DB) *gorm.DB { + if len(info) == 0 { + return g + } + return g.Where("name like ? or command like ?", "%"+info+"%", "%"+info+"%") + } +} diff --git a/core/app/repo/common.go b/core/app/repo/common.go index d1b34c78f..cad791ff9 100644 --- a/core/app/repo/common.go +++ b/core/app/repo/common.go @@ -4,22 +4,24 @@ import ( "fmt" "github.com/1Panel-dev/1Panel/core/constant" + "github.com/1Panel-dev/1Panel/core/global" "gorm.io/gorm" ) -type DBOption func(*gorm.DB) *gorm.DB - type ICommonRepo interface { - WithByID(id uint) DBOption - WithByIDs(ids []uint) DBOption - WithByName(name string) DBOption - WithLikeName(name string) DBOption - WithByType(ty string) DBOption - WithByKey(key string) DBOption - WithOrderBy(orderStr string) DBOption - WithByStatus(status string) DBOption + WithByID(id uint) global.DBOption + WithByGroupID(id uint) global.DBOption - WithOrderRuleBy(orderBy, order string) DBOption + WithByName(name string) global.DBOption + WithByType(ty string) global.DBOption + WithByKey(key string) global.DBOption + WithOrderBy(orderStr string) global.DBOption + WithByStatus(status string) global.DBOption + WithByGroupBelong(group string) global.DBOption + + WithByIDs(ids []uint) global.DBOption + + WithOrderRuleBy(orderBy, order string) global.DBOption } type CommonRepo struct{} @@ -28,57 +30,56 @@ func NewICommonRepo() ICommonRepo { return &CommonRepo{} } -func (c *CommonRepo) WithByID(id uint) DBOption { +func (c *CommonRepo) WithByID(id uint) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("id = ?", id) } } -func (c *CommonRepo) WithByIDs(ids []uint) DBOption { +func (c *CommonRepo) WithByGroupID(id uint) global.DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("group_id = ?", id) + } +} + +func (c *CommonRepo) WithByIDs(ids []uint) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("id in (?)", ids) } } -func (c *CommonRepo) WithByName(name string) DBOption { +func (c *CommonRepo) WithByName(name string) global.DBOption { return func(g *gorm.DB) *gorm.DB { - if len(name) == 0 { - return g - } return g.Where("`name` = ?", name) } } -func (c *CommonRepo) WithLikeName(name string) DBOption { + +func (c *CommonRepo) WithByType(ty string) global.DBOption { return func(g *gorm.DB) *gorm.DB { - if len(name) == 0 { - return g - } - return g.Where("name like ? or command like ?", "%"+name+"%", "%"+name+"%") - } -} -func (c *CommonRepo) WithByType(ty string) DBOption { - return func(g *gorm.DB) *gorm.DB { - if len(ty) == 0 { - return g - } return g.Where("`type` = ?", ty) } } -func (c *CommonRepo) WithByKey(key string) DBOption { +func (c *CommonRepo) WithByKey(key string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("key = ?", key) } } -func (c *CommonRepo) WithByStatus(status string) DBOption { +func (c *CommonRepo) WithByStatus(status string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("status = ?", status) } } -func (c *CommonRepo) WithOrderBy(orderStr string) DBOption { +func (c *CommonRepo) WithByGroupBelong(group string) global.DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("group_belong = ?", group) + } +} + +func (c *CommonRepo) WithOrderBy(orderStr string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Order(orderStr) } } -func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) DBOption { +func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) global.DBOption { switch order { case constant.OrderDesc: order = "desc" diff --git a/core/app/repo/group.go b/core/app/repo/group.go index 9ce1336ef..3cceba0c4 100644 --- a/core/app/repo/group.go +++ b/core/app/repo/group.go @@ -9,16 +9,13 @@ import ( type GroupRepo struct{} type IGroupRepo interface { - Get(opts ...DBOption) (model.Group, error) - GetList(opts ...DBOption) ([]model.Group, error) + Get(opts ...global.DBOption) (model.Group, error) + GetList(opts ...global.DBOption) ([]model.Group, error) Create(group *model.Group) error Update(id uint, vars map[string]interface{}) error - Delete(opts ...DBOption) error + Delete(opts ...global.DBOption) error - WithByID(id uint) DBOption - WithByGroupID(id uint) DBOption - WithByGroupType(ty string) DBOption - WithByDefault(isDefalut bool) DBOption + WithByDefault(isDefalut bool) global.DBOption CancelDefault(groupType string) error } @@ -26,31 +23,13 @@ func NewIGroupRepo() IGroupRepo { return &GroupRepo{} } -func (c *GroupRepo) WithByID(id uint) DBOption { - return func(g *gorm.DB) *gorm.DB { - return g.Where("id = ?", id) - } -} - -func (c *GroupRepo) WithByGroupID(id uint) DBOption { - return func(g *gorm.DB) *gorm.DB { - return g.Where("group_id = ?", id) - } -} - -func (c *GroupRepo) WithByGroupType(ty string) DBOption { - return func(g *gorm.DB) *gorm.DB { - return g.Where("`type` = ?", ty) - } -} - -func (c *GroupRepo) WithByDefault(isDefalut bool) DBOption { +func (c *GroupRepo) WithByDefault(isDefalut bool) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("is_default = ?", isDefalut) } } -func (u *GroupRepo) Get(opts ...DBOption) (model.Group, error) { +func (u *GroupRepo) Get(opts ...global.DBOption) (model.Group, error) { var group model.Group db := global.DB for _, opt := range opts { @@ -60,7 +39,7 @@ func (u *GroupRepo) Get(opts ...DBOption) (model.Group, error) { return group, err } -func (u *GroupRepo) GetList(opts ...DBOption) ([]model.Group, error) { +func (u *GroupRepo) GetList(opts ...global.DBOption) ([]model.Group, error) { var groups []model.Group db := global.DB.Model(&model.Group{}) for _, opt := range opts { @@ -78,7 +57,7 @@ func (u *GroupRepo) Update(id uint, vars map[string]interface{}) error { return global.DB.Model(&model.Group{}).Where("id = ?", id).Updates(vars).Error } -func (u *GroupRepo) Delete(opts ...DBOption) error { +func (u *GroupRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) diff --git a/core/app/repo/host.go b/core/app/repo/host.go index a93d57f50..a0066a02a 100644 --- a/core/app/repo/host.go +++ b/core/app/repo/host.go @@ -9,25 +9,25 @@ import ( type HostRepo struct{} type IHostRepo interface { - Get(opts ...DBOption) (model.Host, error) - GetList(opts ...DBOption) ([]model.Host, error) - Page(limit, offset int, opts ...DBOption) (int64, []model.Host, error) + Get(opts ...global.DBOption) (model.Host, error) + GetList(opts ...global.DBOption) ([]model.Host, error) + Page(limit, offset int, opts ...global.DBOption) (int64, []model.Host, error) Create(host *model.Host) error Update(id uint, vars map[string]interface{}) error UpdateGroup(group, newGroup uint) error - Delete(opts ...DBOption) error + Delete(opts ...global.DBOption) error - WithByInfo(info string) DBOption - WithByPort(port uint) DBOption - WithByUser(user string) DBOption - WithByAddr(addr string) DBOption + WithByInfo(info string) global.DBOption + WithByPort(port uint) global.DBOption + WithByUser(user string) global.DBOption + WithByAddr(addr string) global.DBOption } func NewIHostRepo() IHostRepo { return &HostRepo{} } -func (h *HostRepo) Get(opts ...DBOption) (model.Host, error) { +func (h *HostRepo) Get(opts ...global.DBOption) (model.Host, error) { var host model.Host db := global.DB for _, opt := range opts { @@ -37,7 +37,7 @@ func (h *HostRepo) Get(opts ...DBOption) (model.Host, error) { return host, err } -func (h *HostRepo) GetList(opts ...DBOption) ([]model.Host, error) { +func (h *HostRepo) GetList(opts ...global.DBOption) ([]model.Host, error) { var hosts []model.Host db := global.DB.Model(&model.Host{}) for _, opt := range opts { @@ -47,7 +47,7 @@ func (h *HostRepo) GetList(opts ...DBOption) ([]model.Host, error) { return hosts, err } -func (h *HostRepo) Page(page, size int, opts ...DBOption) (int64, []model.Host, error) { +func (h *HostRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.Host, error) { var users []model.Host db := global.DB.Model(&model.Host{}) for _, opt := range opts { @@ -59,7 +59,7 @@ func (h *HostRepo) Page(page, size int, opts ...DBOption) (int64, []model.Host, return count, users, err } -func (h *HostRepo) WithByInfo(info string) DBOption { +func (h *HostRepo) WithByInfo(info string) global.DBOption { return func(g *gorm.DB) *gorm.DB { if len(info) == 0 { return g @@ -69,29 +69,21 @@ func (h *HostRepo) WithByInfo(info string) DBOption { } } -func (h *HostRepo) WithByPort(port uint) DBOption { +func (h *HostRepo) WithByPort(port uint) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("port = ?", port) } } -func (h *HostRepo) WithByUser(user string) DBOption { +func (h *HostRepo) WithByUser(user string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("user = ?", user) } } -func (h *HostRepo) WithByAddr(addr string) DBOption { +func (h *HostRepo) WithByAddr(addr string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("addr = ?", addr) } } -func (h *HostRepo) WithByGroup(group string) DBOption { - return func(g *gorm.DB) *gorm.DB { - if len(group) == 0 { - return g - } - return g.Where("group_belong = ?", group) - } -} func (h *HostRepo) Create(host *model.Host) error { return global.DB.Create(host).Error @@ -105,7 +97,7 @@ func (h *HostRepo) UpdateGroup(group, newGroup uint) error { return global.DB.Model(&model.Host{}).Where("group_id = ?", group).Updates(map[string]interface{}{"group_id": newGroup}).Error } -func (h *HostRepo) Delete(opts ...DBOption) error { +func (h *HostRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) diff --git a/core/app/repo/logs.go b/core/app/repo/logs.go index 87660aa5a..fbc097109 100644 --- a/core/app/repo/logs.go +++ b/core/app/repo/logs.go @@ -11,16 +11,15 @@ type LogRepo struct{} type ILogRepo interface { CleanLogin() error CreateLoginLog(user *model.LoginLog) error - PageLoginLog(limit, offset int, opts ...DBOption) (int64, []model.LoginLog, error) + PageLoginLog(limit, offset int, opts ...global.DBOption) (int64, []model.LoginLog, error) CleanOperation() error CreateOperationLog(user *model.OperationLog) error - PageOperationLog(limit, offset int, opts ...DBOption) (int64, []model.OperationLog, error) + PageOperationLog(limit, offset int, opts ...global.DBOption) (int64, []model.OperationLog, error) - WithByIP(ip string) DBOption - WithByStatus(status string) DBOption - WithByGroup(group string) DBOption - WithByLikeOperation(operation string) DBOption + WithByIP(ip string) global.DBOption + WithBySource(source string) global.DBOption + WithByLikeOperation(operation string) global.DBOption } func NewILogRepo() ILogRepo { @@ -35,7 +34,7 @@ func (u *LogRepo) CreateLoginLog(log *model.LoginLog) error { return global.DB.Create(log).Error } -func (u *LogRepo) PageLoginLog(page, size int, opts ...DBOption) (int64, []model.LoginLog, error) { +func (u *LogRepo) PageLoginLog(page, size int, opts ...global.DBOption) (int64, []model.LoginLog, error) { var ops []model.LoginLog db := global.DB.Model(&model.LoginLog{}) for _, opt := range opts { @@ -55,7 +54,7 @@ func (u *LogRepo) CreateOperationLog(log *model.OperationLog) error { return global.DB.Create(log).Error } -func (u *LogRepo) PageOperationLog(page, size int, opts ...DBOption) (int64, []model.OperationLog, error) { +func (u *LogRepo) PageOperationLog(page, size int, opts ...global.DBOption) (int64, []model.OperationLog, error) { var ops []model.OperationLog db := global.DB.Model(&model.OperationLog{}) for _, opt := range opts { @@ -67,7 +66,7 @@ func (u *LogRepo) PageOperationLog(page, size int, opts ...DBOption) (int64, []m return count, ops, err } -func (c *LogRepo) WithByStatus(status string) DBOption { +func (c *LogRepo) WithByStatus(status string) global.DBOption { return func(g *gorm.DB) *gorm.DB { if len(status) == 0 { return g @@ -75,23 +74,21 @@ func (c *LogRepo) WithByStatus(status string) DBOption { return g.Where("status = ?", status) } } -func (c *LogRepo) WithByGroup(group string) DBOption { +func (c *LogRepo) WithBySource(source string) global.DBOption { return func(g *gorm.DB) *gorm.DB { - if len(group) == 0 { + if len(source) == 0 { return g } - return g.Where("source = ?", group) + return g.Where("source = ?", source) } } -func (c *LogRepo) WithByIP(ip string) DBOption { +func (c *LogRepo) WithByIP(ip string) global.DBOption { return func(g *gorm.DB) *gorm.DB { - if len(ip) == 0 { - return g - } return g.Where("ip LIKE ?", "%"+ip+"%") } } -func (c *LogRepo) WithByLikeOperation(operation string) DBOption { + +func (c *LogRepo) WithByLikeOperation(operation string) global.DBOption { return func(g *gorm.DB) *gorm.DB { if len(operation) == 0 { return g diff --git a/core/app/repo/setting.go b/core/app/repo/setting.go index 90847c539..e7b3a26cc 100644 --- a/core/app/repo/setting.go +++ b/core/app/repo/setting.go @@ -8,8 +8,8 @@ import ( type SettingRepo struct{} type ISettingRepo interface { - List(opts ...DBOption) ([]model.Setting, error) - Get(opts ...DBOption) (model.Setting, error) + List(opts ...global.DBOption) ([]model.Setting, error) + Get(opts ...global.DBOption) (model.Setting, error) Create(key, value string) error Update(key, value string) error } @@ -18,7 +18,7 @@ func NewISettingRepo() ISettingRepo { return &SettingRepo{} } -func (u *SettingRepo) List(opts ...DBOption) ([]model.Setting, error) { +func (u *SettingRepo) List(opts ...global.DBOption) ([]model.Setting, error) { var settings []model.Setting db := global.DB.Model(&model.Setting{}) for _, opt := range opts { @@ -36,7 +36,7 @@ func (u *SettingRepo) Create(key, value string) error { return global.DB.Create(setting).Error } -func (u *SettingRepo) Get(opts ...DBOption) (model.Setting, error) { +func (u *SettingRepo) Get(opts ...global.DBOption) (model.Setting, error) { var settings model.Setting db := global.DB.Model(&model.Setting{}) for _, opt := range opts { diff --git a/core/app/repo/task.go b/core/app/repo/task.go index 0d7edcef5..c700b7478 100644 --- a/core/app/repo/task.go +++ b/core/app/repo/task.go @@ -13,20 +13,20 @@ 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) + GetFirst(opts ...global.DBOption) (model.Task, error) + Page(page, size int, opts ...global.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 + WithByID(id string) global.DBOption + WithResourceID(id uint) global.DBOption + WithOperate(taskOperate string) global.DBOption } func NewITaskRepo() ITaskRepo { return &TaskRepo{} } -func getTaskDb(opts ...DBOption) *gorm.DB { +func getTaskDb(opts ...global.DBOption) *gorm.DB { db := global.TaskDB for _, opt := range opts { db = opt(db) @@ -34,7 +34,7 @@ func getTaskDb(opts ...DBOption) *gorm.DB { return db } -func getTaskTx(ctx context.Context, opts ...DBOption) *gorm.DB { +func getTaskTx(ctx context.Context, opts ...global.DBOption) *gorm.DB { tx, ok := ctx.Value("db").(*gorm.DB) if ok { for _, opt := range opts { @@ -45,19 +45,19 @@ func getTaskTx(ctx context.Context, opts ...DBOption) *gorm.DB { return getTaskDb(opts...) } -func (t TaskRepo) WithByID(id string) DBOption { +func (t TaskRepo) WithByID(id string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("id = ?", id) } } -func (t TaskRepo) WithOperate(taskOperate string) DBOption { +func (t TaskRepo) WithOperate(taskOperate string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("operate = ?", taskOperate) } } -func (t TaskRepo) WithResourceID(id uint) DBOption { +func (t TaskRepo) WithResourceID(id uint) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("resource_id = ?", id) } @@ -67,7 +67,7 @@ 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) { +func (t TaskRepo) GetFirst(opts ...global.DBOption) (model.Task, error) { var task model.Task db := getTaskDb(opts...).Model(&model.Task{}) if err := db.First(&task).Error; err != nil { @@ -76,7 +76,7 @@ func (t TaskRepo) GetFirst(opts ...DBOption) (model.Task, error) { return task, nil } -func (t TaskRepo) Page(page, size int, opts ...DBOption) (int64, []model.Task, error) { +func (t TaskRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.Task, error) { var tasks []model.Task db := getTaskDb(opts...).Model(&model.Task{}) count := int64(0) diff --git a/core/app/repo/upgrade_log.go b/core/app/repo/upgrade_log.go index 1a9d27b5d..9e0b129dc 100644 --- a/core/app/repo/upgrade_log.go +++ b/core/app/repo/upgrade_log.go @@ -9,22 +9,20 @@ import ( type UpgradeLogRepo struct{} type IUpgradeLogRepo interface { - Get(opts ...DBOption) (model.UpgradeLog, error) - List(opts ...DBOption) ([]model.UpgradeLog, error) + Get(opts ...global.DBOption) (model.UpgradeLog, error) + List(opts ...global.DBOption) ([]model.UpgradeLog, error) Create(log *model.UpgradeLog) error - Page(limit, offset int, opts ...DBOption) (int64, []model.UpgradeLog, error) - Delete(opts ...DBOption) error + Page(limit, offset int, opts ...global.DBOption) (int64, []model.UpgradeLog, error) + Delete(opts ...global.DBOption) error - WithByNodeID(nodeID uint) DBOption - WithByIDs(ids []uint) DBOption - WithByID(id uint) DBOption + WithByNodeID(nodeID uint) global.DBOption } func NewIUpgradeLogRepo() IUpgradeLogRepo { return &UpgradeLogRepo{} } -func (u *UpgradeLogRepo) Get(opts ...DBOption) (model.UpgradeLog, error) { +func (u *UpgradeLogRepo) Get(opts ...global.DBOption) (model.UpgradeLog, error) { var log model.UpgradeLog db := global.DB for _, opt := range opts { @@ -34,7 +32,7 @@ func (u *UpgradeLogRepo) Get(opts ...DBOption) (model.UpgradeLog, error) { return log, err } -func (u *UpgradeLogRepo) List(opts ...DBOption) ([]model.UpgradeLog, error) { +func (u *UpgradeLogRepo) List(opts ...global.DBOption) ([]model.UpgradeLog, error) { var logs []model.UpgradeLog db := global.DB for _, opt := range opts { @@ -56,7 +54,7 @@ func (u *UpgradeLogRepo) Save(log *model.UpgradeLog) error { return global.DB.Save(log).Error } -func (u *UpgradeLogRepo) Delete(opts ...DBOption) error { +func (u *UpgradeLogRepo) Delete(opts ...global.DBOption) error { db := global.DB for _, opt := range opts { db = opt(db) @@ -64,7 +62,7 @@ func (u *UpgradeLogRepo) Delete(opts ...DBOption) error { return db.Delete(&model.UpgradeLog{}).Error } -func (u *UpgradeLogRepo) Page(page, size int, opts ...DBOption) (int64, []model.UpgradeLog, error) { +func (u *UpgradeLogRepo) Page(page, size int, opts ...global.DBOption) (int64, []model.UpgradeLog, error) { var ops []model.UpgradeLog db := global.DB.Model(&model.UpgradeLog{}) for _, opt := range opts { @@ -76,18 +74,8 @@ func (u *UpgradeLogRepo) Page(page, size int, opts ...DBOption) (int64, []model. return count, ops, err } -func (c *UpgradeLogRepo) WithByNodeID(nodeID uint) DBOption { +func (c *UpgradeLogRepo) WithByNodeID(nodeID uint) global.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) - } -} diff --git a/core/app/service/command.go b/core/app/service/command.go index 6434143d8..bb5da1f98 100644 --- a/core/app/service/command.go +++ b/core/app/service/command.go @@ -2,8 +2,8 @@ package service import ( "github.com/1Panel-dev/1Panel/core/app/dto" - "github.com/1Panel-dev/1Panel/core/app/repo" "github.com/1Panel-dev/1Panel/core/constant" + "github.com/1Panel-dev/1Panel/core/global" "github.com/jinzhu/copier" "github.com/pkg/errors" ) @@ -66,15 +66,15 @@ func (u *CommandService) SearchForTree(req dto.OperateByType) ([]dto.CommandTree } func (u *CommandService) SearchWithPage(req dto.SearchCommandWithPage) (int64, interface{}, error) { - options := []repo.DBOption{ + options := []global.DBOption{ commonRepo.WithOrderRuleBy(req.OrderBy, req.Order), commonRepo.WithByType(req.Type), } if len(req.Info) != 0 { - options = append(options, commonRepo.WithLikeName(req.Info)) + options = append(options, commandRepo.WithByInfo(req.Info)) } if req.GroupID != 0 { - options = append(options, groupRepo.WithByGroupID(req.GroupID)) + options = append(options, commonRepo.WithByGroupID(req.GroupID)) } total, commands, err := commandRepo.Page(req.Page, req.PageSize, options...) if err != nil { diff --git a/core/app/service/entry.go b/core/app/service/entry.go index 6ab8eea8c..9c77249b5 100644 --- a/core/app/service/entry.go +++ b/core/app/service/entry.go @@ -3,14 +3,15 @@ package service import "github.com/1Panel-dev/1Panel/core/app/repo" var ( - hostRepo = repo.NewIHostRepo() - commandRepo = repo.NewICommandRepo() - commonRepo = repo.NewICommonRepo() - settingRepo = repo.NewISettingRepo() - backupRepo = repo.NewIBackupRepo() - logRepo = repo.NewILogRepo() - groupRepo = repo.NewIGroupRepo() - launcherRepo = repo.NewILauncherRepo() + hostRepo = repo.NewIHostRepo() + commandRepo = repo.NewICommandRepo() + commonRepo = repo.NewICommonRepo() + settingRepo = repo.NewISettingRepo() + backupRepo = repo.NewIBackupRepo() + logRepo = repo.NewILogRepo() + groupRepo = repo.NewIGroupRepo() + launcherRepo = repo.NewILauncherRepo() + upgradeLogRepo = repo.NewIUpgradeLogRepo() taskRepo = repo.NewITaskRepo() ) diff --git a/core/app/service/group.go b/core/app/service/group.go index 33e3a8b80..ed98d582d 100644 --- a/core/app/service/group.go +++ b/core/app/service/group.go @@ -7,9 +7,9 @@ import ( "github.com/1Panel-dev/1Panel/core/app/dto" "github.com/1Panel-dev/1Panel/core/app/model" - "github.com/1Panel-dev/1Panel/core/app/repo" "github.com/1Panel-dev/1Panel/core/buserr" "github.com/1Panel-dev/1Panel/core/constant" + "github.com/1Panel-dev/1Panel/core/global" httpUtils "github.com/1Panel-dev/1Panel/core/utils/http" "github.com/1Panel-dev/1Panel/core/utils/xpack" "github.com/jinzhu/copier" @@ -30,11 +30,13 @@ func NewIGroupService() IGroupService { } func (u *GroupService) List(req dto.OperateByType) ([]dto.GroupInfo, error) { - options := []repo.DBOption{ - commonRepo.WithByType(req.Type), + options := []global.DBOption{ commonRepo.WithOrderBy("is_default desc"), commonRepo.WithOrderBy("created_at desc"), } + if len(req.Type) != 0 { + options = append(options, commonRepo.WithByType(req.Type)) + } var ( groups []model.Group err error diff --git a/core/app/service/host.go b/core/app/service/host.go index 47acbc5c9..b28ad4226 100644 --- a/core/app/service/host.go +++ b/core/app/service/host.go @@ -6,8 +6,8 @@ import ( "github.com/1Panel-dev/1Panel/core/app/dto" "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/global" "github.com/1Panel-dev/1Panel/core/utils/encrypt" "github.com/1Panel-dev/1Panel/core/utils/ssh" "github.com/jinzhu/copier" @@ -151,12 +151,12 @@ func (u *HostService) GetHostInfo(id uint) (*model.Host, error) { } func (u *HostService) SearchWithPage(req dto.SearchHostWithPage) (int64, interface{}, error) { - var options []repo.DBOption + var options []global.DBOption if len(req.Info) != 0 { - options = append(options, commonRepo.WithLikeName(req.Info)) + options = append(options, hostRepo.WithByInfo(req.Info)) } if req.GroupID != 0 { - options = append(options, groupRepo.WithByGroupID(req.GroupID)) + options = append(options, commonRepo.WithByGroupID(req.GroupID)) } total, hosts, err := hostRepo.Page(req.Page, req.PageSize, options...) if err != nil { diff --git a/core/app/service/logs.go b/core/app/service/logs.go index 3b9dab6e5..2d8046417 100644 --- a/core/app/service/logs.go +++ b/core/app/service/logs.go @@ -74,12 +74,19 @@ func (u *LogService) ListSystemLogFile() ([]string, error) { } func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) { + options := []global.DBOption{ + commonRepo.WithOrderBy("created_at desc"), + } + if len(req.IP) != 0 { + options = append(options, logRepo.WithByIP(req.IP)) + } + if len(req.Status) != 0 { + options = append(options, commonRepo.WithByStatus(req.Status)) + } total, ops, err := logRepo.PageLoginLog( req.Page, req.PageSize, - logRepo.WithByIP(req.IP), - logRepo.WithByStatus(req.Status), - commonRepo.WithOrderBy("created_at desc"), + options..., ) var dtoOps []dto.LoginLog for _, op := range ops { @@ -97,13 +104,21 @@ func (u *LogService) CreateOperationLog(operation *model.OperationLog) error { } func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, interface{}, error) { + options := []global.DBOption{ + commonRepo.WithOrderBy("created_at desc"), + logRepo.WithByLikeOperation(req.Operation), + } + if len(req.Source) != 0 { + options = append(options, logRepo.WithBySource(req.Source)) + } + if len(req.Status) != 0 { + options = append(options, commonRepo.WithByStatus(req.Status)) + } + total, ops, err := logRepo.PageOperationLog( req.Page, req.PageSize, - logRepo.WithByGroup(req.Source), - logRepo.WithByLikeOperation(req.Operation), - logRepo.WithByStatus(req.Status), - commonRepo.WithOrderBy("created_at desc"), + options..., ) var dtoOps []dto.OperationLog for _, op := range ops { diff --git a/core/app/service/task.go b/core/app/service/task.go index 1b291cf80..d703c90b3 100644 --- a/core/app/service/task.go +++ b/core/app/service/task.go @@ -2,7 +2,7 @@ package service import ( "github.com/1Panel-dev/1Panel/core/app/dto" - "github.com/1Panel-dev/1Panel/core/app/repo" + "github.com/1Panel-dev/1Panel/core/global" ) type TaskLogService struct{} @@ -16,7 +16,7 @@ func NewITaskService() ITaskLogService { } func (u *TaskLogService) Page(req dto.SearchTaskLogReq) (int64, []dto.TaskDTO, error) { - opts := []repo.DBOption{ + opts := []global.DBOption{ commonRepo.WithOrderBy("created_at desc"), } if req.Status != "" { diff --git a/core/app/service/upgrade.go b/core/app/service/upgrade.go index 4f2344988..534f1b5e5 100644 --- a/core/app/service/upgrade.go +++ b/core/app/service/upgrade.go @@ -11,6 +11,7 @@ import ( "time" "github.com/1Panel-dev/1Panel/core/app/dto" + "github.com/1Panel-dev/1Panel/core/app/model" "github.com/1Panel-dev/1Panel/core/constant" "github.com/1Panel-dev/1Panel/core/global" "github.com/1Panel-dev/1Panel/core/utils/cmd" @@ -23,6 +24,7 @@ type UpgradeService struct{} type IUpgradeService interface { Upgrade(req dto.Upgrade) error + Rollback(req dto.OperateByID) error LoadNotes(req dto.Upgrade) (string, error) SearchUpgrade() (*dto.UpgradeInfo, error) } @@ -127,6 +129,9 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error { _ = settingRepo.Update("SystemStatus", "Free") return } + itemLog := model.UpgradeLog{NodeID: 0, OldVersion: global.CONF.System.Version, NewVersion: req.Version, BackupFile: originalDir} + _ = upgradeLogRepo.Create(&itemLog) + global.LOG.Info("backup original data successful, now start to upgrade!") if err := files.CopyItem(false, true, path.Join(tmpDir, "1panel*"), "/usr/local/bin"); err != nil { @@ -161,6 +166,15 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error { return nil } +func (u *UpgradeService) Rollback(req dto.OperateByID) error { + log, _ := upgradeLogRepo.Get(commonRepo.WithByID(req.ID)) + if log.ID == 0 { + return constant.ErrRecordNotFound + } + u.handleRollback(log.BackupFile, 3) + return nil +} + func (u *UpgradeService) handleBackup(originalDir string) error { if err := files.CopyItem(false, true, "/usr/local/bin/1panel*", originalDir); err != nil { return err diff --git a/core/global/global.go b/core/global/global.go index 6f9daa27b..9af38d7cc 100644 --- a/core/global/global.go +++ b/core/global/global.go @@ -26,3 +26,5 @@ var ( BackupAccountTokenEntryID cron.EntryID ) + +type DBOption func(*gorm.DB) *gorm.DB diff --git a/core/init/migration/migrations/init.go b/core/init/migration/migrations/init.go index 59ad67c62..e1e4f29c9 100644 --- a/core/init/migration/migrations/init.go +++ b/core/init/migration/migrations/init.go @@ -168,7 +168,7 @@ var InitHost = &gormigrate.Migration{ return err } host := model.Host{ - Name: "localhost", Addr: "127.0.0.1", User: "root", Port: 22, AuthMode: "password", GroupID: hostGroup.ID, + Name: "local", Addr: "127.0.0.1", User: "root", Port: 22, AuthMode: "password", GroupID: hostGroup.ID, } if err := tx.Create(&host).Error; err != nil { return err diff --git a/frontend/src/components/system-upgrade/index.vue b/frontend/src/components/system-upgrade/index.vue index 507bdbddd..30b788d25 100644 --- a/frontend/src/components/system-upgrade/index.vue +++ b/frontend/src/components/system-upgrade/index.vue @@ -32,69 +32,26 @@ ({{ $t('setting.upgradeCheck') }}) {{ $t('setting.upgrading') }} + + - - -
- - {{ $t('setting.versionHelper') }} -
  • {{ $t('setting.versionHelper1') }}
  • -
  • {{ $t('setting.versionHelper2') }}
  • -
    -
    -

    {{ $t('app.version') }}

    -
    - - - {{ upgradeInfo.newVersion }} - - - {{ upgradeInfo.latestVersion }} - - - {{ upgradeInfo.testVersion }} - - - -
    - -
    + + + diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 312a3834c..c98d446b5 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -985,6 +985,8 @@ const message = { fanSpeed: 'Fan Speed', }, terminal: { + local: 'Local', + localHelper: 'The `local` name is used only for system local identification', conn: 'connection', connLocalErr: 'Unable to automatically authenticate, please fill in the local server login information!', testConn: 'Test connection', diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts index 0d11cef18..3ac67f6ec 100644 --- a/frontend/src/lang/modules/tw.ts +++ b/frontend/src/lang/modules/tw.ts @@ -940,6 +940,8 @@ const message = { fanSpeed: '風扇轉速', }, terminal: { + local: '本機', + localHelper: 'local 名稱僅用於系統本機標識', conn: '連接', connLocalErr: '無法自動認證,請填寫本地服務器的登錄信息!', testConn: '連接測試', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 884750f98..0e81e0a00 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -941,6 +941,8 @@ const message = { fanSpeed: '风扇转速', }, terminal: { + local: '本机', + localHelper: 'local 名称仅用于系统本机标识', conn: '连接', connLocalErr: '无法自动认证,请填写本地服务器的登录信息!', testConn: '连接测试', @@ -1499,7 +1501,7 @@ const message = { rollbackHelper: '即将回滚本次恢复,回滚将替换所有本次恢复的文件,过程中可能需要重启 Docker 以及 1Panel 服务,是否继续?', - upgradeRecord: '升级记录', + upgradeRecord: '更新记录', upgrading: '正在升级中,请稍候...', upgradeHelper: '升级操作需要重启 1Panel 服务,是否继续?', noUpgrade: '当前已经是最新版本', diff --git a/frontend/src/views/terminal/host/operate/index.vue b/frontend/src/views/terminal/host/operate/index.vue index fbaca9db5..55c0562f8 100644 --- a/frontend/src/views/terminal/host/operate/index.vue +++ b/frontend/src/views/terminal/host/operate/index.vue @@ -2,10 +2,7 @@ - - {{ dialogData.rowData!.addr }} - - + @@ -53,11 +50,18 @@ - +
    + + +
    - - + + @@ -98,6 +102,7 @@ const drawerVisible = ref(false); const dialogData = ref({ title: '', }); +const itemName = ref(); const groupList = ref(); const acceptParams = (params: DialogProps): void => { @@ -119,7 +124,14 @@ const rules = reactive({ port: [Rules.requiredInput, Rules.port], user: [Rules.requiredInput], authMode: [Rules.requiredSelect], + name: [{ validator: checkName, trigger: 'blur' }], }); +function checkName(rule: any, value: any, callback: any) { + if (value === 'local') { + return callback(new Error(i18n.global.t('terminal.localHelper'))); + } + callback(); +} const loadGroups = async () => { const res = await GetGroupList('host'); diff --git a/frontend/src/views/terminal/terminal/host-create.vue b/frontend/src/views/terminal/terminal/host-create.vue index f8bee4a97..1ae96ec35 100644 --- a/frontend/src/views/terminal/terminal/host-create.vue +++ b/frontend/src/views/terminal/terminal/host-create.vue @@ -10,8 +10,7 @@ type="warning" /> - - {{ hostInfo.addr }} + @@ -43,8 +42,17 @@ - - + + +
    + + +
    +
    @@ -72,12 +80,15 @@ import { addHost, testByInfo } from '@/api/modules/terminal'; import i18n from '@/lang'; import { reactive, ref } from 'vue'; import { MsgError, MsgSuccess } from '@/utils/message'; +import { GetGroupList } from '@/api/modules/group'; const dialogVisible = ref(); const isOK = ref(false); type FormInstance = InstanceType; const hostRef = ref(); +const groupList = ref(); + let hostInfo = reactive({ id: 0, name: '', @@ -122,6 +133,7 @@ const acceptParams = (props: DialogProps) => { hostInfo.addr = '127.0.0.1'; hostInfo.user = 'root'; } + loadGroups(); dialogVisible.value = true; }; @@ -163,6 +175,17 @@ const submitAddHost = (formEl: FormInstance | undefined, ops: string) => { }); }; +const loadGroups = async () => { + const res = await GetGroupList('host'); + groupList.value = res.data; + for (const item of groupList.value) { + if (item.isDefault) { + hostInfo.groupID = item.id; + break; + } + } +}; + defineExpose({ acceptParams, });