baremetal_unconvert_hypervisor_task.go 3.7 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 baremetal
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  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 BaremetalUnconvertHypervisorTask struct {
  27. SBaremetalBaseTask
  28. }
  29. func init() {
  30. taskman.RegisterTask(BaremetalUnconvertHypervisorTask{})
  31. }
  32. func (self *BaremetalUnconvertHypervisorTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  33. baremetal := obj.(*models.SHost)
  34. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_CONVERTING, "")
  35. guests, err := baremetal.GetGuests()
  36. if err != nil {
  37. self.SetStageFailed(ctx, jsonutils.NewString(errors.Wrapf(err, "baremetal.GetGuests").Error()))
  38. return
  39. }
  40. if len(guests) > 1 {
  41. self.SetStageFailed(ctx, jsonutils.NewString("Host guest conut > 1"))
  42. return
  43. }
  44. if len(guests) == 1 {
  45. guest := guests[0]
  46. self.SetStage("OnGuestDeleteComplete", nil)
  47. opts := api.ServerDeleteInput{OverridePendingDelete: true}
  48. guest.StartDeleteGuestTask(ctx, self.UserCred, self.GetTaskId(), opts)
  49. } else {
  50. self.OnGuestDeleteComplete(ctx, baremetal, nil)
  51. }
  52. models.IsolatedDeviceManager.DeleteDevicesByHost(ctx, self.GetUserCred(), baremetal)
  53. }
  54. func (self *BaremetalUnconvertHypervisorTask) OnGuestDeleteComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  55. db.OpsLog.LogEvent(baremetal, db.ACT_UNCONVERT_COMPLETE, "", self.UserCred)
  56. logclient.AddActionLogWithContext(ctx, baremetal, logclient.ACT_UNCONVERT_COMPLETE, nil, self.UserCred, true)
  57. driver, err := baremetal.GetHostDriver()
  58. if err != nil {
  59. self.SetStageFailed(ctx, jsonutils.NewString(errors.Wrapf(err, "GetHostDriver").Error()))
  60. return
  61. }
  62. err = driver.FinishUnconvert(ctx, self.UserCred, baremetal)
  63. if err != nil {
  64. log.Errorf("Fail to exec finish_unconvert: %s", err.Error())
  65. }
  66. self.SetStage("OnPrepareComplete", nil)
  67. baremetal.StartPrepareTask(ctx, self.UserCred, "", self.GetTaskId())
  68. logclient.AddActionLogWithStartable(self, baremetal, logclient.ACT_BM_UNCONVERT_HYPER, nil, self.UserCred, true)
  69. }
  70. func (self *BaremetalUnconvertHypervisorTask) OnGuestDeleteCompleteFailed(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  71. db.OpsLog.LogEvent(baremetal, db.ACT_UNCONVERT_FAIL, body, self.UserCred)
  72. logclient.AddActionLogWithContext(ctx, baremetal, logclient.ACT_UNCONVERT_COMPLETE, nil, self.UserCred, false)
  73. self.SetStage("OnFailSyncstatusComplete", nil)
  74. baremetal.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  75. logclient.AddActionLogWithStartable(self, baremetal, logclient.ACT_BM_UNCONVERT_HYPER, body, self.UserCred, false)
  76. }
  77. func (self *BaremetalUnconvertHypervisorTask) OnPrepareComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  78. self.SetStageComplete(ctx, nil)
  79. }
  80. func (self *BaremetalUnconvertHypervisorTask) OnFailSyncstatusComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  81. self.SetStageFailed(ctx, jsonutils.NewString("Delete server failed"))
  82. }