host_maintenance_task.go 3.2 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 host
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type HostMaintainTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(HostMaintainTask{})
  30. }
  31. func (self *HostMaintainTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  32. host := obj.(*models.SHost)
  33. preferHostId, _ := self.Params.Get("prefer_host_id")
  34. var hostGuests = []*api.GuestBatchMigrateParams{}
  35. err := self.Params.Unmarshal(&hostGuests, "guests")
  36. if err != nil {
  37. self.TaskFailed(ctx, host, jsonutils.NewString(err.Error()))
  38. return
  39. }
  40. guests := make([]*models.SGuest, 0)
  41. hostGuestParams := make([]*api.GuestBatchMigrateParams, 0)
  42. for i := range hostGuests {
  43. guest := models.GuestManager.FetchGuestById(hostGuests[i].Id)
  44. if guest != nil {
  45. guests = append(guests, guest)
  46. hostGuestParams = append(hostGuestParams, hostGuests[i])
  47. }
  48. }
  49. if len(guests) == 0 {
  50. // no guest to migrate
  51. // self.SetStageComplete(ctx, nil)
  52. self.OnGuestsMigrate(ctx, host, nil)
  53. return
  54. }
  55. kwargs := jsonutils.NewDict()
  56. kwargs.Set("guests", jsonutils.Marshal(hostGuestParams))
  57. kwargs.Set("prefer_host_id", preferHostId)
  58. self.SetStage("OnGuestsMigrate", nil)
  59. err = models.GuestManager.StartHostGuestsMigrateTask(ctx, self.UserCred, guests, kwargs, self.Id)
  60. if err != nil {
  61. self.TaskFailed(ctx, host, jsonutils.NewString(err.Error()))
  62. return
  63. }
  64. }
  65. func (self *HostMaintainTask) OnGuestsMigrate(ctx context.Context, host *models.SHost, data jsonutils.JSONObject) {
  66. host.PerformDisable(ctx, self.UserCred, nil, apis.PerformDisableInput{})
  67. host.SetStatus(ctx, self.UserCred, api.BAREMETAL_MAINTAINING, "On host maintain task complete")
  68. logclient.AddSimpleActionLog(host, logclient.ACT_HOST_MAINTAINING, "host maintain", self.UserCred, true)
  69. self.SetStageComplete(ctx, nil)
  70. }
  71. func (self *HostMaintainTask) OnGuestsMigrateFailed(ctx context.Context, host *models.SHost, data jsonutils.JSONObject) {
  72. self.TaskFailed(ctx, host, data)
  73. }
  74. func (self *HostMaintainTask) TaskFailed(ctx context.Context, host *models.SHost, reason jsonutils.JSONObject) {
  75. host.PerformDisable(ctx, self.UserCred, nil, apis.PerformDisableInput{})
  76. host.SetStatus(ctx, self.UserCred, api.BAREMETAL_MAINTAIN_FAIL, "On host maintain task complete failed")
  77. logclient.AddSimpleActionLog(host, logclient.ACT_HOST_MAINTAINING, reason, self.UserCred, false)
  78. self.SetStageFailed(ctx, reason)
  79. }