guest_stop_task.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  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/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type GuestStopTask struct {
  27. SGuestBaseTask
  28. }
  29. func init() {
  30. taskman.RegisterTask(GuestStopTask{})
  31. taskman.RegisterTask(GuestStopAndFreezeTask{})
  32. }
  33. func (self *GuestStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  34. guest := obj.(*models.SGuest)
  35. db.OpsLog.LogEvent(guest, db.ACT_STOPPING, nil, self.UserCred)
  36. self.stopGuest(ctx, guest)
  37. }
  38. func (self *GuestStopTask) stopGuest(ctx context.Context, guest *models.SGuest) {
  39. host, err := guest.GetHost()
  40. if err != nil {
  41. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(errors.Wrapf(err, "GetHost").Error()))
  42. return
  43. }
  44. if !self.IsSubtask() {
  45. guest.SetStatus(ctx, self.GetUserCred(), api.VM_STOPPING, "")
  46. }
  47. self.SetStage("OnGuestStopTaskComplete", nil)
  48. drv, err := guest.GetDriver()
  49. if err != nil {
  50. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  51. return
  52. }
  53. err = drv.RequestStopOnHost(ctx, guest, host, self, !self.IsSubtask())
  54. if err != nil {
  55. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  56. }
  57. }
  58. func (self *GuestStopTask) OnGuestStopTaskComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  59. db.OpsLog.LogEvent(guest, db.ACT_STOP, guest.GetShortDesc(ctx), self.UserCred)
  60. if guest.Status != api.VM_READY && !self.IsSubtask() { // for kvm
  61. guest.SetStatus(ctx, self.GetUserCred(), api.VM_READY, "")
  62. if guest.CpuNumaPin != nil {
  63. guest.SetCpuNumaPin(ctx, self.UserCred, nil, nil)
  64. }
  65. }
  66. models.HostManager.ClearSchedDescCache(guest.HostId)
  67. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_STOP, "success", self.UserCred, true)
  68. if err := self.releaseDevices(ctx, guest); err != nil {
  69. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  70. }
  71. }
  72. func (self *GuestStopTask) OnGuestStopTaskCompleteFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  73. if !self.IsSubtask() {
  74. guest.SetStatus(ctx, self.UserCred, api.VM_STOP_FAILED, reason.String())
  75. }
  76. db.OpsLog.LogEvent(guest, db.ACT_STOP_FAIL, reason.String(), self.UserCred)
  77. self.SetStageFailed(ctx, reason)
  78. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_STOP, reason.String(), self.UserCred, false)
  79. }
  80. func (self *GuestStopTask) releaseDevices(ctx context.Context, guest *models.SGuest) error {
  81. self.SetStage("OnDevicesReleased", nil)
  82. if guest.ShutdownBehavior != api.SHUTDOWN_STOP_RELEASE_GPU {
  83. return self.ScheduleRun(nil)
  84. }
  85. devs, err := guest.GetIsolatedDevices()
  86. if err != nil {
  87. return errors.Wrapf(err, "GetIsolatedDevices of guest %s", guest.GetId())
  88. }
  89. gpus := make([]models.SIsolatedDevice, 0)
  90. for _, dev := range devs {
  91. if dev.IsGPU() {
  92. tmpDev := dev
  93. gpus = append(gpus, tmpDev)
  94. }
  95. }
  96. if len(gpus) == 0 {
  97. return self.ScheduleRun(nil)
  98. }
  99. if err := guest.SetReleasedIsolatedDevices(ctx, self.GetUserCred(), gpus); err != nil {
  100. return errors.Wrapf(err, "SetReleasedIsolatedDevices of guest %s", guest.GetId())
  101. }
  102. if err := guest.DetachIsolatedDevices(ctx, self.GetUserCred(), gpus); err != nil {
  103. return errors.Wrapf(err, "DetachIsolatedDevices of guest %s", guest.GetId())
  104. }
  105. return guest.StartIsolatedDevicesSyncTask(ctx, self.GetUserCred(), false, self.GetTaskId())
  106. }
  107. func (self *GuestStopTask) OnDevicesReleased(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  108. self.SetStageComplete(ctx, nil)
  109. if guest.DisableDelete.IsFalse() && guest.ShutdownBehavior == api.SHUTDOWN_TERMINATE {
  110. guest.StartAutoDeleteGuestTask(ctx, self.UserCred, "")
  111. return
  112. }
  113. }
  114. func (self *GuestStopTask) OnDevicesReleaseFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  115. self.OnGuestStopTaskCompleteFailed(ctx, guest, reason)
  116. }
  117. type GuestStopAndFreezeTask struct {
  118. SGuestBaseTask
  119. }
  120. func (self *GuestStopAndFreezeTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  121. guest := obj.(*models.SGuest)
  122. self.SetStage("OnStopGuest", nil)
  123. err := guest.StartGuestStopTask(ctx, self.UserCred, 60, false, false, self.GetTaskId())
  124. if err != nil {
  125. self.OnStopGuestFailed(ctx, guest, jsonutils.NewString(err.Error()))
  126. }
  127. }
  128. func (self *GuestStopAndFreezeTask) OnStopGuestFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  129. db.OpsLog.LogEvent(guest, db.ACT_FREEZE_FAIL, reason.String(), self.UserCred)
  130. self.SetStageFailed(ctx, reason)
  131. }
  132. func (self *GuestStopAndFreezeTask) OnStopGuest(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  133. self.SetStage("OnSyncStatus", nil)
  134. guest.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  135. }
  136. func (self *GuestStopAndFreezeTask) OnSyncStatus(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  137. _, err := guest.SVirtualResourceBase.PerformFreeze(ctx, self.UserCred, nil, apis.PerformFreezeInput{})
  138. if err != nil {
  139. self.OnStopGuestFailed(ctx, guest, jsonutils.NewString(err.Error()))
  140. return
  141. }
  142. self.SetStageComplete(ctx, nil)
  143. }
  144. func (self *GuestStopAndFreezeTask) OnSyncStatusFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  145. db.OpsLog.LogEvent(guest, db.ACT_FREEZE_FAIL, reason.String(), self.UserCred)
  146. self.SetStageFailed(ctx, reason)
  147. }