From 1bbf501783f945d85056632d769a70258b887677 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:22:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E3=80=81=E5=BA=94=E7=94=A8=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20(#592)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/app_install.go | 5 +--- backend/app/api/v1/website.go | 6 ++-- backend/app/service/app_install.go | 8 +++--- backend/app/service/app_utils.go | 25 ++++++++--------- backend/app/service/website.go | 44 +++++++++++++++--------------- 5 files changed, 41 insertions(+), 47 deletions(-) diff --git a/backend/app/api/v1/app_install.go b/backend/app/api/v1/app_install.go index 1f055b475..928a80bc9 100644 --- a/backend/app/api/v1/app_install.go +++ b/backend/app/api/v1/app_install.go @@ -166,13 +166,10 @@ func (b *BaseApi) OperateInstalled(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - tx, ctx := helper.GetTxAndContext() - if err := appInstallService.Operate(ctx, req); err != nil { - tx.Rollback() + if err := appInstallService.Operate(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } - tx.Commit() helper.SuccessWithData(c, nil) } diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 0dd2f86b3..b40fe0a5f 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -125,14 +125,12 @@ func (b *BaseApi) DeleteWebsite(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - tx, ctx := helper.GetTxAndContext() - err := websiteService.DeleteWebsite(ctx, req) + + err := websiteService.DeleteWebsite(req) if err != nil { - tx.Rollback() helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } - tx.Commit() helper.SuccessWithData(c, nil) } diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index 2049f53e6..5210dec85 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -40,7 +40,7 @@ type IAppInstallService interface { LoadPort(key string) (int64, error) LoadConnInfo(key string) (response.DatabaseConn, error) SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) - Operate(ctx context.Context, req request.AppInstalledOperate) error + Operate(req request.AppInstalledOperate) error Update(req request.AppInstalledUpdate) error SyncAll(systemInit bool) error GetServices(key string) ([]response.AppService, error) @@ -177,8 +177,8 @@ func (a *AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([] return handleInstalled(installs, false) } -func (a *AppInstallService) Operate(ctx context.Context, req request.AppInstalledOperate) error { - install, err := appInstallRepo.GetFirstByCtx(ctx, commonRepo.WithByID(req.InstallId)) +func (a *AppInstallService) Operate(req request.AppInstalledOperate) error { + install, err := appInstallRepo.GetFirstByCtx(context.Background(), commonRepo.WithByID(req.InstallId)) if err != nil { return err } @@ -205,7 +205,7 @@ func (a *AppInstallService) Operate(ctx context.Context, req request.AppInstalle } return syncById(install.ID) case constant.Delete: - if err := deleteAppInstall(ctx, install, req.DeleteBackup, req.ForceDelete, req.DeleteDB); err != nil && !req.ForceDelete { + if err := deleteAppInstall(install, req.DeleteBackup, req.ForceDelete, req.DeleteDB); err != nil && !req.ForceDelete { return err } return nil diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 287613666..5fb53ccf9 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/compose-spec/compose-go/types" "github.com/subosito/gotenv" "math" @@ -145,7 +146,7 @@ func handleAppInstallErr(ctx context.Context, install *model.AppInstall) error { return nil } -func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBackup bool, forceDelete bool, deleteDB bool) error { +func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete bool, deleteDB bool) error { op := files.NewFileOp() appDir := install.GetPath() dir, _ := os.Stat(appDir) @@ -154,36 +155,34 @@ func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBacku if err != nil && !forceDelete { return handleErr(install, err, out) } - if err := op.DeleteDir(appDir); err != nil && !forceDelete { - return err - } } + tx, ctx := helper.GetTxAndContext() + defer tx.Rollback() if err := appInstallRepo.Delete(ctx, install); err != nil { return err } if err := deleteLink(ctx, &install, deleteDB, forceDelete); err != nil && !forceDelete { return err } + _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("app"), commonRepo.WithByName(install.App.Key), backupRepo.WithByDetailName(install.Name)) + _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(install.App.Key)) + if install.App.Key == constant.AppMysql { + _ = mysqlRepo.DeleteAll(ctx) + } uploadDir := fmt.Sprintf("%s/1panel/uploads/app/%s/%s", global.CONF.System.BaseDir, install.App.Key, install.Name) if _, err := os.Stat(uploadDir); err == nil { _ = os.RemoveAll(uploadDir) } if deleteBackup { - localDir, err := loadLocalDir() - if err != nil && !forceDelete { - return err - } + localDir, _ := loadLocalDir() backupDir := fmt.Sprintf("%s/app/%s/%s", localDir, install.App.Key, install.Name) if _, err := os.Stat(backupDir); err == nil { _ = os.RemoveAll(backupDir) } global.LOG.Infof("delete app %s-%s backups successful", install.App.Key, install.Name) } - _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("app"), commonRepo.WithByName(install.App.Key), backupRepo.WithByDetailName(install.Name)) - _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(install.App.Key)) - if install.App.Key == constant.AppMysql { - _ = mysqlRepo.DeleteAll(ctx) - } + _ = op.DeleteDir(appDir) + tx.Commit() return nil } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 0e764d014..1273d445e 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -5,8 +5,10 @@ import ( "context" "crypto/x509" "encoding/pem" + "errors" "fmt" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" + "gorm.io/gorm" "os" "path" "reflect" @@ -24,8 +26,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/utils/files" - "github.com/pkg/errors" - "gorm.io/gorm" ) type WebsiteService struct { @@ -38,7 +38,7 @@ type IWebsiteService interface { OpWebsite(req request.WebsiteOp) error GetWebsiteOptions() ([]string, error) UpdateWebsite(req request.WebsiteUpdate) error - DeleteWebsite(ctx context.Context, req request.WebsiteDelete) error + DeleteWebsite(req request.WebsiteDelete) error GetWebsite(id uint) (response.WebsiteDTO, error) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) @@ -159,7 +159,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error) Operate: constant.Delete, ForceDelete: true, } - if err := NewIAppInstalledService().Operate(context.Background(), req); err != nil { + if err := NewIAppInstalledService().Operate(req); err != nil { global.LOG.Errorf(err.Error()) } } @@ -323,7 +323,7 @@ func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) { return res, nil } -func (w WebsiteService) DeleteWebsite(ctx context.Context, req request.WebsiteDelete) error { +func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error { website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID)) if err != nil { return err @@ -332,40 +332,40 @@ func (w WebsiteService) DeleteWebsite(ctx context.Context, req request.WebsiteDe return err } + tx, ctx := helper.GetTxAndContext() + defer tx.Rollback() + _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("website"), commonRepo.WithByName(website.Alias)) + if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil { + return err + } + if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil { + return err + } + if checkIsLinkApp(website) && req.DeleteApp { appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID)) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return err } if !reflect.DeepEqual(model.AppInstall{}, appInstall) { - if err := deleteAppInstall(ctx, appInstall, true, req.ForceDelete, true); err != nil && !req.ForceDelete { + if err := deleteAppInstall(appInstall, true, req.ForceDelete, true); err != nil && !req.ForceDelete { return err } } } + tx.Commit() - uploadDir := fmt.Sprintf("%s/1panel/uploads/website/%s", global.CONF.System.BaseDir, website.Alias) - if _, err := os.Stat(uploadDir); err == nil { - _ = os.RemoveAll(uploadDir) - } if req.DeleteBackup { - localDir, err := loadLocalDir() - if err != nil && !req.ForceDelete { - return err - } + localDir, _ := loadLocalDir() backupDir := fmt.Sprintf("%s/website/%s", localDir, website.Alias) if _, err := os.Stat(backupDir); err == nil { _ = os.RemoveAll(backupDir) } global.LOG.Infof("delete website %s backups successful", website.Alias) } - _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("website"), commonRepo.WithByName(website.Alias)) - - if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil { - return err - } - if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil { - return err + uploadDir := fmt.Sprintf("%s/1panel/uploads/website/%s", global.CONF.System.BaseDir, website.Alias) + if _, err := os.Stat(uploadDir); err == nil { + _ = os.RemoveAll(uploadDir) } return nil } @@ -932,7 +932,7 @@ func (w WebsiteService) UpdatePHPConfig(req request.WebsitePHPConfigUpdate) (err InstallId: appInstall.ID, Operate: constant.Restart, } - if err = NewIAppInstalledService().Operate(context.Background(), appInstallReq); err != nil { + if err = NewIAppInstalledService().Operate(appInstallReq); err != nil { _ = fileOp.WriteFile(phpConfigPath, strings.NewReader(string(contentBytes)), 0755) return err }