2023-11-14 18:44:09 +08:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2024-01-19 16:48:41 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2023-11-29 22:04:08 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
2023-11-14 18:44:09 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-12-18 17:54:15 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2023-11-29 22:04:08 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/service"
|
2024-01-19 16:48:41 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2024-01-08 17:40:42 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2024-01-19 16:48:41 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cloud_storage/client"
|
2023-11-14 18:44:09 +08:00
|
|
|
"github.com/go-gormigrate/gormigrate/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var UpdateAcmeAccount = &gormigrate.Migration{
|
2023-11-16 14:38:07 +08:00
|
|
|
ID: "20231117-update-acme-account",
|
2023-11-14 18:44:09 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.WebsiteAcmeAccount{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-11-20 10:44:10 +08:00
|
|
|
|
2023-11-27 14:00:09 +08:00
|
|
|
var AddWebsiteCA = &gormigrate.Migration{
|
2023-12-01 16:54:09 +08:00
|
|
|
ID: "20231125-add-website-ca",
|
2023-11-20 10:44:10 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-11-27 14:00:09 +08:00
|
|
|
if err := tx.AutoMigrate(&model.WebsiteCA{}); err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-11-27 12:04:09 +08:00
|
|
|
|
2023-11-27 14:00:09 +08:00
|
|
|
var UpdateWebsiteSSL = &gormigrate.Migration{
|
2023-11-28 15:42:10 +08:00
|
|
|
ID: "20231128-update-website-ssl",
|
2023-11-27 12:04:09 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2023-11-27 14:00:09 +08:00
|
|
|
if err := tx.AutoMigrate(&model.WebsiteSSL{}); err != nil {
|
2023-11-27 12:04:09 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-11-28 15:00:10 +08:00
|
|
|
|
|
|
|
var AddDockerSockPath = &gormigrate.Migration{
|
|
|
|
ID: "20231128-add-docker-sock-path",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(&model.Setting{Key: "DockerSockPath", Value: "unix:///var/run/docker.sock"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-11-29 10:46:07 +08:00
|
|
|
|
|
|
|
var AddDatabaseSSL = &gormigrate.Migration{
|
|
|
|
ID: "20231126-add-database-ssl",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.Database{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-11-29 22:04:08 +08:00
|
|
|
|
|
|
|
var AddDefaultCA = &gormigrate.Migration{
|
2023-12-01 16:54:09 +08:00
|
|
|
ID: "20231129-add-default-ca",
|
2023-11-29 22:04:08 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
caService := service.NewIWebsiteCAService()
|
|
|
|
if _, err := caService.Create(request.WebsiteCACreate{
|
|
|
|
CommonName: "1Panel-CA",
|
|
|
|
Country: "CN",
|
|
|
|
KeyType: "P256",
|
|
|
|
Name: "1Panel",
|
|
|
|
Organization: "FIT2CLOUD",
|
|
|
|
OrganizationUint: "1Panel",
|
|
|
|
Province: "Beijing",
|
|
|
|
City: "Beijing",
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-12-04 14:56:09 +08:00
|
|
|
|
|
|
|
var AddSettingRecycleBin = &gormigrate.Migration{
|
|
|
|
ID: "20231129-add-setting-recycle-bin",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(&model.Setting{Key: "FileRecycleBin", Value: "enable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-12-18 17:54:15 +08:00
|
|
|
|
|
|
|
var UpdateWebsiteBackupRecord = &gormigrate.Migration{
|
|
|
|
ID: "20231218-update-backup-record-for-website",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
backupRepo := repo.NewIBackupRepo()
|
|
|
|
websitesBackups, _ := backupRepo.ListRecord(repo.NewCommonRepo().WithByType("website"))
|
|
|
|
if len(websitesBackups) > 0 {
|
|
|
|
for _, backup := range websitesBackups {
|
|
|
|
backup.DetailName = backup.Name
|
|
|
|
_ = backupRepo.UpdateRecord(&backup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-02 21:54:28 +08:00
|
|
|
|
|
|
|
var AddTablePHPExtensions = &gormigrate.Migration{
|
|
|
|
ID: "20240102-add-php-extensions",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.PHPExtensions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "默认", Extensions: "bcmath,gd,gettext,intl,pcntl,shmop,soap,sockets,sysvsem,xmlrpc,zip"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-07 22:52:41 +08:00
|
|
|
|
|
|
|
var AddTableDatabasePostgresql = &gormigrate.Migration{
|
|
|
|
ID: "20231225-add-table-database_postgresql",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.DatabasePostgresql{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.AutoMigrate(&model.Cronjob{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var jobs []model.Cronjob
|
|
|
|
if err := tx.Where("type == ?", "database").Find(&jobs).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, job := range jobs {
|
2024-01-11 11:01:40 +08:00
|
|
|
if job.DBName == "all" {
|
|
|
|
if err := tx.Model(&model.Cronjob{}).Where("id = ?", job.ID).Update("db_type", "mysql").Error; err != nil {
|
|
|
|
global.LOG.Errorf("update db type of cronjob %s failed, err: %v", job.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 22:52:41 +08:00
|
|
|
var db model.DatabaseMysql
|
|
|
|
if err := tx.Where("id == ?", job.DBName).First(&db).Error; err != nil {
|
2024-01-08 17:40:42 +08:00
|
|
|
continue
|
2024-01-07 22:52:41 +08:00
|
|
|
}
|
|
|
|
var database model.Database
|
|
|
|
if err := tx.Where("name == ?", db.MysqlName).First(&database).Error; err != nil {
|
2024-01-08 17:40:42 +08:00
|
|
|
continue
|
2024-01-07 22:52:41 +08:00
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Cronjob{}).Where("id = ?", job.ID).Update("db_type", database.Type).Error; err != nil {
|
2024-01-08 17:40:42 +08:00
|
|
|
global.LOG.Errorf("update db type of cronjob %s failed, err: %v", job.Name, err)
|
|
|
|
continue
|
2024-01-07 22:52:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-09 12:43:33 +08:00
|
|
|
|
|
|
|
var AddPostgresqlSuperUser = &gormigrate.Migration{
|
|
|
|
ID: "20231225-add-postgresql-super_user",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.AutoMigrate(&model.DatabasePostgresql{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-11 11:01:40 +08:00
|
|
|
|
|
|
|
var UpdateCronjobWithWebsite = &gormigrate.Migration{
|
|
|
|
ID: "20230809-update-cronjob-with-website",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var cronjobs []model.Cronjob
|
|
|
|
if err := tx.Where("(type = ? OR type = ?) AND website != ?", "website", "cutWebsiteLog", "all").Find(&cronjobs).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, job := range cronjobs {
|
|
|
|
var web model.Website
|
|
|
|
if err := tx.Where("primary_domain = ?", job.Website).First(&web).Error; err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tx.Model(&model.Cronjob{}).
|
|
|
|
Where("id = ?", job.ID).
|
|
|
|
Updates(map[string]interface{}{"website": web.ID}).Error; err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-01-19 16:48:41 +08:00
|
|
|
|
|
|
|
var UpdateOneDriveToken = &gormigrate.Migration{
|
|
|
|
ID: "20240117-update-onedrive-token",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
var (
|
|
|
|
backup model.BackupAccount
|
|
|
|
clientSetting model.Setting
|
|
|
|
secretSetting model.Setting
|
|
|
|
)
|
|
|
|
_ = tx.Where("type = ?", "OneDrive").First(&backup).Error
|
|
|
|
if backup.ID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(backup.Credential) == 0 {
|
|
|
|
global.LOG.Error("OneDrive configuration lacks token information, please rebind.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = tx.Where("key = ?", "OneDriveID").First(&clientSetting).Error
|
|
|
|
if clientSetting.ID == 0 {
|
|
|
|
global.LOG.Error("system configuration lacks clientID information, please retry.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_ = tx.Where("key = ?", "OneDriveSc").First(&secretSetting).Error
|
|
|
|
if secretSetting.ID == 0 {
|
|
|
|
global.LOG.Error("system configuration lacks clientID information, please retry.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
idItem, _ := base64.StdEncoding.DecodeString(clientSetting.Value)
|
|
|
|
global.CONF.System.OneDriveID = string(idItem)
|
|
|
|
scItem, _ := base64.StdEncoding.DecodeString(secretSetting.Value)
|
|
|
|
global.CONF.System.OneDriveSc = string(scItem)
|
|
|
|
|
|
|
|
varMap := make(map[string]interface{})
|
2024-01-19 23:06:41 +08:00
|
|
|
varMap["isCN"] = false
|
|
|
|
varMap["client_id"] = global.CONF.System.OneDriveID
|
|
|
|
varMap["client_secret"] = global.CONF.System.OneDriveSc
|
|
|
|
varMap["redirect_uri"] = constant.OneDriveRedirectURI
|
|
|
|
varMap["refresh_token"] = backup.Credential
|
|
|
|
token, refreshToken, err := client.RefreshToken("refresh_token", varMap)
|
2024-01-19 16:48:41 +08:00
|
|
|
varMap["refresh_status"] = constant.StatusSuccess
|
|
|
|
varMap["refresh_time"] = time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
if err != nil {
|
|
|
|
varMap["refresh_msg"] = err.Error()
|
|
|
|
varMap["refresh_status"] = constant.StatusFailed
|
|
|
|
}
|
|
|
|
varMap["refresh_token"] = refreshToken
|
|
|
|
itemVars, _ := json.Marshal(varMap)
|
|
|
|
if err := tx.Model(&model.BackupAccount{}).
|
|
|
|
Where("id = ?", backup.ID).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"credential": token,
|
|
|
|
"vars": string(itemVars),
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|