1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

feat: 修改应用同步逻辑

This commit is contained in:
zhengkunwang223 2023-02-14 11:17:02 +08:00 committed by zhengkunwang223
parent d1dc8c6a50
commit 5d14ea1da0
7 changed files with 108 additions and 41 deletions

View File

@ -6,11 +6,14 @@ import (
) )
type AppRes struct { type AppRes struct {
Version string `json:"version"` Items []*AppDTO `json:"items"`
CanUpdate bool `json:"canUpdate"` Total int64 `json:"total"`
Items []*AppDTO `json:"items"` }
Tags []model.Tag `json:"tags"`
Total int64 `json:"total"` type AppUpdateRes struct {
Version string `json:"version"`
CanUpdate bool `json:"canUpdate"`
DownloadPath string `json:"downloadPath"`
} }
type AppDTO struct { type AppDTO struct {

View File

@ -98,23 +98,7 @@ func (a AppService) PageApp(req request.AppSearch) (interface{}, error) {
} }
res.Items = appDTOs res.Items = appDTOs
res.Total = total res.Total = total
tags, err := tagRepo.All()
if err != nil {
return nil, err
}
res.Tags = tags
setting, err := NewISettingService().GetSettingInfo()
if err != nil {
return nil, err
}
repoInfo, err := git.CheckAndGetInfo(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName)
if err != nil {
return nil, err
}
if common.CompareVersion(repoInfo.Version, setting.AppStoreVersion) {
res.CanUpdate = true
}
return res, nil return res, nil
} }
@ -351,8 +335,38 @@ func (a AppService) SyncInstalled(installId uint) error {
return appInstallRepo.Save(&appInstall) return appInstallRepo.Save(&appInstall)
} }
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
res := &response.AppUpdateRes{}
setting, err := NewISettingService().GetSettingInfo()
if err != nil {
return nil, err
}
contentRes, err := git.CheckAndGetContent(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName, setting.SystemVersion, "apps/list.json")
if err != nil {
return nil, err
}
list := &dto.AppList{}
if err = json.Unmarshal(contentRes.Content, list); err != nil {
return nil, err
}
res.Version = list.Version
if common.CompareVersion(list.Version, setting.AppStoreVersion) {
res.CanUpdate = true
res.DownloadPath = contentRes.DownloadPath
return res, err
}
return nil, err
}
func (a AppService) SyncAppList() error { func (a AppService) SyncAppList() error {
if err := getAppFromRepo(); err != nil { updateRes, err := a.GetAppUpdate()
if err != nil {
return err
}
if !updateRes.CanUpdate {
return nil
}
if err := getAppFromRepo(updateRes.DownloadPath, updateRes.Version); err != nil {
global.LOG.Errorf("get app from oss error: %s", err.Error()) global.LOG.Errorf("get app from oss error: %s", err.Error())
return err return err
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/utils/git"
"math" "math"
"os" "os"
"path" "path"
@ -499,20 +498,11 @@ func handleErr(install model.AppInstall, err error, out string) error {
return reErr return reErr
} }
func getAppFromRepo() error { func getAppFromRepo(downloadPath, version string) error {
repoInfo, err := git.CheckAndGetInfo(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName) downloadUrl := fmt.Sprintf("%s/%s/apps-%s.tar.gz", downloadPath, version, version)
if err != nil {
return err
}
appDir := constant.AppResourceDir appDir := constant.AppResourceDir
setting, err := NewISettingService().GetSettingInfo()
if err != nil { global.LOG.Infof("download file from %s", downloadUrl)
return err
}
if !common.CompareVersion(repoInfo.Version, setting.AppStoreVersion) {
return nil
}
downloadUrl := fmt.Sprintf("%sapps-%s.tar.gz", repoInfo.DownloadPath, repoInfo.Version)
fileOp := files.NewFileOp() fileOp := files.NewFileOp()
if _, err := fileOp.CopyAndBackup(appDir); err != nil { if _, err := fileOp.CopyAndBackup(appDir); err != nil {
return err return err
@ -524,7 +514,7 @@ func getAppFromRepo() error {
if err := fileOp.Decompress(packagePath, constant.ResourceDir, files.TarGz); err != nil { if err := fileOp.Decompress(packagePath, constant.ResourceDir, files.TarGz); err != nil {
return err return err
} }
_ = NewISettingService().Update("AppStoreVersion", repoInfo.Version) _ = NewISettingService().Update("AppStoreVersion", version)
defer func() { defer func() {
_ = fileOp.DeleteFile(packagePath) _ = fileOp.DeleteFile(packagePath)
}() }()

View File

@ -12,4 +12,5 @@ type System struct {
AppRepoName string `mapstructure:"app_repo_name"` AppRepoName string `mapstructure:"app_repo_name"`
EncryptKey string `mapstructure:"encrypt_key"` EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"` BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"`
} }

View File

@ -3,10 +3,12 @@ package git
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"gitee.com/openeuler/go-gitee/gitee" "gitee.com/openeuler/go-gitee/gitee"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/antihax/optional"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"net/http" "net/http"
"time" "time"
@ -20,6 +22,12 @@ type RepoInfo struct {
DownloadPath string DownloadPath string
} }
type RepoContent struct {
RepoType string
Content []byte
DownloadPath string
}
var gitRepoTypes = []string{"gitee", "github"} var gitRepoTypes = []string{"gitee", "github"}
func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) { func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) {
@ -39,6 +47,23 @@ func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) {
return nil, errors.New("all remote repo get failed") return nil, errors.New("all remote repo get failed")
} }
func CheckAndGetContent(owner, repoName, branch, path string) (*RepoContent, error) {
for _, repoType := range gitRepoTypes {
url := fmt.Sprintf("https://%s.com/%s/%s", repoType, owner, repoName)
if checkValid(url) {
res, err := getContentFromBranch(repoType, owner, repoName, branch, path)
if err == nil {
return res, nil
} else {
global.LOG.Errorf("get %s last version failed %s", repoType, err.Error())
}
} else {
global.LOG.Errorf("check %s valid [%s] failed", repoType, url)
}
}
return nil, errors.New("all repo get failed")
}
func checkValid(addr string) bool { func checkValid(addr string) bool {
timeout := 2 * time.Second timeout := 2 * time.Second
tr := &http.Transport{ tr := &http.Transport{
@ -82,3 +107,41 @@ func getLatestRepoInfo(repoType, owner, repoName string) (*RepoInfo, error) {
} }
return &repoInfo, nil return &repoInfo, nil
} }
func getContentFromBranch(repoType, owner, repoName, branch, path string) (*RepoContent, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var repoContent RepoContent
repoContent.RepoType = repoType
if repoType == "gitee" {
client := gitee.NewAPIClient(gitee.NewConfiguration())
content, res, err := client.RepositoriesApi.GetV5ReposOwnerRepoContentsPath(ctx, owner, repoName, path, &gitee.GetV5ReposOwnerRepoContentsPathOpts{
Ref: optional.NewString(branch),
})
if res.StatusCode != 200 || err != nil {
return nil, err
}
bs64Bytes, err := base64.StdEncoding.DecodeString(content.Content)
if err != nil {
return nil, err
}
repoContent.Content = bs64Bytes
repoContent.DownloadPath = fmt.Sprintf("https://gitee.com/%s/%s/releases/download/", owner, repoName)
return &repoContent, nil
} else {
client := github.NewClient(nil)
content, _, res, err := client.Repositories.GetContents(ctx, owner, repoName, path, &github.RepositoryContentGetOptions{
Ref: branch,
})
if res.StatusCode != 200 || err != nil {
return nil, err
}
bs64Bytes, err := base64.StdEncoding.DecodeString(*content.Content)
if err != nil {
return nil, err
}
repoContent.Content = bs64Bytes
repoContent.DownloadPath = fmt.Sprintf("https://github.com/%s/%s/releases/download", owner, repoName)
return &repoContent, nil
}
}

View File

@ -23,10 +23,7 @@ export namespace App {
export interface AppResPage { export interface AppResPage {
total: number; total: number;
canUpdate: boolean;
version: string;
items: App.App[]; items: App.App[];
tags: App.Tag[];
} }
export interface AppDetail extends CommonModel { export interface AppDetail extends CommonModel {

View File

@ -126,7 +126,6 @@ const search = async (req: App.AppReq) => {
await SearchApp(req) await SearchApp(req)
.then((res) => { .then((res) => {
apps.value = res.data.items; apps.value = res.data.items;
canUpdate.value = res.data.canUpdate;
}) })
.finally(() => { .finally(() => {
loading.value = false; loading.value = false;