2022-11-18 14:27:40 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-04 18:54:04 +08:00
|
|
|
"context"
|
2022-12-02 10:31:07 +08:00
|
|
|
"encoding/json"
|
2022-11-18 14:27:40 +08:00
|
|
|
"fmt"
|
2023-03-08 11:04:22 +08:00
|
|
|
"math"
|
2022-11-18 16:14:23 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-11-22 22:47:38 +08:00
|
|
|
"reflect"
|
2022-11-18 16:14:23 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-23 22:44:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
|
2023-06-06 16:03:22 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-03-21 18:44:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
2022-12-20 20:30:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2023-03-07 18:20:52 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-12-20 20:30:14 +08:00
|
|
|
|
2022-11-29 17:39:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
|
2022-11-18 14:27:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppInstallService struct {
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
type IAppInstallService interface {
|
|
|
|
Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error)
|
2023-08-29 10:50:15 +08:00
|
|
|
CheckExist(req request.AppInstalledInfo) (*response.AppInstalledCheck, error)
|
|
|
|
LoadPort(req dto.OperationWithNameAndType) (int64, error)
|
|
|
|
LoadConnInfo(req dto.OperationWithNameAndType) (response.DatabaseConn, error)
|
2023-03-20 10:38:39 +08:00
|
|
|
SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error)
|
2023-04-12 14:22:29 +08:00
|
|
|
Operate(req request.AppInstalledOperate) error
|
2023-03-20 10:38:39 +08:00
|
|
|
Update(req request.AppInstalledUpdate) error
|
2023-07-03 16:36:18 +08:00
|
|
|
IgnoreUpgrade(req request.AppInstalledIgnoreUpgrade) error
|
2023-03-20 10:38:39 +08:00
|
|
|
SyncAll(systemInit bool) error
|
|
|
|
GetServices(key string) ([]response.AppService, error)
|
|
|
|
GetUpdateVersions(installId uint) ([]dto.AppVersion, error)
|
2023-05-17 15:46:29 +08:00
|
|
|
GetParams(id uint) (*response.AppConfig, error)
|
2023-03-20 10:38:39 +08:00
|
|
|
ChangeAppPort(req request.PortUpdate) error
|
2023-09-06 21:22:12 +08:00
|
|
|
GetDefaultConfigByKey(key, name string) (string, error)
|
2023-03-20 10:38:39 +08:00
|
|
|
DeleteCheck(installId uint) ([]dto.AppResource, error)
|
2023-08-23 22:44:14 +08:00
|
|
|
|
|
|
|
GetInstallList() ([]dto.AppInstallInfo, error)
|
2023-03-20 10:38:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAppInstalledService() IAppInstallService {
|
|
|
|
return &AppInstallService{}
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:44:14 +08:00
|
|
|
func (a *AppInstallService) GetInstallList() ([]dto.AppInstallInfo, error) {
|
|
|
|
var datas []dto.AppInstallInfo
|
|
|
|
appInstalls, err := appInstallRepo.ListBy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, install := range appInstalls {
|
|
|
|
datas = append(datas, dto.AppInstallInfo{ID: install.ID, Key: install.App.Key, Name: install.Name})
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error) {
|
2023-07-18 14:22:36 +08:00
|
|
|
var (
|
|
|
|
opts []repo.DBOption
|
|
|
|
total int64
|
|
|
|
installs []model.AppInstall
|
|
|
|
err error
|
|
|
|
)
|
2022-11-28 13:50:53 +08:00
|
|
|
|
|
|
|
if req.Name != "" {
|
|
|
|
opts = append(opts, commonRepo.WithLikeName(req.Name))
|
|
|
|
}
|
2023-01-16 15:30:24 +08:00
|
|
|
if len(req.Tags) != 0 {
|
|
|
|
tags, err := tagRepo.GetByKeys(req.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
var tagIds []uint
|
|
|
|
for _, t := range tags {
|
|
|
|
tagIds = append(tagIds, t.ID)
|
|
|
|
}
|
|
|
|
appTags, err := appTagRepo.GetByTagIds(tagIds)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
var appIds []uint
|
|
|
|
for _, t := range appTags {
|
|
|
|
appIds = append(appIds, t.AppId)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, appInstallRepo.WithAppIdsIn(appIds))
|
|
|
|
}
|
2022-11-28 13:50:53 +08:00
|
|
|
|
2023-07-18 14:22:36 +08:00
|
|
|
if req.Update {
|
|
|
|
installs, err = appInstallRepo.ListBy(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
total, installs, err = appInstallRepo.Page(req.Page, req.PageSize, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2022-11-22 14:22:25 +08:00
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
installDTOs, err := handleInstalled(installs, req.Update)
|
2022-11-22 14:22:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2023-07-18 14:22:36 +08:00
|
|
|
if req.Update {
|
|
|
|
total = int64(len(installDTOs))
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
|
|
|
|
return total, installDTOs, nil
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (a *AppInstallService) CheckExist(req request.AppInstalledInfo) (*response.AppInstalledCheck, error) {
|
2022-12-14 15:08:21 +08:00
|
|
|
res := &response.AppInstalledCheck{
|
2022-11-22 22:47:38 +08:00
|
|
|
IsExist: false,
|
|
|
|
}
|
2023-08-29 10:50:15 +08:00
|
|
|
|
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(req.Key))
|
2022-11-18 16:14:23 +08:00
|
|
|
if err != nil {
|
2022-11-22 22:47:38 +08:00
|
|
|
return res, nil
|
2022-11-18 16:14:23 +08:00
|
|
|
}
|
2022-11-23 15:37:26 +08:00
|
|
|
res.App = app.Name
|
2023-08-29 10:50:15 +08:00
|
|
|
|
|
|
|
var appInstall model.AppInstall
|
|
|
|
if len(req.Name) == 0 {
|
2023-08-29 14:32:17 +08:00
|
|
|
appInstall, _ = appInstallRepo.GetFirst(appInstallRepo.WithAppId(app.ID))
|
2023-08-29 10:50:15 +08:00
|
|
|
} else {
|
2023-09-04 17:40:14 +08:00
|
|
|
appInstall, _ = appInstallRepo.GetFirst(appInstallRepo.WithAppId(app.ID), commonRepo.WithByName(req.Name))
|
2023-08-29 14:32:17 +08:00
|
|
|
}
|
2023-08-29 10:50:15 +08:00
|
|
|
|
2023-08-29 14:32:17 +08:00
|
|
|
if reflect.DeepEqual(appInstall, model.AppInstall{}) {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
if err = syncById(appInstall.ID); err != nil {
|
|
|
|
return nil, err
|
2022-12-04 17:45:54 +08:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:59:06 +08:00
|
|
|
res.ContainerName = appInstall.ContainerName
|
2022-11-22 22:47:38 +08:00
|
|
|
res.Name = appInstall.Name
|
|
|
|
res.Version = appInstall.Version
|
|
|
|
res.CreatedAt = appInstall.CreatedAt
|
|
|
|
res.Status = appInstall.Status
|
|
|
|
res.AppInstallID = appInstall.ID
|
|
|
|
res.IsExist = true
|
2023-08-29 10:50:15 +08:00
|
|
|
res.InstallPath = path.Join(constant.AppInstallDir, appInstall.App.Key, appInstall.Name)
|
2023-08-24 18:20:15 +08:00
|
|
|
res.HttpPort = appInstall.HttpPort
|
|
|
|
res.HttpsPort = appInstall.HttpsPort
|
2022-11-22 22:47:38 +08:00
|
|
|
|
|
|
|
return res, nil
|
2022-11-18 16:14:23 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (a *AppInstallService) LoadPort(req dto.OperationWithNameAndType) (int64, error) {
|
|
|
|
app, err := appInstallRepo.LoadBaseInfo(req.Type, req.Name)
|
2022-12-04 19:20:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return int64(0), nil
|
|
|
|
}
|
|
|
|
return app.Port, nil
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (a *AppInstallService) LoadConnInfo(req dto.OperationWithNameAndType) (response.DatabaseConn, error) {
|
2023-04-11 10:26:25 +08:00
|
|
|
var data response.DatabaseConn
|
2023-08-29 10:50:15 +08:00
|
|
|
app, err := appInstallRepo.LoadBaseInfo(req.Type, req.Name)
|
2022-12-20 20:30:14 +08:00
|
|
|
if err != nil {
|
2023-04-11 10:26:25 +08:00
|
|
|
return data, nil
|
2022-12-20 20:30:14 +08:00
|
|
|
}
|
2023-04-11 10:26:25 +08:00
|
|
|
data.Password = app.Password
|
|
|
|
data.ServiceName = app.ServiceName
|
2023-04-18 15:30:57 +08:00
|
|
|
data.Port = app.Port
|
2023-04-11 10:26:25 +08:00
|
|
|
return data, nil
|
2022-12-20 20:30:14 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) {
|
2023-01-16 15:30:24 +08:00
|
|
|
var (
|
|
|
|
installs []model.AppInstall
|
|
|
|
err error
|
|
|
|
opts []repo.DBOption
|
|
|
|
)
|
2022-11-18 14:27:40 +08:00
|
|
|
if req.Type != "" {
|
|
|
|
apps, err := appRepo.GetBy(appRepo.WithType(req.Type))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ids []uint
|
|
|
|
for _, app := range apps {
|
|
|
|
ids = append(ids, app.ID)
|
|
|
|
}
|
2023-01-16 15:30:24 +08:00
|
|
|
if req.Unused {
|
|
|
|
opts = append(opts, appInstallRepo.WithIdNotInWebsite())
|
|
|
|
}
|
|
|
|
opts = append(opts, appInstallRepo.WithAppIdsIn(ids))
|
2023-02-07 16:29:54 +08:00
|
|
|
installs, err = appInstallRepo.ListBy(opts...)
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-07 16:29:54 +08:00
|
|
|
installs, err = appInstallRepo.ListBy()
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
return handleInstalled(installs, false)
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:22:29 +08:00
|
|
|
func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
|
|
|
|
install, err := appInstallRepo.GetFirstByCtx(context.Background(), commonRepo.WithByID(req.InstallId))
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
if !req.ForceDelete && !files.NewFileOp().Stat(install.GetPath()) {
|
|
|
|
return buserr.New(constant.ErrInstallDirNotFound)
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
dockerComposePath := install.GetComposePath()
|
|
|
|
switch req.Operate {
|
2023-03-01 11:51:49 +08:00
|
|
|
case constant.Rebuild:
|
2023-03-08 11:04:22 +08:00
|
|
|
return rebuildApp(install)
|
2023-03-01 11:51:49 +08:00
|
|
|
case constant.Start:
|
|
|
|
out, err := compose.Start(dockerComposePath)
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return handleErr(install, err, out)
|
|
|
|
}
|
2023-03-07 15:43:12 +08:00
|
|
|
return syncById(install.ID)
|
2023-03-01 11:51:49 +08:00
|
|
|
case constant.Stop:
|
2022-12-06 15:59:25 +08:00
|
|
|
out, err := compose.Stop(dockerComposePath)
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return handleErr(install, err, out)
|
|
|
|
}
|
2023-03-07 15:43:12 +08:00
|
|
|
return syncById(install.ID)
|
2022-12-14 15:08:21 +08:00
|
|
|
case constant.Restart:
|
2022-11-18 14:27:40 +08:00
|
|
|
out, err := compose.Restart(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
return handleErr(install, err, out)
|
|
|
|
}
|
2023-03-07 15:43:12 +08:00
|
|
|
return syncById(install.ID)
|
2022-12-14 15:08:21 +08:00
|
|
|
case constant.Delete:
|
2023-04-12 14:22:29 +08:00
|
|
|
if err := deleteAppInstall(install, req.DeleteBackup, req.ForceDelete, req.DeleteDB); err != nil && !req.ForceDelete {
|
2022-11-18 14:27:40 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-14 15:08:21 +08:00
|
|
|
case constant.Sync:
|
2022-11-18 14:27:40 +08:00
|
|
|
return syncById(install.ID)
|
2023-03-08 11:04:22 +08:00
|
|
|
case constant.Upgrade:
|
2023-07-25 17:22:20 +08:00
|
|
|
return upgradeInstall(install.ID, req.DetailId, req.Backup)
|
2022-11-18 14:27:40 +08:00
|
|
|
default:
|
|
|
|
return errors.New("operate not support")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
|
2023-03-08 11:04:22 +08:00
|
|
|
installed, err := appInstallRepo.GetFirst(commonRepo.WithByID(req.InstallId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-08 23:58:42 +08:00
|
|
|
changePort := false
|
2023-03-08 11:04:22 +08:00
|
|
|
port, ok := req.Params["PANEL_APP_PORT_HTTP"]
|
|
|
|
if ok {
|
|
|
|
portN := int(math.Ceil(port.(float64)))
|
|
|
|
if portN != installed.HttpPort {
|
2023-03-08 23:58:42 +08:00
|
|
|
changePort = true
|
2023-03-08 11:04:22 +08:00
|
|
|
httpPort, err := checkPort("PANEL_APP_PORT_HTTP", req.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
installed.HttpPort = httpPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ports, ok := req.Params["PANEL_APP_PORT_HTTPS"]
|
|
|
|
if ok {
|
|
|
|
portN := int(math.Ceil(ports.(float64)))
|
|
|
|
if portN != installed.HttpsPort {
|
|
|
|
httpsPort, err := checkPort("PANEL_APP_PORT_HTTPS", req.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
installed.HttpsPort = httpsPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:48:19 +08:00
|
|
|
backupDockerCompose := installed.DockerCompose
|
2023-05-17 15:46:29 +08:00
|
|
|
if req.Advanced {
|
2023-05-18 16:48:19 +08:00
|
|
|
composeMap := make(map[string]interface{})
|
2023-05-31 15:59:00 +08:00
|
|
|
if req.EditCompose {
|
|
|
|
if err = yaml.Unmarshal([]byte(req.DockerCompose), &composeMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = yaml.Unmarshal([]byte(installed.DockerCompose), &composeMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 14:46:57 +08:00
|
|
|
}
|
2023-06-05 14:59:26 +08:00
|
|
|
if err = addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil {
|
2023-05-18 16:48:19 +08:00
|
|
|
return err
|
2023-05-17 15:46:29 +08:00
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
composeByte, err := yaml.Marshal(composeMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-17 15:46:29 +08:00
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
installed.DockerCompose = string(composeByte)
|
2023-05-31 15:59:00 +08:00
|
|
|
if req.ContainerName == "" {
|
|
|
|
req.Params[constant.ContainerName] = installed.ContainerName
|
|
|
|
} else {
|
|
|
|
req.Params[constant.ContainerName] = req.ContainerName
|
2023-06-05 14:59:26 +08:00
|
|
|
if installed.ContainerName != req.ContainerName {
|
|
|
|
exist, _ := appInstallRepo.GetFirst(appInstallRepo.WithContainerName(req.ContainerName), appInstallRepo.WithIDNotIs(installed.ID))
|
|
|
|
if exist.ID > 0 {
|
|
|
|
return buserr.New(constant.ErrContainerName)
|
|
|
|
}
|
|
|
|
containerExist, err := checkContainerNameIsExist(req.ContainerName, installed.GetPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if containerExist {
|
|
|
|
return buserr.New(constant.ErrContainerName)
|
|
|
|
}
|
|
|
|
installed.ContainerName = req.ContainerName
|
|
|
|
}
|
2023-05-31 15:59:00 +08:00
|
|
|
}
|
2023-05-17 15:46:29 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 11:04:22 +08:00
|
|
|
envPath := path.Join(installed.GetPath(), ".env")
|
|
|
|
oldEnvMaps, err := godotenv.Read(envPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
backupEnvMaps := oldEnvMaps
|
2023-03-08 11:04:22 +08:00
|
|
|
handleMap(req.Params, oldEnvMaps)
|
|
|
|
paramByte, err := json.Marshal(oldEnvMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
installed.Env = string(paramByte)
|
2023-03-09 14:24:59 +08:00
|
|
|
if err := env.Write(oldEnvMaps, envPath); err != nil {
|
2023-03-08 11:04:22 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(installed.DockerCompose), 0755)
|
2023-03-08 23:58:42 +08:00
|
|
|
if err := rebuildApp(installed); err != nil {
|
2023-05-18 16:48:19 +08:00
|
|
|
_ = env.Write(backupEnvMaps, envPath)
|
|
|
|
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(backupDockerCompose), 0755)
|
2023-03-08 23:58:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
installed.Status = constant.Running
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &installed)
|
|
|
|
|
2023-03-08 23:58:42 +08:00
|
|
|
website, _ := websiteRepo.GetFirst(websiteRepo.WithAppInstallId(installed.ID))
|
|
|
|
if changePort && website.ID != 0 && website.Status == constant.Running {
|
2023-04-10 16:26:22 +08:00
|
|
|
go func() {
|
2023-05-18 16:48:19 +08:00
|
|
|
nginxInstall, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf(buserr.WithErr(constant.ErrUpdateBuWebsite, err).Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config := nginxInstall.SiteConfig.Config
|
|
|
|
servers := config.FindServers()
|
|
|
|
if len(servers) == 0 {
|
|
|
|
global.LOG.Errorf(buserr.WithErr(constant.ErrUpdateBuWebsite, errors.New("nginx config is not valid")).Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
server := servers[0]
|
|
|
|
proxy := fmt.Sprintf("http://127.0.0.1:%d", installed.HttpPort)
|
|
|
|
server.UpdateRootProxy([]string{proxy})
|
|
|
|
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
global.LOG.Errorf(buserr.WithErr(constant.ErrUpdateBuWebsite, err).Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := nginxCheckAndReload(nginxInstall.SiteConfig.OldContent, config.FilePath, nginxInstall.Install.ContainerName); err != nil {
|
|
|
|
global.LOG.Errorf(buserr.WithErr(constant.ErrUpdateBuWebsite, err).Error())
|
|
|
|
return
|
|
|
|
}
|
2023-04-10 16:26:22 +08:00
|
|
|
}()
|
|
|
|
}
|
2023-03-08 23:58:42 +08:00
|
|
|
return nil
|
2023-03-08 11:04:22 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 16:36:18 +08:00
|
|
|
func (a *AppInstallService) IgnoreUpgrade(req request.AppInstalledIgnoreUpgrade) error {
|
2023-07-07 11:38:11 +08:00
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(req.DetailID))
|
2023-07-03 16:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-06 18:48:22 +08:00
|
|
|
appDetail.IgnoreUpgrade = req.Operate == "ignore"
|
2023-07-03 16:36:18 +08:00
|
|
|
return appDetailRepo.Update(context.Background(), appDetail)
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) SyncAll(systemInit bool) error {
|
2023-02-07 16:29:54 +08:00
|
|
|
allList, err := appInstallRepo.ListBy()
|
2022-11-18 14:27:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-14 14:03:51 +08:00
|
|
|
for _, i := range allList {
|
2023-06-01 14:47:13 +08:00
|
|
|
if i.Status == constant.Installing || i.Status == constant.Upgrading {
|
2023-03-20 10:38:39 +08:00
|
|
|
if systemInit {
|
|
|
|
i.Status = constant.Error
|
2023-06-08 10:58:10 +08:00
|
|
|
i.Message = "1Panel restart causes the task to terminate"
|
2023-04-06 00:09:58 +08:00
|
|
|
_ = appInstallRepo.Save(context.Background(), &i)
|
2023-03-20 10:38:39 +08:00
|
|
|
}
|
2023-03-14 14:03:51 +08:00
|
|
|
continue
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2023-07-06 16:30:22 +08:00
|
|
|
if !systemInit {
|
|
|
|
if err := syncById(i.ID); err != nil {
|
|
|
|
global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error())
|
|
|
|
}
|
2023-03-14 14:03:51 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) GetServices(key string) ([]response.AppService, error) {
|
2022-12-14 15:08:21 +08:00
|
|
|
var res []response.AppService
|
2023-08-31 15:14:11 +08:00
|
|
|
if DatabaseKeys[key] {
|
2023-09-01 12:28:12 +08:00
|
|
|
dbs, _ := databaseRepo.GetList(databaseRepo.WithByFrom("local"), commonRepo.WithByType(key))
|
2023-08-31 15:14:11 +08:00
|
|
|
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,
|
|
|
|
})
|
2022-12-02 10:31:07 +08:00
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) GetUpdateVersions(installId uint) ([]dto.AppVersion, error) {
|
2022-11-18 14:27:40 +08:00
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
|
|
|
|
var versions []dto.AppVersion
|
|
|
|
if err != nil {
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(install.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
details, err := appDetailRepo.GetBy(appDetailRepo.WithAppId(app.ID))
|
|
|
|
if err != nil {
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
for _, detail := range details {
|
2023-07-03 16:36:18 +08:00
|
|
|
if detail.IgnoreUpgrade {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-07 19:09:23 +08:00
|
|
|
if common.IsCrossVersion(install.Version, detail.Version) && !app.CrossVersionUpdate {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
if common.CompareVersion(detail.Version, install.Version) {
|
|
|
|
versions = append(versions, dto.AppVersion{
|
|
|
|
Version: detail.Version,
|
|
|
|
DetailId: detail.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) ChangeAppPort(req request.PortUpdate) error {
|
2023-03-07 18:20:52 +08:00
|
|
|
if common.ScanPort(int(req.Port)) {
|
|
|
|
return buserr.WithDetail(constant.ErrPortInUsed, req.Port, nil)
|
|
|
|
}
|
2023-03-07 20:59:14 +08:00
|
|
|
|
|
|
|
appInstall, err := appInstallRepo.LoadBaseInfo(req.Key, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateInstallInfoInDB(req.Key, "", "port", true, strconv.FormatInt(req.Port, 10)); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
appRess, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithLinkId(appInstall.ID))
|
|
|
|
for _, appRes := range appRess {
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(appRes.AppInstallId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := compose.Restart(fmt.Sprintf("%s/%s/%s/docker-compose.yml", constant.AppInstallDir, appInstall.App.Key, appInstall.Name)); err != nil {
|
|
|
|
global.LOG.Errorf("docker-compose restart %s[%s] failed, err: %v", appInstall.App.Key, appInstall.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func (a *AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error) {
|
2022-12-01 19:25:02 +08:00
|
|
|
var res []dto.AppResource
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appInstall.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-06 16:38:16 +08:00
|
|
|
websites, _ := websiteRepo.GetBy(websiteRepo.WithAppInstallId(appInstall.ID))
|
|
|
|
for _, website := range websites {
|
|
|
|
res = append(res, dto.AppResource{
|
|
|
|
Type: "website",
|
|
|
|
Name: website.PrimaryDomain,
|
|
|
|
})
|
2022-12-01 19:25:02 +08:00
|
|
|
}
|
2023-02-22 17:13:08 +08:00
|
|
|
if app.Key == constant.AppOpenresty {
|
2022-12-01 19:25:02 +08:00
|
|
|
websites, _ := websiteRepo.GetBy()
|
|
|
|
for _, website := range websites {
|
|
|
|
res = append(res, dto.AppResource{
|
|
|
|
Type: "website",
|
|
|
|
Name: website.PrimaryDomain,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if app.Type == "runtime" {
|
|
|
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithLinkId(appInstall.ID))
|
|
|
|
for _, resource := range resources {
|
|
|
|
linkInstall, _ := appInstallRepo.GetFirst(commonRepo.WithByID(resource.AppInstallId))
|
|
|
|
res = append(res, dto.AppResource{
|
|
|
|
Type: "app",
|
|
|
|
Name: linkInstall.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 21:22:12 +08:00
|
|
|
func (a *AppInstallService) GetDefaultConfigByKey(key, name string) (string, error) {
|
|
|
|
baseInfo, err := appInstallRepo.LoadBaseInfo(key, name)
|
2022-12-09 17:16:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-06-06 16:03:22 +08:00
|
|
|
|
|
|
|
fileOp := files.NewFileOp()
|
2023-09-06 21:22:12 +08:00
|
|
|
filePath := path.Join(constant.AppResourceDir, "remote", baseInfo.Key, baseInfo.Version, "conf")
|
2023-06-06 16:03:22 +08:00
|
|
|
if !fileOp.Stat(filePath) {
|
2023-09-06 21:22:12 +08:00
|
|
|
filePath = path.Join(constant.AppResourceDir, baseInfo.Key, "versions", baseInfo.Version, "conf")
|
2023-06-06 16:03:22 +08:00
|
|
|
}
|
|
|
|
if !fileOp.Stat(filePath) {
|
|
|
|
return "", buserr.New(constant.ErrPathNotFound)
|
|
|
|
}
|
|
|
|
|
2023-09-06 21:22:12 +08:00
|
|
|
if key == constant.AppMysql || key == constant.AppMariaDB {
|
2022-12-09 17:16:07 +08:00
|
|
|
filePath = path.Join(filePath, "my.cnf")
|
|
|
|
}
|
2022-12-13 20:03:54 +08:00
|
|
|
if key == constant.AppRedis {
|
2022-12-09 17:16:07 +08:00
|
|
|
filePath = path.Join(filePath, "redis.conf")
|
|
|
|
}
|
2023-02-22 17:13:08 +08:00
|
|
|
if key == constant.AppOpenresty {
|
2022-12-09 17:16:07 +08:00
|
|
|
filePath = path.Join(filePath, "nginx.conf")
|
|
|
|
}
|
|
|
|
contentByte, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(contentByte), nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:46:29 +08:00
|
|
|
func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) {
|
2023-01-03 14:57:13 +08:00
|
|
|
var (
|
2023-05-17 15:46:29 +08:00
|
|
|
params []response.AppParam
|
2023-01-03 14:57:13 +08:00
|
|
|
appForm dto.AppForm
|
|
|
|
envs = make(map[string]interface{})
|
2023-05-17 15:46:29 +08:00
|
|
|
res response.AppConfig
|
2023-01-03 14:57:13 +08:00
|
|
|
)
|
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
detail, err := appDetailRepo.GetFirst(commonRepo.WithByID(install.AppDetailId))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 16:51:28 +08:00
|
|
|
if err = json.Unmarshal([]byte(detail.Params), &appForm); err != nil {
|
2023-01-03 14:57:13 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 16:51:28 +08:00
|
|
|
if err = json.Unmarshal([]byte(install.Env), &envs); err != nil {
|
2023-01-03 14:57:13 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, form := range appForm.FormFields {
|
|
|
|
if v, ok := envs[form.EnvKey]; ok {
|
2023-03-08 11:04:22 +08:00
|
|
|
appParam := response.AppParam{
|
|
|
|
Edit: false,
|
|
|
|
Key: form.EnvKey,
|
|
|
|
Rule: form.Rule,
|
|
|
|
Type: form.Type,
|
|
|
|
}
|
|
|
|
if form.Edit {
|
|
|
|
appParam.Edit = true
|
|
|
|
}
|
|
|
|
appParam.LabelZh = form.LabelZh
|
|
|
|
appParam.LabelEn = form.LabelEn
|
2023-03-22 15:38:30 +08:00
|
|
|
appParam.Value = v
|
2023-01-03 14:57:13 +08:00
|
|
|
if form.Type == "service" {
|
|
|
|
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithServiceName(v.(string)))
|
2023-03-22 15:38:30 +08:00
|
|
|
appParam.ShowValue = appInstall.Name
|
|
|
|
} else if form.Type == "select" {
|
|
|
|
for _, fv := range form.Values {
|
|
|
|
if fv.Value == v {
|
|
|
|
appParam.ShowValue = fv.Label
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appParam.Values = form.Values
|
2023-01-03 14:57:13 +08:00
|
|
|
}
|
2023-05-17 15:46:29 +08:00
|
|
|
params = append(params, appParam)
|
2023-08-03 16:51:28 +08:00
|
|
|
} else {
|
|
|
|
params = append(params, response.AppParam{
|
|
|
|
Edit: form.Edit,
|
|
|
|
Key: form.EnvKey,
|
|
|
|
Rule: form.Rule,
|
|
|
|
Type: form.Type,
|
|
|
|
LabelZh: form.LabelZh,
|
|
|
|
LabelEn: form.LabelEn,
|
|
|
|
Value: form.Default,
|
|
|
|
Values: form.Values,
|
|
|
|
})
|
2023-01-03 14:57:13 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-17 15:46:29 +08:00
|
|
|
|
2023-05-18 16:48:19 +08:00
|
|
|
config := getAppCommonConfig(envs)
|
2023-05-31 15:59:00 +08:00
|
|
|
config.DockerCompose = install.DockerCompose
|
2023-05-17 15:46:29 +08:00
|
|
|
res.Params = params
|
2023-07-13 15:43:18 +08:00
|
|
|
if config.ContainerName == "" {
|
|
|
|
config.ContainerName = install.ContainerName
|
|
|
|
}
|
2023-05-18 16:48:19 +08:00
|
|
|
res.AppContainerConfig = config
|
2023-05-17 15:46:29 +08:00
|
|
|
return &res, nil
|
2023-01-03 14:57:13 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 14:27:40 +08:00
|
|
|
func syncById(installId uint) error {
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-09 19:05:42 +08:00
|
|
|
if appInstall.Status == constant.Installing {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
containerNames, err := getContainerNames(appInstall)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cli, err := docker.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containers, err := cli.ListContainersByName(containerNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
errorContainers []string
|
|
|
|
notFoundContainers []string
|
|
|
|
runningContainers []string
|
2022-12-04 12:59:09 +08:00
|
|
|
exitedContainers []string
|
2022-11-18 14:27:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, n := range containers {
|
2022-12-04 12:59:09 +08:00
|
|
|
switch n.State {
|
|
|
|
case "exited":
|
|
|
|
exitedContainers = append(exitedContainers, n.Names[0])
|
|
|
|
case "running":
|
2022-11-18 14:27:40 +08:00
|
|
|
runningContainers = append(runningContainers, n.Names[0])
|
2022-12-04 12:59:09 +08:00
|
|
|
default:
|
|
|
|
errorContainers = append(errorContainers, n.Names[0])
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
}
|
2023-07-06 16:30:22 +08:00
|
|
|
for _, name := range containerNames {
|
2022-11-18 14:27:40 +08:00
|
|
|
exist := false
|
2023-07-06 16:30:22 +08:00
|
|
|
for _, container := range containers {
|
|
|
|
if common.ExistWithStrArray(name, container.Names) {
|
2022-11-18 14:27:40 +08:00
|
|
|
exist = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exist {
|
2023-07-06 16:30:22 +08:00
|
|
|
notFoundContainers = append(notFoundContainers, name)
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
containerCount := len(containers)
|
|
|
|
errCount := len(errorContainers)
|
|
|
|
notFoundCount := len(notFoundContainers)
|
2022-12-04 12:59:09 +08:00
|
|
|
existedCount := len(exitedContainers)
|
2022-11-18 14:27:40 +08:00
|
|
|
normalCount := len(containerNames)
|
|
|
|
runningCount := len(runningContainers)
|
|
|
|
|
|
|
|
if containerCount == 0 {
|
|
|
|
appInstall.Status = constant.Error
|
2023-07-06 16:30:22 +08:00
|
|
|
appInstall.Message = i18n.GetMsgWithMap("ErrContainerNotFound", map[string]interface{}{"name": strings.Join(containerNames, ",")})
|
2023-04-06 00:09:58 +08:00
|
|
|
return appInstallRepo.Save(context.Background(), &appInstall)
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2023-07-06 16:30:22 +08:00
|
|
|
if errCount == 0 && existedCount == 0 && notFoundCount == 0 {
|
2022-11-18 14:27:40 +08:00
|
|
|
appInstall.Status = constant.Running
|
2023-04-06 00:09:58 +08:00
|
|
|
return appInstallRepo.Save(context.Background(), &appInstall)
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2022-12-04 12:59:09 +08:00
|
|
|
if existedCount == normalCount {
|
|
|
|
appInstall.Status = constant.Stopped
|
2023-04-06 00:09:58 +08:00
|
|
|
return appInstallRepo.Save(context.Background(), &appInstall)
|
2022-12-04 12:59:09 +08:00
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
if errCount == normalCount {
|
|
|
|
appInstall.Status = constant.Error
|
|
|
|
}
|
|
|
|
if runningCount < normalCount {
|
|
|
|
appInstall.Status = constant.UnHealthy
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:30:22 +08:00
|
|
|
var errMsg string
|
2022-11-18 14:27:40 +08:00
|
|
|
if errCount > 0 {
|
2023-07-06 16:30:22 +08:00
|
|
|
errMsg += i18n.GetMsgWithMap("ErrContainerMsg", map[string]interface{}{"name": strings.Join(errorContainers, ",")})
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
|
|
|
if notFoundCount > 0 {
|
2023-07-06 16:30:22 +08:00
|
|
|
errMsg += i18n.GetMsgWithMap("ErrContainerNotFound", map[string]interface{}{"name": strings.Join(notFoundContainers, ",")})
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2023-07-06 16:30:22 +08:00
|
|
|
appInstall.Message = errMsg
|
2023-04-06 00:09:58 +08:00
|
|
|
return appInstallRepo.Save(context.Background(), &appInstall)
|
2022-11-18 14:27:40 +08:00
|
|
|
}
|
2022-12-08 16:00:59 +08:00
|
|
|
|
2022-12-21 12:21:27 +08:00
|
|
|
func updateInstallInfoInDB(appKey, appName, param string, isRestart bool, value interface{}) error {
|
2022-12-23 18:00:19 +08:00
|
|
|
if param != "password" && param != "port" && param != "user-password" {
|
2022-12-12 19:04:45 +08:00
|
|
|
return nil
|
2022-12-08 16:00:59 +08:00
|
|
|
}
|
2022-12-21 12:21:27 +08:00
|
|
|
appInstall, err := appInstallRepo.LoadBaseInfo(appKey, appName)
|
2022-12-12 14:06:39 +08:00
|
|
|
if err != nil {
|
2022-12-12 19:04:45 +08:00
|
|
|
return nil
|
2022-12-08 16:00:59 +08:00
|
|
|
}
|
2022-12-12 17:17:39 +08:00
|
|
|
envPath := fmt.Sprintf("%s/%s/%s/.env", constant.AppInstallDir, appKey, appInstall.Name)
|
2023-04-07 11:30:10 +08:00
|
|
|
lineBytes, err := os.ReadFile(envPath)
|
2022-12-12 17:17:39 +08:00
|
|
|
if err != nil {
|
2022-12-12 19:04:45 +08:00
|
|
|
return err
|
2022-12-12 17:17:39 +08:00
|
|
|
}
|
2022-12-23 18:00:19 +08:00
|
|
|
|
|
|
|
envKey := ""
|
|
|
|
switch param {
|
|
|
|
case "password":
|
2023-08-29 10:50:15 +08:00
|
|
|
if appKey == "mysql" || appKey == "mariadb" {
|
2023-08-06 22:28:03 +08:00
|
|
|
envKey = "PANEL_DB_ROOT_PASSWORD="
|
|
|
|
} else {
|
|
|
|
envKey = "PANEL_REDIS_ROOT_PASSWORD="
|
|
|
|
}
|
2022-12-23 18:00:19 +08:00
|
|
|
case "port":
|
2022-12-12 17:17:39 +08:00
|
|
|
envKey = "PANEL_APP_PORT_HTTP="
|
2022-12-23 18:00:19 +08:00
|
|
|
case "user-password":
|
|
|
|
envKey = "PANEL_DB_USER_PASSWORD="
|
2022-12-12 17:17:39 +08:00
|
|
|
}
|
|
|
|
files := strings.Split(string(lineBytes), "\n")
|
|
|
|
var newFiles []string
|
|
|
|
for _, line := range files {
|
|
|
|
if strings.HasPrefix(line, envKey) {
|
|
|
|
newFiles = append(newFiles, fmt.Sprintf("%s%v", envKey, value))
|
|
|
|
} else {
|
|
|
|
newFiles = append(newFiles, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
2022-12-12 19:04:45 +08:00
|
|
|
return err
|
2022-12-12 17:17:39 +08:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString(strings.Join(newFiles, "\n"))
|
|
|
|
if err != nil {
|
2022-12-12 19:04:45 +08:00
|
|
|
return err
|
2022-12-12 17:17:39 +08:00
|
|
|
}
|
|
|
|
|
2022-12-08 16:00:59 +08:00
|
|
|
oldVal, newVal := "", ""
|
|
|
|
if param == "password" {
|
|
|
|
oldVal = fmt.Sprintf("\"PANEL_DB_ROOT_PASSWORD\":\"%v\"", appInstall.Password)
|
|
|
|
newVal = fmt.Sprintf("\"PANEL_DB_ROOT_PASSWORD\":\"%v\"", value)
|
2023-08-06 22:28:03 +08:00
|
|
|
if appKey == "redis" {
|
|
|
|
oldVal = fmt.Sprintf("\"PANEL_REDIS_ROOT_PASSWORD\":\"%v\"", appInstall.Password)
|
|
|
|
newVal = fmt.Sprintf("\"PANEL_REDIS_ROOT_PASSWORD\":\"%v\"", value)
|
|
|
|
}
|
2022-12-12 14:06:39 +08:00
|
|
|
_ = appInstallRepo.BatchUpdateBy(map[string]interface{}{
|
|
|
|
"param": strings.ReplaceAll(appInstall.Param, oldVal, newVal),
|
|
|
|
"env": strings.ReplaceAll(appInstall.Env, oldVal, newVal),
|
|
|
|
}, commonRepo.WithByID(appInstall.ID))
|
2022-12-26 16:15:42 +08:00
|
|
|
}
|
|
|
|
if param == "user-password" {
|
2023-03-21 18:44:35 +08:00
|
|
|
oldVal = fmt.Sprintf("\"PANEL_DB_USER_PASSWORD\":\"%v\"", appInstall.UserPassword)
|
2022-12-26 16:15:42 +08:00
|
|
|
newVal = fmt.Sprintf("\"PANEL_DB_USER_PASSWORD\":\"%v\"", value)
|
|
|
|
_ = appInstallRepo.BatchUpdateBy(map[string]interface{}{
|
|
|
|
"param": strings.ReplaceAll(appInstall.Param, oldVal, newVal),
|
|
|
|
"env": strings.ReplaceAll(appInstall.Env, oldVal, newVal),
|
|
|
|
}, commonRepo.WithByID(appInstall.ID))
|
2022-12-08 16:00:59 +08:00
|
|
|
}
|
|
|
|
if param == "port" {
|
|
|
|
oldVal = fmt.Sprintf("\"PANEL_APP_PORT_HTTP\":%v", appInstall.Port)
|
|
|
|
newVal = fmt.Sprintf("\"PANEL_APP_PORT_HTTP\":%v", value)
|
2022-12-12 14:06:39 +08:00
|
|
|
_ = appInstallRepo.BatchUpdateBy(map[string]interface{}{
|
|
|
|
"param": strings.ReplaceAll(appInstall.Param, oldVal, newVal),
|
|
|
|
"env": strings.ReplaceAll(appInstall.Env, oldVal, newVal),
|
|
|
|
"http_port": value,
|
|
|
|
}, commonRepo.WithByID(appInstall.ID))
|
2022-12-08 16:00:59 +08:00
|
|
|
}
|
2022-12-12 19:04:45 +08:00
|
|
|
|
|
|
|
ComposeFile := fmt.Sprintf("%s/%s/%s/docker-compose.yml", constant.AppInstallDir, appKey, appInstall.Name)
|
|
|
|
stdout, err := compose.Down(ComposeFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(stdout)
|
|
|
|
}
|
|
|
|
stdout, err = compose.Up(ComposeFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(stdout)
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-08 16:00:59 +08:00
|
|
|
}
|