2022-10-20 18:45:47 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-25 18:34:33 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-25 18:34:33 +08:00
|
|
|
"gorm.io/gorm"
|
2022-10-20 18:45:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MysqlRepo struct{}
|
|
|
|
|
|
|
|
type IMysqlRepo interface {
|
2022-10-24 18:46:19 +08:00
|
|
|
Get(opts ...DBOption) (model.DatabaseMysql, error)
|
2022-10-31 23:52:39 +08:00
|
|
|
WithByMysqlName(mysqlName string) DBOption
|
2022-10-24 18:46:19 +08:00
|
|
|
List(opts ...DBOption) ([]model.DatabaseMysql, error)
|
2022-10-20 18:45:47 +08:00
|
|
|
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
|
|
|
|
Create(mysql *model.DatabaseMysql) error
|
|
|
|
Delete(opts ...DBOption) error
|
2022-10-24 18:46:19 +08:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
2022-10-31 23:52:39 +08:00
|
|
|
LoadRunningVersion(keys []string) ([]string, error)
|
|
|
|
LoadBaseInfoByName(name string) (*RootInfo, error)
|
2022-11-02 16:28:54 +08:00
|
|
|
LoadRedisBaseInfo() (*RootInfo, error)
|
2022-11-08 14:34:41 +08:00
|
|
|
UpdateDatabaseInfo(id uint, vars map[string]interface{}) error
|
2022-10-20 18:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIMysqlRepo() IMysqlRepo {
|
|
|
|
return &MysqlRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MysqlRepo) Get(opts ...DBOption) (model.DatabaseMysql, error) {
|
|
|
|
var mysql model.DatabaseMysql
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&mysql).Error
|
|
|
|
return mysql, err
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:46:19 +08:00
|
|
|
func (u *MysqlRepo) List(opts ...DBOption) ([]model.DatabaseMysql, error) {
|
|
|
|
var users []model.DatabaseMysql
|
|
|
|
db := global.DB.Model(&model.DatabaseMysql{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&users).Error
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (u *MysqlRepo) Page(page, size int, opts ...DBOption) (int64, []model.DatabaseMysql, error) {
|
|
|
|
var users []model.DatabaseMysql
|
|
|
|
db := global.DB.Model(&model.DatabaseMysql{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Find(&users).Error
|
|
|
|
return count, users, err
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
func (u *MysqlRepo) LoadRunningVersion(keys []string) ([]string, error) {
|
2022-10-25 18:34:33 +08:00
|
|
|
var (
|
|
|
|
apps []model.App
|
2022-10-31 23:52:39 +08:00
|
|
|
appInstall []model.AppInstall
|
2022-10-25 18:34:33 +08:00
|
|
|
results []string
|
|
|
|
)
|
2022-10-31 23:52:39 +08:00
|
|
|
if err := global.DB.Where("name in (?)", keys).Find(&apps).Error; err != nil {
|
2022-10-25 18:34:33 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, app := range apps {
|
2022-10-31 23:52:39 +08:00
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).Find(&appInstall).Error; err != nil {
|
2022-10-25 18:34:33 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
for _, item := range appInstall {
|
|
|
|
results = append(results, item.Name)
|
2022-10-25 18:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RootInfo struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Port int64 `json:"port"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
ContainerName string `json:"containerName"`
|
|
|
|
Param string `json:"param"`
|
|
|
|
Env string `json:"env"`
|
2022-10-31 23:52:39 +08:00
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MysqlRepo) LoadBaseInfoByName(name string) (*RootInfo, error) {
|
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
info RootInfo
|
|
|
|
)
|
|
|
|
if err := global.DB.Where("name = ?", name).First(&appInstall).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := global.DB.Where("id = ?", appInstall.AppId).First(&app).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.Password = password
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("error password in db")
|
|
|
|
}
|
|
|
|
port, ok := envMap["PANEL_APP_PORT_HTTP"].(float64)
|
|
|
|
if ok {
|
|
|
|
info.Port = int64(port)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("error port in db")
|
|
|
|
}
|
|
|
|
info.ID = appInstall.ID
|
|
|
|
info.Key = app.Key
|
|
|
|
info.ContainerName = appInstall.ContainerName
|
|
|
|
info.Name = appInstall.Name
|
|
|
|
info.Env = appInstall.Env
|
|
|
|
info.Param = appInstall.Param
|
|
|
|
return &info, nil
|
2022-10-25 18:34:33 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (u *MysqlRepo) LoadRedisBaseInfo() (*RootInfo, error) {
|
2022-10-25 18:34:33 +08:00
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
info RootInfo
|
|
|
|
)
|
2022-10-31 23:52:39 +08:00
|
|
|
if err := global.DB.Where("key = ?", "redis").First(&app).Error; err != nil {
|
2022-10-25 18:34:33 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
2022-10-25 18:34:33 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.Password = password
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("error password in db")
|
|
|
|
}
|
|
|
|
port, ok := envMap["PANEL_APP_PORT_HTTP"].(float64)
|
|
|
|
if ok {
|
|
|
|
info.Port = int64(port)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("error port in db")
|
|
|
|
}
|
|
|
|
info.ID = appInstall.ID
|
|
|
|
info.ContainerName = appInstall.ContainerName
|
|
|
|
info.Name = appInstall.Name
|
|
|
|
info.Env = appInstall.Env
|
|
|
|
info.Param = appInstall.Param
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (u *MysqlRepo) Create(mysql *model.DatabaseMysql) error {
|
|
|
|
return global.DB.Create(mysql).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MysqlRepo) Delete(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.DatabaseMysql{}).Error
|
|
|
|
}
|
2022-10-24 18:46:19 +08:00
|
|
|
|
|
|
|
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.DatabaseMysql{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|
2022-10-25 18:34:33 +08:00
|
|
|
|
2022-11-08 14:34:41 +08:00
|
|
|
func (u *MysqlRepo) UpdateDatabaseInfo(id uint, vars map[string]interface{}) error {
|
2022-10-25 18:34:33 +08:00
|
|
|
if err := global.DB.Model(&model.AppInstall{}).Where("id = ?", id).Updates(vars).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
func (c *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
|
2022-10-25 18:34:33 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2022-10-31 23:52:39 +08:00
|
|
|
return g.Where("mysql_name = ?", mysqlName)
|
2022-10-25 18:34:33 +08:00
|
|
|
}
|
|
|
|
}
|