guest_delete_on_host_task.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 guest
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. func init() {
  26. taskman.RegisterTask(GuestDeleteOnHostTask{})
  27. }
  28. type GuestDeleteOnHostTask struct {
  29. SGuestBaseTask
  30. }
  31. func (self *GuestDeleteOnHostTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  32. guest := obj.(*models.SGuest)
  33. hostId, err := self.Params.GetString("host_id")
  34. if err != nil {
  35. self.OnFail(ctx, guest, jsonutils.NewString("Missing param host_id"))
  36. return
  37. }
  38. host := models.HostManager.FetchHostById(hostId)
  39. if host == nil {
  40. self.OnFail(ctx, guest, jsonutils.NewString("Host is nil"))
  41. return
  42. }
  43. self.SetStage("OnStopGuest", nil)
  44. self.Params.Set("is_force", jsonutils.JSONTrue)
  45. drv, err := guest.GetDriver()
  46. if err != nil {
  47. self.OnFail(ctx, guest, jsonutils.NewString(err.Error()))
  48. return
  49. }
  50. if err := drv.RequestStopOnHost(ctx, guest, host, self, true); err != nil {
  51. log.Errorf("RequestStopGuestForDelete fail %s", err)
  52. self.OnStopGuest(ctx, guest, nil)
  53. }
  54. }
  55. func (self *GuestDeleteOnHostTask) OnStopGuest(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  56. hostId, _ := self.Params.GetString("host_id")
  57. isPurge := jsonutils.QueryBoolean(self.Params, "purge", false)
  58. if !isPurge {
  59. self.SetStage("OnUnDeployGuest", nil)
  60. guest.StartUndeployGuestTask(ctx, self.GetUserCred(), self.GetTaskId(), hostId)
  61. } else {
  62. self.OnUnDeployGuest(ctx, guest, nil)
  63. }
  64. }
  65. func (self *GuestDeleteOnHostTask) OnUnDeployGuestFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  66. self.OnFail(ctx, guest, data)
  67. }
  68. func (self *GuestDeleteOnHostTask) OnUnDeployGuest(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  69. hostId, _ := self.Params.GetString("host_id")
  70. if guest.BackupHostId == hostId {
  71. _, err := db.Update(guest, func() error {
  72. guest.BackupHostId = ""
  73. guest.BackupGuestStatus = compute.VM_INIT
  74. return nil
  75. })
  76. if err != nil {
  77. self.OnFail(ctx, guest, jsonutils.NewString(err.Error()))
  78. return
  79. }
  80. disks, err := guest.GetDisks()
  81. if err != nil {
  82. self.OnFail(ctx, guest, jsonutils.NewString(err.Error()))
  83. return
  84. }
  85. for i := 0; i < len(disks); i++ {
  86. disk := &disks[i]
  87. _, err := db.Update(disk, func() error {
  88. disk.BackupStorageId = ""
  89. return nil
  90. })
  91. if err != nil {
  92. self.OnFail(ctx, guest, jsonutils.NewString(err.Error()))
  93. return
  94. }
  95. }
  96. logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_DELETE_BACKUP, "GuestDeleteOnHost", self.UserCred, true)
  97. db.OpsLog.LogEvent(guest, db.ACT_DELETE_BACKUP, guest.GetShortDesc(ctx), self.UserCred)
  98. }
  99. self.SetStage("OnSync", nil)
  100. guest.StartSyncTask(ctx, self.UserCred, false, self.GetTaskId())
  101. }
  102. func (self *GuestDeleteOnHostTask) OnSync(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  103. self.SetStageComplete(ctx, nil)
  104. }
  105. func (self *GuestDeleteOnHostTask) OnFail(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  106. hostId, _ := self.Params.GetString("host_id")
  107. if guest.BackupHostId == hostId {
  108. logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_DELETE_BACKUP, "GuestDeleteOnHost", self.UserCred, false)
  109. db.OpsLog.LogEvent(guest, db.ACT_DELETE_BACKUP_FAILED, "GuestDeleteOnHost", self.UserCred)
  110. }
  111. failedStatus, _ := self.Params.GetString("failed_status")
  112. if len(failedStatus) > 0 {
  113. guest.SetStatus(ctx, self.UserCred, failedStatus, reason.String())
  114. }
  115. self.SetStageFailed(ctx, reason)
  116. }