baremetal_server_stop_task.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  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. )
  25. type BaremetalServerStopTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(BaremetalServerStopTask{})
  30. }
  31. func (self *BaremetalServerStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  32. guest := obj.(*models.SGuest)
  33. db.OpsLog.LogEvent(guest, db.ACT_STOPPING, "", self.UserCred)
  34. guest.SetStatus(ctx, self.UserCred, api.VM_START_STOP, "")
  35. baremetal, _ := guest.GetHost()
  36. if baremetal != nil {
  37. self.OnStopGuestFail(ctx, guest, "Baremetal is None")
  38. return
  39. }
  40. params := jsonutils.NewDict()
  41. timeout, err := self.Params.Int("timeout")
  42. if err != nil {
  43. timeout = 30
  44. }
  45. if jsonutils.QueryBoolean(self.Params, "is_force", false) || jsonutils.QueryBoolean(self.Params, "reset", false) {
  46. timeout = 0
  47. }
  48. params.Set("timeout", jsonutils.NewInt(timeout))
  49. url := fmt.Sprintf("/baremetals/%s/servers/%s/stop", baremetal.Id, guest.Id)
  50. headers := self.GetTaskRequestHeader()
  51. self.SetStage("OnGuestStopTaskComplete", nil)
  52. _, err = baremetal.BaremetalSyncRequest(ctx, "POST", url, headers, params)
  53. if err != nil {
  54. log.Errorln(err)
  55. self.OnStopGuestFail(ctx, guest, err.Error())
  56. }
  57. }
  58. func (self *BaremetalServerStopTask) OnGuestStopTaskComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  59. if guest.Status == api.VM_STOPPING {
  60. guest.SetStatus(ctx, self.UserCred, api.VM_READY, "")
  61. db.OpsLog.LogEvent(guest, db.ACT_STOP, "", self.UserCred)
  62. }
  63. baremetal, _ := guest.GetHost()
  64. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_READY, "")
  65. self.SetStageComplete(ctx, nil)
  66. if guest.Status == api.VM_READY {
  67. if !jsonutils.QueryBoolean(self.Params, "reset", false) && guest.DisableDelete.IsFalse() && guest.ShutdownBehavior == api.SHUTDOWN_TERMINATE {
  68. guest.StartAutoDeleteGuestTask(ctx, self.UserCred, "")
  69. }
  70. }
  71. }
  72. func (self *BaremetalServerStopTask) OnGuestStopTaskCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  73. guest.SetStatus(ctx, self.UserCred, db.ACT_STOP_FAIL, data.String())
  74. db.OpsLog.LogEvent(guest, db.ACT_STOP_FAIL, data, self.UserCred)
  75. baremetal, _ := guest.GetHost()
  76. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_READY, data.String())
  77. self.SetStageFailed(ctx, data)
  78. }
  79. func (self *BaremetalServerStopTask) OnStopGuestFail(ctx context.Context, guest *models.SGuest, reason string) {
  80. self.OnGuestStopTaskCompleteFailed(ctx, guest, jsonutils.NewString(reason))
  81. }