host_guests_migrate_task.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/log"
  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. )
  24. func init() {
  25. taskman.RegisterTask(HostGuestsMigrateTask{})
  26. }
  27. type HostGuestsMigrateTask struct {
  28. taskman.STask
  29. }
  30. func (self *HostGuestsMigrateTask) OnInit(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guests := make([]*api.GuestBatchMigrateParams, 0)
  32. err := self.Params.Unmarshal(&guests, "guests")
  33. if err != nil {
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. return
  36. }
  37. preferHostId, _ := self.Params.GetString("prefer_host_id")
  38. self.SetStage("OnMigrateComplete", nil)
  39. for i := range objs {
  40. guest := objs[i].(*models.SGuest)
  41. if guests[i].LiveMigrate {
  42. err := guest.StartGuestLiveMigrateTask(ctx, self.UserCred,
  43. guests[i].OldStatus, preferHostId, &guests[i].SkipCpuCheck, &guests[i].SkipKernelCheck,
  44. guests[i].EnableTLS, guests[i].QuciklyFinish, guests[i].MaxBandwidthMb, nil, self.Id,
  45. )
  46. if err != nil {
  47. log.Errorln(err)
  48. }
  49. } else {
  50. err := guest.StartMigrateTask(ctx, self.UserCred, guests[i].RescueMode,
  51. false, guests[i].OldStatus, preferHostId, self.Id)
  52. if err != nil {
  53. log.Errorln(err)
  54. }
  55. }
  56. }
  57. }
  58. func (self *HostGuestsMigrateTask) OnMigrateComplete(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
  59. self.SetStageComplete(ctx, nil)
  60. }
  61. func (self *HostGuestsMigrateTask) OnMigrateCompleteFailed(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
  62. self.SetStageFailed(ctx, data)
  63. }