guest_rescue.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/utils"
  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/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. func (self *SGuest) PerformStartRescue(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject,
  27. data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  28. if self.Hypervisor != api.HYPERVISOR_KVM {
  29. return nil, httperrors.NewBadRequestError("Cannot rescue guest hypervisor %s", self.Hypervisor)
  30. }
  31. if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) {
  32. return nil, httperrors.NewInvalidStatusError("guest status must be ready or running")
  33. }
  34. // Start rescue vm task
  35. err := self.StartRescueTask(ctx, userCred, jsonutils.NewDict(), "")
  36. if err != nil {
  37. return nil, httperrors.NewInvalidStatusError("guest.StartGuestRescueTask: %s", err.Error())
  38. }
  39. // Now it only support kvm guest os rescue
  40. return nil, nil
  41. }
  42. func (self *SGuest) PerformStopRescue(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject,
  43. data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  44. if !self.RescueMode {
  45. return nil, httperrors.NewInvalidStatusError("guest is not in rescue mode")
  46. }
  47. // Start rescue vm task
  48. err := self.StopRescueTask(ctx, userCred, data.(*jsonutils.JSONDict), "")
  49. if err != nil {
  50. return nil, httperrors.NewInvalidStatusError("guest.StopGuestRescueTask: %s", err.Error())
  51. }
  52. // Now it only support kvm guest os rescue
  53. return nil, nil
  54. }
  55. func (self *SGuest) UpdateRescueMode(mode bool) error {
  56. _, err := db.Update(self, func() error {
  57. self.RescueMode = mode
  58. return nil
  59. })
  60. if err != nil {
  61. return errors.Wrap(err, "Update LightMode")
  62. }
  63. return nil
  64. }
  65. func (self *SGuest) StartRescueTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error {
  66. // Now only support KVM
  67. taskName := "StartRescueTask"
  68. task, err := taskman.TaskManager.NewTask(ctx, taskName, self, userCred, data, parentTaskId, "", nil)
  69. if err != nil {
  70. return err
  71. }
  72. err = task.ScheduleRun(nil)
  73. if err != nil {
  74. return err
  75. }
  76. return nil
  77. }
  78. func (self *SGuest) StopRescueTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error {
  79. taskName := "StopRescueTask"
  80. task, err := taskman.TaskManager.NewTask(ctx, taskName, self, userCred, data, parentTaskId, "", nil)
  81. if err != nil {
  82. return err
  83. }
  84. err = task.ScheduleRun(nil)
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }