From 695aacbe14459295625c63c7f384e391a4499ed0 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 6 Jul 2023 16:30:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=94=E7=94=A8=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20(#1564)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app_install.go | 37 ++++++++++++------------------ backend/app/service/app_utils.go | 37 +++++++++++++++--------------- backend/i18n/lang/en.yaml | 2 ++ backend/i18n/lang/zh-Hant.yaml | 2 ++ backend/i18n/lang/zh.yaml | 2 ++ backend/utils/compose/compose.go | 5 ++++ 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index ff209c627..d2b65ea73 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/1Panel-dev/1Panel/backend/i18n" "math" "os" "path" @@ -376,8 +377,10 @@ func (a *AppInstallService) SyncAll(systemInit bool) error { } continue } - if err := syncById(i.ID); err != nil { - global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error()) + if !systemInit { + if err := syncById(i.ID); err != nil { + global.LOG.Errorf("sync install app[%s] error,mgs: %s", i.Name, err.Error()) + } } } return nil @@ -602,12 +605,10 @@ func syncById(installId uint) error { if appInstall.Status == constant.Installing { return nil } - containerNames, err := getContainerNames(appInstall) if err != nil { return err } - cli, err := docker.NewClient() if err != nil { return err @@ -633,16 +634,16 @@ func syncById(installId uint) error { errorContainers = append(errorContainers, n.Names[0]) } } - for _, old := range containerNames { + for _, name := range containerNames { exist := false - for _, new := range containers { - if common.ExistWithStrArray(old, new.Names) { + for _, container := range containers { + if common.ExistWithStrArray(name, container.Names) { exist = true break } } if !exist { - notFoundContainers = append(notFoundContainers, old) + notFoundContainers = append(notFoundContainers, name) } } @@ -655,10 +656,10 @@ func syncById(installId uint) error { if containerCount == 0 { 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) } - if errCount == 0 && existedCount == 0 { + if errCount == 0 && existedCount == 0 && notFoundCount == 0 { appInstall.Status = constant.Running return appInstallRepo.Save(context.Background(), &appInstall) } @@ -673,22 +674,14 @@ func syncById(installId uint) error { appInstall.Status = constant.UnHealthy } - var errMsg strings.Builder + var errMsg string if errCount > 0 { - errMsg.Write([]byte(string(rune(errCount)) + " error containers:")) - for _, e := range errorContainers { - errMsg.Write([]byte(e)) - } - errMsg.Write([]byte("\n")) + errMsg += i18n.GetMsgWithMap("ErrContainerMsg", map[string]interface{}{"name": strings.Join(errorContainers, ",")}) } if notFoundCount > 0 { - errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:")) - for _, e := range notFoundContainers { - errMsg.Write([]byte(e)) - } - errMsg.Write([]byte("\n")) + errMsg += i18n.GetMsgWithMap("ErrContainerNotFound", map[string]interface{}{"name": strings.Join(notFoundContainers, ",")}) } - appInstall.Message = errMsg.String() + appInstall.Message = errMsg return appInstallRepo.Save(context.Background(), &appInstall) } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 77b2b639c..98a6147b4 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -359,7 +359,6 @@ func getContainerNames(install model.AppInstall) ([]string, error) { return nil, err } containerMap := make(map[string]struct{}) - containerMap[install.ContainerName] = struct{}{} for _, service := range project.AllServices() { if service.ContainerName == "${CONTAINER_NAME}" || service.ContainerName == "" { continue @@ -571,19 +570,28 @@ func checkContainerNameIsExist(containerName, appDir string) (bool, error) { func upApp(appInstall *model.AppInstall) { upProject := func(appInstall *model.AppInstall) (err error) { if err == nil { - var composeService *composeV2.ComposeService - composeService, err = getServiceFromInstall(appInstall) + var ( + out string + errMsg string + ) + out, err = compose.Pull(appInstall.GetComposePath()) 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 } - err = composeService.ComposePull() + out, err = compose.Up(appInstall.GetComposePath()) if err != nil { - appInstall.Status = constant.PullErr - return err - } - err = composeService.ComposeUp() - if err != nil { - appInstall.Status = constant.Error + if out != "" { + appInstall.Message = errMsg + out + } return err } return @@ -592,14 +600,7 @@ func upApp(appInstall *model.AppInstall) { } } if err := upProject(appInstall); err != nil { - otherMsg := "" - 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() + appInstall.Status = constant.Error } else { appInstall.Status = constant.Running } diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 60bb9c6e2..b0e365578 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -41,6 +41,8 @@ ErrHttpReqFailed: "Request failed {{.err}}" ErrHttpReqNotFound: "The file does not exist" ErrNoSuchHost: "Network connection failed" 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 ErrFileCanNotRead: "File can not read" diff --git a/backend/i18n/lang/zh-Hant.yaml b/backend/i18n/lang/zh-Hant.yaml index 449cc09e5..bb158b8ea 100644 --- a/backend/i18n/lang/zh-Hant.yaml +++ b/backend/i18n/lang/zh-Hant.yaml @@ -41,6 +41,8 @@ ErrHttpReqFailed: "請求失敗 {{.err}}" ErrHttpReqNotFound: "文件不存在" ErrNoSuchHost: "網路連接失敗" ErrImagePullTimeOut: "鏡像拉取超時" +ErrContainerNotFound: '{{ .name }} 容器不存在' +ErrContainerMsg: '{{ .name }} 容器異常,具體請在容器頁面查看日誌' #file ErrFileCanNotRead: "此文件不支持預覽" diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 2768a6e30..f6e68bce0 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -41,6 +41,8 @@ ErrHttpReqFailed: "请求失败 {{.err}}" ErrHttpReqNotFound: "文件不存在" ErrNoSuchHost: "网络连接失败" ErrImagePullTimeOut: '镜像拉取超时' +ErrContainerNotFound: '{{ .name }} 容器不存在' +ErrContainerMsg: '{{ .name }} 容器异常,具体请在容器页面查看日志' #file ErrFileCanNotRead: "此文件不支持预览" diff --git a/backend/utils/compose/compose.go b/backend/utils/compose/compose.go index 022c68525..08ade3bba 100644 --- a/backend/utils/compose/compose.go +++ b/backend/utils/compose/compose.go @@ -4,6 +4,11 @@ import ( "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) { stdout, err := cmd.Execf("docker-compose -f %s up -d", filePath) return stdout, err