2024-07-23 14:48:37 +08:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2024-08-02 16:49:43 +08:00
|
|
|
"encoding/json"
|
2024-07-31 18:29:41 +08:00
|
|
|
"os"
|
2024-07-23 14:48:37 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/service"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/common"
|
2024-07-31 18:29:41 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
|
2024-07-23 14:48:37 +08:00
|
|
|
|
|
|
|
"github.com/go-gormigrate/gormigrate/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var AddTable = &gormigrate.Migration{
|
|
|
|
ID: "20240722-add-table",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(
|
|
|
|
&model.AppDetail{},
|
|
|
|
&model.AppInstallResource{},
|
|
|
|
&model.AppInstall{},
|
|
|
|
&model.AppTag{},
|
|
|
|
&model.Tag{},
|
|
|
|
&model.App{},
|
|
|
|
&model.BackupRecord{},
|
|
|
|
&model.Clam{},
|
|
|
|
&model.Command{},
|
|
|
|
&model.ComposeTemplate{},
|
|
|
|
&model.Compose{},
|
|
|
|
&model.Cronjob{},
|
|
|
|
&model.Database{},
|
|
|
|
&model.DatabaseMysql{},
|
|
|
|
&model.DatabasePostgresql{},
|
|
|
|
&model.Favorite{},
|
|
|
|
&model.Forward{},
|
|
|
|
&model.Firewall{},
|
|
|
|
&model.Ftp{},
|
|
|
|
&model.Group{},
|
|
|
|
&model.Host{},
|
|
|
|
&model.ImageRepo{},
|
|
|
|
&model.JobRecords{},
|
|
|
|
&model.MonitorBase{},
|
|
|
|
&model.MonitorIO{},
|
|
|
|
&model.MonitorNetwork{},
|
|
|
|
&model.PHPExtensions{},
|
|
|
|
&model.RedisCommand{},
|
|
|
|
&model.Runtime{},
|
|
|
|
&model.Setting{},
|
|
|
|
&model.Snapshot{},
|
|
|
|
&model.SnapshotStatus{},
|
|
|
|
&model.Tag{},
|
|
|
|
&model.Website{},
|
|
|
|
&model.WebsiteAcmeAccount{},
|
|
|
|
&model.WebsiteCA{},
|
|
|
|
&model.WebsiteDnsAccount{},
|
|
|
|
&model.WebsiteDomain{},
|
|
|
|
&model.WebsiteSSL{},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitHost = &gormigrate.Migration{
|
|
|
|
ID: "20240722-init-host",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
group := model.Group{
|
|
|
|
Name: "default", Type: "host", IsDefault: true,
|
|
|
|
}
|
|
|
|
if err := tx.Create(&group).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
host := model.Host{
|
|
|
|
Name: "localhost", Addr: "127.0.0.1", User: "root", Port: 22, AuthMode: "password", GroupID: group.ID,
|
|
|
|
}
|
|
|
|
if err := tx.Create(&host).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitSetting = &gormigrate.Migration{
|
|
|
|
ID: "20240722-init-setting",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
2024-07-25 18:13:57 +08:00
|
|
|
encryptKey := common.RandStr(16)
|
|
|
|
global.CONF.System.EncryptKey = encryptKey
|
|
|
|
if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: encryptKey}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-02 17:29:04 +08:00
|
|
|
if _, err := os.Stat("/opt/1panel/nodeJson"); err == nil {
|
|
|
|
type nodeInfo struct {
|
2024-08-13 15:33:34 +08:00
|
|
|
MasterRequestAddr string `json:"masterRequestAddr"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
ServerCrt string `json:"serverCrt"`
|
|
|
|
ServerKey string `json:"serverKey"`
|
|
|
|
CurrentNode string `json:"currentNode"`
|
2024-08-02 17:29:04 +08:00
|
|
|
}
|
|
|
|
nodeJson, err := os.ReadFile("/opt/1panel/nodeJson")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var node nodeInfo
|
|
|
|
if err := json.Unmarshal(nodeJson, &node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-13 15:33:34 +08:00
|
|
|
itemKey, _ := encrypt.StringEncryptWithBase64(node.ServerKey)
|
2024-08-02 17:29:04 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ServerKey", Value: itemKey}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-13 15:33:34 +08:00
|
|
|
itemCrt, _ := encrypt.StringEncryptWithBase64(node.ServerCrt)
|
2024-08-02 17:29:04 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "ServerCrt", Value: itemCrt}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-13 15:33:34 +08:00
|
|
|
itemToken, _ := encrypt.StringEncryptWithBase64(node.Token)
|
|
|
|
if err := tx.Create(&model.Setting{Key: "Token", Value: itemToken}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.CONF.System.MasterRequestAddr = node.MasterRequestAddr
|
|
|
|
global.CONF.System.MasterRequestToken = itemToken
|
2024-08-02 17:29:04 +08:00
|
|
|
global.CurrentNode = node.CurrentNode
|
|
|
|
} else {
|
|
|
|
global.CurrentNode = "127.0.0.1"
|
2024-08-02 16:49:43 +08:00
|
|
|
}
|
2024-08-02 17:29:04 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "CurrentNode", Value: global.CurrentNode}).Error; err != nil {
|
2024-08-02 16:49:43 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-07-25 18:13:57 +08:00
|
|
|
|
2024-07-23 14:48:37 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "DockerSockPath", Value: "unix:///var/run/docker.sock"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemVersion", Value: global.CONF.System.Version}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SystemStatus", Value: "Free"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "LocalTime", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "TimeZone", Value: common.LoadTimeZoneByCmd()}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "NtpSite", Value: "pool.ntp.org"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "LastCleanTime", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "LastCleanSize", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "LastCleanData", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-05 18:22:25 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "DefaultNetwork", Value: "all"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorStatus", Value: "enable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorStoreDays", Value: "7"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "MonitorInterval", Value: "5"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "AppStoreVersion", Value: ""}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "AppStoreSyncStatus", Value: "SyncSuccess"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "AppStoreLastModified", Value: "0"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Create(&model.Setting{Key: "FileRecycleBin", Value: "enable"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.Setting{Key: "SnapshotIgnore", Value: "*.sock"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-31 18:29:41 +08:00
|
|
|
|
2024-08-02 16:49:43 +08:00
|
|
|
_ = os.Remove(("/opt/1panel/nodeJson"))
|
2024-07-23 14:48:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitImageRepo = &gormigrate.Migration{
|
|
|
|
ID: "20240722-init-imagerepo",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
item := &model.ImageRepo{
|
|
|
|
Name: "Docker Hub",
|
|
|
|
Protocol: "https",
|
|
|
|
DownloadUrl: "docker.io",
|
|
|
|
Status: constant.StatusSuccess,
|
|
|
|
}
|
|
|
|
if err := tx.Create(item).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitDefaultGroup = &gormigrate.Migration{
|
|
|
|
ID: "20240722-init-default-group",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
websiteGroup := &model.Group{
|
|
|
|
Name: "默认",
|
|
|
|
IsDefault: true,
|
|
|
|
Type: "website",
|
|
|
|
}
|
|
|
|
if err := tx.Create(websiteGroup).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commandGroup := &model.Group{IsDefault: true, Name: "默认", Type: "command"}
|
|
|
|
if err := tx.Create(commandGroup).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitDefaultCA = &gormigrate.Migration{
|
|
|
|
ID: "20240722-init-default-ca",
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var InitPHPExtensions = &gormigrate.Migration{
|
|
|
|
ID: "20240722-add-php-extensions",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "默认", Extensions: "bcmath,gd,gettext,intl,pcntl,shmop,soap,sockets,sysvsem,xmlrpc,zip"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "WordPress", Extensions: "exif,igbinary,imagick,intl,zip,apcu,memcached,opcache,redis,bc,image,shmop,mysqli,pdo_mysql,gd"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "Flarum", Extensions: "curl,gd,pdo_mysql,mysqli,bz2,exif,yaf,imap"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "苹果CMS-V10", Extensions: "mysqli,pdo_mysql,zip,gd,redis,memcache,memcached"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Create(&model.PHPExtensions{Name: "SeaCMS", Extensions: "mysqli,pdo_mysql,gd,curl"}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2024-07-29 18:09:57 +08:00
|
|
|
|
|
|
|
var AddTask = &gormigrate.Migration{
|
2024-08-02 16:00:15 +08:00
|
|
|
ID: "20240802-add-task",
|
2024-07-29 18:09:57 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(
|
|
|
|
&model.Task{})
|
|
|
|
},
|
|
|
|
}
|
2024-08-07 13:52:34 +08:00
|
|
|
|
|
|
|
var UpdateWebsite = &gormigrate.Migration{
|
2024-08-12 17:08:52 +08:00
|
|
|
ID: "20240812-update-website",
|
2024-08-07 13:52:34 +08:00
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(
|
|
|
|
&model.Website{})
|
|
|
|
},
|
|
|
|
}
|
2024-08-08 18:26:36 +08:00
|
|
|
|
|
|
|
var UpdateWebsiteDomain = &gormigrate.Migration{
|
|
|
|
ID: "20240808-update-website-domain",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
return tx.AutoMigrate(
|
|
|
|
&model.WebsiteDomain{})
|
|
|
|
},
|
|
|
|
}
|