feat: 下载离线包改为从OSS下载

This commit is contained in:
zhengkunwang223 2023-02-17 15:13:55 +08:00 committed by zhengkunwang223
parent abbca655ac
commit d47559e410
7 changed files with 29 additions and 210 deletions

View File

@ -4,8 +4,10 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/git"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
@ -336,23 +338,30 @@ func (a AppService) SyncInstalled(installId uint) error {
}
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
res := &response.AppUpdateRes{}
res := &response.AppUpdateRes{
CanUpdate: false,
}
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")
versionRes, err := http.Get(fmt.Sprintf("%s/%s/%s/appstore/apps.json", global.CONF.System.RepoUrl, global.CONF.System.Mode, setting.SystemVersion))
if err != nil {
return nil, err
}
defer versionRes.Body.Close()
body, err := ioutil.ReadAll(versionRes.Body)
if err != nil {
return nil, err
}
list := &dto.AppList{}
if err = json.Unmarshal(contentRes.Content, list); err != nil {
if err = json.Unmarshal(body, list); err != nil {
return nil, err
}
res.Version = list.Version
if common.CompareVersion(list.Version, setting.AppStoreVersion) {
res.CanUpdate = true
res.DownloadPath = contentRes.DownloadPath
res.DownloadPath = fmt.Sprintf("%s/%s/%s/appstore/apps-%s.tar.gz", global.CONF.System.RepoUrl, global.CONF.System.Mode, setting.SystemVersion, list.Version)
return res, err
}
return nil, err
@ -380,7 +389,6 @@ func (a AppService) SyncAppList() error {
if err := json.Unmarshal(content, list); err != nil {
return err
}
var (
tags []*model.Tag
appTags []*model.AppTag

View File

@ -499,7 +499,7 @@ func handleErr(install model.AppInstall, err error, out string) error {
}
func getAppFromRepo(downloadPath, version string) error {
downloadUrl := fmt.Sprintf("%s/%s/apps-%s.tar.gz", downloadPath, version, version)
downloadUrl := fmt.Sprintf(downloadPath)
appDir := constant.AppResourceDir
global.LOG.Infof("download file from %s", downloadUrl)

View File

@ -1,17 +1,16 @@
package configs
type System struct {
Port string `mapstructure:"port"`
DbFile string `mapstructure:"db_file"`
DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"`
TmpDir string `mapstructure:"tmp_dir"`
Cache string `mapstructure:"cache"`
Backup string `mapstructure:"backup"`
AppRepoOwner string `mapstructure:"app_repo_owner"`
AppRepoName string `mapstructure:"app_repo_name"`
EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"`
Port string `mapstructure:"port"`
DbFile string `mapstructure:"db_file"`
DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"`
TmpDir string `mapstructure:"tmp_dir"`
Cache string `mapstructure:"cache"`
Backup string `mapstructure:"backup"`
EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"`
RepoUrl string `mapstructure:"repo_url"`
}

View File

@ -30,10 +30,7 @@ func Init() {
if config.System.Mode != "" {
mode = config.System.Mode
}
if mode != "release" {
if !fileOp.Stat("/opt/1panel/conf/app.yaml") {
panic("conf file is not exist")
}
if mode == "dev" && fileOp.Stat("/opt/1panel/conf/app.yaml") {
v.SetConfigName("app")
v.AddConfigPath(path.Join("/opt/1panel/conf"))
if err := v.ReadInConfig(); err != nil {

View File

@ -1,147 +0,0 @@
package git
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"gitee.com/openeuler/go-gitee/gitee"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/antihax/optional"
"github.com/google/go-github/github"
"net/http"
"time"
)
type RepoInfo struct {
RepoType string
Version string
ReleaseNote string
CreatedAt string
DownloadPath string
}
type RepoContent struct {
RepoType string
Content []byte
DownloadPath string
}
var gitRepoTypes = []string{"gitee", "github"}
func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) {
for _, repoType := range gitRepoTypes {
url := fmt.Sprintf("https://%s.com/%s/%s", repoType, owner, repoName)
if checkValid(url) {
res, err := getLatestRepoInfo(repoType, owner, repoName)
if err == nil {
return res, nil
} else {
global.LOG.Errorf("get %s last release version failed %s", repoType, err.Error())
}
} else {
global.LOG.Errorf("get %s remote repo [%s] failed", repoType, url)
}
}
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 {
timeout := 2 * time.Second
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := http.Client{
Transport: tr,
Timeout: timeout,
}
if _, err := client.Get(addr); err != nil {
return false
}
return true
}
func getLatestRepoInfo(repoType, owner, repoName string) (*RepoInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var repoInfo RepoInfo
repoInfo.RepoType = repoType
if repoType == "gitee" {
client := gitee.NewAPIClient(gitee.NewConfiguration())
stats, res, err := client.RepositoriesApi.GetV5ReposOwnerRepoReleasesLatest(ctx, owner, repoName, &gitee.GetV5ReposOwnerRepoReleasesLatestOpts{})
if res.StatusCode != 200 || err != nil {
return nil, err
}
repoInfo.Version = stats.Name
repoInfo.ReleaseNote = stats.Body
repoInfo.CreatedAt = stats.CreatedAt.Format("2006-01-02 15:04:05")
repoInfo.DownloadPath = fmt.Sprintf("https://gitee.com/%s/%s/releases/download/%s/", owner, repoName, repoInfo.Version)
} else {
client := github.NewClient(nil)
stats, res, err := client.Repositories.GetLatestRelease(ctx, owner, repoName)
if res.StatusCode != 200 || err != nil {
return nil, err
}
repoInfo.Version = *stats.Name
repoInfo.ReleaseNote = *stats.Body
repoInfo.CreatedAt = stats.PublishedAt.Add(8 * time.Hour).Format("2006-01-02 15:04:05")
repoInfo.DownloadPath = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/", owner, repoName, repoInfo.Version)
}
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

@ -1,9 +1,8 @@
system:
db_file: 1Panel.db
app_repo_owner: zhengkunwang223
app_repo_name: appstore
base_dir: /opt
mode: dev
repo_url: https://1panel.oss-cn-hangzhou.aliyuncs.com/package
log:
level: debug
@ -11,17 +10,3 @@ log:
log_name: 1Panel
log_suffix: .log
log_backup: 10
cors:
mode: whitelist
whitelist:
- allow-origin: example1.com
allow-headers: content-type
allow-methods: GET, POST
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials: true
- allow-origin: example2.com
allow-headers: content-type
allow-methods: GET, POST
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials: true

View File

@ -1,24 +1 @@
$primary-color: #005eeb;
$menu-item-backgroup-color: rgba(255, 255, 255, 0.3);
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
$colors: (
'primary': (
'base': $primary-color,
),
'success': (
'base': #67c23a,
),
'warning': (
'base': #e6a23c,
),
'danger': (
'base': #f56c6c,
),
'error': (
'base': #f56c6c,
),
'info': (
'base': #909399,
),
)
);