1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +08:00

feat: 优化网站、应用删除逻辑 (#592)

This commit is contained in:
zhengkunwang223 2023-04-12 14:22:29 +08:00 committed by GitHub
parent b16308b7ea
commit 1bbf501783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 47 deletions

View File

@ -166,13 +166,10 @@ func (b *BaseApi) OperateInstalled(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
} }
tx, ctx := helper.GetTxAndContext() if err := appInstallService.Operate(req); err != nil {
if err := appInstallService.Operate(ctx, req); err != nil {
tx.Rollback()
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
tx.Commit()
helper.SuccessWithData(c, nil) helper.SuccessWithData(c, nil)
} }

View File

@ -125,14 +125,12 @@ func (b *BaseApi) DeleteWebsite(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return return
} }
tx, ctx := helper.GetTxAndContext()
err := websiteService.DeleteWebsite(ctx, req) err := websiteService.DeleteWebsite(req)
if err != nil { if err != nil {
tx.Rollback()
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return
} }
tx.Commit()
helper.SuccessWithData(c, nil) helper.SuccessWithData(c, nil)
} }

View File

@ -40,7 +40,7 @@ type IAppInstallService interface {
LoadPort(key string) (int64, error) LoadPort(key string) (int64, error)
LoadConnInfo(key string) (response.DatabaseConn, error) LoadConnInfo(key string) (response.DatabaseConn, error)
SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, 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 Update(req request.AppInstalledUpdate) error
SyncAll(systemInit bool) error SyncAll(systemInit bool) error
GetServices(key string) ([]response.AppService, error) GetServices(key string) ([]response.AppService, error)
@ -177,8 +177,8 @@ func (a *AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]
return handleInstalled(installs, false) return handleInstalled(installs, false)
} }
func (a *AppInstallService) Operate(ctx context.Context, req request.AppInstalledOperate) error { func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
install, err := appInstallRepo.GetFirstByCtx(ctx, commonRepo.WithByID(req.InstallId)) install, err := appInstallRepo.GetFirstByCtx(context.Background(), commonRepo.WithByID(req.InstallId))
if err != nil { if err != nil {
return err return err
} }
@ -205,7 +205,7 @@ func (a *AppInstallService) Operate(ctx context.Context, req request.AppInstalle
} }
return syncById(install.ID) return syncById(install.ID)
case constant.Delete: 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 err
} }
return nil return nil

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/subosito/gotenv" "github.com/subosito/gotenv"
"math" "math"
@ -145,7 +146,7 @@ func handleAppInstallErr(ctx context.Context, install *model.AppInstall) error {
return nil 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() op := files.NewFileOp()
appDir := install.GetPath() appDir := install.GetPath()
dir, _ := os.Stat(appDir) dir, _ := os.Stat(appDir)
@ -154,36 +155,34 @@ func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBacku
if err != nil && !forceDelete { if err != nil && !forceDelete {
return handleErr(install, err, out) 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 { if err := appInstallRepo.Delete(ctx, install); err != nil {
return err return err
} }
if err := deleteLink(ctx, &install, deleteDB, forceDelete); err != nil && !forceDelete { if err := deleteLink(ctx, &install, deleteDB, forceDelete); err != nil && !forceDelete {
return err 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) 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 { if _, err := os.Stat(uploadDir); err == nil {
_ = os.RemoveAll(uploadDir) _ = os.RemoveAll(uploadDir)
} }
if deleteBackup { if deleteBackup {
localDir, err := loadLocalDir() localDir, _ := loadLocalDir()
if err != nil && !forceDelete {
return err
}
backupDir := fmt.Sprintf("%s/app/%s/%s", localDir, install.App.Key, install.Name) backupDir := fmt.Sprintf("%s/app/%s/%s", localDir, install.App.Key, install.Name)
if _, err := os.Stat(backupDir); err == nil { if _, err := os.Stat(backupDir); err == nil {
_ = os.RemoveAll(backupDir) _ = os.RemoveAll(backupDir)
} }
global.LOG.Infof("delete app %s-%s backups successful", install.App.Key, install.Name) 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)) _ = op.DeleteDir(appDir)
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(install.App.Key)) tx.Commit()
if install.App.Key == constant.AppMysql {
_ = mysqlRepo.DeleteAll(ctx)
}
return nil return nil
} }

View File

@ -5,8 +5,10 @@ import (
"context" "context"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"gorm.io/gorm"
"os" "os"
"path" "path"
"reflect" "reflect"
@ -24,8 +26,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/pkg/errors"
"gorm.io/gorm"
) )
type WebsiteService struct { type WebsiteService struct {
@ -38,7 +38,7 @@ type IWebsiteService interface {
OpWebsite(req request.WebsiteOp) error OpWebsite(req request.WebsiteOp) error
GetWebsiteOptions() ([]string, error) GetWebsiteOptions() ([]string, error)
UpdateWebsite(req request.WebsiteUpdate) 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) GetWebsite(id uint) (response.WebsiteDTO, error)
CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error)
GetWebsiteDomain(websiteId uint) ([]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, Operate: constant.Delete,
ForceDelete: true, ForceDelete: true,
} }
if err := NewIAppInstalledService().Operate(context.Background(), req); err != nil { if err := NewIAppInstalledService().Operate(req); err != nil {
global.LOG.Errorf(err.Error()) global.LOG.Errorf(err.Error())
} }
} }
@ -323,7 +323,7 @@ func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) {
return res, nil 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)) website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil { if err != nil {
return err return err
@ -332,40 +332,40 @@ func (w WebsiteService) DeleteWebsite(ctx context.Context, req request.WebsiteDe
return err 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 { if checkIsLinkApp(website) && req.DeleteApp {
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID)) appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err return err
} }
if !reflect.DeepEqual(model.AppInstall{}, appInstall) { 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 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 { if req.DeleteBackup {
localDir, err := loadLocalDir() localDir, _ := loadLocalDir()
if err != nil && !req.ForceDelete {
return err
}
backupDir := fmt.Sprintf("%s/website/%s", localDir, website.Alias) backupDir := fmt.Sprintf("%s/website/%s", localDir, website.Alias)
if _, err := os.Stat(backupDir); err == nil { if _, err := os.Stat(backupDir); err == nil {
_ = os.RemoveAll(backupDir) _ = os.RemoveAll(backupDir)
} }
global.LOG.Infof("delete website %s backups successful", website.Alias) global.LOG.Infof("delete website %s backups successful", website.Alias)
} }
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("website"), commonRepo.WithByName(website.Alias)) uploadDir := fmt.Sprintf("%s/1panel/uploads/website/%s", global.CONF.System.BaseDir, website.Alias)
if _, err := os.Stat(uploadDir); err == nil {
if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil { _ = os.RemoveAll(uploadDir)
return err
}
if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil {
return err
} }
return nil return nil
} }
@ -932,7 +932,7 @@ func (w WebsiteService) UpdatePHPConfig(req request.WebsitePHPConfigUpdate) (err
InstallId: appInstall.ID, InstallId: appInstall.ID,
Operate: constant.Restart, 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) _ = fileOp.WriteFile(phpConfigPath, strings.NewReader(string(contentBytes)), 0755)
return err return err
} }