ha_guest_stop_task.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/log"
  19. "yunion.io/x/onecloud/pkg/apis/compute"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. func init() {
  25. taskman.RegisterTask(HAGuestStopTask{})
  26. }
  27. type HAGuestStopTask struct {
  28. GuestStopTask
  29. }
  30. func (self *HAGuestStopTask) OnGuestStopTaskComplete(
  31. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  32. ) {
  33. host := models.HostManager.FetchHostById(guest.BackupHostId)
  34. if host.HostStatus != api.HOST_ONLINE {
  35. self.GuestStopTask.OnGuestStopTaskComplete(ctx, guest, data)
  36. return
  37. }
  38. self.SetStage("OnSlaveGuestStopTaskComplete", nil)
  39. drv, err := guest.GetDriver()
  40. if err != nil {
  41. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  42. return
  43. }
  44. err = drv.RequestStopOnHost(ctx, guest, host, self, true)
  45. if err != nil {
  46. log.Errorf("RequestStopOnHost fail %s", err)
  47. self.OnGuestStopTaskCompleteFailed(
  48. ctx, guest, jsonutils.NewString(err.Error()))
  49. }
  50. }
  51. func (self *HAGuestStopTask) OnSlaveGuestStopTaskComplete(
  52. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  53. ) {
  54. self.GuestStopTask.OnGuestStopTaskComplete(ctx, guest, data)
  55. }
  56. func (self *HAGuestStopTask) OnSlaveGuestStopTaskCompleteFailed(
  57. ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject,
  58. ) {
  59. guest.SetBackupGuestStatus(self.UserCred, compute.VM_STOP_FAILED, data.String())
  60. self.OnGuestStopTaskCompleteFailed(ctx, guest, data)
  61. }