guest_disk_snapshot_task.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/apis/compute"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. func init() {
  28. taskman.RegisterTask(GuestDiskSnapshotTask{})
  29. }
  30. type GuestDiskSnapshotTask struct {
  31. SGuestBaseTask
  32. }
  33. func (self *GuestDiskSnapshotTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  34. guest := obj.(*models.SGuest)
  35. self.DoDiskSnapshot(ctx, guest)
  36. }
  37. func (self *GuestDiskSnapshotTask) DoDiskSnapshot(ctx context.Context, guest *models.SGuest) {
  38. diskId, err := self.Params.GetString("disk_id")
  39. if err != nil {
  40. self.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
  41. return
  42. }
  43. snapshotId, err := self.Params.GetString("snapshot_id")
  44. if err != nil {
  45. self.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
  46. return
  47. }
  48. params := jsonutils.NewDict()
  49. params.Set("guest_old_status", jsonutils.NewString(guest.Status))
  50. self.SetStage("OnDiskSnapshotComplete", params)
  51. guest.SetStatus(ctx, self.UserCred, api.VM_SNAPSHOT, "")
  52. drv, err := guest.GetDriver()
  53. if err != nil {
  54. self.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
  55. return
  56. }
  57. err = drv.RequestDiskSnapshot(ctx, guest, self, snapshotId, diskId)
  58. if err != nil {
  59. self.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
  60. return
  61. }
  62. }
  63. func (self *GuestDiskSnapshotTask) OnDiskSnapshotComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  64. res := data.(*jsonutils.JSONDict)
  65. snapshotId, _ := self.Params.GetString("snapshot_id")
  66. iSnapshot, _ := models.SnapshotManager.FetchById(snapshotId)
  67. snapshot := iSnapshot.(*models.SSnapshot)
  68. location, err := res.GetString("location")
  69. if err != nil {
  70. log.Infof("OnDiskSnapshotComplete called with data no location")
  71. return
  72. }
  73. var osType string
  74. if snapshot.DiskType == compute.DISK_TYPE_SYS {
  75. osType = guest.GetOS()
  76. }
  77. _, err = db.Update(snapshot, func() error {
  78. snapshot.Location = location
  79. snapshot.Status = api.SNAPSHOT_READY
  80. snapshot.OsType = osType
  81. return nil
  82. })
  83. if err != nil {
  84. self.TaskFailed(ctx, guest, jsonutils.NewString(fmt.Sprintf("update sanpshot failed: %s", err)))
  85. return
  86. }
  87. self.TaskComplete(ctx, guest, nil)
  88. }
  89. func (self *GuestDiskSnapshotTask) OnDiskSnapshotCompleteFailed(ctx context.Context, guest *models.SGuest, err jsonutils.JSONObject) {
  90. self.TaskFailed(ctx, guest, err)
  91. }
  92. func (self *GuestDiskSnapshotTask) TaskComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  93. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_DISK_CREATE_SNAPSHOT, nil, self.UserCred, true)
  94. status, _ := self.Params.GetString("guest_old_status")
  95. guest.SetStatus(ctx, self.UserCred, status, "on guest disk snapshot complete")
  96. self.SetStageComplete(ctx, nil)
  97. }
  98. func (self *GuestDiskSnapshotTask) TaskFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  99. guest.SetStatus(ctx, self.UserCred, api.VM_SNAPSHOT_FAILED, reason.String())
  100. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_DISK_CREATE_SNAPSHOT, reason, self.UserCred, false)
  101. self.SetStageFailed(ctx, reason)
  102. }