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

fix: 解决应用升级失败的问题 (#1220)

This commit is contained in:
zhengkunwang223 2023-06-01 17:05:14 +08:00 committed by GitHub
parent 35ca52620c
commit 317017a2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files"
http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
"gopkg.in/yaml.v3"
"io"
"net/http"
@ -391,7 +392,7 @@ func (a AppService) SyncAppListFromLocal() {
appDirEntries, err := os.ReadDir(appDir)
app, err := handleLocalApp(localAppDir)
if err != nil {
global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppErr", map[string]interface{}{"name": app.Name, "err": err.Error()}))
global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppErr", map[string]interface{}{"name": dirEntry.Name(), "err": err.Error()}))
continue
}
var appDetails []model.AppDetail
@ -591,7 +592,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
return nil, err
}
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode)
versionRes, err := http.Get(versionUrl)
versionRes, err := http2.GetHttpRes(versionUrl)
if err != nil {
return nil, err
}

View File

@ -49,7 +49,6 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
port, ok := params[key]
if ok {
portN := int(math.Ceil(port.(float64)))
oldInstalled, _ := appInstallRepo.ListBy(appInstallRepo.WithPort(portN))
if len(oldInstalled) > 0 {
var apps []string
@ -283,7 +282,9 @@ func upgradeInstall(installId uint, detailId uint) error {
}
}
servicesMap[install.ServiceName] = servicesMap[oldServiceName]
delete(servicesMap, oldServiceName)
if install.ServiceName != oldServiceName {
delete(servicesMap, oldServiceName)
}
envs := make(map[string]interface{})
if upErr = json.Unmarshal([]byte(install.Env), &envs); upErr != nil {

View File

@ -39,6 +39,11 @@ ErrAppDirNull: 'version folder does not exist'
LocalAppErr: "App {{.name}} sync failed! {{.err}}"
ErrContainerName: "ContainerName is already exist"
ErrAppSystemRestart: "1Panel restart causes the task to terminate"
ErrCreateHttpClient: "Failed to create HTTP request {{.err}}"
ErrHttpReqTimeOut: "Request timed out {{.err}}"
ErrHttpReqFailed: "Request failed {{.err}}"
ErrHttpReqNotFound: "The file does not exist"
ErrNoSuchHost: "Network connection failed"
#file
ErrFileCanNotRead: "File can not read"

View File

@ -39,6 +39,11 @@ ErrAppDirNull: '版本文件夹不存在'
LocalAppErr: "应用 {{.name}} 同步失败!{{.err}}"
ErrContainerName: "容器名称已存在"
ErrAppSystemRestart: "1Panel 重启导致任务终止"
ErrCreateHttpClient: "创建HTTP请求失败 {{.err}}"
ErrHttpReqTimeOut: "请求超时 {{.err}}"
ErrHttpReqFailed: "请求失败 {{.err}}"
ErrHttpReqNotFound: "文件不存在"
ErrNoSuchHost: "网络连接失败"
#file
ErrFileCanNotRead: "此文件不支持预览"

View File

@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
cZip "github.com/klauspost/compress/zip"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
@ -252,11 +253,10 @@ func (f FileOp) DownloadFileWithProcess(url, dst, key string) error {
}
func (f FileOp) DownloadFile(url, dst string) error {
resp, err := http.Get(url)
resp, err := http2.GetHttpRes(url)
if err != nil {
return fmt.Errorf("get download file [%s] error, err %s", dst, err.Error())
return err
}
defer resp.Body.Close()
out, err := os.Create(dst)
if err != nil {

39
backend/utils/http/get.go Normal file
View File

@ -0,0 +1,39 @@
package http
import (
"context"
"github.com/1Panel-dev/1Panel/backend/buserr"
"net/http"
"strings"
"time"
)
func GetHttpRes(url string) (*http.Response, error) {
client := &http.Client{
Transport: &http.Transport{
IdleConnTimeout: 10 * time.Second,
},
}
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
if err != nil {
return nil, buserr.WithMap("ErrCreateHttpClient", map[string]interface{}{"err": err.Error()}, err)
}
resp, err := client.Do(req)
if err != nil {
if err == context.DeadlineExceeded {
return nil, buserr.WithMap("ErrHttpReqTimeOut", map[string]interface{}{"err": err.Error()}, err)
} else {
if strings.Contains(err.Error(), "no such host") {
return nil, buserr.New("ErrNoSuchHost")
}
return nil, buserr.WithMap("ErrHttpReqFailed", map[string]interface{}{"err": err.Error()}, err)
}
}
if resp.StatusCode == 404 {
return nil, buserr.New("ErrHttpReqNotFound")
}
return resp, nil
}

View File

@ -209,6 +209,7 @@ const message = {
building: '制作镜像中',
downloaderr: '下载失败',
upgrading: '升级中',
upgradeerr: '升级失败',
},
units: {
second: '秒',