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

feat: 应用同步优化 (#1564)

This commit is contained in:
zhengkunwang223 2023-07-06 16:30:22 +08:00 committed by GitHub
parent 71107fa42f
commit 695aacbe14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 40 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/i18n"
"math" "math"
"os" "os"
"path" "path"
@ -376,8 +377,10 @@ func (a *AppInstallService) SyncAll(systemInit bool) error {
} }
continue continue
} }
if err := syncById(i.ID); err != nil { if !systemInit {
global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error()) if err := syncById(i.ID); err != nil {
global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error())
}
} }
} }
return nil return nil
@ -602,12 +605,10 @@ func syncById(installId uint) error {
if appInstall.Status == constant.Installing { if appInstall.Status == constant.Installing {
return nil return nil
} }
containerNames, err := getContainerNames(appInstall) containerNames, err := getContainerNames(appInstall)
if err != nil { if err != nil {
return err return err
} }
cli, err := docker.NewClient() cli, err := docker.NewClient()
if err != nil { if err != nil {
return err return err
@ -633,16 +634,16 @@ func syncById(installId uint) error {
errorContainers = append(errorContainers, n.Names[0]) errorContainers = append(errorContainers, n.Names[0])
} }
} }
for _, old := range containerNames { for _, name := range containerNames {
exist := false exist := false
for _, new := range containers { for _, container := range containers {
if common.ExistWithStrArray(old, new.Names) { if common.ExistWithStrArray(name, container.Names) {
exist = true exist = true
break break
} }
} }
if !exist { if !exist {
notFoundContainers = append(notFoundContainers, old) notFoundContainers = append(notFoundContainers, name)
} }
} }
@ -655,10 +656,10 @@ func syncById(installId uint) error {
if containerCount == 0 { if containerCount == 0 {
appInstall.Status = constant.Error appInstall.Status = constant.Error
appInstall.Message = "container is not found" appInstall.Message = i18n.GetMsgWithMap("ErrContainerNotFound", map[string]interface{}{"name": strings.Join(containerNames, ",")})
return appInstallRepo.Save(context.Background(), &appInstall) return appInstallRepo.Save(context.Background(), &appInstall)
} }
if errCount == 0 && existedCount == 0 { if errCount == 0 && existedCount == 0 && notFoundCount == 0 {
appInstall.Status = constant.Running appInstall.Status = constant.Running
return appInstallRepo.Save(context.Background(), &appInstall) return appInstallRepo.Save(context.Background(), &appInstall)
} }
@ -673,22 +674,14 @@ func syncById(installId uint) error {
appInstall.Status = constant.UnHealthy appInstall.Status = constant.UnHealthy
} }
var errMsg strings.Builder var errMsg string
if errCount > 0 { if errCount > 0 {
errMsg.Write([]byte(string(rune(errCount)) + " error containers:")) errMsg += i18n.GetMsgWithMap("ErrContainerMsg", map[string]interface{}{"name": strings.Join(errorContainers, ",")})
for _, e := range errorContainers {
errMsg.Write([]byte(e))
}
errMsg.Write([]byte("\n"))
} }
if notFoundCount > 0 { if notFoundCount > 0 {
errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:")) errMsg += i18n.GetMsgWithMap("ErrContainerNotFound", map[string]interface{}{"name": strings.Join(notFoundContainers, ",")})
for _, e := range notFoundContainers {
errMsg.Write([]byte(e))
}
errMsg.Write([]byte("\n"))
} }
appInstall.Message = errMsg.String() appInstall.Message = errMsg
return appInstallRepo.Save(context.Background(), &appInstall) return appInstallRepo.Save(context.Background(), &appInstall)
} }

View File

