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-04-07 11:30:10 +08:00
|
|
|
"io"
|
2023-02-17 15:13:55 +08:00
|
|
|
"net/http"
|
2022-11-08 14:34:41 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2023-04-07 11:30:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
|
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"
|
|
|
|
"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/files"
|
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-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 != "" {
|
|
|
|
opts = append(opts, commonRepo.WithLikeName(req.Name))
|
|
|
|
}
|
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())
|
|
|
|
}
|
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
|
|
|
|
}
|
2022-12-14 15:08:21 +08:00
|
|
|
var appDTOs []*response.AppDTO
|
2022-09-22 16:16:04 +08:00
|
|
|
for _, a := range apps {
|
2022-12-14 15:08:21 +08:00
|
|
|
appDTO := &response.AppDTO{
|
2022-09-22 16:16:04 +08:00
|
|
|
App: a,
|
|
|
|
}
|
|
|
|
appDTOs = append(appDTOs, appDTO)
|
|
|
|
appTags, err := appTagRepo.GetByAppId(a.ID)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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-03-30 16:47:47 +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
|
|
|
)
|
|
|
|
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" {
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appId))
|
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
fileOp := files.NewFileOp()
|
2023-04-02 16:54:00 +08:00
|
|
|
buildPath := path.Join(constant.AppResourceDir, app.Key, "versions", detail.Version, "build")
|
|
|
|
paramsPath := path.Join(buildPath, "config.json")
|
2023-03-30 16:47:47 +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-04-02 16:54:00 +08:00
|
|
|
composePath := path.Join(buildPath, "docker-compose.yml")
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
|
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-04-11 14:38:27 +08:00
|
|
|
err = buserr.New(constant.ErrNameIsExist)
|
|
|
|
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
|
|
|
|
)
|
|
|
|
httpPort, err = checkPort("PANEL_APP_PORT_HTTP", req.Params)
|
2022-10-11 16:27:58 +08:00
|
|
|
if err != nil {
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
httpsPort, err = checkPort("PANEL_APP_PORT_HTTPS", req.Params)
|
2022-10-11 16:27:58 +08:00
|
|
|
if err != nil {
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-09-27 16:57:23 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
appDetail, err = appDetailRepo.GetFirst(commonRepo.WithByID(req.AppDetailId))
|
2022-09-26 16:32:40 +08:00
|
|
|
if err != nil {
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
app, err = appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
2022-10-07 15:49:39 +08:00
|
|
|
if err != nil {
|
2022-11-02 15:19:14 +08:00
|
|
|
return nil, err
|
2022-10-07 15:49:39 +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-04-11 14:38:27 +08:00
|
|
|
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-04-11 14:38:27 +08:00
|
|
|
err = buserr.New("")
|
|
|
|
return
|
2023-02-22 13:56:03 +08:00
|
|
|
}
|
|
|
|
servicesMap := value.(map[string]interface{})
|
2022-09-29 18:16:56 +08:00
|
|
|
changeKeys := make(map[string]string, len(servicesMap))
|
2023-02-22 13:56:03 +08:00
|
|
|
index := 0
|
|
|
|
for k := range servicesMap {
|
2022-09-29 18:16:56 +08:00
|
|
|
serviceName := k + "-" + common.RandStr(4)
|
|
|
|
changeKeys[k] = serviceName
|
|
|
|
containerName := constant.ContainerPrefix + k + "-" + common.RandStr(4)
|
2023-02-22 13:56:03 +08:00
|
|
|
if index > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req.Params["CONTAINER_NAME"] = containerName
|
2022-10-11 16:27:58 +08:00
|
|
|
appInstall.ServiceName = serviceName
|
|
|
|
appInstall.ContainerName = containerName
|
2023-02-22 13:56:03 +08:00
|
|
|
index++
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
|
|
|
for k, v := range changeKeys {
|
|
|
|
servicesMap[v] = servicesMap[k]
|
|
|
|
delete(servicesMap, k)
|
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = copyAppData(app.Key, appDetail.Version, req.Name, req.Params, app.Resource == constant.AppResourceLocal); err != nil {
|
|
|
|
return
|
2023-02-22 13:56:03 +08:00
|
|
|
}
|
2022-10-10 15:10:53 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2023-04-11 14:38:27 +08:00
|
|
|
if err = fileOp.WriteFile(appInstall.GetComposePath(), strings.NewReader(string(composeByte)), 0775); err != nil {
|
|
|
|
return
|
2022-09-29 18:16:56 +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)
|
2022-09-29 18:16:56 +08:00
|
|
|
|
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-04-11 14:38:27 +08:00
|
|
|
if err = upAppPre(app, appInstall); err != nil {
|
|
|
|
return
|
2023-03-31 12:16:14 +08:00
|
|
|
}
|
2023-04-11 14:38:27 +08:00
|
|
|
go upApp(appInstall)
|
2022-12-09 16:03:00 +08:00
|
|
|
go updateToolApp(appInstall)
|
2023-04-06 17:16:16 +08:00
|
|
|
ports := []int{appInstall.HttpPort}
|
|
|
|
if appInstall.HttpsPort > 0 {
|
|
|
|
ports = append(ports, appInstall.HttpsPort)
|
|
|
|
}
|
2023-04-07 11:30:10 +08:00
|
|
|
go func() {
|
|
|
|
_ = OperateFirewallPort(nil, ports)
|
|
|
|
}()
|
2023-04-11 14:38:27 +08:00
|
|
|
return
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
|
|
|
|
2023-02-14 11:17:02 +08:00
|
|
|
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
|
2023-02-17 15:13:55 +08:00
|
|
|
res := &response.AppUpdateRes{
|
|
|
|
CanUpdate: false,
|
|
|
|
}
|
2023-02-14 11:17:02 +08:00
|
|
|
setting, err := NewISettingService().GetSettingInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-27 17:22:14 +08:00
|
|
|
versionUrl := fmt.Sprintf("%s/%s/%s/appstore/apps.json", global.CONF.System.RepoUrl, global.CONF.System.Mode, setting.SystemVersion)
|
|
|
|
versionRes, err := http.Get(versionUrl)
|
2023-02-17 15:13:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer versionRes.Body.Close()
|
2023-04-07 11:30:10 +08:00
|
|
|
body, err := io.ReadAll(versionRes.Body)
|
2023-02-14 11:17:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list := &dto.AppList{}
|
2023-02-17 15:13:55 +08:00
|
|
|
if err = json.Unmarshal(body, list); err != nil {
|
2023-02-14 11:17:02 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Version = list.Version
|
2023-02-17 16:21:13 +08:00
|
|
|
if setting.AppStoreVersion == "" || common.CompareVersion(list.Version, setting.AppStoreVersion) {
|
2023-02-14 11:17:02 +08:00
|
|
|
res.CanUpdate = true
|
2023-02-17 15:13:55 +08:00
|
|
|
res.DownloadPath = fmt.Sprintf("%s/%s/%s/appstore/apps-%s.tar.gz", global.CONF.System.RepoUrl, global.CONF.System.Mode, setting.SystemVersion, list.Version)
|
2023-02-14 11:17:02 +08:00
|
|
|
return res, err
|
|
|
|
}
|
2023-02-17 16:21:13 +08:00
|
|
|
return res, nil
|
2023-02-14 11:17:02 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 14:02:14 +08:00
|
|
|
func (a AppService) SyncAppListFromLocal() {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
appDir := constant.LocalAppResourceDir
|
|
|
|
listFile := path.Join(appDir, "list.json")
|
|
|
|
if !fileOp.Stat(listFile) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
global.LOG.Infof("start sync local apps...")
|
|
|
|
content, err := fileOp.GetContent(listFile)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get list.json content failed %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
list := &dto.AppList{}
|
|
|
|
if err := json.Unmarshal(content, list); err != nil {
|
|
|
|
global.LOG.Errorf("unmarshal list.json failed %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oldApps, _ := appRepo.GetBy(appRepo.WithResource(constant.AppResourceLocal))
|
|
|
|
appsMap := getApps(oldApps, list.Items, true)
|
|
|
|
for _, l := range list.Items {
|
|
|
|
localKey := "local" + l.Key
|
|
|
|
app := appsMap[localKey]
|
|
|
|
icon, err := os.ReadFile(path.Join(appDir, l.Key, "metadata", "logo.png"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] icon error: %s", l.Name, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iconStr := base64.StdEncoding.EncodeToString(icon)
|
|
|
|
app.Icon = iconStr
|
|
|
|
app.TagsKey = append(l.Tags, "Local")
|
|
|
|
app.Recommend = 9999
|
|
|
|
versions := l.Versions
|
|
|
|
detailsMap := getAppDetails(app.Details, versions)
|
|
|
|
|
|
|
|
for _, v := range versions {
|
|
|
|
detail := detailsMap[v]
|
|
|
|
detailPath := path.Join(appDir, l.Key, "versions", v)
|
|
|
|
if _, err := os.Stat(detailPath); err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] folder error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
readmeStr, err := os.ReadFile(path.Join(detailPath, "README.md"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] README error: %s", detailPath, err.Error())
|
|
|
|
}
|
|
|
|
detail.Readme = string(readmeStr)
|
|
|
|
dockerComposeStr, err := os.ReadFile(path.Join(detailPath, "docker-compose.yml"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] docker-compose.yml error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
detail.DockerCompose = string(dockerComposeStr)
|
|
|
|
paramStr, err := os.ReadFile(path.Join(detailPath, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] form.json error: %s", detailPath, err.Error())
|
|
|
|
}
|
|
|
|
detail.Params = string(paramStr)
|
|
|
|
detailsMap[v] = detail
|
|
|
|
}
|
|
|
|
var newDetails []model.AppDetail
|
|
|
|
for _, v := range detailsMap {
|
|
|
|
newDetails = append(newDetails, v)
|
|
|
|
}
|
|
|
|
app.Details = newDetails
|
|
|
|
appsMap[localKey] = app
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
addAppArray []model.App
|
|
|
|
updateArray []model.App
|
|
|
|
appIds []uint
|
|
|
|
)
|
|
|
|
for _, v := range appsMap {
|
|
|
|
if v.ID == 0 {
|
|
|
|
addAppArray = append(addAppArray, v)
|
|
|
|
} else {
|
|
|
|
updateArray = append(updateArray, v)
|
|
|
|
appIds = append(appIds, v.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx, ctx := getTxAndContext()
|
|
|
|
if len(addAppArray) > 0 {
|
|
|
|
if err := appRepo.BatchCreate(ctx, addAppArray); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, update := range updateArray {
|
|
|
|
if err := appRepo.Save(ctx, &update); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := appTagRepo.DeleteByAppIds(ctx, appIds); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apps := append(addAppArray, updateArray...)
|
|
|
|
var (
|
|
|
|
addDetails []model.AppDetail
|
|
|
|
updateDetails []model.AppDetail
|
|
|
|
appTags []*model.AppTag
|
|
|
|
)
|
|
|
|
tags, _ := tagRepo.All()
|
|
|
|
tagMap := make(map[string]uint, len(tags))
|
|
|
|
for _, app := range tags {
|
|
|
|
tagMap[app.Key] = app.ID
|
|
|
|
}
|
|
|
|
for _, a := range apps {
|
|
|
|
for _, t := range a.TagsKey {
|
|
|
|
tagId, ok := tagMap[t]
|
|
|
|
if ok {
|
|
|
|
appTags = append(appTags, &model.AppTag{
|
|
|
|
AppId: a.ID,
|
|
|
|
TagId: tagId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range a.Details {
|
|
|
|
d.AppId = a.ID
|
|
|
|
if d.ID == 0 {
|
|
|
|
addDetails = append(addDetails, d)
|
|
|
|
} else {
|
|
|
|
updateDetails = append(updateDetails, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(addDetails) > 0 {
|
|
|
|
if err := appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, u := range updateDetails {
|
|
|
|
if err := appDetailRepo.Update(ctx, u); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(appTags) > 0 {
|
|
|
|
if err := appTagRepo.BatchCreate(ctx, appTags); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
global.LOG.Infof("sync local apps success")
|
|
|
|
}
|
|
|
|
func (a AppService) SyncAppListFromRemote() error {
|
2023-02-14 11:17:02 +08:00
|
|
|
updateRes, err := a.GetAppUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !updateRes.CanUpdate {
|
2023-02-27 17:22:14 +08:00
|
|
|
global.LOG.Infof("The latest version is [%s] The app store is already up to date", updateRes.Version)
|
2023-02-14 11:17:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := getAppFromRepo(updateRes.DownloadPath, updateRes.Version); err != nil {
|
2022-11-18 18:02:14 +08:00
|
|
|
global.LOG.Errorf("get app from oss error: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2022-10-12 18:57:22 +08:00
|
|
|
appDir := constant.AppResourceDir
|
2022-09-22 16:16:04 +08:00
|
|
|
listFile := path.Join(appDir, "list.json")
|
|
|
|
content, err := os.ReadFile(listFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
list := &dto.AppList{}
|
|
|
|
if err := json.Unmarshal(content, list); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
2022-09-30 17:56:06 +08:00
|
|
|
tags []*model.Tag
|
|
|
|
appTags []*model.AppTag
|
2022-09-22 16:16:04 +08:00
|
|
|
)
|
|
|
|
for _, t := range list.Tags {
|
|
|
|
tags = append(tags, &model.Tag{
|
|
|
|
Key: t.Key,
|
|
|
|
Name: t.Name,
|
|
|
|
})
|
|
|
|
}
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-08 14:02:14 +08:00
|
|
|
appsMap := getApps(oldApps, list.Items, false)
|
2022-09-22 16:16:04 +08:00
|
|
|
for _, l := range list.Items {
|
2022-09-30 17:56:06 +08:00
|
|
|
app := appsMap[l.Key]
|
2022-11-18 14:27:40 +08:00
|
|
|
icon, err := os.ReadFile(path.Join(appDir, l.Key, "metadata", "logo.png"))
|
2022-09-22 16:16:04 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] icon error: %s", l.Name, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iconStr := base64.StdEncoding.EncodeToString(icon)
|
2022-09-30 17:56:06 +08:00
|
|
|
app.Icon = iconStr
|
2022-09-22 16:16:04 +08:00
|
|
|
app.TagsKey = l.Tags
|
2023-02-08 16:21:17 +08:00
|
|
|
if l.Recommend > 0 {
|
|
|
|
app.Recommend = l.Recommend
|
|
|
|
} else {
|
|
|
|
app.Recommend = 9999
|
|
|
|
}
|
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 {
|
2022-09-30 17:56:06 +08:00
|
|
|
detail := detailsMap[v]
|
2022-11-18 14:27:40 +08:00
|
|
|
detailPath := path.Join(appDir, l.Key, "versions", v)
|
2022-09-22 16:16:04 +08:00
|
|
|
if _, err := os.Stat(detailPath); err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] folder error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
readmeStr, err := os.ReadFile(path.Join(detailPath, "README.md"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] README error: %s", detailPath, err.Error())
|
|
|
|
}
|
|
|
|
detail.Readme = string(readmeStr)
|
|
|
|
dockerComposeStr, err := os.ReadFile(path.Join(detailPath, "docker-compose.yml"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] docker-compose.yml error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
detail.DockerCompose = string(dockerComposeStr)
|
2022-10-13 16:46:38 +08:00
|
|
|
paramStr, err := os.ReadFile(path.Join(detailPath, "config.json"))
|
2022-09-22 16:16:04 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] form.json error: %s", detailPath, err.Error())
|
|
|
|
}
|
2022-09-26 16:32:40 +08:00
|
|
|
detail.Params = string(paramStr)
|
2022-09-30 17:56:06 +08:00
|
|
|
detailsMap[v] = detail
|
|
|
|
}
|
|
|
|
var newDetails []model.AppDetail
|
|
|
|
for _, v := range detailsMap {
|
|
|
|
newDetails = append(newDetails, v)
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
app.Details = newDetails
|
|
|
|
appsMap[l.Key] = app
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
addAppArray []model.App
|
|
|
|
updateArray []model.App
|
2023-04-08 14:02:14 +08:00
|
|
|
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 {
|
|
|
|
updateArray = append(updateArray, v)
|
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:27:40 +08:00
|
|
|
tx, ctx := getTxAndContext()
|
2022-09-30 17:56:06 +08:00
|
|
|
if len(addAppArray) > 0 {
|
|
|
|
if err := appRepo.BatchCreate(ctx, addAppArray); err != nil {
|
2022-09-22 16:16:04 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tagRepo.DeleteAll(ctx); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tags) > 0 {
|
|
|
|
if err := tagRepo.BatchCreate(ctx, tags); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
for _, update := range updateArray {
|
|
|
|
if err := appRepo.Save(ctx, &update); err != nil {
|
2022-09-22 16:16:04 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
apps := append(addAppArray, updateArray...)
|
2022-09-22 16:16:04 +08:00
|
|
|
|
|
|
|
var (
|
2022-09-30 17:56:06 +08:00
|
|
|
addDetails []model.AppDetail
|
|
|
|
updateDetails []model.AppDetail
|
2022-09-22 16:16:04 +08:00
|
|
|
)
|
|
|
|
for _, a := range apps {
|
|
|
|
for _, t := range a.TagsKey {
|
|
|
|
tagId, ok := tagMap[t]
|
|
|
|
if ok {
|
|
|
|
appTags = append(appTags, &model.AppTag{
|
|
|
|
AppId: a.ID,
|
|
|
|
TagId: tagId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range a.Details {
|
|
|
|
d.AppId = a.ID
|
2022-09-30 17:56:06 +08:00
|
|
|
if d.ID == 0 {
|
|
|
|
addDetails = append(addDetails, d)
|
|
|
|
} else {
|
|
|
|
updateDetails = append(updateDetails, d)
|
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
if len(addDetails) > 0 {
|
|
|
|
if err := appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
for _, u := range updateDetails {
|
|
|
|
if err := appDetailRepo.Update(ctx, u); err != nil {
|
2022-09-22 16:16:04 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 17:56:06 +08:00
|
|
|
if err := appTagRepo.DeleteAll(ctx); err != nil {
|
2022-09-22 16:16:04 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(appTags) > 0 {
|
|
|
|
if err := appTagRepo.BatchCreate(ctx, appTags); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|