mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +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;"`
|
||||
ResourceId uint `json:"resourceId" gorm:"type:integer;"`
|
||||
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
|
||||
WithByMysqlList() DBOption
|
||||
WithAppInstallID(appInstallID uint) DBOption
|
||||
WithType(dbType string) DBOption
|
||||
}
|
||||
|
||||
func NewIDatabaseRepo() IDatabaseRepo {
|
||||
@ -75,6 +76,13 @@ func (d *DatabaseRepo) WithoutByFrom(from string) DBOption {
|
||||
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 {
|
||||
return func(g *gorm.DB) *gorm.DB {
|
||||
return g.Where("app_install_id = ?", appInstallID)
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"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)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -426,25 +426,50 @@ func (a *AppInstallService) SyncAll(systemInit bool) 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
|
||||
for _, install := range installs {
|
||||
paramMap := make(map[string]string)
|
||||
if install.Param != "" {
|
||||
_ = json.Unmarshal([]byte(install.Param), ¶mMap)
|
||||
if DatabaseKeys[key] {
|
||||
dbs, _ := databaseRepo.GetList(databaseRepo.WithByFrom("local"), databaseRepo.WithType(key))
|
||||
if len(dbs) == 0 {
|
||||
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
|
||||
}
|
||||
|
@ -81,11 +81,47 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
|
||||
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 {
|
||||
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 {
|
||||
case "mysql", "mariadb", "postgresql":
|
||||
case "mysql", "mariadb", "postgresql", "mongodb":
|
||||
if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
|
||||
if password != "" {
|
||||
authParam := dto.AuthParam{
|
||||
@ -96,24 +132,14 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
||||
return err
|
||||
}
|
||||
appInstall.Param = string(authByte)
|
||||
database.Password = password.(string)
|
||||
if app.Key == "mysql" || app.Key == "mariadb" {
|
||||
database := &model.Database{
|
||||
AppInstallID: appInstall.ID,
|
||||
Name: appInstall.Name,
|
||||
Type: app.Key,
|
||||
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
|
||||
}
|
||||
database.Username = "root"
|
||||
}
|
||||
if rootUser, ok := params["PANEL_DB_ROOT_USER"]; ok {
|
||||
database.Username = rootUser.(string)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
case "redis":
|
||||
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)
|
||||
}
|
||||
database.Password = password.(string)
|
||||
}
|
||||
}
|
||||
if err := databaseRepo.Create(ctx, database); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if app.Type == "website" || app.Type == "tool" {
|
||||
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{
|
||||
ID: database.ID,
|
||||
ForceDelete: forceDelete,
|
||||
Database: database.MysqlName,
|
||||
}); err != nil && !forceDelete {
|
||||
return err
|
||||
}
|
||||
@ -519,6 +550,8 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
|
||||
envParams[k] = t
|
||||
case float64:
|
||||
envParams[k] = strconv.FormatFloat(t, 'f', -1, 32)
|
||||
case uint:
|
||||
envParams[k] = strconv.Itoa(int(t))
|
||||
default:
|
||||
envParams[k] = t.(string)
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ func Init() {
|
||||
migrations.AddTableFirewall,
|
||||
migrations.AddMariaDB,
|
||||
migrations.UpdateDatabase,
|
||||
migrations.UpdateAppInstallResource,
|
||||
})
|
||||
if err := m.Migrate(); err != nil {
|
||||
global.LOG.Error(err)
|
||||
|
@ -663,3 +663,18 @@ var UpdateDatabase = &gormigrate.Migration{
|
||||
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