From 000c475626ce2af09ba14489bb0dc7b442dc13a9 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Tue, 27 Sep 2022 16:57:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=20=E4=BC=98=E5=8C=96=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/repo/app_install.go | 2 +- backend/app/service/app.go | 37 +++++-- backend/utils/common/common.go | 11 +++ backend/utils/compose/compose.go | 20 +--- backend/utils/files/file_op.go | 6 +- frontend/src/components/file-list/index.vue | 2 +- frontend/src/lang/modules/en.ts | 2 + frontend/src/lang/modules/zh.ts | 1 + frontend/src/routers/modules/app-store.ts | 22 +++++ .../src/views/app-store/detail/install.vue | 16 ++-- frontend/src/views/app-store/index.vue | 48 ++++++---- .../src/views/app-store/installed/index.vue | 96 +++++++++++++------ .../host/file-management/process/index.vue | 9 +- 13 files changed, 185 insertions(+), 87 deletions(-) diff --git a/backend/app/repo/app_install.go b/backend/app/repo/app_install.go index 09f43134d..1d130428c 100644 --- a/backend/app/repo/app_install.go +++ b/backend/app/repo/app_install.go @@ -23,7 +23,7 @@ func (a AppInstallRepo) Create(install *model.AppInstall) error { } func (a AppInstallRepo) Save(install model.AppInstall) error { - db := global.DB.Model(&model.AppInstall{}) + db := global.DB return db.Save(&install).Error } diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 6796412ef..9e3ef7ede 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -14,6 +14,7 @@ import ( "github.com/1Panel-dev/1Panel/utils/files" "github.com/joho/godotenv" "golang.org/x/net/context" + "math" "os" "path" "reflect" @@ -165,34 +166,42 @@ func (a AppService) Operate(req dto.AppInstallOperate) error { if err != nil { return handleErr(install, err, out) } + install.Status = constant.Running case dto.Down: out, err := compose.Down(dockerComposePath) if err != nil { return handleErr(install, err, out) } + install.Status = constant.Stopped case dto.Restart: out, err := compose.Restart(dockerComposePath) if err != nil { return handleErr(install, err, out) } + install.Status = constant.Running case dto.Delete: op := files.NewFileOp() appDir := path.Join(global.CONF.System.AppDir, install.App.Key, install.ContainerName) dir, _ := os.Stat(appDir) if dir == nil { - _ = appInstallRepo.Delete(commonRepo.WithByID(install.ID)) - break + return appInstallRepo.Delete(commonRepo.WithByID(install.ID)) } - _ = op.DeleteDir(appDir) - out, err := compose.Rmf(dockerComposePath) + out, err := compose.Down(dockerComposePath) if err != nil { return handleErr(install, err, out) } + out, err = compose.Rmf(dockerComposePath) + if err != nil { + return handleErr(install, err, out) + } + _ = op.DeleteDir(appDir) _ = appInstallRepo.Delete(commonRepo.WithByID(install.ID)) + return nil default: return errors.New("operate not support") } - return nil + + return appInstallRepo.Save(install) } func handleErr(install model.AppInstall, err error, out string) error { @@ -207,6 +216,15 @@ func handleErr(install model.AppInstall, err error, out string) error { } func (a AppService) Install(name string, appDetailId uint, params map[string]interface{}) error { + + port, ok := params["PORT"] + if ok { + port := int(math.Floor(port.(float64))) + if common.ScanPort(string(port)) { + return errors.New("port is in used") + } + } + appDetail, err := appDetailRepo.GetAppDetail(commonRepo.WithByID(appDetailId)) if err != nil { return err @@ -229,11 +247,11 @@ func (a AppService) Install(name string, appDetailId uint, params map[string]int } resourceDir := path.Join(global.CONF.System.ResourceDir, "apps", app.Key, appDetail.Version) installDir := path.Join(global.CONF.System.AppDir, app.Key) + installAppDir := path.Join(installDir, appDetail.Version) op := files.NewFileOp() - if err := op.CopyDir(resourceDir, installDir); err != nil { + if err := op.Copy(resourceDir, installAppDir); err != nil { return err } - installAppDir := path.Join(installDir, appDetail.Version) containerNameDir := path.Join(installDir, containerName) if err := op.Rename(installAppDir, containerNameDir); err != nil { return err @@ -279,6 +297,11 @@ func upApp(composeFilePath string, appInstall model.AppInstall) { } } +func (a AppService) SyncInstalled() error { + + return nil +} + func (a AppService) Sync() error { //TODO 从 oss 拉取最新列表 var appConfig model.AppConfig diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index da3135530..249f5351b 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -5,6 +5,7 @@ import ( "fmt" "io" mathRand "math/rand" + "net" "regexp" "strconv" "strings" @@ -60,3 +61,13 @@ func RandStr(n int) string { } return string(b) } + +func ScanPort(port string) bool { + + ln, err := net.Listen("tcp", ":"+port) + if err != nil { + return true + } + defer ln.Close() + return false +} diff --git a/backend/utils/compose/compose.go b/backend/utils/compose/compose.go index 6bbe44016..c98562a4b 100644 --- a/backend/utils/compose/compose.go +++ b/backend/utils/compose/compose.go @@ -5,35 +5,23 @@ import "os/exec" func Up(filePath string) (string, error) { cmd := exec.Command("docker-compose", "-f", filePath, "up", "-d") stdout, err := cmd.CombinedOutput() - if err != nil { - return string(stdout), err - } - return string(stdout), nil + return string(stdout), err } func Down(filePath string) (string, error) { cmd := exec.Command("docker-compose", "-f", filePath, "down") stdout, err := cmd.CombinedOutput() - if err != nil { - return "", err - } - return string(stdout), nil + return string(stdout), err } func Restart(filePath string) (string, error) { cmd := exec.Command("docker-compose", "-f", filePath, "restart") stdout, err := cmd.CombinedOutput() - if err != nil { - return "", err - } - return string(stdout), nil + return string(stdout), err } func Rmf(filePath string) (string, error) { cmd := exec.Command("docker-compose", "-f", filePath, "rm", "-f") stdout, err := cmd.CombinedOutput() - if err != nil { - return "", err - } - return string(stdout), nil + return string(stdout), err } diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 5256293a5..7dffe82f8 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -207,8 +207,8 @@ func (f FileOp) CopyDir(src, dst string) error { if err != nil { return err } - dstDir := filepath.Join(dst, srcInfo.Name()) - if err := f.Fs.MkdirAll(dstDir, srcInfo.Mode()); err != nil { + //dstDir := filepath.Join(dst, srcInfo.Name()) + if err := f.Fs.MkdirAll(dst, srcInfo.Mode()); err != nil { return err } @@ -221,7 +221,7 @@ func (f FileOp) CopyDir(src, dst string) error { for _, obj := range obs { fSrc := filepath.Join(src, obj.Name()) - fDst := filepath.Join(dstDir, obj.Name()) + fDst := filepath.Join(dst, obj.Name()) if obj.IsDir() { err = f.CopyDir(fSrc, fDst) diff --git a/frontend/src/components/file-list/index.vue b/frontend/src/components/file-list/index.vue index 2dca2d591..1d2f6bb1b 100644 --- a/frontend/src/components/file-list/index.vue +++ b/frontend/src/components/file-list/index.vue @@ -48,7 +48,7 @@ let rowName = ref(''); let data = ref(); let loading = ref(false); let paths = ref([]); -let req = reactive({ path: '/', expand: true, page: 1, pageSize: 20 }); +let req = reactive({ path: '/', expand: true, page: 1, pageSize: 300 }); const props = defineProps({ path: { diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index c103e04e3..a7807b596 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -415,5 +415,7 @@ export default { name: 'Name', description: 'Description', delete: 'Delete', + deleteWarn: + 'Delete the operation data and delete the operation data. This operation cannot be rolled back. Do you want to continue?', }, }; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 8b8c25c95..636eef503 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -407,5 +407,6 @@ export default { name: '名称', description: '描述', delete: '删除', + deleteWarn: '删除操作会把数据一并删除,此操作不可回滚,是否继续?', }, }; diff --git a/frontend/src/routers/modules/app-store.ts b/frontend/src/routers/modules/app-store.ts index 940083e17..6071e90d4 100644 --- a/frontend/src/routers/modules/app-store.ts +++ b/frontend/src/routers/modules/app-store.ts @@ -15,6 +15,28 @@ const appStoreRouter = { name: 'App', component: () => import('@/views/app-store/index.vue'), meta: {}, + children: [ + { + path: 'all', + name: 'AppAll', + component: () => import('@/views/app-store/apps/index.vue'), + props: true, + hidden: true, + meta: { + activeMenu: '/apps', + }, + }, + { + path: 'installed', + name: 'AppInstalled', + component: () => import('@/views/app-store/installed/index.vue'), + props: true, + hidden: true, + meta: { + activeMenu: '/apps', + }, + }, + ], }, { path: '/apps/detail/:id', diff --git a/frontend/src/views/app-store/detail/install.vue b/frontend/src/views/app-store/detail/install.vue index d078061c5..b71c1a310 100644 --- a/frontend/src/views/app-store/detail/install.vue +++ b/frontend/src/views/app-store/detail/install.vue @@ -6,11 +6,8 @@
- + +
-