guest_reset_task.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. func init() {
  25. taskman.RegisterTask(GuestSoftResetTask{})
  26. taskman.RegisterTask(GuestHardResetTask{})
  27. taskman.RegisterTask(GuestRestartTask{})
  28. }
  29. type GuestSoftResetTask struct {
  30. SGuestBaseTask
  31. }
  32. func (self *GuestSoftResetTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  33. guest := obj.(*models.SGuest)
  34. drv, err := guest.GetDriver()
  35. if err != nil {
  36. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  37. return
  38. }
  39. err = drv.RequestSoftReset(ctx, guest, self)
  40. if err != nil {
  41. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  42. return
  43. }
  44. self.SetStageComplete(ctx, nil)
  45. }
  46. type GuestHardResetTask struct {
  47. SGuestBaseTask
  48. }
  49. func (self *GuestHardResetTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  50. guest := obj.(*models.SGuest)
  51. self.StopServer(ctx, guest)
  52. }
  53. func (self *GuestHardResetTask) StopServer(ctx context.Context, guest *models.SGuest) {
  54. guest.SetStatus(ctx, self.UserCred, api.VM_STOPPING, "")
  55. self.SetStage("OnServerStopComplete", nil)
  56. guest.StartGuestStopTask(ctx, self.UserCred, 30, false, false, self.GetTaskId())
  57. // logclient.AddActionLogWith(guest, logclient.ACT_VM_RESTART, `{"is_force": true}`, self.UserCred, true)
  58. }
  59. func (self *GuestHardResetTask) OnServerStopComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  60. self.StartServer(ctx, guest)
  61. }
  62. func (self *GuestHardResetTask) StartServer(ctx context.Context, guest *models.SGuest) {
  63. self.SetStage("OnServerStartComplete", nil)
  64. guest.StartGueststartTask(ctx, self.UserCred, nil, self.GetTaskId())
  65. }
  66. func (self *GuestHardResetTask) OnServerStartComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  67. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_RESTART, nil, self.GetUserCred(), true)
  68. self.SetStageComplete(ctx, nil)
  69. }
  70. type GuestRestartTask struct {
  71. GuestHardResetTask
  72. }
  73. func (self *GuestRestartTask) StopServer(ctx context.Context, guest *models.SGuest) {
  74. self.SetStage("OnServerStopComplete", nil)
  75. isForce := jsonutils.QueryBoolean(self.Params, "is_force", false)
  76. guest.StartGuestStopTask(ctx, self.UserCred, 60, isForce, false, self.GetTaskId())
  77. // logclient.AddActionLog(guest, logclient.ACT_VM_RESTART, `{"is_force": false}`, self.UserCred, true)
  78. }