guest_undeploy_task.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/httputils"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. )
  23. type GuestUndeployTask struct {
  24. SGuestBaseTask
  25. }
  26. func init() {
  27. taskman.RegisterTask(GuestUndeployTask{})
  28. }
  29. func (self *GuestUndeployTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  30. guest := obj.(*models.SGuest)
  31. targetHostId, _ := self.Params.GetString("target_host_id")
  32. self.SetStage("OnGuestUndeployComplete", nil)
  33. if len(targetHostId) == 0 {
  34. if len(guest.BackupHostId) > 0 {
  35. self.SetStage("OnMasterHostUndeployGuestComplete", nil)
  36. }
  37. targetHostId = guest.HostId
  38. }
  39. var host *models.SHost
  40. if len(targetHostId) > 0 {
  41. host = models.HostManager.FetchHostById(targetHostId)
  42. }
  43. if host != nil {
  44. drv, err := guest.GetDriver()
  45. if err != nil {
  46. self.OnStartDeleteGuestFail(ctx, err)
  47. return
  48. }
  49. err = drv.RequestUndeployGuestOnHost(ctx, guest, host, self)
  50. if err != nil {
  51. self.OnStartDeleteGuestFail(ctx, err)
  52. }
  53. } else {
  54. self.SetStageComplete(ctx, nil)
  55. }
  56. }
  57. func (self *GuestUndeployTask) OnMasterHostUndeployGuestComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  58. self.SetStage("OnGuestUndeployComplete", nil)
  59. host := models.HostManager.FetchHostById(guest.BackupHostId)
  60. if host != nil {
  61. drv, err := guest.GetDriver()
  62. if err != nil {
  63. self.OnStartDeleteGuestFail(ctx, err)
  64. return
  65. }
  66. err = drv.RequestUndeployGuestOnHost(ctx, guest, host, self)
  67. if err != nil {
  68. self.OnStartDeleteGuestFail(ctx, err)
  69. }
  70. } else {
  71. self.SetStageComplete(ctx, nil)
  72. }
  73. }
  74. func (self *GuestUndeployTask) OnGuestUndeployComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  75. self.SetStageComplete(ctx, nil)
  76. }
  77. func (self *GuestUndeployTask) OnStartDeleteGuestFail(ctx context.Context, err error) {
  78. jsonErr, ok := err.(*httputils.JSONClientError)
  79. if ok {
  80. if jsonErr.Code == 404 {
  81. self.SetStageComplete(ctx, nil)
  82. return
  83. }
  84. }
  85. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  86. }