Merge pull request #10581 from swordqiu/hotfix/qj-recycle-disk-when-rebuild-root

fix(hostman): recycle local disk file when rebuilding root
This commit is contained in:
Zexi Li
2021-04-02 19:42:41 +08:00
committed by GitHub
2 changed files with 7 additions and 3 deletions
@@ -71,7 +71,7 @@ func AddDiskHandler(prefix string, app *appsrv.Application) {
app.AddHandler("POST",
fmt.Sprintf("%s/%s/<storageId>/<action>/<diskId>", prefix, keyWord),
auth.Authenticate(perfomrDiskActions))
auth.Authenticate(performDiskActions))
app.AddHandler("GET",
fmt.Sprintf("%s/%s/<storageId>/<diskId>/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)))
+3
View File
@@ -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()
}
}