2022-08-16 15:30:23 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2023-07-24 03:24:38 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-01-29 08:38:34 +00:00
|
|
|
"fmt"
|
2023-06-05 10:31:26 +00:00
|
|
|
"strings"
|
2022-09-09 09:17:02 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-12-18 15:00:24 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-01-29 08:38:34 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-02-10 08:10:40 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-05-04 06:14:38 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2022-08-16 15:30:23 +00:00
|
|
|
|
|
|
|
"github.com/go-gormigrate/gormigrate/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var AddTableOperationLog = &gormigrate.Migration{
|
|
|
|
ID: "20200809-add-table-operation-log",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2022-11-15 09:20:57 +00:00
|
|
|
return tx.AutoMigrate(&model.OperationLog{}, &model.LoginLog{})
|
2022-08-16 15:30:23 +00:00
|
|
|
},
|
|
|
|
}
|
2022-08-18 10:54:21 +00:00
|
|
|
|
|
|
|
var AddTableHost = &gormigrate.Migration{
|
|
|
|
ID: "20200818-add-table-host",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2022-08-31 15:16:10 +00:00
|
|
|
if err := tx.AutoMigrate(&model.Host{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.AutoMigrate(&model.Group{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.AutoMigrate(&model.Command{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-01 08:48:43 +00:00
|
|
|
group := model.Group{
|
2023-03-07 02:19:12 +00:00
|
|
|
Name: "default", Type: "host", IsDefault: true,
|
2022-09-01 08:48:43 +00:00
|
|
|
}
|
|
|
|
if err := tx.Create(&group).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-03 06:37:17 +00:00
|
|
|
host := model.Host{
|
2023-03-11 09:51:42 +00:00
|
|
|
Name: "localhost", Addr: "127.0.0.1", User: "root", Port: 22, AuthMode: "password", GroupID: group.ID,
|
2023-01-03 06:37:17 +00:00
|
|
|
}
|
|
|
|
if err := tx.Create(&host).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 15:16:10 +00:00
|
|
|
return nil
|
2022-08-18 10:54:21 +00:00
|
|
|
},
|
|
|
|
}
|
2022-09-08 03:39:14 +00:00
|
|
|
|
2022-09-15 02:44:43 +00:00
|
|
|
var AddTableMonitor = &gormigrate.Migration{
|
2022-09-08 03:39:14 +00:00
|
|
|
ID: "20200905-add-table-monitor",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(&model.MonitorBase{}, &model.MonitorIO{}, &model.MonitorNetwork{})
|
|
|
|
},
|
|
|
|
}
|
2022-09-15 02:44:43 +00:00
|
|
|
|
|
|
|
var AddTableSetting = &gormigrate.Migration{
|
|
|
|
ID: "20200908-add-table-setting",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Setting{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-04 06:14:38 +00:00
|
|
|
encryptKey := common.RandStr(16)
|
2023-04-27 14:44:16 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "UserName", Value: global.CONF.System.Username}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-04 06:14:38 +00:00
|
|
|
global.CONF.System.EncryptKey = encryptKey
|
|
|
|
pass, _ := encrypt.StringEncrypt(global.CONF.System.Password)
|
|
|
|
if err := tx.Create(&model.Setting{Key: "Password", Value: pass}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-21 07:20:04 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "Email", Value: ""}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "PanelName", Value: "1Panel"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 10:45:03 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "Language", Value: "zh"}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-16 10:27:22 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "Theme", Value: "light"}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SessionTimeout", Value: "86400"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-09 09:17:02 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "LocalTime", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-15 02:44:43 +00:00
|
|
|
|
2023-03-10 05:47:31 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ServerPort", Value: global.CONF.System.Port}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-04-27 14:44:16 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SecurityEntrance", Value: global.CONF.System.Entrance}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-10 08:10:40 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "JWTSigningKey", Value: common.RandStr(16)}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-04 06:14:38 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: encryptKey}).Error; err != nil {
|
2023-02-13 03:04:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-10 08:10:40 +00:00
|
|
|
|
2022-12-02 02:23:35 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ExpirationTime", Value: time.Now().AddDate(0, 0, 10).Format("2006-01-02 15:04:05")}).Error; err != nil {
|
2022-09-29 08:15:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-23 11:23:41 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ExpirationDays", Value: "0"}).Error; err != nil {
|
2022-09-09 09:17:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-01 06:05:55 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ComplexityVerification", Value: "enable"}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MFAStatus", Value: "disable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-14 15:27:17 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "MFASecret", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-15 02:44:43 +00:00
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorStatus", Value: "enable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-31 14:06:37 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorStoreDays", Value: "7"}).Error; err != nil {
|
2022-09-15 02:44:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MessageType", Value: "none"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "EmailVars", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "WeChatVars", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "DingVars", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-14 09:14:57 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemVersion", Value: global.CONF.System.Version}).Error; err != nil {
|
2023-01-09 14:55:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-02 07:01:37 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemStatus", Value: "Free"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-17 09:19:35 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "AppStoreVersion", Value: ""}).Error; err != nil {
|
2023-02-10 10:07:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-15 02:44:43 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-09-16 10:53:45 +00:00
|
|
|
|
|
|
|
var AddTableBackupAccount = &gormigrate.Migration{
|
|
|
|
ID: "20200916-add-table-backup",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2022-10-27 15:09:39 +00:00
|
|
|
if err := tx.AutoMigrate(&model.BackupAccount{}, &model.BackupRecord{}); err != nil {
|
2022-09-16 10:53:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-29 08:38:34 +00:00
|
|
|
|
2022-09-16 10:53:45 +00:00
|
|
|
item := &model.BackupAccount{
|
2022-09-19 11:42:06 +00:00
|
|
|
Type: "LOCAL",
|
2023-01-29 08:38:34 +00:00
|
|
|
Vars: fmt.Sprintf("{\"dir\":\"%s\"}", global.CONF.System.Backup),
|
2022-09-16 10:53:45 +00:00
|
|
|
}
|
|
|
|
if err := tx.Create(item).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-09-20 11:12:48 +00:00
|
|
|
|
|
|
|
var AddTableCronjob = &gormigrate.Migration{
|
|
|
|
ID: "20200921-add-table-cronjob",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2022-09-23 09:21:27 +00:00
|
|
|
return tx.AutoMigrate(&model.Cronjob{}, &model.JobRecords{})
|
2022-09-20 11:12:48 +00:00
|
|
|
},
|
|
|
|
}
|
2022-09-22 08:16:04 +00:00
|
|
|
|
|
|
|
var AddTableApp = &gormigrate.Migration{
|
|
|
|
ID: "20200921-add-table-app",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-03-02 05:54:07 +00:00
|
|
|
return tx.AutoMigrate(&model.App{}, &model.AppDetail{}, &model.Tag{}, &model.AppTag{}, &model.AppInstall{}, &model.AppInstallResource{})
|
2022-09-22 08:16:04 +00:00
|
|
|
},
|
|
|
|
}
|
2022-10-09 08:17:15 +00:00
|
|
|
|
|
|
|
var AddTableImageRepo = &gormigrate.Migration{
|
|
|
|
ID: "20201009-add-table-imagerepo",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2022-12-06 07:06:42 +00:00
|
|
|
if err := tx.AutoMigrate(&model.ImageRepo{}, &model.ComposeTemplate{}, &model.Compose{}); err != nil {
|
2022-10-09 08:17:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
item := &model.ImageRepo{
|
|
|
|
Name: "Docker Hub",
|
2023-02-01 08:46:13 +00:00
|
|
|
Protocol: "https",
|
2022-10-09 08:17:15 +00:00
|
|
|
DownloadUrl: "docker.io",
|
2022-12-18 15:00:24 +00:00
|
|
|
Status: constant.StatusSuccess,
|
2022-10-09 08:17:15 +00:00
|
|
|
}
|
|
|
|
if err := tx.Create(item).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-10-20 10:45:47 +00:00
|
|
|
|
|
|
|
var AddTableDatabaseMysql = &gormigrate.Migration{
|
|
|
|
ID: "20201020-add-table-database_mysql",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(&model.DatabaseMysql{})
|
|
|
|
},
|
|
|
|
}
|
2022-10-28 09:04:57 +00:00
|
|
|
|
|
|
|
var AddTableWebsite = &gormigrate.Migration{
|
|
|
|
ID: "20201009-add-table-website",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-03-22 13:42:30 +00:00
|
|
|
if err := tx.AutoMigrate(&model.Website{}, &model.WebsiteDomain{}, &model.WebsiteDnsAccount{}, &model.WebsiteSSL{}, &model.WebsiteAcmeAccount{}); err != nil {
|
2022-10-28 09:04:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-01-06 10:53:25 +00:00
|
|
|
|
2023-01-09 14:55:10 +00:00
|
|
|
var AddTableSnap = &gormigrate.Migration{
|
|
|
|
ID: "20230106-add-table-snap",
|
2023-01-06 10:53:25 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Snapshot{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-03-22 13:42:30 +00:00
|
|
|
|
|
|
|
var AddDefaultGroup = &gormigrate.Migration{
|
|
|
|
ID: "2023022-change-default-group",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
defaultGroup := &model.Group{
|
|
|
|
Name: "默认",
|
|
|
|
IsDefault: true,
|
|
|
|
Type: "website",
|
|
|
|
}
|
2023-03-23 08:26:33 +00:00
|
|
|
if err := tx.Create(defaultGroup).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Group{}).Where("name = ? AND type = ?", "default", "host").Update("name", "默认").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Website{}).Where("1 = 1").Update("website_group_id", defaultGroup.ID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-22 13:42:30 +00:00
|
|
|
return tx.Migrator().DropTable("website_groups")
|
|
|
|
},
|
|
|
|
}
|
2023-03-29 06:58:28 +00:00
|
|
|
|
|
|
|
var AddTableRuntime = &gormigrate.Migration{
|
2023-04-06 08:38:16 +00:00
|
|
|
ID: "20230406-add-table-runtime",
|
2023-03-29 06:58:28 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-04-12 13:52:30 +00:00
|
|
|
return tx.AutoMigrate(&model.Runtime{})
|
2023-03-29 06:58:28 +00:00
|
|
|
},
|
|
|
|
}
|
2023-04-08 06:02:14 +00:00
|
|
|
|
|
|
|
var UpdateTableApp = &gormigrate.Migration{
|
|
|
|
ID: "20230408-update-table-app",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.App{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-04-10 13:50:24 +00:00
|
|
|
|
|
|
|
var UpdateTableHost = &gormigrate.Migration{
|
|
|
|
ID: "20230410-update-table-host",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Host{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-04-12 13:52:30 +00:00
|
|
|
|
|
|
|
var UpdateTableWebsite = &gormigrate.Migration{
|
2023-04-18 10:46:58 +00:00
|
|
|
ID: "20230418-update-table-website",
|
2023-04-12 13:52:30 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-04-17 08:54:34 +00:00
|
|
|
if err := tx.AutoMigrate(&model.Website{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Website{}).Where("1 = 1").Update("site_dir", "/").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-04-12 13:52:30 +00:00
|
|
|
},
|
|
|
|
}
|
2023-04-14 10:54:34 +00:00
|
|
|
|
2023-04-25 06:34:16 +00:00
|
|
|
var AddEntranceAndSSL = &gormigrate.Migration{
|
|
|
|
ID: "20230414-add-entrance-and-ssl",
|
2023-04-14 10:54:34 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-04-27 14:44:16 +00:00
|
|
|
if err := tx.Model(&model.Setting{}).
|
|
|
|
Where("key = ? AND value = ?", "SecurityEntrance", "onepanel").
|
|
|
|
Updates(map[string]interface{}{"value": ""}).Error; err != nil {
|
2023-04-14 10:54:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-04-25 06:34:16 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SSLType", Value: "self"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SSLID", Value: "0"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SSL", Value: "disable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-14 10:54:34 +00:00
|
|
|
return tx.AutoMigrate(&model.Website{})
|
|
|
|
},
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
|
|
|
|
var UpdateTableSetting = &gormigrate.Migration{
|
2023-05-16 09:31:47 +00:00
|
|
|
ID: "20200516-update-table-setting",
|
2023-05-15 14:40:05 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-05-16 09:31:47 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "AppStoreLastModified", Value: "0"}).Error; err != nil {
|
2023-05-15 14:40:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var UpdateTableAppDetail = &gormigrate.Migration{
|
2023-05-16 09:31:47 +00:00
|
|
|
ID: "20200517-update-table-app-detail",
|
2023-05-15 14:40:05 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-05-16 09:31:47 +00:00
|
|
|
if err := tx.AutoMigrate(&model.App{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
if err := tx.AutoMigrate(&model.AppDetail{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-05-19 13:47:46 +00:00
|
|
|
|
|
|
|
var AddBindAndAllowIPs = &gormigrate.Migration{
|
2023-05-24 10:36:41 +00:00
|
|
|
ID: "20230517-add-bind-and-allow",
|
2023-05-19 13:47:46 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(&model.Setting{Key: "BindDomain", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "AllowIPs", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-31 06:03:04 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "TimeZone", Value: common.LoadTimeZoneByCmd()}).Error; err != nil {
|
2023-05-22 09:45:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-24 07:43:21 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "NtpSite", Value: "pool.ntp.org"}).Error; err != nil {
|
2023-05-22 09:45:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-07-31 14:06:37 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorInterval", Value: "5"}).Error; err != nil {
|
2023-05-25 10:02:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-19 13:47:46 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 10:36:41 +00:00
|
|
|
|
|
|
|
var UpdateCronjobWithSecond = &gormigrate.Migration{
|
|
|
|
ID: "20200524-update-table-cronjob",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Cronjob{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-05 10:31:26 +00:00
|
|
|
var jobs []model.Cronjob
|
|
|
|
if err := tx.Where("exclusion_rules != ?", "").Find(&jobs).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, job := range jobs {
|
|
|
|
if strings.Contains(job.ExclusionRules, ";") {
|
|
|
|
newRules := strings.ReplaceAll(job.ExclusionRules, ";", ",")
|
|
|
|
if err := tx.Model(&model.Cronjob{}).Where("id = ?", job.ID).Update("exclusion_rules", newRules).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 10:36:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-05-30 06:22:57 +00:00
|
|
|
|
|
|
|
var UpdateWebsite = &gormigrate.Migration{
|
|
|
|
ID: "20200530-update-table-website",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Website{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-06-23 15:06:13 +00:00
|
|
|
|
|
|
|
var AddBackupAccountDir = &gormigrate.Migration{
|
|
|
|
ID: "20200620-add-backup-dir",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-06-28 06:30:11 +00:00
|
|
|
if err := tx.AutoMigrate(&model.BackupAccount{}, &model.Cronjob{}); err != nil {
|
2023-06-23 15:06:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-06-25 09:52:13 +00:00
|
|
|
|
|
|
|
var AddMfaInterval = &gormigrate.Migration{
|
|
|
|
ID: "20230625-add-mfa-interval",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MFAInterval", Value: "30"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-29 03:18:15 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-04 08:16:12 +00:00
|
|
|
if err := tx.Create(&model.Setting{Key: "OneDriveID", Value: "MDEwOTM1YTktMWFhOS00ODU0LWExZGMtNmU0NWZlNjI4YzZi"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "OneDriveSc", Value: "akpuOFF+YkNXOU1OLWRzS1ZSRDdOcG1LT2ZRM0RLNmdvS1RkVWNGRA=="}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-25 09:52:13 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-07-03 08:36:18 +00:00
|
|
|
|
|
|
|
var UpdateAppDetail = &gormigrate.Migration{
|
|
|
|
ID: "20230704-update-app-detail",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.AppDetail{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.AppDetail{}).Where("1 = 1").Update("ignore_upgrade", "0").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-07-03 08:48:14 +00:00
|
|
|
|
|
|
|
var EncryptHostPassword = &gormigrate.Migration{
|
|
|
|
ID: "20230703-encrypt-host-password",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var hosts []model.Host
|
2023-07-12 13:11:20 +00:00
|
|
|
if err := tx.Where("1 = 1").Find(&hosts).Error; err != nil {
|
2023-07-03 08:48:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 08:10:19 +00:00
|
|
|
var encryptSetting model.Setting
|
|
|
|
if err := tx.Where("key = ?", "EncryptKey").Find(&encryptSetting).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.CONF.System.EncryptKey = encryptSetting.Value
|
|
|
|
|
2023-07-03 08:48:14 +00:00
|
|
|
for _, host := range hosts {
|
|
|
|
if len(host.Password) != 0 {
|
|
|
|
pass, err := encrypt.StringEncrypt(host.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Host{}).Where("id = ?", host.ID).Update("password", pass).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(host.PrivateKey) != 0 {
|
|
|
|
key, err := encrypt.StringEncrypt(host.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-12 13:11:20 +00:00
|
|
|
if err := tx.Model(&model.Host{}).Where("id = ?", host.ID).Update("private_key", key).Error; err != nil {
|
2023-07-03 08:48:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 06:02:12 +00:00
|
|
|
if len(host.PassPhrase) != 0 {
|
|
|
|
pass, err := encrypt.StringEncrypt(host.PassPhrase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-12 13:11:20 +00:00
|
|
|
if err := tx.Model(&model.Host{}).Where("id = ?", host.ID).Update("pass_phrase", pass).Error; err != nil {
|
2023-07-05 06:02:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 08:48:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-07-19 08:25:41 +00:00
|
|
|
|
|
|
|
var AddRemoteDB = &gormigrate.Migration{
|
2023-07-24 03:24:38 +00:00
|
|
|
ID: "20230724-add-remote-db",
|
2023-07-19 08:25:41 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-08-29 02:50:15 +00:00
|
|
|
if err := tx.AutoMigrate(&model.DatabaseMysql{}); err != nil {
|
2023-07-24 03:24:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
)
|
|
|
|
if err := global.DB.Where("key = ?", "mysql").First(&app).Error; err != nil {
|
2023-08-02 08:47:30 +00:00
|
|
|
return nil
|
2023-07-24 03:24:38 +00:00
|
|
|
}
|
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("error password in app env")
|
|
|
|
}
|
2023-08-29 02:50:15 +00:00
|
|
|
if err := tx.Create(&model.Database{
|
2023-07-24 03:24:38 +00:00
|
|
|
Name: "local",
|
|
|
|
Type: "mysql",
|
|
|
|
Version: appInstall.Version,
|
|
|
|
From: "local",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Username: "root",
|
|
|
|
Password: password,
|
|
|
|
}).Error; err != nil {
|
2023-07-19 08:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-08-03 10:36:01 +00:00
|
|
|
|
|
|
|
var UpdateRedisParam = &gormigrate.Migration{
|
|
|
|
ID: "20230804-update-redis-param",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
)
|
|
|
|
if err := global.DB.Where("key = ?", "redis").First(&app).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
appInstall.Param = strings.ReplaceAll(appInstall.Param, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
|
|
|
appInstall.DockerCompose = strings.ReplaceAll(appInstall.DockerCompose, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
|
|
|
appInstall.Env = strings.ReplaceAll(appInstall.Env, "PANEL_DB_ROOT_PASSWORD", "PANEL_REDIS_ROOT_PASSWORD")
|
|
|
|
if err := tx.Model(&model.AppInstall{}).Where("id = ?", appInstall.ID).Updates(appInstall).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
2023-08-09 06:14:12 +00:00
|
|
|
|
|
|
|
var UpdateCronjobWithDb = &gormigrate.Migration{
|
|
|
|
ID: "20230809-update-cronjob-with-db",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var cronjobs []model.Cronjob
|
|
|
|
if err := global.DB.Where("type = ? AND db_name != ?", "database", "all").Find(&cronjobs).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, job := range cronjobs {
|
|
|
|
var db model.DatabaseMysql
|
|
|
|
if err := global.DB.Where("name = ?", job.DBName).First(&db).Error; err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Cronjob{}).
|
|
|
|
Where("id = ?", job.ID).
|
|
|
|
Updates(map[string]interface{}{"db_name": db.ID}).Error; err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-08-15 14:48:11 +00:00
|
|
|
|
|
|
|
var AddTableFirewall = &gormigrate.Migration{
|
2023-08-30 06:56:11 +00:00
|
|
|
ID: "20230828-add-table-firewall",
|
2023-08-15 14:48:11 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-08-23 14:44:14 +00:00
|
|
|
if err := tx.AutoMigrate(&model.Firewall{}, model.SnapshotStatus{}, &model.Cronjob{}); err != nil {
|
2023-08-15 14:48:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-08-30 06:56:11 +00:00
|
|
|
_ = tx.Exec("alter table remote_dbs rename to databases;").Error
|
2023-08-29 02:50:15 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:36:16 +00:00
|
|
|
var AddMariaDB = &gormigrate.Migration{
|
|
|
|
ID: "20230828-add-mariadb",
|
2023-08-29 02:50:15 +00:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
)
|
|
|
|
if err := tx.AutoMigrate(&model.Database{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("key = ?", "mariadb").First(&app).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("error password in app env")
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Database{
|
|
|
|
AppInstallID: appInstall.ID,
|
|
|
|
Name: appInstall.Name,
|
|
|
|
Type: "mariadb",
|
|
|
|
Version: appInstall.Version,
|
|
|
|
From: "local",
|
|
|
|
Address: appInstall.ServiceName,
|
|
|
|
Port: uint(appInstall.HttpPort),
|
|
|
|
Username: "root",
|
|
|
|
Password: password,
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-29 06:36:16 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-08-29 02:50:15 +00:00
|
|
|
|
2023-08-29 06:36:16 +00:00
|
|
|
var UpdateDatabase = &gormigrate.Migration{
|
|
|
|
ID: "20230829-update-database",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-08-29 02:50:15 +00:00
|
|
|
if err := global.DB.Model(&model.DatabaseMysql{}).Where("`from` != ?", "local").Updates(map[string]interface{}{
|
|
|
|
"from": "remote",
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
appMysql model.App
|
|
|
|
appInstallMysql model.AppInstall
|
|
|
|
localDatabase model.Database
|
|
|
|
)
|
|
|
|
_ = global.DB.Where("name = ? AND address = ?", "local", "127.0.0.1").First(&localDatabase).Error
|
|
|
|
if localDatabase.ID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("key = ?", "mysql").First(&appMysql).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("app_id = ?", appMysql.ID).First(&appInstallMysql).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := global.DB.Model(&model.Database{}).Where("id = ?", localDatabase.ID).Updates(map[string]interface{}{
|
|
|
|
"app_install_id": appInstallMysql.ID,
|
|
|
|
"name": appInstallMysql.Name,
|
|
|
|
"address": appInstallMysql.ServiceName,
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-15 14:48:11 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-08-31 07:14:11 +00:00
|
|
|
|
|
|
|
var UpdateAppInstallResource = &gormigrate.Migration{
|
|
|
|
ID: "20230831-update-app_install_resource",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.AppInstallResource{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := global.DB.Model(&model.AppInstallResource{}).Where("1 = 1").Updates(map[string]interface{}{
|
|
|
|
"from": "local",
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|