ha_guest_deploy_task.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. func init() {
  25. taskman.RegisterTask(GuestDeployBackupTask{})
  26. taskman.RegisterTask(HAGuestDeployTask{})
  27. }
  28. type HAGuestDeployTask struct {
  29. GuestDeployTask
  30. }
  31. func (self *HAGuestDeployTask) OnDeployWaitServerStop(
  32. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  33. ) {
  34. host := models.HostManager.FetchHostById(guest.BackupHostId)
  35. if host.HostStatus != api.HOST_ONLINE {
  36. self.GuestDeployTask.OnDeployWaitServerStop(ctx, guest, data)
  37. } else {
  38. self.DeployBackup(ctx, guest, nil)
  39. }
  40. }
  41. func (self *HAGuestDeployTask) DeployBackup(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  42. self.SetStage("OnDeploySlaveGuestComplete", nil)
  43. host := models.HostManager.FetchHostById(guest.BackupHostId)
  44. drv, err := guest.GetDriver()
  45. if err != nil {
  46. self.OnDeployGuestFail(ctx, guest, err)
  47. return
  48. }
  49. err = drv.RequestDeployGuestOnHost(ctx, guest, host, self)
  50. if err != nil {
  51. self.OnDeployGuestFail(ctx, guest, err)
  52. return
  53. }
  54. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOYING_BACKUP, "")
  55. }
  56. func (self *HAGuestDeployTask) OnDeploySlaveGuestComplete(
  57. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  58. ) {
  59. guest.SetGuestBackupMirrorJobNotReady(ctx, self.UserCred)
  60. host, _ := guest.GetHost()
  61. self.SetStage("OnDeployGuestComplete", nil)
  62. self.DeployOnHost(ctx, guest, host)
  63. }
  64. func (self *HAGuestDeployTask) OnDeploySlaveGuestCompleteFailed(
  65. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  66. ) {
  67. guest.SetGuestBackupMirrorJobNotReady(ctx, self.UserCred)
  68. self.OnDeployGuestFail(ctx, guest, fmt.Errorf("deploy backup failed %s", data))
  69. }
  70. type GuestDeployBackupTask struct {
  71. HAGuestDeployTask
  72. }
  73. func (self *GuestDeployBackupTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  74. guest := obj.(*models.SGuest)
  75. if len(guest.BackupHostId) == 0 {
  76. self.OnDeployGuestCompleteFailed(ctx, guest, jsonutils.NewString("Guest dosen't have backup host"))
  77. return
  78. }
  79. self.SetStage("OnDeployGuestComplete", nil)
  80. host := models.HostManager.FetchHostById(guest.BackupHostId)
  81. drv, err := guest.GetDriver()
  82. if err != nil {
  83. self.OnDeployGuestCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  84. return
  85. }
  86. err = drv.RequestDeployGuestOnHost(ctx, guest, host, self)
  87. if err != nil {
  88. self.OnDeployGuestCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  89. }
  90. }
  91. func (self *GuestDeployBackupTask) OnDeployGuestComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  92. guest.SetGuestBackupMirrorJobNotReady(ctx, self.UserCred)
  93. self.SetStageComplete(ctx, nil)
  94. }
  95. func (self *GuestDeployBackupTask) OnDeployGuestCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  96. guest.SetGuestBackupMirrorJobNotReady(ctx, self.UserCred)
  97. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOYING_BACKUP_FAILED, data.String())
  98. self.SetStageComplete(ctx, nil)
  99. }