From ff87cd5989a8e63fd6f58b430aff16acd6f21a9a Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Wed, 1 Mar 2023 11:51:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=94=E7=94=A8=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=87=8D=E5=BB=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app.go | 179 +++++++++--------- backend/app/service/app_install.go | 16 +- backend/constant/app.go | 4 + backend/utils/compose/compose.go | 5 + frontend/src/lang/modules/en.ts | 5 +- frontend/src/lang/modules/zh.ts | 5 +- .../src/views/app-store/installed/index.vue | 14 +- 7 files changed, 127 insertions(+), 101 deletions(-) diff --git a/backend/app/service/app.go b/backend/app/service/app.go index ef4fd18e3..464a0fd9f 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -20,7 +20,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/common" - "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" "gopkg.in/yaml.v3" ) @@ -34,7 +33,7 @@ type IAppService interface { GetApp(key string) (*response.AppDTO, error) GetAppDetail(appId uint, version string) (response.AppDetailDTO, error) Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error) - SyncInstalled(installId uint) error + //SyncInstalled(installId uint) error SyncAppList() error } @@ -259,94 +258,94 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( return &appInstall, nil } -func (a AppService) SyncInstalled(installId uint) error { - appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId)) - if err != nil { - return err - } - - containerNames, err := getContainerNames(appInstall) - if err != nil { - return err - } - - cli, err := docker.NewClient() - if err != nil { - return err - } - containers, err := cli.ListContainersByName(containerNames) - if err != nil { - return err - } - var ( - errorContainers []string - notFoundContainers []string - runningContainers []string - ) - - for _, n := range containers { - if n.State != "running" { - errorContainers = append(errorContainers, n.Names[0]) - } else { - runningContainers = append(runningContainers, n.Names[0]) - } - } - for _, old := range containerNames { - exist := false - for _, new := range containers { - if common.ExistWithStrArray(old, new.Names) { - exist = true - break - } - } - if !exist { - notFoundContainers = append(notFoundContainers, old) - } - } - - containerCount := len(containers) - errCount := len(errorContainers) - notFoundCount := len(notFoundContainers) - normalCount := len(containerNames) - runningCount := len(runningContainers) - - if containerCount == 0 { - appInstall.Status = constant.Error - appInstall.Message = "container is not found" - return appInstallRepo.Save(&appInstall) - } - if errCount == 0 && notFoundCount == 0 { - appInstall.Status = constant.Running - return appInstallRepo.Save(&appInstall) - } - if errCount == normalCount { - appInstall.Status = constant.Error - } - if notFoundCount == normalCount { - appInstall.Status = constant.Stopped - } - if runningCount < normalCount { - appInstall.Status = constant.UnHealthy - } - - var errMsg strings.Builder - if errCount > 0 { - errMsg.Write([]byte(string(rune(errCount)) + " error containers:")) - for _, e := range errorContainers { - errMsg.Write([]byte(e)) - } - errMsg.Write([]byte("\n")) - } - if notFoundCount > 0 { - errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:")) - for _, e := range notFoundContainers { - errMsg.Write([]byte(e)) - } - errMsg.Write([]byte("\n")) - } - appInstall.Message = errMsg.String() - return appInstallRepo.Save(&appInstall) -} +//func (a AppService) SyncInstalled(installId uint) error { +// appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId)) +// if err != nil { +// return err +// } +// +// containerNames, err := getContainerNames(appInstall) +// if err != nil { +// return err +// } +// +// cli, err := docker.NewClient() +// if err != nil { +// return err +// } +// containers, err := cli.ListContainersByName(containerNames) +// if err != nil { +// return err +// } +// var ( +// errorContainers []string +// notFoundContainers []string +// runningContainers []string +// ) +// +// for _, n := range containers { +// if n.State != "running" { +// errorContainers = append(errorContainers, n.Names[0]) +// } else { +// runningContainers = append(runningContainers, n.Names[0]) +// } +// } +// for _, old := range containerNames { +// exist := false +// for _, new := range containers { +// if common.ExistWithStrArray(old, new.Names) { +// exist = true +// break +// } +// } +// if !exist { +// notFoundContainers = append(notFoundContainers, old) +// } +// } +// +// containerCount := len(containers) +// errCount := len(errorContainers) +// notFoundCount := len(notFoundContainers) +// normalCount := len(containerNames) +// runningCount := len(runningContainers) +// +// if containerCount == 0 { +// appInstall.Status = constant.ContainerNotFound +// appInstall.Message = "container is not found" +// return appInstallRepo.Save(&appInstall) +// } +// if errCount == 0 && notFoundCount == 0 { +// appInstall.Status = constant.Running +// return appInstallRepo.Save(&appInstall) +// } +// if errCount == normalCount { +// appInstall.Status = constant.Error +// } +// if notFoundCount == normalCount { +// appInstall.Status = constant.Stopped +// } +// if runningCount < normalCount { +// appInstall.Status = constant.UnHealthy +// } +// +// var errMsg strings.Builder +// if errCount > 0 { +// errMsg.Write([]byte(string(rune(errCount)) + " error containers:")) +// for _, e := range errorContainers { +// errMsg.Write([]byte(e)) +// } +// errMsg.Write([]byte("\n")) +// } +// if notFoundCount > 0 { +// errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:")) +// for _, e := range notFoundContainers { +// errMsg.Write([]byte(e)) +// } +// errMsg.Write([]byte("\n")) +// } +// appInstall.Message = errMsg.String() +// return appInstallRepo.Save(&appInstall) +//} func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) { res := &response.AppUpdateRes{ diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index b39706fff..27398030d 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -159,13 +159,23 @@ func (a AppInstallService) Operate(req request.AppInstalledOperate) error { } dockerComposePath := install.GetComposePath() switch req.Operate { - case constant.Up: - out, err := compose.Up(dockerComposePath) + case constant.Rebuild: + out, err := compose.Down(dockerComposePath) + if err != nil { + return handleErr(install, err, out) + } + out, err = compose.Up(dockerComposePath) + if err != nil { + return handleErr(install, err, out) + } + return nil + case constant.Start: + out, err := compose.Start(dockerComposePath) if err != nil { return handleErr(install, err, out) } install.Status = constant.Running - case constant.Down: + case constant.Stop: out, err := compose.Stop(dockerComposePath) if err != nil { return handleErr(install, err, out) diff --git a/backend/constant/app.go b/backend/constant/app.go index c50ad8934..655b4e3da 100644 --- a/backend/constant/app.go +++ b/backend/constant/app.go @@ -6,6 +6,7 @@ const ( Error = "Error" Stopped = "Stopped" Installing = "Installing" + Syncing = "Syncing" ContainerPrefix = "1Panel-" @@ -22,10 +23,13 @@ type AppOperate string var ( Up AppOperate = "up" Down AppOperate = "down" + Start AppOperate = "start" + Stop AppOperate = "stop" Restart AppOperate = "restart" Delete AppOperate = "delete" Sync AppOperate = "sync" Backup AppOperate = "backup" Restore AppOperate = "restore" Update AppOperate = "update" + Rebuild AppOperate = "rebuild" ) diff --git a/backend/utils/compose/compose.go b/backend/utils/compose/compose.go index a35a8e11e..b990275f0 100644 --- a/backend/utils/compose/compose.go +++ b/backend/utils/compose/compose.go @@ -16,6 +16,11 @@ func Down(filePath string) (string, error) { return stdout, err } +func Start(filePath string) (string, error) { + stdout, err := cmd.Execf("docker-compose -f %s start", filePath) + return stdout, err +} + func Stop(filePath string) (string, error) { stdout, err := cmd.Execf("docker-compose -f %s stop", filePath) return stdout, err diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 8a13f51f3..ea69fcc45 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -859,8 +859,9 @@ export default { status: 'Status', container: 'Container', restart: 'Restart', - up: 'Start', - down: 'Stop', + start: 'Start', + stop: 'Stop', + rebuild: 'Rebuild', name: 'Name', description: 'Description', delete: 'Delete', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index a875a3703..0a8fc16f6 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -864,8 +864,9 @@ export default { status: '状态', container: '容器', restart: '重启', - up: '启动', - down: '停止', + start: '启动', + stop: '停止', + rebuild: '重建', name: '名称', description: '描述', delete: '删除', diff --git a/frontend/src/views/app-store/installed/index.vue b/frontend/src/views/app-store/installed/index.vue index 035ecab28..c189c2838 100644 --- a/frontend/src/views/app-store/installed/index.vue +++ b/frontend/src/views/app-store/installed/index.vue @@ -297,6 +297,12 @@ const buttons = [ openOperate(row, 'sync'); }, }, + { + label: i18n.global.t('app.rebuild'), + click: (row: any) => { + openOperate(row, 'rebuild'); + }, + }, { label: i18n.global.t('app.restart'), click: (row: any) => { @@ -304,18 +310,18 @@ const buttons = [ }, }, { - label: i18n.global.t('app.up'), + label: i18n.global.t('app.start'), click: (row: any) => { - openOperate(row, 'up'); + openOperate(row, 'start'); }, disabled: (row: any) => { return row.status === 'Running'; }, }, { - label: i18n.global.t('app.down'), + label: i18n.global.t('app.stop'), click: (row: any) => { - openOperate(row, 'down'); + openOperate(row, 'stop'); }, disabled: (row: any) => { return row.status !== 'Running';