guest_suspend_task.go 2.5 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. api "yunion.io/x/onecloud/pkg/apis/compute"
  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. "yunion.io/x/onecloud/pkg/util/logclient"
  23. )
  24. type GuestSuspendTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(GuestSuspendTask{})
  29. }
  30. func (self *GuestSuspendTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guest := obj.(*models.SGuest)
  32. db.OpsLog.LogEvent(guest, db.ACT_STOPPING, "", self.UserCred)
  33. guest.SetStatus(ctx, self.UserCred, api.VM_SUSPENDING, "GuestSusPendTask")
  34. self.SetStage("OnSuspendComplete", nil)
  35. drv, err := guest.GetDriver()
  36. if err != nil {
  37. self.OnSuspendGuestFail(guest, err.Error())
  38. return
  39. }
  40. err = drv.RequestSuspendOnHost(ctx, guest, self)
  41. if err != nil {
  42. self.OnSuspendGuestFail(guest, err.Error())
  43. }
  44. }
  45. func (self *GuestSuspendTask) OnSuspendComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  46. guest := obj.(*models.SGuest)
  47. guest.SetStatus(ctx, self.UserCred, api.VM_SUSPEND, "")
  48. db.OpsLog.LogEvent(guest, db.ACT_STOP, "", self.UserCred)
  49. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_SUSPEND, "success", self.UserCred, true)
  50. self.SetStageComplete(ctx, nil)
  51. }
  52. func (self *GuestSuspendTask) OnSuspendCompleteFailed(ctx context.Context, obj db.IStandaloneModel, err jsonutils.JSONObject) {
  53. guest := obj.(*models.SGuest)
  54. guest.SetStatus(ctx, self.UserCred, api.VM_RUNNING, "")
  55. db.OpsLog.LogEvent(guest, db.ACT_STOP_FAIL, err.String(), self.UserCred)
  56. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_SUSPEND, err.String(), self.UserCred, false)
  57. self.SetStageFailed(ctx, err)
  58. }
  59. func (self *GuestSuspendTask) OnSuspendGuestFail(guest *models.SGuest, reason string) {
  60. guest.SetStatus(context.Background(), self.UserCred, api.VM_SUSPEND_FAILED, reason)
  61. }