instance_snapshot_create_task.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 snapshot
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type InstanceSnapshotCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(InstanceSnapshotCreateTask{})
  32. }
  33. func (self *InstanceSnapshotCreateTask) SetStageFailed(ctx context.Context, reason jsonutils.JSONObject) {
  34. self.finalReleasePendingUsage(ctx)
  35. self.STask.SetStageFailed(ctx, reason)
  36. }
  37. func (self *InstanceSnapshotCreateTask) finalReleasePendingUsage(ctx context.Context) {
  38. pendingUsage := models.SRegionQuota{}
  39. err := self.GetPendingUsage(&pendingUsage, 0)
  40. if err == nil && !pendingUsage.IsEmpty() {
  41. quotas.CancelPendingUsage(ctx, self.UserCred, &pendingUsage, &pendingUsage, true) // final cleanup
  42. }
  43. }
  44. func (self *InstanceSnapshotCreateTask) taskFail(
  45. ctx context.Context, isp *models.SInstanceSnapshot, guest *models.SGuest, reason jsonutils.JSONObject) {
  46. if guest == nil {
  47. guest = models.GuestManager.FetchGuestById(isp.GuestId)
  48. }
  49. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_FAILED, reason.String())
  50. guest.SetStatus(ctx, self.UserCred, compute.VM_INSTANCE_SNAPSHOT_FAILED, reason.String())
  51. db.OpsLog.LogEvent(isp, db.ACT_ALLOCATE_FAIL, reason, self.UserCred)
  52. logclient.AddActionLogWithStartable(self, isp, logclient.ACT_CREATE, reason, self.UserCred, false)
  53. notifyclient.NotifySystemErrorWithCtx(ctx, isp.GetId(), isp.Name, compute.INSTANCE_SNAPSHOT_FAILED, reason.String())
  54. self.SetStageFailed(ctx, reason)
  55. }
  56. func (self *InstanceSnapshotCreateTask) taskComplete(
  57. ctx context.Context, isp *models.SInstanceSnapshot, guest *models.SGuest, data jsonutils.JSONObject) {
  58. self.finalReleasePendingUsage(ctx)
  59. if guest == nil {
  60. guest = models.GuestManager.FetchGuestById(isp.GuestId)
  61. }
  62. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_READY, "")
  63. guest.StartSyncstatus(ctx, self.UserCred, "")
  64. db.OpsLog.LogEvent(isp, db.ACT_ALLOCATE, "instance snapshot create success", self.UserCred)
  65. logclient.AddActionLogWithStartable(self, isp, logclient.ACT_CREATE, "", self.UserCred, true)
  66. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  67. Obj: isp,
  68. Action: notifyclient.ActionCreate,
  69. })
  70. self.SetStageComplete(ctx, nil)
  71. }
  72. func (self *InstanceSnapshotCreateTask) OnInit(
  73. ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  74. isp := obj.(*models.SInstanceSnapshot)
  75. guest := models.GuestManager.FetchGuestById(isp.GuestId)
  76. self.SetStage("OnInstanceSnapshot", nil)
  77. params := jsonutils.NewDict()
  78. params.Set("disk_index", jsonutils.NewInt(0))
  79. if err := isp.GetRegionDriver().RequestCreateInstanceSnapshot(ctx, guest, isp, self, params); err != nil {
  80. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  81. return
  82. }
  83. }
  84. func (self *InstanceSnapshotCreateTask) OnKvmDiskSnapshot(
  85. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  86. guest := models.GuestManager.FetchGuestById(isp.GuestId)
  87. diskIndex, err := self.Params.Int("disk_index")
  88. if err != nil {
  89. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  90. return
  91. }
  92. params := jsonutils.NewDict()
  93. params.Set("disk_index", jsonutils.NewInt(diskIndex+1))
  94. if err := isp.GetRegionDriver().RequestCreateInstanceSnapshot(ctx, guest, isp, self, params); err != nil {
  95. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  96. return
  97. }
  98. }
  99. func (self *InstanceSnapshotCreateTask) OnKvmDiskSnapshotFailed(
  100. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  101. self.taskFail(ctx, isp, nil, data)
  102. }
  103. func (self *InstanceSnapshotCreateTask) OnInstanceSnapshot(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  104. guest, _ := isp.GetGuest()
  105. if isp.WithMemory {
  106. resp := new(hostapi.GuestMemorySnapshotResponse)
  107. if err := data.Unmarshal(resp); err != nil {
  108. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  109. return
  110. }
  111. if _, err := db.Update(isp, func() error {
  112. isp.MemorySizeKB = int(resp.SizeKB)
  113. isp.MemoryFilePath = resp.MemorySnapshotPath
  114. isp.MemoryFileChecksum = resp.Checksum
  115. return nil
  116. }); err != nil {
  117. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  118. return
  119. }
  120. }
  121. self.taskComplete(ctx, isp, guest, data)
  122. }
  123. func (self *InstanceSnapshotCreateTask) OnInstanceSnapshotFailed(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  124. self.taskFail(ctx, isp, nil, data)
  125. }