ha_disk_create_task.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 disk
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  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. )
  26. func init() {
  27. taskman.RegisterTask(HADiskCreateTask{})
  28. taskman.RegisterTask(DiskCreateBackupTask{})
  29. }
  30. type HADiskCreateTask struct {
  31. DiskCreateTask
  32. }
  33. func (self *HADiskCreateTask) OnStorageCacheImageComplete(
  34. ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject,
  35. ) {
  36. storage := models.StorageManager.FetchStorageById(disk.BackupStorageId)
  37. storagecache := storage.GetStoragecache()
  38. imageId := disk.GetTemplateId()
  39. self.SetStage("OnBackupStorageCacheImageComplete", nil)
  40. input := api.CacheImageInput{
  41. ImageId: imageId,
  42. Format: disk.DiskFormat,
  43. ParentTaskId: self.GetTaskId(),
  44. }
  45. storagecache.StartImageCacheTask(ctx, self.UserCred, input)
  46. }
  47. func (self *HADiskCreateTask) OnBackupStorageCacheImageComplete(
  48. ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject,
  49. ) {
  50. self.DiskCreateTask.OnStorageCacheImageComplete(ctx, disk, data)
  51. }
  52. func (self *HADiskCreateTask) OnDiskReady(
  53. ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject,
  54. ) {
  55. rebuild, _ := self.GetParams().Bool("rebuild")
  56. snapshot, _ := self.GetParams().GetString("snapshot")
  57. storage := models.StorageManager.FetchStorageById(disk.BackupStorageId)
  58. host, err := storage.GetMasterHost()
  59. if err != nil {
  60. self.OnBackupAllocateFailed(ctx, disk, jsonutils.NewString(errors.Wrapf(err, "storage.GetMasterHost").Error()))
  61. return
  62. }
  63. db.OpsLog.LogEvent(disk, db.ACT_BACKUP_ALLOCATING, disk.GetShortDesc(ctx), self.GetUserCred())
  64. disk.SetStatus(ctx, self.UserCred, api.DISK_BACKUP_STARTALLOC,
  65. fmt.Sprintf("Backup disk start alloc use host %s(%s)", host.Name, host.Id),
  66. )
  67. self.SetStage("OnSlaveDiskReady", nil)
  68. if err := disk.StartAllocate(ctx, host, storage,
  69. self.GetTaskId(), self.GetUserCred(), rebuild, snapshot, self,
  70. ); err != nil {
  71. self.OnBackupAllocateFailed(ctx, disk,
  72. jsonutils.NewString(fmt.Sprintf("Backup disk alloctate failed: %s", err.Error())))
  73. }
  74. }
  75. func (self *HADiskCreateTask) OnBackupAllocateFailed(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) {
  76. disk.SetStatus(ctx, self.UserCred, api.DISK_BACKUP_ALLOC_FAILED, data.String())
  77. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  78. Obj: disk,
  79. Action: notifyclient.ActionCreate,
  80. IsFail: true,
  81. })
  82. self.SetStageFailed(ctx, data)
  83. }
  84. func (self *HADiskCreateTask) OnSlaveDiskReady(
  85. ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject,
  86. ) {
  87. self.DiskCreateTask.OnDiskReady(ctx, disk, data)
  88. }
  89. type DiskCreateBackupTask struct {
  90. HADiskCreateTask
  91. }
  92. func (self *DiskCreateBackupTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  93. disk := obj.(*models.SDisk)
  94. storage := models.StorageManager.FetchStorageById(disk.BackupStorageId)
  95. storagecache := storage.GetStoragecache()
  96. imageId := disk.GetTemplateId()
  97. if len(imageId) > 0 {
  98. self.SetStage("OnBackupStorageCacheImageComplete", nil)
  99. input := api.CacheImageInput{
  100. ImageId: imageId,
  101. Format: disk.DiskFormat,
  102. ParentTaskId: self.GetTaskId(),
  103. }
  104. storagecache.StartImageCacheTask(ctx, self.UserCred, input)
  105. } else {
  106. self.OnBackupStorageCacheImageComplete(ctx, disk, nil)
  107. }
  108. }
  109. func (self *DiskCreateBackupTask) OnBackupStorageCacheImageComplete(
  110. ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject,
  111. ) {
  112. self.HADiskCreateTask.OnDiskReady(ctx, disk, data)
  113. }
  114. func (self *DiskCreateBackupTask) OnSlaveDiskReady(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) {
  115. bkStorage := models.StorageManager.FetchStorageById(disk.BackupStorageId)
  116. bkStorage.ClearSchedDescCache()
  117. disk.SetStatus(ctx, self.UserCred, api.DISK_READY, "")
  118. db.OpsLog.LogEvent(disk, db.ACT_BACKUP_ALLOCATE, disk.GetShortDesc(ctx), self.UserCred)
  119. self.SetStageComplete(ctx, nil)
  120. }