Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions backend/app/api/v1/app_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions backend/app/api/v1/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions backend/app/service/app_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
25 changes: 12 additions & 13 deletions backend/app/service/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
44 changes: 22 additions & 22 deletions backend/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down