From 225afd447303b7e42f4c42aa0f97f3a11a2d5530 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 2 Apr 2021 02:14:15 +0800 Subject: [PATCH] fix(hostman): recycle local disk file when rebuilding root --- pkg/hostman/storageman/diskhandlers/diskhandler.go | 7 ++++--- pkg/hostman/storageman/storage_local.go | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/hostman/storageman/diskhandlers/diskhandler.go b/pkg/hostman/storageman/diskhandlers/diskhandler.go index 3f0c614dc5..349184b825 100644 --- a/pkg/hostman/storageman/diskhandlers/diskhandler.go +++ b/pkg/hostman/storageman/diskhandlers/diskhandler.go @@ -71,7 +71,7 @@ func AddDiskHandler(prefix string, app *appsrv.Application) { app.AddHandler("POST", fmt.Sprintf("%s/%s///", prefix, keyWord), - auth.Authenticate(perfomrDiskActions)) + auth.Authenticate(performDiskActions)) app.AddHandler("GET", fmt.Sprintf("%s/%s///status", prefix, keyWord), auth.Authenticate(getDiskStatus)) @@ -206,7 +206,7 @@ func saveToGlance(ctx context.Context, w http.ResponseWriter, r *http.Request) { hostutils.ResponseOk(ctx, w) } -func perfomrDiskActions(ctx context.Context, w http.ResponseWriter, r *http.Request) { +func performDiskActions(ctx context.Context, w http.ResponseWriter, r *http.Request) { params, _, body := appsrv.FetchEnv(ctx, w, r) if body == nil { body = jsonutils.NewDict() @@ -230,7 +230,8 @@ func perfomrDiskActions(ctx context.Context, w http.ResponseWriter, r *http.Requ var disk storageman.IDisk var err error - if action != "create" { + rebuild, _ := body.Bool("disk", "rebuild") + if action != "create" || rebuild { disk, err = storage.GetDiskById(diskId) if err != nil { hostutils.Response(ctx, w, httperrors.NewGeneralError(errors.Wrapf(err, "GetDiskById(%s)", diskId))) diff --git a/pkg/hostman/storageman/storage_local.go b/pkg/hostman/storageman/storage_local.go index 7f618ffe47..4620d710fb 100644 --- a/pkg/hostman/storageman/storage_local.go +++ b/pkg/hostman/storageman/storage_local.go @@ -192,10 +192,13 @@ func (s *SLocalStorage) DeleteDiskfile(diskpath string) error { destFile = fmt.Sprintf("%s.%d", path.Base(diskpath), time.Now().Unix()) ) if err := procutils.NewCommand("mkdir", "-p", destDir).Run(); err != nil { + log.Errorf("Fail to mkdir %s for recycle: %s", destDir, err) return err } + log.Infof("Move deleted disk file %s to recycle %s", diskpath, destDir) return procutils.NewCommand("mv", "-f", diskpath, path.Join(destDir, destFile)).Run() } else { + log.Infof("Delete disk file %s immediately", diskpath) return procutils.NewCommand("rm", "-rf", diskpath).Run() } }