@ -359,7 +359,6 @@ func getContainerNames(install model.AppInstall) ([]string, error) {
return nil, err return nil, err
} }
containerMap := make(map[string]struct{}) containerMap := make(map[string]struct{})
containerMap[install.ContainerName] = struct{}{}
for _, service := range project.AllServices() { for _, service := range project.AllServices() {
if service.ContainerName == "${CONTAINER_NAME}" || service.ContainerName == "" { if service.ContainerName == "${CONTAINER_NAME}" || service.ContainerName == "" {
continue continue
@ -571,19 +570,28 @@ func checkContainerNameIsExist(containerName, appDir string) (bool, error) {
func upApp(appInstall *model.AppInstall) { func upApp(appInstall *model.AppInstall) {
upProject := func(appInstall *model.AppInstall) (err error) { upProject := func(appInstall *model.AppInstall) (err error) {
if err == nil { if err == nil {
var composeService *composeV2.ComposeService var (
composeService, err = getServiceFromInstall(appInstall) out string
errMsg string
)
out, err = compose.Pull(appInstall.GetComposePath())
if err != nil { if err != nil {
if out != "" {
if strings.Contains(out, "no such host") {
errMsg = i18n.GetMsgByKey("ErrNoSuchHost") + ":"
}
if strings.Contains(out, "timeout") {
errMsg = i18n.GetMsgByKey("ErrImagePullTimeOut") + ":"
}
appInstall.Message = errMsg + out
}
return err return err
} }
err = composeService.ComposePull() out, err = compose.Up(appInstall.GetComposePath())
if err != nil { if err != nil {
appInstall.Status = constant.PullErr if out != "" {
return err appInstall.Message = errMsg + out
} }
err = composeService.ComposeUp()
if err != nil {
appInstall.Status = constant.Error
return err return err
} }
return return
@ -592,14 +600,7 @@ func upApp(appInstall *model.AppInstall) {
} }
} }
if err := upProject(appInstall); err != nil { if err := upProject(appInstall); err != nil {
otherMsg := "" appInstall.Status = constant.Error
if strings.Contains(err.Error(), "no such host") {
otherMsg = i18n.GetMsgByKey("ErrNoSuchHost") + ":"
}
if strings.Contains(err.Error(), "timeout") {
otherMsg = i18n.GetMsgByKey("ErrImagePullTimeOut") + ":"
}
appInstall.Message = otherMsg + err.Error()
} else { } else {
appInstall.Status = constant.Running appInstall.Status = constant.Running
} }

View File

@ -41,6 +41,8 @@ ErrHttpReqFailed: "Request failed {{.err}}"
ErrHttpReqNotFound: "The file does not exist" ErrHttpReqNotFound: "The file does not exist"
ErrNoSuchHost: "Network connection failed" ErrNoSuchHost: "Network connection failed"
ErrImagePullTimeOut: 'Image pull timeout' ErrImagePullTimeOut: 'Image pull timeout'
ErrContainerNotFound: '{{ .name }} container does not exist'
ErrContainerMsg: '{{ .name }} container is abnormal, please check the log on the container page for details'
#file #file
ErrFileCanNotRead: "File can not read" ErrFileCanNotRead: "File can not read"

View File

@ -41,6 +41,8 @@ ErrHttpReqFailed: "請求失敗 {{.err}}"
ErrHttpReqNotFound: "文件不存在" ErrHttpReqNotFound: "文件不存在"
ErrNoSuchHost: "網路連接失敗" ErrNoSuchHost: "網路連接失敗"
ErrImagePullTimeOut: "鏡像拉取超時" ErrImagePullTimeOut: "鏡像拉取超時"
ErrContainerNotFound: '{{ .name }} 容器不存在'
ErrContainerMsg: '{{ .name }} 容器異常,具體請在容器頁面查看日誌'
#file #file
ErrFileCanNotRead: "此文件不支持預覽" ErrFileCanNotRead: "此文件不支持預覽"

View File

@ -41,6 +41,8 @@ ErrHttpReqFailed: "请求失败 {{.err}}"
ErrHttpReqNotFound: "文件不存在" ErrHttpReqNotFound: "文件不存在"
ErrNoSuchHost: "网络连接失败" ErrNoSuchHost: "网络连接失败"
ErrImagePullTimeOut: '镜像拉取超时' ErrImagePullTimeOut: '镜像拉取超时'
ErrContainerNotFound: '{{ .name }} 容器不存在'
ErrContainerMsg: '{{ .name }} 容器异常,具体请在容器页面查看日志'
#file #file
ErrFileCanNotRead: "此文件不支持预览" ErrFileCanNotRead: "此文件不支持预览"

View File

@ -4,6 +4,11 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
) )
func Pull(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s pull", filePath)
return stdout, err
}
func Up(filePath string) (string, error) { func Up(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath) stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath)
return stdout, err return stdout, err