2022-09-22 16:16:04 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-21 15:54:34 +08:00
|
|
|
"context"
|
2022-09-22 16:16:04 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2023-02-17 15:13:55 +08:00
|
|
|
"fmt"
|
2023-09-16 14:36:15 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-12-12 10:36:12 +08:00
|
|
|
"path/filepath"
|
2023-09-16 14:36:15 +08:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-12-14 15:08:21 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2023-05-18 16:48:19 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-05-31 14:51:01 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-05-18 16:48:19 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-06-01 17:05:14 +08:00
|
|
|
http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
|
2024-05-29 11:05:09 +08:00
|
|
|
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
|
2024-06-05 23:46:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
|
2022-09-29 18:16:56 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-09-22 16:16:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppService struct {
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:38:09 +08:00
|
|
|
type IAppService interface {
|
|
|
|
PageApp(req request.AppSearch) (interface{}, error)
|
|
|
|
GetAppTags() ([]response.TagDTO, error)
|
|
|
|
GetApp(key string) (*response.AppDTO, error)
|
2023-03-30 16:47:47 +08:00
|
|
|
GetAppDetail(appId uint, version, appType string) (response.AppDetailDTO, error)
|
2023-02-13 15:38:09 +08:00
|
|
|
Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error)
|
2023-04-08 14:02:14 +08:00
|
|
|
SyncAppListFromRemote() error
|
2023-03-14 13:54:42 +08:00
|
|
|
GetAppUpdate() (*response.AppUpdateRes, error)
|
2023-04-03 17:47:23 +08:00
|
|
|
GetAppDetailByID(id uint) (*response.AppDetailDTO, error)
|
2023-04-08 14:02:14 +08:00
|
|
|
SyncAppListFromLocal()
|
2023-07-06 18:48:22 +08:00
|
|
|
GetIgnoredApp() ([]response.IgnoredApp, error)
|
2023-02-13 15:38:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAppService() IAppService {
|
|
|
|
return &AppService{}
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:08:21 +08:00
|
|
|
func (a AppService) PageApp(req request.AppSearch) (interface{}, error) {
|
2022-09-22 16:16:04 +08:00
|
|
|
var opts []repo.DBOption
|
2023-02-08 16:21:17 +08:00
|
|
|
opts = append(opts, appRepo.OrderByRecommend())
|
2022-09-22 16:16:04 +08:00
|
|
|
if req.Name != "" {
|
2024-01-03 13:26:29 +08:00
|
|
|
opts = append(opts, appRepo.WithLikeName(req.Name))
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-11-02 15:19:14 +08:00
|
|
|
if req.Type != "" {
|
|
|
|
opts = append(opts, appRepo.WithType(req.Type))
|
|
|
|
}
|
2023-02-08 16:21:17 +08:00
|
|
|
if req.Recommend {
|
|
|
|
opts = append(opts, appRepo.GetRecommend())
|
|
|
|
}
|
2023-12-04 17:00:10 +08:00
|
|
|
if req.Resource != "" && req.Resource != "all" {
|
2023-09-26 22:50:16 +08:00
|
|
|
opts = append(opts, appRepo.WithResource(req.Resource))
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
if len(req.Tags) != 0 {
|
|
|
|
tags, err := tagRepo.GetByKeys(req.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tagIds []uint
|
|
|
|
for _, t := range tags {
|
|
|
|
tagIds = append(tagIds, t.ID)
|
|
|
|
}
|
|
|
|
appTags, err := appTagRepo.GetByTagIds(tagIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var appIds []uint
|
|
|
|
for _, t := range appTags {
|
|
|
|
appIds = append(appIds, t.AppId)
|
|
|
|
}
|
|
|
|
opts = append(opts, commonRepo.WithIdsIn(appIds))
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-12-14 15:08:21 +08:00
|
|
|
var res response.AppRes
|
2022-09-22 16:16:04 +08:00
|
|
|
total, apps, err := appRepo.Page(req.Page, req.PageSize, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-03 15:41:45 +08:00
|
|
|
var appDTOs []*response.AppDto
|
2023-07-10 18:49:08 +08:00
|
|
|
for _, ap := range apps {
|
|
|
|
ap.ReadMe = ""
|
|
|
|
ap.Website = ""
|
|
|
|
ap.Document = ""
|
|
|
|
ap.Github = ""
|
2024-06-03 15:41:45 +08:00
|
|
|
appDTO := &response.AppDto{
|
|
|
|
ID: ap.ID,
|
|
|
|
Name: ap.Name,
|
|
|
|
Key: ap.Key,
|
|
|
|
Type: ap.Type,
|
|
|
|
Icon: ap.Icon,
|
|
|
|
ShortDescZh: ap.ShortDescZh,
|
|
|
|
ShortDescEn: ap.ShortDescEn,
|
|
|
|
Resource: ap.Resource,
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
appDTOs = append(appDTOs, appDTO)
|
2023-07-10 18:49:08 +08:00
|
|
|
appTags, err := appTagRepo.GetByAppId(ap.ID)
|
2022-09-22 16:16:04 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var tagIds []uint
|
|
|
|
for _, at := range appTags {
|
|
|
|
tagIds = append(tagIds, at.TagId)
|
|
|
|
}
|
|
|
|
tags, err := tagRepo.GetByIds(tagIds)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
appDTO.Tags = tags
|
2023-07-10 18:49:08 +08:00
|
|
|
installs, _ := appInstallRepo.ListBy(appInstallRepo.WithAppId(ap.ID))
|
2023-05-22 21:39:42 +08:00
|
|
|
appDTO.Installed = len(installs) > 0
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
res.Items = appDTOs
|
|
|
|
res.Total = total
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
func (a AppService) GetAppTags() ([]response.TagDTO, error) {
|
|
|
|
tags, err := tagRepo.All()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res []response.TagDTO
|
|
|
|
for _, tag := range tags {
|
|
|
|
res = append(res, response.TagDTO{
|
|
|
|
Tag: tag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 17:09:21 +08:00
|
|
|
func (a AppService) GetApp(key string) (*response.AppDTO, error) {
|
2022-12-14 15:08:21 +08:00
|
|
|
var appDTO response.AppDTO
|
2023-02-08 17:09:21 +08:00
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(key))
|
2022-09-23 16:33:55 +08:00
|
|
|
if err != nil {
|
2023-01-16 15:30:24 +08:00
|
|
|
return nil, err
|
2022-09-23 16:33:55 +08:00
|
|
|
}
|
|
|
|
appDTO.App = app
|
2022-10-03 17:35:39 +08:00
|
|
|
details, err := appDetailRepo.GetBy(appDetailRepo.WithAppId(app.ID))
|
2022-09-23 16:33:55 +08:00
|
|
|
if err != nil {
|
2023-01-16 15:30:24 +08:00
|
|
|
return nil, err
|
2022-09-23 16:33:55 +08:00
|
|
|
}
|
|
|
|
var versionsRaw []string
|
|
|
|
for _, detail := range details {
|
|
|
|
versionsRaw = append(versionsRaw, detail.Version)
|
|
|
|
}
|
2022-10-03 17:35:39 +08:00
|
|
|
appDTO.Versions = common.GetSortedVersions(versionsRaw)
|
2022-09-23 16:33:55 +08:00
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
return &appDTO, nil
|
2022-09-23 16:33:55 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 22:50:16 +08:00
|
|
|
func (a AppService) GetAppDetail(appID uint, version, appType string) (response.AppDetailDTO, error) {
|
2022-09-23 16:33:55 +08:00
|
|
|
var (
|
2022-12-14 15:08:21 +08:00
|
|
|
appDetailDTO response.AppDetailDTO
|
2022-09-30 17:56:06 +08:00
|
|
|
opts []repo.DBOption
|
2022-09-23 16:33:55 +08:00
|
|
|
)
|
2023-09-26 22:50:16 +08:00
|
|
|
opts = append(opts, appDetailRepo.WithAppId(appID), appDetailRepo.WithVersion(version))
|
2022-10-03 17:35:39 +08:00
|
|
|
detail, err := appDetailRepo.GetFirst(opts...)
|
2022-09-23 16:33:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
appDetailDTO.AppDetail = detail
|
2022-12-01 16:45:00 +08:00
|
|
|
appDetailDTO.Enable = true
|
|
|
|
|
2023-03-30 16:47:47 +08:00
|
|
|
if appType == "runtime" {
|
2023-09-26 22:50:16 +08:00
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appID))
|
2023-03-30 16:47:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
fileOp := files.NewFileOp()
|
2023-12-12 10:36:12 +08:00
|
|
|
|
|
|
|
versionPath := filepath.Join(app.GetAppResourcePath(), detail.Version)
|
2023-06-29 15:02:11 +08:00
|
|
|
if !fileOp.Stat(versionPath) || detail.Update {
|
2023-06-07 16:09:25 +08:00
|
|
|
if err = downloadApp(app, detail, nil); err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 17:50:14 +08:00
|
|
|
switch app.Type {
|
|
|
|
case constant.RuntimePHP:
|
2023-12-12 10:36:12 +08:00
|
|
|
buildPath := filepath.Join(versionPath, "build")
|
|
|
|
paramsPath := filepath.Join(buildPath, "config.json")
|
2023-09-25 17:50:14 +08:00
|
|
|
if !fileOp.Stat(paramsPath) {
|
|
|
|
return appDetailDTO, buserr.New(constant.ErrFileNotExist)
|
|
|
|
}
|
|
|
|
param, err := fileOp.GetContent(paramsPath)
|
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
paramMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(param, ¶mMap); err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
appDetailDTO.Params = paramMap
|
2023-12-12 10:36:12 +08:00
|
|
|
composePath := filepath.Join(buildPath, "docker-compose.yml")
|
2023-09-25 17:50:14 +08:00
|
|
|
if !fileOp.Stat(composePath) {
|
|
|
|
return appDetailDTO, buserr.New(constant.ErrFileNotExist)
|
|
|
|
}
|
|
|
|
compose, err := fileOp.GetContent(composePath)
|
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
composeMap := make(map[string]interface{})
|
|
|
|
if err := yaml.Unmarshal(compose, &composeMap); err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
if service, ok := composeMap["services"]; ok {
|
|
|
|
servicesMap := service.(map[string]interface{})
|
|
|
|
for k := range servicesMap {
|
|
|
|
appDetailDTO.Image = k
|
|
|
|
}
|
2023-04-02 16:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 16:47:47 +08:00
|
|
|
} else {
|
|
|
|
paramMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(detail.Params), ¶mMap); err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
appDetailDTO.Params = paramMap
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:18:12 +08:00
|
|
|
if appDetailDTO.DockerCompose == "" {
|
2023-12-12 10:36:12 +08:00
|
|
|
filename := filepath.Base(appDetailDTO.DownloadUrl)
|
2023-11-03 18:18:12 +08:00
|
|
|
dockerComposeUrl := fmt.Sprintf("%s%s", strings.TrimSuffix(appDetailDTO.DownloadUrl, filename), "docker-compose.yml")
|
2024-05-29 11:05:09 +08:00
|
|
|
statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet)
|
2023-11-03 18:18:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, buserr.WithDetail("ErrGetCompose", err.Error(), err)
|
|
|
|
}
|
2024-05-29 11:05:09 +08:00
|
|
|
if statusCode > 200 {
|
|
|
|
return appDetailDTO, buserr.WithDetail("ErrGetCompose", string(composeRes), err)
|
2023-11-03 18:18:12 +08:00
|
|
|
}
|
2024-05-29 11:05:09 +08:00
|
|
|
detail.DockerCompose = string(composeRes)
|
2023-11-03 18:18:12 +08:00
|
|
|
_ = appDetailRepo.Update(context.Background(), detail)
|
2024-05-29 11:05:09 +08:00
|
|
|
appDetailDTO.DockerCompose = string(composeRes)
|
2023-11-03 18:18:12 +08:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:39:38 +08:00
|
|
|
appDetailDTO.HostMode = isHostModel(appDetailDTO.DockerCompose)
|
|
|
|
|
2022-12-01 16:45:00 +08:00
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(detail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
if err := checkLimit(app); err != nil {
|
|
|
|
appDetailDTO.Enable = false
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
return appDetailDTO, nil
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
func (a AppService) GetAppDetailByID(id uint) (*response.AppDetailDTO, error) {
|
|
|
|
res := &response.AppDetailDTO{}
|
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.AppDetail = appDetail
|
|
|
|
paramMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(appDetail.Params), ¶mMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Params = paramMap
|
2023-11-03 15:39:38 +08:00
|
|
|
res.HostMode = isHostModel(appDetail.DockerCompose)
|
2023-04-03 17:47:23 +08:00
|
|
|
return res, nil
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
|
2023-07-06 18:48:22 +08:00
|
|
|
func (a AppService) GetIgnoredApp() ([]response.IgnoredApp, error) {
|
|
|
|
var res []response.IgnoredApp
|
|
|
|
details, _ := appDetailRepo.GetBy(appDetailRepo.WithIgnored())
|
|
|
|
if len(details) == 0 {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
for _, detail := range details {
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(detail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, response.IgnoredApp{
|
|
|
|
Name: app.Name,
|
|
|
|
Version: detail.Version,
|
|
|
|
DetailID: detail.ID,
|
|
|
|
Icon: app.Icon,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (appInstall *model.AppInstall, err error) {
|
|
|
|
if err = docker.CreateDefaultDockerNetwork(); err != nil {
|
|
|
|
err = buserr.WithDetail(constant.Err1PanelNetworkFailed, err.Error(), nil)
|
|
|
|
return
|
2023-03-22 18:22:29 +08:00
|
|
|
}
|
2023-02-07 16:29:54 +08:00
|
|
|
if list, _ := appInstallRepo.ListBy(commonRepo.WithByName(req.Name)); len(list) > 0 {
|
2023-10-10 19:26:32 +08:00
|
|
|
err = buserr.New(constant.ErrAppNameExist)
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-12-02 10:31:07 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
var (
|
|
|
|
httpPort int
|
|
|
|
httpsPort int
|
|
|
|
appDetail model.AppDetail
|
|
|
|
app model.App
|
|
|
|
)
|
2023-09-07 11:38:10 +08:00
|
|
|
appDetail, err = appDetailRepo.GetFirst(commonRepo.WithByID(req.AppDetailId))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app, err = appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-16 14:36:15 +08:00
|
|
|
if DatabaseKeys[app.Key] > 0 {
|
|
|
|
if existDatabases, _ := databaseRepo.GetList(commonRepo.WithByName(req.Name)); len(existDatabases) > 0 {
|
|
|
|
err = buserr.New(constant.ErrRemoteExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-07-07 23:15:10 +08:00
|
|
|
for key := range req.Params {
|
2023-07-10 14:47:08 +08:00
|
|
|
if !strings.Contains(key, "PANEL_APP_PORT") {
|
2023-07-07 23:15:10 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var port int
|
|
|
|
if port, err = checkPort(key, req.Params); err == nil {
|
|
|
|
if key == "PANEL_APP_PORT_HTTP" {
|
|
|
|
httpPort = port
|
|
|
|
}
|
|
|
|
if key == "PANEL_APP_PORT_HTTPS" {
|
|
|
|
httpsPort = port
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 16:57:23 +08:00
|
|
|
}
|
2023-07-07 23:15:10 +08:00
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
if err = checkRequiredAndLimit(app); err != nil {
|
|
|
|
return
|
2022-10-10 22:56:42 +08:00
|
|
|
}
|
2023-02-22 13:56:03 +08:00
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
appInstall = &model.AppInstall{
|
2022-12-21 15:54:34 +08:00
|
|
|
Name: req.Name,
|
2022-09-29 18:16:56 +08:00
|
|
|
AppId: appDetail.AppId,
|
|
|
|
AppDetailId: appDetail.ID,
|
|
|
|
Version: appDetail.Version,
|
|
|
|
Status: constant.Installing,
|
2022-10-11 16:27:58 +08:00
|
|
|
HttpPort: httpPort,
|
|
|
|
HttpsPort: httpsPort,
|
|
|
|
App: app,
|
2022-10-09 23:35:24 +08:00
|
|
|
}
|
2022-09-29 18:16:56 +08:00
|
|
|
composeMap := make(map[string]interface{})
|
2023-05-22 19:19:40 +08:00
|
|
|
if req.EditCompose {
|
|
|
|
if err = yaml.Unmarshal([]byte(req.DockerCompose), &composeMap); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = yaml.Unmarshal([]byte(appDetail.DockerCompose), &composeMap); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
2023-02-22 13:56:03 +08:00
|
|
|
|
|
|
|
value, ok := composeMap["services"]
|
|
|
|
if !ok {
|
2023-05-15 22:40:05 +08:00
|
|
|
err = buserr.New(constant.ErrFileParse)
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2023-02-22 13:56:03 +08:00
|
|
|
}
|
|
|
|
servicesMap := value.(map[string]interface{})
|
2023-05-23 10:31:45 +08:00
|
|
|
containerName := constant.ContainerPrefix + app.Key + "-" + common.RandStr(4)
|
|
|
|
if req.Advanced && req.ContainerName != "" {
|
|
|
|
containerName = req.ContainerName
|
2023-05-31 15:39:04 +08:00
|
|
|
appInstalls, _ := appInstallRepo.ListBy(appInstallRepo.WithContainerName(containerName))
|
|
|
|
if len(appInstalls) > 0 {
|
|
|
|
err = buserr.New(constant.ErrContainerName)
|
|
|
|
return
|
|
|
|
}
|
2023-06-05 14:59:26 +08:00
|
|
|
containerExist := false
|
|
|
|
containerExist, err = checkContainerNameIsExist(req.ContainerName, appInstall.GetPath())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if containerExist {
|
|
|
|
err = buserr.New(constant.ErrContainerName)
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 10:31:45 +08:00
|
|
|
}
|
|
|
|
req.Params[constant.ContainerName] = containerName
|
|
|
|
appInstall.ContainerName = containerName
|
|
|
|
|
2023-02-22 13:56:03 +08:00
|
|
|
index := 0
|
2023-08-29 22:22:18 +08:00
|
|
|
serviceName := ""
|
2023-02-22 13:56:03 +08:00
|
|
|
for k := range servicesMap {
|
2023-08-29 22:22:18 +08:00
|
|
|
serviceName = k
|
2023-02-22 13:56:03 +08:00
|
|
|
if index > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
index++
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
2024-02-05 15:00:13 +08:00
|
|
|
if app.Limit == 0 && appInstall.Name != serviceName && len(servicesMap) == 1 {
|
2023-08-29 22:22:18 +08:00
|
|
|
servicesMap[appInstall.Name] = servicesMap[serviceName]
|
|
|
|
delete(servicesMap, serviceName)
|
|
|
|
serviceName = appInstall.Name
|
|
|
|
}
|
|
|
|
appInstall.ServiceName = serviceName
|
2023-05-17 13:46:28 +08:00
|
|
|
|
2023-05-18 16:48:19 +08:00
|
|
|
if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil {
|
|
|
|
return
|
2023-05-17 13:46:28 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
var (
|
|
|
|
composeByte []byte
|
|
|
|
paramByte []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
composeByte, err = yaml.Marshal(composeMap)
|
2022-09-29 18:16:56 +08:00
|
|
|
if err != nil {
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
appInstall.DockerCompose = string(composeByte)
|
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
hErr := handleAppInstallErr(ctx, appInstall)
|
|
|
|
if hErr != nil {
|
|
|
|
global.LOG.Errorf("delete app dir error %s", hErr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-09-16 14:36:15 +08:00
|
|
|
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
|
2023-08-31 15:14:11 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
paramByte, err = json.Marshal(req.Params)
|
2023-04-07 09:16:09 +08:00
|
|
|
if err != nil {
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2023-04-07 09:16:09 +08:00
|
|
|
}
|
|
|
|
appInstall.Env = string(paramByte)
|
2024-06-03 15:41:45 +08:00
|
|
|
|
|
|
|
containerNames, err := getContainerNames(*appInstall)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(containerNames) > 0 {
|
|
|
|
appInstall.ContainerName = strings.Join(containerNames, ",")
|
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
if err = appInstallRepo.Create(ctx, appInstall); err != nil {
|
|
|
|
return
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
if err = createLink(ctx, app, appInstall, req.Params); err != nil {
|
|
|
|
return
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
go func() {
|
2023-08-24 18:20:15 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2024-06-05 14:18:08 +08:00
|
|
|
appInstall.Status = constant.UpErr
|
2023-05-16 17:31:47 +08:00
|
|
|
appInstall.Message = err.Error()
|
2023-08-24 18:20:15 +08:00
|
|
|
_ = appInstallRepo.Save(context.Background(), appInstall)
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
2023-08-24 18:20:15 +08:00
|
|
|
}()
|
|
|
|
if err = copyData(app, appDetail, appInstall, req); err != nil {
|
2023-05-15 22:40:05 +08:00
|
|
|
return
|
|
|
|
}
|
2023-08-23 22:48:14 +08:00
|
|
|
if err = runScript(appInstall, "init"); err != nil {
|
2023-05-30 14:46:57 +08:00
|
|
|
return
|
|
|
|
}
|
2024-01-30 16:11:41 +08:00
|
|
|
upApp(appInstall, req.PullImage)
|
2023-05-15 22:40:05 +08:00
|
|
|
}()
|
2022-12-09 16:03:00 +08:00
|
|
|
go updateToolApp(appInstall)
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 14:02:14 +08:00
|
|
|
func (a AppService) SyncAppListFromLocal() {
|
2023-05-16 17:31:47 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
localAppDir := constant.LocalAppResourceDir
|
|
|
|
if !fileOp.Stat(localAppDir) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
dirEntries []os.DirEntry
|
|
|
|
localApps []model.App
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Errorf("Sync local app failed %v", err)
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("Starting local application synchronization ...")
|
2023-05-16 17:31:47 +08:00
|
|
|
dirEntries, err = os.ReadDir(localAppDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
if dirEntry.IsDir() {
|
2023-12-12 10:36:12 +08:00
|
|
|
appDir := filepath.Join(localAppDir, dirEntry.Name())
|
2023-05-16 17:31:47 +08:00
|
|
|
appDirEntries, err := os.ReadDir(appDir)
|
2023-06-02 10:26:27 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf(i18n.GetMsgWithMap("ErrAppDirNull", map[string]interface{}{"name": dirEntry.Name(), "err": err.Error()}))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
app, err := handleLocalApp(appDir)
|
2023-05-16 17:31:47 +08:00
|
|
|
if err != nil {
|
2023-06-01 17:05:14 +08:00
|
|
|
global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppErr", map[string]interface{}{"name": dirEntry.Name(), "err": err.Error()}))
|
2023-05-16 17:31:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var appDetails []model.AppDetail
|
|
|
|
for _, appDirEntry := range appDirEntries {
|
|
|
|
if appDirEntry.IsDir() {
|
|
|
|
appDetail := model.AppDetail{
|
|
|
|
Version: appDirEntry.Name(),
|
|
|
|
Status: constant.AppNormal,
|
|
|
|
}
|
2023-12-12 10:36:12 +08:00
|
|
|
versionDir := filepath.Join(appDir, appDirEntry.Name())
|
2023-05-31 14:51:01 +08:00
|
|
|
if err = handleLocalAppDetail(versionDir, &appDetail); err != nil {
|
|
|
|
global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppVersionErr", map[string]interface{}{"name": app.Name, "version": appDetail.Version, "err": err.Error()}))
|
2023-05-16 17:31:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
appDetails = append(appDetails, appDetail)
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 14:51:01 +08:00
|
|
|
if len(appDetails) > 0 {
|
|
|
|
app.Details = appDetails
|
|
|
|
localApps = append(localApps, *app)
|
|
|
|
} else {
|
|
|
|
global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppVersionNull", map[string]interface{}{"name": app.Name}))
|
|
|
|
}
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
newApps []model.App
|
|
|
|
deleteApps []model.App
|
|
|
|
updateApps []model.App
|
|
|
|
oldAppIds []uint
|
|
|
|
|
|
|
|
deleteAppIds []uint
|
|
|
|
deleteAppDetails []model.AppDetail
|
|
|
|
newAppDetails []model.AppDetail
|
|
|
|
updateDetails []model.AppDetail
|
|
|
|
|
|
|
|
appTags []*model.AppTag
|
|
|
|
)
|
|
|
|
|
|
|
|
oldApps, _ := appRepo.GetBy(appRepo.WithResource(constant.AppResourceLocal))
|
|
|
|
apps := make(map[string]model.App, len(oldApps))
|
|
|
|
for _, old := range oldApps {
|
|
|
|
old.Status = constant.AppTakeDown
|
|
|
|
apps[old.Key] = old
|
|
|
|
}
|
|
|
|
for _, app := range localApps {
|
|
|
|
if oldApp, ok := apps[app.Key]; ok {
|
|
|
|
app.ID = oldApp.ID
|
|
|
|
appDetails := make(map[string]model.AppDetail, len(oldApp.Details))
|
|
|
|
for _, old := range oldApp.Details {
|
|
|
|
old.Status = constant.AppTakeDown
|
|
|
|
appDetails[old.Version] = old
|
|
|
|
}
|
|
|
|
for i, newDetail := range app.Details {
|
|
|
|
version := newDetail.Version
|
|
|
|
newDetail.Status = constant.AppNormal
|
|
|
|
newDetail.AppId = app.ID
|
|
|
|
oldDetail, exist := appDetails[version]
|
|
|
|
if exist {
|
|
|
|
newDetail.ID = oldDetail.ID
|
2023-07-03 12:30:14 +08:00
|
|
|
delete(appDetails, version)
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
|
|
|
app.Details[i] = newDetail
|
|
|
|
}
|
2023-07-03 12:30:14 +08:00
|
|
|
for _, v := range appDetails {
|
|
|
|
app.Details = append(app.Details, v)
|
|
|
|
}
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
|
|
|
app.TagsKey = append(app.TagsKey, constant.AppResourceLocal)
|
|
|
|
apps[app.Key] = app
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, app := range apps {
|
|
|
|
if app.ID == 0 {
|
|
|
|
newApps = append(newApps, app)
|
|
|
|
} else {
|
|
|
|
oldAppIds = append(oldAppIds, app.ID)
|
|
|
|
if app.Status == constant.AppTakeDown {
|
|
|
|
installs, _ := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID))
|
|
|
|
if len(installs) > 0 {
|
2023-06-02 14:25:19 +08:00
|
|
|
updateApps = append(updateApps, app)
|
2023-05-16 17:31:47 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
deleteAppIds = append(deleteAppIds, app.ID)
|
|
|
|
deleteApps = append(deleteApps, app)
|
|
|
|
deleteAppDetails = append(deleteAppDetails, app.Details...)
|
|
|
|
} else {
|
|
|
|
updateApps = append(updateApps, app)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, _ := tagRepo.All()
|
|
|
|
tagMap := make(map[string]uint, len(tags))
|
|
|
|
for _, tag := range tags {
|
|
|
|
tagMap[tag.Key] = tag.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, ctx := getTxAndContext()
|
|
|
|
defer tx.Rollback()
|
|
|
|
if len(newApps) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appRepo.BatchCreate(ctx, newApps); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, update := range updateApps {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appRepo.Save(ctx, &update); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(deleteApps) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appRepo.BatchDelete(ctx, deleteApps); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appDetailRepo.DeleteByAppIds(ctx, deleteAppIds); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appTagRepo.DeleteByAppIds(ctx, oldAppIds); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, newApp := range newApps {
|
|
|
|
if newApp.ID > 0 {
|
|
|
|
for _, detail := range newApp.Details {
|
|
|
|
detail.AppId = newApp.ID
|
|
|
|
newAppDetails = append(newAppDetails, detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, update := range updateApps {
|
|
|
|
for _, detail := range update.Details {
|
|
|
|
if detail.ID == 0 {
|
|
|
|
detail.AppId = update.ID
|
|
|
|
newAppDetails = append(newAppDetails, detail)
|
|
|
|
} else {
|
|
|
|
if detail.Status == constant.AppNormal {
|
|
|
|
updateDetails = append(updateDetails, detail)
|
|
|
|
} else {
|
|
|
|
deleteAppDetails = append(deleteAppDetails, detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allApps := append(newApps, updateApps...)
|
|
|
|
for _, app := range allApps {
|
|
|
|
for _, t := range app.TagsKey {
|
|
|
|
tagId, ok := tagMap[t]
|
|
|
|
if ok {
|
|
|
|
appTags = append(appTags, &model.AppTag{
|
|
|
|
AppId: app.ID,
|
|
|
|
TagId: tagId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newAppDetails) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appDetailRepo.BatchCreate(ctx, newAppDetails); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, updateAppDetail := range updateDetails {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appDetailRepo.Update(ctx, updateAppDetail); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(deleteAppDetails) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appDetailRepo.BatchDelete(ctx, deleteAppDetails); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(oldAppIds) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appTagRepo.DeleteByAppIds(ctx, oldAppIds); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(appTags) > 0 {
|
2023-06-02 10:26:27 +08:00
|
|
|
if err = appTagRepo.BatchCreate(ctx, appTags); err != nil {
|
2023-05-16 17:31:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("Synchronization of local applications completed")
|
2023-04-08 14:02:14 +08:00
|
|
|
}
|
2023-05-25 14:58:16 +08:00
|
|
|
|
|
|
|
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
|
|
|
|
res := &response.AppUpdateRes{
|
|
|
|
CanUpdate: false,
|
|
|
|
}
|
2023-10-09 15:46:27 +08:00
|
|
|
|
2023-05-25 14:58:16 +08:00
|
|
|
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode)
|
2024-06-05 23:46:11 +08:00
|
|
|
_, versionRes, err := http2.HandleGet(versionUrl, http.MethodGet)
|
2023-05-25 14:58:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-05 23:46:11 +08:00
|
|
|
lastModifiedStr := string(versionRes)
|
2023-05-25 14:58:16 +08:00
|
|
|
lastModified, err := strconv.Atoi(lastModifiedStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-09 15:46:27 +08:00
|
|
|
setting, err := NewISettingService().GetSettingInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-03 18:18:12 +08:00
|
|
|
if setting.AppStoreSyncStatus == constant.Syncing {
|
|
|
|
res.IsSyncing = true
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:58:16 +08:00
|
|
|
appStoreLastModified, _ := strconv.Atoi(setting.AppStoreLastModified)
|
2023-08-03 18:37:57 +08:00
|
|
|
res.AppStoreLastModified = appStoreLastModified
|
2023-05-25 14:58:16 +08:00
|
|
|
if setting.AppStoreLastModified == "" || lastModified != appStoreLastModified {
|
|
|
|
res.CanUpdate = true
|
|
|
|
return res, err
|
|
|
|
}
|
2024-04-28 22:10:44 +08:00
|
|
|
apps, _ := appRepo.GetBy(appRepo.WithResource(constant.AppResourceRemote))
|
|
|
|
for _, app := range apps {
|
|
|
|
if app.Icon == "" {
|
|
|
|
res.CanUpdate = true
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:36:32 +08:00
|
|
|
list, err := getAppList()
|
2023-10-09 15:46:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
2023-08-25 18:44:11 +08:00
|
|
|
}
|
2023-08-28 15:32:12 +08:00
|
|
|
if list.Extra.Version != "" && setting.SystemVersion != list.Extra.Version && !common.CompareVersion(setting.SystemVersion, list.Extra.Version) {
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Errorf("The current version is too low to synchronize with the App Store. The minimum required version is %s", list.Extra.Version)
|
2023-08-25 18:44:11 +08:00
|
|
|
return nil, buserr.New("ErrVersionTooLow")
|
|
|
|
}
|
2023-10-09 15:46:27 +08:00
|
|
|
res.AppList = list
|
2023-05-25 14:58:16 +08:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAppFromRepo(downloadPath string) error {
|
|
|
|
downloadUrl := downloadPath
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("[AppStore] download file from %s", downloadUrl)
|
2023-05-25 14:58:16 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2023-12-12 10:36:12 +08:00
|
|
|
packagePath := filepath.Join(constant.ResourceDir, filepath.Base(downloadUrl))
|
2024-06-05 16:04:10 +08:00
|
|
|
if err := fileOp.DownloadFileWithProxy(downloadUrl, packagePath); err != nil {
|
2023-05-25 14:58:16 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := fileOp.Decompress(packagePath, constant.ResourceDir, files.SdkZip, ""); err != nil {
|
2023-05-25 14:58:16 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = fileOp.DeleteFile(packagePath)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 15:46:27 +08:00
|
|
|
func getAppList() (*dto.AppList, error) {
|
|
|
|
list := &dto.AppList{}
|
|
|
|
if err := getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-12 10:36:12 +08:00
|
|
|
listFile := filepath.Join(constant.ResourceDir, "1panel.json")
|
2023-10-09 15:46:27 +08:00
|
|
|
content, err := os.ReadFile(listFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(content, list); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:18:12 +08:00
|
|
|
var InitTypes = map[string]struct{}{
|
|
|
|
"runtime": {},
|
|
|
|
"php": {},
|
|
|
|
"node": {},
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:37:57 +08:00
|
|
|
func (a AppService) SyncAppListFromRemote() (err error) {
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("Starting synchronization with App Store...")
|
2023-02-14 11:17:02 +08:00
|
|
|
updateRes, err := a.GetAppUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !updateRes.CanUpdate {
|
2023-11-03 18:18:12 +08:00
|
|
|
if updateRes.IsSyncing {
|
|
|
|
global.LOG.Infof("AppStore is Syncing!")
|
|
|
|
return
|
|
|
|
}
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("The App Store is at the latest version")
|
2023-08-03 18:37:57 +08:00
|
|
|
return
|
2023-02-14 11:17:02 +08:00
|
|
|
}
|
2023-11-03 18:18:12 +08:00
|
|
|
|
2023-05-25 14:58:16 +08:00
|
|
|
list := &dto.AppList{}
|
2023-10-09 15:46:27 +08:00
|
|
|
if updateRes.AppList == nil {
|
|
|
|
list, err = getAppList()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list = updateRes.AppList
|
2023-08-03 18:37:57 +08:00
|
|
|
}
|
2023-11-03 18:18:12 +08:00
|
|
|
settingService := NewISettingService()
|
|
|
|
_ = settingService.Update("AppStoreSyncStatus", constant.Syncing)
|
2023-08-03 18:37:57 +08:00
|
|
|
|
2022-09-22 16:16:04 +08:00
|
|
|
var (
|
2023-05-16 17:31:47 +08:00
|
|
|
tags []*model.Tag
|
|
|
|
appTags []*model.AppTag
|
|
|
|
oldAppIds []uint
|
2022-09-22 16:16:04 +08:00
|
|
|
)
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, t := range list.Extra.Tags {
|
2022-09-22 16:16:04 +08:00
|
|
|
tags = append(tags, &model.Tag{
|
|
|
|
Key: t.Key,
|
|
|
|
Name: t.Name,
|
2023-10-08 14:54:14 +08:00
|
|
|
Sort: t.Sort,
|
2022-09-22 16:16:04 +08:00
|
|
|
})
|
|
|
|
}
|
2023-04-08 14:02:14 +08:00
|
|
|
oldApps, err := appRepo.GetBy(appRepo.WithResource(constant.AppResourceRemote))
|
2022-09-30 17:56:06 +08:00
|
|
|
if err != nil {
|
2023-08-03 18:37:57 +08:00
|
|
|
return
|
2022-09-30 17:56:06 +08:00
|
|
|
}
|
2023-05-16 17:31:47 +08:00
|
|
|
for _, old := range oldApps {
|
|
|
|
oldAppIds = append(oldAppIds, old.ID)
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:16:14 +08:00
|
|
|
transport := xpack.LoadRequestTransport()
|
2023-05-15 22:40:05 +08:00
|
|
|
baseRemoteUrl := fmt.Sprintf("%s/%s/1panel", global.CONF.System.AppRepo, global.CONF.System.Mode)
|
2023-05-16 17:31:47 +08:00
|
|
|
appsMap := getApps(oldApps, list.Apps)
|
2023-11-03 18:18:12 +08:00
|
|
|
|
|
|
|
global.LOG.Infof("Starting synchronization of application details...")
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, l := range list.Apps {
|
|
|
|
app := appsMap[l.AppProperty.Key]
|
2024-06-05 23:46:11 +08:00
|
|
|
_, iconRes, err := httpUtil.HandleGetWithTransport(l.Icon, http.MethodGet, transport)
|
2023-05-15 22:40:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2024-04-28 22:10:44 +08:00
|
|
|
iconStr := ""
|
2024-05-29 11:05:09 +08:00
|
|
|
if !strings.Contains(string(iconRes), "<xml>") {
|
|
|
|
iconStr = base64.StdEncoding.EncodeToString(iconRes)
|
2024-04-28 22:10:44 +08:00
|
|
|
}
|
|
|
|
|
2022-09-30 17:56:06 +08:00
|
|
|
app.Icon = iconStr
|
2023-05-15 22:40:05 +08:00
|
|
|
app.TagsKey = l.AppProperty.Tags
|
|
|
|
if l.AppProperty.Recommend > 0 {
|
|
|
|
app.Recommend = l.AppProperty.Recommend
|
2023-02-08 16:21:17 +08:00
|
|
|
} else {
|
|
|
|
app.Recommend = 9999
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
app.ReadMe = l.ReadMe
|
|
|
|
app.LastModified = l.LastModified
|
2022-09-22 16:16:04 +08:00
|
|
|
versions := l.Versions
|
2022-09-30 17:56:06 +08:00
|
|
|
detailsMap := getAppDetails(app.Details, versions)
|
2022-09-22 16:16:04 +08:00
|
|
|
for _, v := range versions {
|
2023-05-15 22:40:05 +08:00
|
|
|
version := v.Name
|
|
|
|
detail := detailsMap[version]
|
2023-08-28 16:18:13 +08:00
|
|
|
versionUrl := fmt.Sprintf("%s/%s/%s", baseRemoteUrl, app.Key, version)
|
2023-11-03 18:18:12 +08:00
|
|
|
|
|
|
|
if _, ok := InitTypes[app.Type]; ok {
|
|
|
|
dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml")
|
2024-06-05 23:46:11 +08:00
|
|
|
_, composeRes, err := httpUtil.HandleGetWithTransport(dockerComposeUrl, http.MethodGet, transport)
|
2023-11-03 18:18:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-29 11:05:09 +08:00
|
|
|
detail.DockerCompose = string(composeRes)
|
2023-11-03 18:18:12 +08:00
|
|
|
} else {
|
|
|
|
detail.DockerCompose = ""
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
|
|
|
|
paramByte, _ := json.Marshal(v.AppForm)
|
|
|
|
detail.Params = string(paramByte)
|
2023-08-28 16:18:13 +08:00
|
|
|
detail.DownloadUrl = fmt.Sprintf("%s/%s", versionUrl, app.Key+"-"+version+".tar.gz")
|
2023-05-15 22:40:05 +08:00
|
|
|
detail.DownloadCallBackUrl = v.DownloadCallBackUrl
|
2023-06-29 15:02:11 +08:00
|
|
|
detail.Update = true
|
|
|
|
detail.LastModified = v.LastModified
|
2023-05-15 22:40:05 +08:00
|
|
|
detailsMap[version] = detail
|
2022-09-30 17:56:06 +08:00
|
|
|
}
|
|
|
|
var newDetails []model.AppDetail
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, detail := range detailsMap {
|
|
|
|
newDetails = append(newDetails, detail)
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
app.Details = newDetails
|
2023-05-15 22:40:05 +08:00
|
|
|
appsMap[l.AppProperty.Key] = app
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
|
2023-11-03 18:18:12 +08:00
|
|
|
global.LOG.Infof("Synchronization of application details Success")
|
|
|
|
|
2022-09-30 17:56:06 +08:00
|
|
|
var (
|
2023-05-15 22:40:05 +08:00
|
|
|
addAppArray []model.App
|
|
|
|
updateAppArray []model.App
|
|
|
|
deleteAppArray []model.App
|
|
|
|
deleteIds []uint
|
|
|
|
tagMap = make(map[string]uint, len(tags))
|
2022-09-30 17:56:06 +08:00
|
|
|
)
|
2023-04-08 14:02:14 +08:00
|
|
|
|
2022-09-30 17:56:06 +08:00
|
|
|
for _, v := range appsMap {
|
|
|
|
if v.ID == 0 {
|
|
|
|
addAppArray = append(addAppArray, v)
|
|
|
|
} else {
|
2023-05-15 22:40:05 +08:00
|
|
|
if v.Status == constant.AppTakeDown {
|
|
|
|
installs, _ := appInstallRepo.ListBy(appInstallRepo.WithAppId(v.ID))
|
|
|
|
if len(installs) > 0 {
|
|
|
|
updateAppArray = append(updateAppArray, v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deleteAppArray = append(deleteAppArray, v)
|
|
|
|
deleteIds = append(deleteIds, v.ID)
|
|
|
|
} else {
|
|
|
|
updateAppArray = append(updateAppArray, v)
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
tx, ctx := getTxAndContext()
|
2023-05-16 17:31:47 +08:00
|
|
|
defer tx.Rollback()
|
2022-09-30 17:56:06 +08:00
|
|
|
if len(addAppArray) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appRepo.BatchCreate(ctx, addAppArray); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
if len(deleteAppArray) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appRepo.BatchDelete(ctx, deleteAppArray); err != nil {
|
|
|
|
return
|
2023-05-15 22:40:05 +08:00
|
|
|
}
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appDetailRepo.DeleteByAppIds(ctx, deleteIds); err != nil {
|
|
|
|
return
|
2023-05-15 22:40:05 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = tagRepo.DeleteAll(ctx); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
if len(tags) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = tagRepo.BatchCreate(ctx, tags); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
for _, t := range tags {
|
|
|
|
tagMap[t.Key] = t.ID
|
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, update := range updateAppArray {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appRepo.Save(ctx, &update); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
apps := append(addAppArray, updateAppArray...)
|
2022-09-22 16:16:04 +08:00
|
|
|
|
|
|
|
var (
|
2022-09-30 17:56:06 +08:00
|
|
|
addDetails []model.AppDetail
|
|
|
|
updateDetails []model.AppDetail
|
2023-05-15 22:40:05 +08:00
|
|
|
deleteDetails []model.AppDetail
|
2022-09-22 16:16:04 +08:00
|
|
|
)
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, app := range apps {
|
|
|
|
for _, t := range app.TagsKey {
|
2022-09-22 16:16:04 +08:00
|
|
|
tagId, ok := tagMap[t]
|
|
|
|
if ok {
|
|
|
|
appTags = append(appTags, &model.AppTag{
|
2023-05-15 22:40:05 +08:00
|
|
|
AppId: app.ID,
|
2022-09-22 16:16:04 +08:00
|
|
|
TagId: tagId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
for _, d := range app.Details {
|
|
|
|
d.AppId = app.ID
|
2022-09-30 17:56:06 +08:00
|
|
|
if d.ID == 0 {
|
|
|
|
addDetails = append(addDetails, d)
|
|
|
|
} else {
|
2023-05-15 22:40:05 +08:00
|
|
|
if d.Status == constant.AppTakeDown {
|
2023-05-30 21:54:58 +08:00
|
|
|
runtime, _ := runtimeRepo.GetFirst(runtimeRepo.WithDetailId(d.ID))
|
2023-06-02 14:31:19 +08:00
|
|
|
if runtime != nil {
|
2023-06-03 13:19:23 +08:00
|
|
|
updateDetails = append(updateDetails, d)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
installs, _ := appInstallRepo.ListBy(appInstallRepo.WithDetailIdsIn([]uint{d.ID}))
|
|
|
|
if len(installs) > 0 {
|
|
|
|
updateDetails = append(updateDetails, d)
|
2023-05-30 21:54:58 +08:00
|
|
|
continue
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
deleteDetails = append(deleteDetails, d)
|
|
|
|
} else {
|
|
|
|
updateDetails = append(updateDetails, d)
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
if len(addDetails) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
|
|
|
|
return
|
2022-09-30 17:56:06 +08:00
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2023-05-22 17:10:51 +08:00
|
|
|
if len(deleteDetails) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appDetailRepo.BatchDelete(ctx, deleteDetails); err != nil {
|
|
|
|
return
|
2023-05-22 17:10:51 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
for _, u := range updateDetails {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appDetailRepo.Update(ctx, u); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
|
2023-05-16 17:31:47 +08:00
|
|
|
if len(oldAppIds) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appTagRepo.DeleteByAppIds(ctx, oldAppIds); err != nil {
|
|
|
|
return
|
2023-05-16 17:31:47 +08:00
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2023-05-16 17:31:47 +08:00
|
|
|
|
2022-09-22 16:16:04 +08:00
|
|
|
if len(appTags) > 0 {
|
2023-08-03 18:37:57 +08:00
|
|
|
if err = appTagRepo.BatchCreate(ctx, appTags); err != nil {
|
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
2023-11-03 18:18:12 +08:00
|
|
|
|
|
|
|
_ = settingService.Update("AppStoreSyncStatus", constant.SyncSuccess)
|
|
|
|
_ = settingService.Update("AppStoreLastModified", strconv.Itoa(list.LastModified))
|
|
|
|
|
2023-10-09 15:46:27 +08:00
|
|
|
global.LOG.Infof("Synchronization with the App Store was successful!")
|
2023-08-03 18:37:57 +08:00
|
|
|
return
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|