disk_backup_recovery_task.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 backup
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type DiskBackupRecoveryTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(DiskBackupRecoveryTask{})
  31. }
  32. func (self *DiskBackupRecoveryTask) taskFaild(ctx context.Context, backup *models.SDiskBackup, reason jsonutils.JSONObject) {
  33. reasonStr, _ := reason.GetString()
  34. backup.SetStatus(ctx, self.UserCred, api.BACKUP_STATUS_RECOVERY_FAILED, reasonStr)
  35. logclient.AddActionLogWithStartable(self, backup, logclient.ACT_RECOVERY, reason, self.UserCred, false)
  36. self.SetStageFailed(ctx, reason)
  37. }
  38. func (self *DiskBackupRecoveryTask) taskSuccess(ctx context.Context, backup *models.SDiskBackup, data *jsonutils.JSONDict) {
  39. backup.SetStatus(ctx, self.UserCred, api.BACKUP_STATUS_READY, "")
  40. logclient.AddActionLogWithStartable(self, backup, logclient.ACT_RECOVERY, nil, self.UserCred, true)
  41. self.SetStageComplete(ctx, data)
  42. }
  43. func (self *DiskBackupRecoveryTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  44. backup := obj.(*models.SDiskBackup)
  45. diskName, _ := self.Params.GetString("disk_name")
  46. if diskName == "" {
  47. diskName = backup.DiskConfig.Name
  48. }
  49. diskConfig := &backup.DiskConfig.DiskConfig
  50. diskConfig.ImageId = ""
  51. diskConfig.SnapshotId = ""
  52. diskConfig.BackupId = backup.GetId()
  53. input := api.DiskCreateInput{}
  54. input.GenerateName = diskName
  55. input.Description = fmt.Sprintf("recovered from backup %s", backup.GetName())
  56. input.Hypervisor = api.HYPERVISOR_KVM
  57. if backup.DiskConfig != nil {
  58. if backup.DiskConfig.BackupAsTar != nil && backup.DiskConfig.BackupAsTar.ContainerId != "" {
  59. input.Hypervisor = api.HYPERVISOR_POD
  60. }
  61. }
  62. input.DiskConfig = diskConfig
  63. ownerId := backup.GetOwnerId()
  64. input.ProjectDomainId = ownerId.GetProjectDomainId()
  65. input.ProjectId = ownerId.GetProjectId()
  66. if backup.IsEncrypted() {
  67. input.EncryptKeyId = &backup.EncryptKeyId
  68. }
  69. params := input.JSON(input)
  70. diskObj, err := db.DoCreate(models.DiskManager, ctx, self.UserCred, nil, params, ownerId)
  71. if err != nil {
  72. self.taskFaild(ctx, backup, jsonutils.NewString(err.Error()))
  73. return
  74. }
  75. disk := diskObj.(*models.SDisk)
  76. err = backup.InheritTo(ctx, self.UserCred, disk)
  77. if err != nil {
  78. self.taskFaild(ctx, backup, jsonutils.NewString(err.Error()))
  79. return
  80. }
  81. func() {
  82. lockman.LockObject(ctx, disk)
  83. defer lockman.ReleaseObject(ctx, disk)
  84. disk.PostCreate(ctx, self.UserCred, backup.GetOwnerId(), nil, params)
  85. }()
  86. params.Set("disk_id", jsonutils.NewString(disk.Id))
  87. self.SetStage("OnCreateDisk", params)
  88. params.Set("parent_task_id", jsonutils.NewString(self.GetTaskId()))
  89. models.DiskManager.OnCreateComplete(ctx, []db.IModel{disk}, self.UserCred, ownerId, nil, []jsonutils.JSONObject{params})
  90. }
  91. func (self *DiskBackupRecoveryTask) OnCreateDisk(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
  92. diskId, _ := self.Params.GetString("disk_id")
  93. disk := models.DiskManager.FetchDiskById(diskId)
  94. if disk == nil {
  95. self.taskFaild(ctx, backup, jsonutils.NewString(fmt.Sprintf("disk %s disappeared", diskId)))
  96. return
  97. }
  98. imageId := backup.DiskConfig.ImageId
  99. snapshotId := backup.DiskConfig.SnapshotId
  100. db.Update(disk, func() error {
  101. disk.TemplateId = imageId
  102. disk.SnapshotId = snapshotId
  103. return nil
  104. })
  105. self.taskSuccess(ctx, backup, nil)
  106. }
  107. func (self *DiskBackupRecoveryTask) OnCreateDiskFailed(ctx context.Context, backup *models.SDiskBackup, data jsonutils.JSONObject) {
  108. self.taskFaild(ctx, backup, data)
  109. }