mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-09 15:06:37 +08:00
feat: 应用安装支持选择多数据库 (#2109)
This commit is contained in:
parent
03f53f7042
commit
7891f9225b
4 changed files with 55 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||||
"github.com/1Panel-dev/1Panel/backend/global"
|
"github.com/1Panel-dev/1Panel/backend/global"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -11,20 +12,21 @@ type DatabaseRepo struct{}
|
||||||
type IDatabaseRepo interface {
|
type IDatabaseRepo interface {
|
||||||
GetList(opts ...DBOption) ([]model.Database, error)
|
GetList(opts ...DBOption) ([]model.Database, error)
|
||||||
Page(limit, offset int, opts ...DBOption) (int64, []model.Database, error)
|
Page(limit, offset int, opts ...DBOption) (int64, []model.Database, error)
|
||||||
Create(database *model.Database) error
|
Create(ctx context.Context, database *model.Database) error
|
||||||
Update(id uint, vars map[string]interface{}) error
|
Update(id uint, vars map[string]interface{}) error
|
||||||
Delete(opts ...DBOption) error
|
Delete(ctx context.Context, opts ...DBOption) error
|
||||||
Get(opts ...DBOption) (model.Database, error)
|
Get(opts ...DBOption) (model.Database, error)
|
||||||
WithByFrom(from string) DBOption
|
WithByFrom(from string) DBOption
|
||||||
WithoutByFrom(from string) DBOption
|
WithoutByFrom(from string) DBOption
|
||||||
WithByMysqlList() DBOption
|
WithByMysqlList() DBOption
|
||||||
|
WithAppInstallID(appInstallID uint) DBOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIDatabaseRepo() IDatabaseRepo {
|
func NewIDatabaseRepo() IDatabaseRepo {
|
||||||
return &DatabaseRepo{}
|
return &DatabaseRepo{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) {
|
func (d *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) {
|
||||||
var database model.Database
|
var database model.Database
|
||||||
db := global.DB
|
db := global.DB
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@ -34,7 +36,7 @@ func (u *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) {
|
||||||
return database, err
|
return database, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Database, error) {
|
func (d *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Database, error) {
|
||||||
var users []model.Database
|
var users []model.Database
|
||||||
db := global.DB.Model(&model.Database{})
|
db := global.DB.Model(&model.Database{})
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@ -46,7 +48,7 @@ func (u *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Da
|
||||||
return count, users, err
|
return count, users, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) {
|
func (d *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) {
|
||||||
var databases []model.Database
|
var databases []model.Database
|
||||||
db := global.DB.Model(&model.Database{})
|
db := global.DB.Model(&model.Database{})
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@ -56,36 +58,37 @@ func (u *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) {
|
||||||
return databases, err
|
return databases, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DatabaseRepo) WithByMysqlList() DBOption {
|
func (d *DatabaseRepo) WithByMysqlList() DBOption {
|
||||||
return func(g *gorm.DB) *gorm.DB {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return g.Where("type == ? OR type == ?", "mysql", "mariadb")
|
return g.Where("type = ? OR type = ?", "mysql", "mariadb")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DatabaseRepo) WithByFrom(from string) DBOption {
|
func (d *DatabaseRepo) WithByFrom(from string) DBOption {
|
||||||
return func(g *gorm.DB) *gorm.DB {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return g.Where("`from` == ?", from)
|
return g.Where("`from` = ?", from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DatabaseRepo) WithoutByFrom(from string) DBOption {
|
func (d *DatabaseRepo) WithoutByFrom(from string) DBOption {
|
||||||
return func(g *gorm.DB) *gorm.DB {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return g.Where("`from` != ?", from)
|
return g.Where("`from` != ?", from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (d *DatabaseRepo) WithAppInstallID(appInstallID uint) DBOption {
|
||||||
func (u *DatabaseRepo) Create(database *model.Database) error {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return global.DB.Create(database).Error
|
return g.Where("app_install_id = ?", appInstallID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *DatabaseRepo) Update(id uint, vars map[string]interface{}) error {
|
func (d *DatabaseRepo) Create(ctx context.Context, database *model.Database) error {
|
||||||
|
return getTx(ctx).Create(database).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DatabaseRepo) Update(id uint, vars map[string]interface{}) error {
|
||||||
return global.DB.Model(&model.Database{}).Where("id = ?", id).Updates(vars).Error
|
return global.DB.Model(&model.Database{}).Where("id = ?", id).Updates(vars).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *DatabaseRepo) Delete(opts ...DBOption) error {
|
func (d *DatabaseRepo) Delete(ctx context.Context, opts ...DBOption) error {
|
||||||
db := global.DB
|
return getTx(ctx, opts...).Delete(&model.Database{}).Error
|
||||||
for _, opt := range opts {
|
|
||||||
db = opt(db)
|
|
||||||
}
|
|
||||||
return db.Delete(&model.Database{}).Error
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,13 +352,20 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
|
||||||
appInstall.ContainerName = containerName
|
appInstall.ContainerName = containerName
|
||||||
|
|
||||||
index := 0
|
index := 0
|
||||||
|
serviceName := ""
|
||||||
for k := range servicesMap {
|
for k := range servicesMap {
|
||||||
appInstall.ServiceName = k
|
serviceName = k
|
||||||
if index > 0 {
|
if index > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
if app.Limit == 0 {
|
||||||
|
servicesMap[appInstall.Name] = servicesMap[serviceName]
|
||||||
|
delete(servicesMap, serviceName)
|
||||||
|
serviceName = appInstall.Name
|
||||||
|
}
|
||||||
|
appInstall.ServiceName = serviceName
|
||||||
|
|
||||||
if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil {
|
if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -96,7 +96,24 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
appInstall.Param = string(authByte)
|
appInstall.Param = string(authByte)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case "redis":
|
case "redis":
|
||||||
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
||||||
|
@ -141,6 +158,7 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
||||||
var createMysql dto.MysqlDBCreate
|
var createMysql dto.MysqlDBCreate
|
||||||
createMysql.Name = dbConfig.DbName
|
createMysql.Name = dbConfig.DbName
|
||||||
createMysql.Username = dbConfig.DbUser
|
createMysql.Username = dbConfig.DbUser
|
||||||
|
createMysql.Database = dbInstall.Name
|
||||||
createMysql.Format = "utf8mb4"
|
createMysql.Format = "utf8mb4"
|
||||||
createMysql.Permission = "%"
|
createMysql.Permission = "%"
|
||||||
createMysql.Password = dbConfig.Password
|
createMysql.Password = dbConfig.Password
|
||||||
|
@ -229,13 +247,16 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool) error {
|
func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool) error {
|
||||||
|
if install.App.Key == "mysql" || install.App.Key == "mariadb" {
|
||||||
|
_ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID))
|
||||||
|
}
|
||||||
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
||||||
if len(resources) == 0 {
|
if len(resources) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, re := range resources {
|
for _, re := range resources {
|
||||||
mysqlService := NewIMysqlService()
|
mysqlService := NewIMysqlService()
|
||||||
if re.Key == "mysql" && deleteDB {
|
if re.Key == "mysql" || re.Key == "mariadb" && deleteDB {
|
||||||
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
||||||
if reflect.DeepEqual(database, model.DatabaseMysql{}) {
|
if reflect.DeepEqual(database, model.DatabaseMysql{}) {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -109,7 +109,7 @@ func (u *DatabaseService) Create(req dto.DatabaseCreate) error {
|
||||||
if err := copier.Copy(&db, &req); err != nil {
|
if err := copier.Copy(&db, &req); err != nil {
|
||||||
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
||||||
}
|
}
|
||||||
if err := databaseRepo.Create(&db); err != nil {
|
if err := databaseRepo.Create(context.Background(), &db); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -120,7 +120,7 @@ func (u *DatabaseService) Delete(id uint) error {
|
||||||
if db.ID == 0 {
|
if db.ID == 0 {
|
||||||
return constant.ErrRecordNotFound
|
return constant.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
if err := databaseRepo.Delete(commonRepo.WithByID(id)); err != nil {
|
if err := databaseRepo.Delete(context.Background(), commonRepo.WithByID(id)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if db.From != "local" {
|
if db.From != "local" {
|
||||||
|
|
Loading…
Add table
Reference in a new issue