2022-09-26 16:32:40 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-09 23:35:24 +08:00
|
|
|
"context"
|
2022-11-29 17:39:10 +08:00
|
|
|
"encoding/json"
|
2024-05-30 15:13:21 +08:00
|
|
|
|
2023-12-28 16:29:18 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-08-29 10:50:15 +08:00
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
"gorm.io/gorm/clause"
|
2022-12-21 12:21:27 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-11-29 17:39:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-03 17:35:39 +08:00
|
|
|
"gorm.io/gorm"
|
2022-09-26 16:32:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppInstallRepo struct{}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
type IAppInstallRepo interface {
|
|
|
|
WithDetailIdsIn(detailIds []uint) DBOption
|
|
|
|
WithDetailIdNotIn(detailIds []uint) DBOption
|
|
|
|
WithAppId(appId uint) DBOption
|
|
|
|
WithAppIdsIn(appIds []uint) DBOption
|
|
|
|
WithStatus(status string) DBOption
|
|
|
|
WithServiceName(serviceName string) DBOption
|
2023-05-31 15:39:04 +08:00
|
|
|
WithContainerName(containerName string) DBOption
|
2023-03-28 18:00:06 +08:00
|
|
|
WithPort(port int) DBOption
|
|
|
|
WithIdNotInWebsite() DBOption
|
2023-06-05 14:59:26 +08:00
|
|
|
WithIDNotIs(id uint) DBOption
|
2023-03-28 18:00:06 +08:00
|
|
|
ListBy(opts ...DBOption) ([]model.AppInstall, error)
|
|
|
|
GetFirst(opts ...DBOption) (model.AppInstall, error)
|
|
|
|
Create(ctx context.Context, install *model.AppInstall) error
|
2023-04-06 00:09:58 +08:00
|
|
|
Save(ctx context.Context, install *model.AppInstall) error
|
2023-03-28 18:00:06 +08:00
|
|
|
DeleteBy(opts ...DBOption) error
|
|
|
|
Delete(ctx context.Context, install model.AppInstall) error
|
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.AppInstall, error)
|
|
|
|
BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error
|
|
|
|
LoadBaseInfo(key string, name string) (*RootInfo, error)
|
2023-04-04 18:54:04 +08:00
|
|
|
GetFirstByCtx(ctx context.Context, opts ...DBOption) (model.AppInstall, error)
|
2023-03-28 18:00:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAppInstallRepo() IAppInstallRepo {
|
|
|
|
return &AppInstallRepo{}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithDetailIdsIn(detailIds []uint) DBOption {
|
2022-10-03 17:35:39 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_detail_id in (?)", detailIds)
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 10:54:09 +08:00
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithDetailIdNotIn(detailIds []uint) DBOption {
|
2022-10-12 10:54:09 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_detail_id not in (?)", detailIds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithAppId(appId uint) DBOption {
|
2022-10-07 15:49:39 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_id = ?", appId)
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 17:04:57 +08:00
|
|
|
|
2023-06-05 14:59:26 +08:00
|
|
|
func (a *AppInstallRepo) WithIDNotIs(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id != ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithAppIdsIn(appIds []uint) DBOption {
|
2022-10-28 17:04:57 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_id in (?)", appIds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithStatus(status string) DBOption {
|
2022-10-07 15:49:39 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 17:35:39 +08:00
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithServiceName(serviceName string) DBOption {
|
2022-10-11 16:27:58 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("service_name = ?", serviceName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 15:39:04 +08:00
|
|
|
func (a *AppInstallRepo) WithContainerName(containerName string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("container_name = ?", containerName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:29:54 +08:00
|
|
|
func (a *AppInstallRepo) WithPort(port int) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("https_port = ? or http_port = ?", port, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) WithIdNotInWebsite() DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("id not in (select app_install_id from websites)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:29:54 +08:00
|
|
|
func (a *AppInstallRepo) ListBy(opts ...DBOption) ([]model.AppInstall, error) {
|
2022-09-26 16:32:40 +08:00
|
|
|
var install []model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2023-03-02 18:47:48 +08:00
|
|
|
err := db.Preload("App").Find(&install).Error
|
2022-09-29 18:16:56 +08:00
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) GetFirst(opts ...DBOption) (model.AppInstall, error) {
|
2022-09-29 18:16:56 +08:00
|
|
|
var install model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2023-03-02 18:47:48 +08:00
|
|
|
err := db.Preload("App").First(&install).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
2023-04-04 18:54:04 +08:00
|
|
|
func (a *AppInstallRepo) GetFirstByCtx(ctx context.Context, opts ...DBOption) (model.AppInstall, error) {
|
|
|
|
var install model.AppInstall
|
|
|
|
db := getTx(ctx, opts...).Model(&model.AppInstall{})
|
|
|
|
err := db.Preload("App").First(&install).Error
|
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) Create(ctx context.Context, install *model.AppInstall) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
db := getTx(ctx).Model(&model.AppInstall{})
|
2023-04-11 14:38:27 +08:00
|
|
|
return db.Omit(clause.Associations).Create(&install).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
2022-09-26 22:54:38 +08:00
|
|
|
|
2023-04-06 00:09:58 +08:00
|
|
|
func (a *AppInstallRepo) Save(ctx context.Context, install *model.AppInstall) error {
|
2023-07-25 17:20:25 +08:00
|
|
|
return getTx(ctx).Save(&install).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) DeleteBy(opts ...DBOption) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
return getDb(opts...).Delete(&model.AppInstall{}).Error
|
2022-09-26 22:54:38 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) Delete(ctx context.Context, install model.AppInstall) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getTx(ctx).Model(&model.AppInstall{})
|
2022-10-09 23:35:24 +08:00
|
|
|
return db.Delete(&install).Error
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) Page(page, size int, opts ...DBOption) (int64, []model.AppInstall, error) {
|
2022-09-26 16:32:40 +08:00
|
|
|
var apps []model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2022-09-26 16:32:40 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-03-02 18:47:48 +08:00
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Preload("App").Find(&apps).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
return count, apps, err
|
|
|
|
}
|
2022-10-03 17:35:39 +08:00
|
|
|
|
2022-12-20 21:22:36 +08:00
|
|
|
func (a *AppInstallRepo) BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
|
|
|
if len(opts) == 0 {
|
|
|
|
db = db.Where("1=1")
|
2022-10-03 17:35:39 +08:00
|
|
|
}
|
2023-02-09 16:13:06 +08:00
|
|
|
return db.Updates(&maps).Error
|
2022-10-03 17:35:39 +08:00
|
|
|
}
|
2022-11-29 17:39:10 +08:00
|
|
|
|
|
|
|
type RootInfo struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2024-05-30 15:13:21 +08:00
|
|
|
Status string `json:"status"`
|
2022-11-29 17:39:10 +08:00
|
|
|
Port int64 `json:"port"`
|
2023-04-01 00:51:25 +08:00
|
|
|
HttpsPort int64 `json:"httpsPort"`
|
2023-09-10 17:18:16 +08:00
|
|
|
UserName string `json:"userName"`
|
2022-11-29 17:39:10 +08:00
|
|
|
Password string `json:"password"`
|
2023-03-21 18:44:35 +08:00
|
|
|
UserPassword string `json:"userPassword"`
|
2022-11-29 17:39:10 +08:00
|
|
|
ContainerName string `json:"containerName"`
|
2023-04-11 10:26:25 +08:00
|
|
|
ServiceName string `json:"serviceName"`
|
2022-11-29 17:39:10 +08:00
|
|
|
Param string `json:"param"`
|
|
|
|
Env string `json:"env"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Version string `json:"version"`
|
2023-12-28 16:29:18 +08:00
|
|
|
AppPath string `json:"app_path"`
|
2022-11-29 17:39:10 +08:00
|
|
|
}
|
|
|
|
|
2022-12-21 12:21:27 +08:00
|
|
|
func (a *AppInstallRepo) LoadBaseInfo(key string, name string) (*RootInfo, error) {
|
2022-11-29 17:39:10 +08:00
|
|
|
var (
|
|
|
|
app model.App
|
|
|
|
appInstall model.AppInstall
|
|
|
|
info RootInfo
|
|
|
|
)
|
|
|
|
if err := global.DB.Where("key = ?", key).First(&app).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-21 12:21:27 +08:00
|
|
|
if len(name) == 0 {
|
|
|
|
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := global.DB.Where("app_id = ? AND name = ?", app.ID, name).First(&appInstall).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-29 17:39:10 +08:00
|
|
|
}
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-06 22:28:03 +08:00
|
|
|
switch app.Key {
|
2023-08-29 10:50:15 +08:00
|
|
|
case "mysql", "mariadb":
|
2023-08-06 22:28:03 +08:00
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.Password = password
|
|
|
|
}
|
|
|
|
case "redis":
|
|
|
|
password, ok := envMap["PANEL_REDIS_ROOT_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.Password = password
|
|
|
|
}
|
2023-12-28 16:29:18 +08:00
|
|
|
case "mongodb", constant.AppPostgresql:
|
2023-09-10 17:18:16 +08:00
|
|
|
user, ok := envMap["PANEL_DB_ROOT_USER"].(string)
|
|
|
|
if ok {
|
|
|
|
info.UserName = user
|
|
|
|
}
|
|
|
|
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.Password = password
|
|
|
|
}
|
2022-11-29 17:39:10 +08:00
|
|
|
}
|
2023-08-06 22:28:03 +08:00
|
|
|
|
2023-03-21 18:44:35 +08:00
|
|
|
userPassword, ok := envMap["PANEL_DB_USER_PASSWORD"].(string)
|
|
|
|
if ok {
|
|
|
|
info.UserPassword = userPassword
|
|
|
|
}
|
2022-12-09 16:03:00 +08:00
|
|
|
info.Port = int64(appInstall.HttpPort)
|
2023-04-01 00:51:25 +08:00
|
|
|
info.HttpsPort = int64(appInstall.HttpsPort)
|
2022-11-29 17:39:10 +08:00
|
|
|
info.ID = appInstall.ID
|
2023-04-11 10:26:25 +08:00
|
|
|
info.ServiceName = appInstall.ServiceName
|
2022-11-29 17:39:10 +08:00
|
|
|
info.ContainerName = appInstall.ContainerName
|
|
|
|
info.Name = appInstall.Name
|
|
|
|
info.Env = appInstall.Env
|
|
|
|
info.Param = appInstall.Param
|
|
|
|
info.Version = appInstall.Version
|
2023-09-06 21:22:12 +08:00
|
|
|
info.Key = app.Key
|
2023-12-28 16:29:18 +08:00
|
|
|
appInstall.App = app
|
|
|
|
info.AppPath = appInstall.GetAppPath()
|
2024-05-30 15:13:21 +08:00
|
|
|
info.Status = appInstall.Status
|
2022-11-29 17:39:10 +08:00
|
|
|
return &info, nil
|
|
|
|
}
|