mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 14:08:06 +08:00
feat: 应用安装支持选择 mariadb 和其他 mysql (#2130)
This commit is contained in:
parent
2ed2a7ed8c
commit
ff0ed46554
@ -6,4 +6,5 @@ type AppInstallResource struct {
|
|||||||
LinkId uint `json:"linkId" gorm:"type:integer;not null;"`
|
LinkId uint `json:"linkId" gorm:"type:integer;not null;"`
|
||||||
ResourceId uint `json:"resourceId" gorm:"type:integer;"`
|
ResourceId uint `json:"resourceId" gorm:"type:integer;"`
|
||||||
Key string `json:"key" gorm:"type:varchar(64);not null"`
|
Key string `json:"key" gorm:"type:varchar(64);not null"`
|
||||||
|
From string `json:"from" gorm:"type:varchar(64);not null;default:local"`
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ type IDatabaseRepo interface {
|
|||||||
WithoutByFrom(from string) DBOption
|
WithoutByFrom(from string) DBOption
|
||||||
WithByMysqlList() DBOption
|
WithByMysqlList() DBOption
|
||||||
WithAppInstallID(appInstallID uint) DBOption
|
WithAppInstallID(appInstallID uint) DBOption
|
||||||
|
WithType(dbType string) DBOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIDatabaseRepo() IDatabaseRepo {
|
func NewIDatabaseRepo() IDatabaseRepo {
|
||||||
@ -75,6 +76,13 @@ func (d *DatabaseRepo) WithoutByFrom(from string) DBOption {
|
|||||||
return g.Where("`from` != ?", from)
|
return g.Where("`from` != ?", from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DatabaseRepo) WithType(dbType string) DBOption {
|
||||||
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
|
return g.Where("`type` = ?", dbType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *DatabaseRepo) WithAppInstallID(appInstallID uint) DBOption {
|
func (d *DatabaseRepo) WithAppInstallID(appInstallID uint) DBOption {
|
||||||
return func(g *gorm.DB) *gorm.DB {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return g.Where("app_install_id = ?", appInstallID)
|
return g.Where("app_install_id = ?", appInstallID)
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -389,6 +390,14 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
if hostName, ok := req.Params["PANEL_DB_HOST"]; ok {
|
||||||
|
database, _ := databaseRepo.Get(commonRepo.WithByName(hostName.(string)))
|
||||||
|
if !reflect.DeepEqual(database, model.Database{}) {
|
||||||
|
req.Params["PANEL_DB_HOST"] = database.Address
|
||||||
|
req.Params["PANEL_DB_PORT"] = database.Port
|
||||||
|
req.Params["PANEL_DB_HOST_NAME"] = hostName
|
||||||
|
}
|
||||||
|
}
|
||||||
paramByte, err = json.Marshal(req.Params)
|
paramByte, err = json.Marshal(req.Params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -426,25 +426,50 @@ func (a *AppInstallService) SyncAll(systemInit bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AppInstallService) GetServices(key string) ([]response.AppService, error) {
|
func (a *AppInstallService) GetServices(key string) ([]response.AppService, error) {
|
||||||
app, err := appRepo.GetFirst(appRepo.WithKey(key))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.Running))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var res []response.AppService
|
var res []response.AppService
|
||||||
for _, install := range installs {
|
if DatabaseKeys[key] {
|
||||||
paramMap := make(map[string]string)
|
dbs, _ := databaseRepo.GetList(databaseRepo.WithByFrom("local"), databaseRepo.WithType(key))
|
||||||
if install.Param != "" {
|
if len(dbs) == 0 {
|
||||||
_ = json.Unmarshal([]byte(install.Param), ¶mMap)
|
return res, nil
|
||||||
|
}
|
||||||
|
for _, db := range dbs {
|
||||||
|
service := response.AppService{
|
||||||
|
Label: db.Name,
|
||||||
|
Value: db.Name,
|
||||||
|
}
|
||||||
|
if db.AppInstallID > 0 {
|
||||||
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(db.AppInstallID))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paramMap := make(map[string]string)
|
||||||
|
if install.Param != "" {
|
||||||
|
_ = json.Unmarshal([]byte(install.Param), ¶mMap)
|
||||||
|
}
|
||||||
|
service.Config = paramMap
|
||||||
|
}
|
||||||
|
res = append(res, service)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
app, err := appRepo.GetFirst(appRepo.WithKey(key))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.Running))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, install := range installs {
|
||||||
|
paramMap := make(map[string]string)
|
||||||
|
if install.Param != "" {
|
||||||
|
_ = json.Unmarshal([]byte(install.Param), ¶mMap)
|
||||||
|
}
|
||||||
|
res = append(res, response.AppService{
|
||||||
|
Label: install.Name,
|
||||||
|
Value: install.ServiceName,
|
||||||
|
Config: paramMap,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
res = append(res, response.AppService{
|
|
||||||
Label: install.Name,
|
|
||||||
Value: install.ServiceName,
|
|
||||||
Config: paramMap,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@ -81,11 +81,47 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var DatabaseKeys = map[string]bool{
|
||||||
|
"mysql": true,
|
||||||
|
"mariadb": true,
|
||||||
|
"postgresql": true,
|
||||||
|
"mongodb": true,
|
||||||
|
"redis": true,
|
||||||
|
"memcached": true,
|
||||||
|
}
|
||||||
|
|
||||||
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
|
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
|
||||||
var dbConfig dto.AppDatabase
|
var dbConfig dto.AppDatabase
|
||||||
if app.Type == "runtime" {
|
if app.Type == "runtime" && DatabaseKeys[app.Key] {
|
||||||
|
database := &model.Database{
|
||||||
|
AppInstallID: appInstall.ID,
|
||||||
|
Name: appInstall.Name,
|
||||||
|
Type: app.Key,
|
||||||
|
Version: appInstall.Version,
|
||||||
|
From: "local",
|
||||||
|
Address: appInstall.ServiceName,
|
||||||
|
}
|
||||||
|
detail, err := appDetailRepo.GetFirst(commonRepo.WithByID(appInstall.AppDetailId))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
formFields := &dto.AppForm{}
|
||||||
|
if err := json.Unmarshal([]byte(detail.Params), formFields); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, form := range formFields.FormFields {
|
||||||
|
if form.EnvKey == "PANEL_APP_PORT_HTTP" {
|
||||||
|
portFloat, ok := form.Default.(float64)
|
||||||
|
if ok {
|
||||||
|
database.Port = uint(int(portFloat))
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch app.Key {
|
switch app.Key {
|
||||||
case "mysql", "mariadb", "postgresql":
|
case "mysql", "mariadb", "postgresql", "mongodb":
|
||||||
if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
|
if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
|
||||||
if password != "" {
|
if password != "" {
|
||||||
authParam := dto.AuthParam{
|
authParam := dto.AuthParam{
|
||||||
@ -96,24 +132,14 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
appInstall.Param = string(authByte)
|
appInstall.Param = string(authByte)
|
||||||
|
database.Password = password.(string)
|
||||||
if app.Key == "mysql" || app.Key == "mariadb" {
|
if app.Key == "mysql" || app.Key == "mariadb" {
|
||||||
database := &model.Database{
|
database.Username = "root"
|
||||||
AppInstallID: appInstall.ID,
|
}
|
||||||
Name: appInstall.Name,
|
if rootUser, ok := params["PANEL_DB_ROOT_USER"]; ok {
|
||||||
Type: app.Key,
|
database.Username = rootUser.(string)
|
||||||
Version: appInstall.Version,
|
|
||||||
From: "local",
|
|
||||||
Address: appInstall.ServiceName,
|
|
||||||
Username: "root",
|
|
||||||
Password: password.(string),
|
|
||||||
Port: 3306,
|
|
||||||
}
|
|
||||||
if err := databaseRepo.Create(ctx, database); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case "redis":
|
case "redis":
|
||||||
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
||||||
@ -127,8 +153,12 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
|||||||
}
|
}
|
||||||
appInstall.Param = string(authByte)
|
appInstall.Param = string(authByte)
|
||||||
}
|
}
|
||||||
|
database.Password = password.(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err := databaseRepo.Create(ctx, database); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if app.Type == "website" || app.Type == "tool" {
|
if app.Type == "website" || app.Type == "tool" {
|
||||||
paramByte, err := json.Marshal(params)
|
paramByte, err := json.Marshal(params)
|
||||||
@ -264,6 +294,7 @@ func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, f
|
|||||||
if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
|
if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
|
||||||
ID: database.ID,
|
ID: database.ID,
|
||||||
ForceDelete: forceDelete,
|
ForceDelete: forceDelete,
|
||||||
|
Database: database.MysqlName,
|
||||||
}); err != nil && !forceDelete {
|
}); err != nil && !forceDelete {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -519,6 +550,8 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
|
|||||||
envParams[k] = t
|
envParams[k] = t
|
||||||
case float64:
|
case float64:
|
||||||
envParams[k] = strconv.FormatFloat(t, 'f', -1, 32)
|
envParams[k] = strconv.FormatFloat(t, 'f', -1, 32)
|
||||||
|
case uint:
|
||||||
|
envParams[k] = strconv.Itoa(int(t))
|
||||||
default:
|
default:
|
||||||
envParams[k] = t.(string)
|
envParams[k] = t.(string)
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ func Init() {
|
|||||||
migrations.AddTableFirewall,
|
migrations.AddTableFirewall,
|
||||||
migrations.AddMariaDB,
|
migrations.AddMariaDB,
|
||||||
migrations.UpdateDatabase,
|
migrations.UpdateDatabase,
|
||||||
|
migrations.UpdateAppInstallResource,
|
||||||
})
|
})
|
||||||
if err := m.Migrate(); err != nil {
|
if err := m.Migrate(); err != nil {
|
||||||
global.LOG.Error(err)
|
global.LOG.Error(err)
|
||||||
|
@ -663,3 +663,18 @@ var UpdateDatabase = &gormigrate.Migration{
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user