image_delete_task.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tasks
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/image"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/image/models"
  25. "yunion.io/x/onecloud/pkg/image/options"
  26. )
  27. type ImageDeleteTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(ImageDeleteTask{})
  32. }
  33. func (self *ImageDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  34. image := obj.(*models.SImage)
  35. // imageStatus, _ := self.Params.GetString("image_status")
  36. isPurge := jsonutils.QueryBoolean(self.Params, "purge", false)
  37. isOverridePendingDelete := jsonutils.QueryBoolean(self.Params, "override_pending_delete", false)
  38. if options.Options.EnablePendingDelete && !isPurge && !isOverridePendingDelete {
  39. // imageStatus == models.IMAGE_STATUS_ACTIVE
  40. if image.PendingDeleted {
  41. self.SetStageComplete(ctx, nil)
  42. return
  43. }
  44. self.startPendingDeleteImage(ctx, image)
  45. } else {
  46. self.startDeleteImage(ctx, image)
  47. }
  48. }
  49. func (self *ImageDeleteTask) startPendingDeleteImage(ctx context.Context, image *models.SImage) {
  50. image.StopTorrents()
  51. image.DoPendingDelete(ctx, self.UserCred)
  52. self.SetStageComplete(ctx, nil)
  53. }
  54. func (self *ImageDeleteTask) startDeleteImage(ctx context.Context, image *models.SImage) {
  55. err := image.Remove(ctx, self.UserCred)
  56. if err != nil {
  57. msg := fmt.Sprintf("fail to remove %s %s", image.Name, err)
  58. log.Errorf("%v", msg)
  59. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  60. return
  61. }
  62. image.SetStatus(ctx, self.UserCred, api.IMAGE_STATUS_DELETED, "delete")
  63. image.RealDelete(ctx, self.UserCred)
  64. self.SetStageComplete(ctx, nil)
  65. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  66. Obj: image,
  67. Action: notifyclient.ActionDelete,
  68. })
  69. }