instance_snapshot_reset_task.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type InstanceSnapshotResetTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(InstanceSnapshotResetTask{})
  30. }
  31. func (self *InstanceSnapshotResetTask) taskFail(
  32. ctx context.Context, isp *models.SInstanceSnapshot, guest *models.SGuest, reason jsonutils.JSONObject) {
  33. if guest == nil {
  34. guest = models.GuestManager.FetchGuestById(isp.GuestId)
  35. }
  36. guest.SetStatus(ctx, self.UserCred, compute.VM_SNAPSHOT_RESET_FAILED, reason.String())
  37. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_READY, "")
  38. db.OpsLog.LogEvent(guest, db.ACT_VM_RESET_SNAPSHOT_FAILED, reason, self.UserCred)
  39. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_RESET, reason, self.UserCred, false)
  40. notifyclient.NotifySystemErrorWithCtx(ctx, guest.GetId(), isp.Name, compute.VM_SNAPSHOT_RESET_FAILED, reason.String())
  41. self.SetStageFailed(ctx, reason)
  42. }
  43. func (self *InstanceSnapshotResetTask) taskComplete(
  44. ctx context.Context, isp *models.SInstanceSnapshot, guest *models.SGuest, data jsonutils.JSONObject) {
  45. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_READY, "")
  46. if guest == nil {
  47. guest = models.GuestManager.FetchGuestById(isp.GuestId)
  48. }
  49. guest.StartSyncstatus(ctx, self.UserCred, "")
  50. db.OpsLog.LogEvent(isp, db.ACT_VM_RESET_SNAPSHOT, "instance snapshot reset success", self.UserCred)
  51. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_RESET, false, self.UserCred, true)
  52. self.SetStageComplete(ctx, nil)
  53. }
  54. func (self *InstanceSnapshotResetTask) OnInit(
  55. ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  56. isp := obj.(*models.SInstanceSnapshot)
  57. guest := models.GuestManager.FetchGuestById(isp.GuestId)
  58. self.SetStage("OnInstanceSnapshotReset", nil)
  59. params := jsonutils.NewDict()
  60. params.Set("disk_index", jsonutils.NewInt(0))
  61. withMem := jsonutils.QueryBoolean(self.Params, "with_memory", false)
  62. params.Set("with_memory", jsonutils.NewBool(withMem))
  63. if err := isp.GetRegionDriver().RequestResetToInstanceSnapshot(ctx, guest, isp, self, params); err != nil {
  64. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  65. return
  66. }
  67. }
  68. func (self *InstanceSnapshotResetTask) OnKvmDiskReset(
  69. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  70. guest := models.GuestManager.FetchGuestById(isp.GuestId)
  71. diskIndex, err := self.Params.Int("disk_index")
  72. if err != nil {
  73. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  74. return
  75. }
  76. params := jsonutils.NewDict()
  77. params.Set("disk_index", jsonutils.NewInt(diskIndex+1))
  78. withMem := jsonutils.QueryBoolean(self.Params, "with_memory", false)
  79. params.Set("with_memory", jsonutils.NewBool(withMem))
  80. if err := isp.GetRegionDriver().RequestResetToInstanceSnapshot(ctx, guest, isp, self, params); err != nil {
  81. self.taskFail(ctx, isp, guest, jsonutils.NewString(err.Error()))
  82. return
  83. }
  84. }
  85. func (self *InstanceSnapshotResetTask) OnKvmDiskResetFailed(
  86. ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  87. self.taskFail(ctx, isp, nil, data)
  88. }
  89. func (self *InstanceSnapshotResetTask) OnInstanceSnapshotReset(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  90. guest, _ := isp.GetGuest()
  91. if jsonutils.QueryBoolean(self.Params, "auto_start", false) {
  92. self.SetStage("OnGuestStartComplete", nil)
  93. isp.SetStatus(ctx, self.UserCred, compute.INSTANCE_SNAPSHOT_READY, "")
  94. guest.StartGueststartTask(ctx, self.UserCred, nil, self.GetTaskId())
  95. } else {
  96. self.taskComplete(ctx, isp, guest, data)
  97. }
  98. }
  99. func (self *InstanceSnapshotResetTask) OnInstanceSnapshotResetFailed(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  100. self.taskFail(ctx, isp, nil, data)
  101. }
  102. func (self *InstanceSnapshotResetTask) OnGuestStartComplete(ctx context.Context, isp *models.SInstanceSnapshot, data jsonutils.JSONObject) {
  103. guest, _ := isp.GetGuest()
  104. self.taskComplete(ctx, isp, guest, data)
  105. }