guest_syncstatus_task.go 3.0 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. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type GuestSyncstatusTask struct {
  27. SGuestBaseTask
  28. }
  29. func init() {
  30. taskman.RegisterTask(GuestSyncstatusTask{})
  31. }
  32. func (self *GuestSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  33. guest := obj.(*models.SGuest)
  34. host, err := guest.GetHost()
  35. if err != nil {
  36. guest.SetStatus(ctx, self.UserCred, api.VM_UNKNOWN, fmt.Sprintf("get host error: %v", err))
  37. self.SetStageComplete(ctx, nil)
  38. return
  39. }
  40. if !host.IsBaremetal && host.HostStatus == api.HOST_OFFLINE {
  41. guest.SetStatus(ctx, self.UserCred, api.VM_UNKNOWN, "host offline")
  42. self.SetStageComplete(ctx, nil)
  43. return
  44. }
  45. self.SetStage("OnGetStatusComplete", nil)
  46. drv, err := guest.GetDriver()
  47. if err != nil {
  48. self.OnGetStatusCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  49. return
  50. }
  51. err = drv.RequestSyncstatusOnHost(ctx, guest, host, self.UserCred, self)
  52. if err != nil {
  53. self.OnGetStatusCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  54. return
  55. }
  56. }
  57. func (self *GuestSyncstatusTask) getOriginStatus() string {
  58. os, _ := self.GetParams().GetString("origin_status")
  59. return os
  60. }
  61. func (self *GuestSyncstatusTask) OnGetStatusComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  62. guest := obj.(*models.SGuest)
  63. if len(guest.ExternalId) == 0 {
  64. log.Debugf("OnGetStatusSucc guest %s(%s) status %s", guest.Name, guest.Id, body)
  65. resp := new(api.HostUploadGuestStatusInput)
  66. body.Unmarshal(resp)
  67. if err := guest.SetStatusFromHost(ctx, self.GetUserCred(), *resp, self.HasParentTask(), self.getOriginStatus()); err != nil {
  68. log.Warningf("SetStatusFromHost for guest %s error: %v", guest.GetId(), err)
  69. }
  70. logclient.AddSimpleActionLog(guest, logclient.ACT_VM_SYNC_STATUS, "", self.UserCred, true)
  71. }
  72. self.SetStageComplete(ctx, nil)
  73. }
  74. func (self *GuestSyncstatusTask) OnGetStatusCompleteFailed(ctx context.Context, obj db.IStandaloneModel, err jsonutils.JSONObject) {
  75. guest := obj.(*models.SGuest)
  76. guest.SetStatus(ctx, self.UserCred, api.VM_UNKNOWN, err.String())
  77. logclient.AddSimpleActionLog(guest, logclient.ACT_VM_SYNC_STATUS, err, self.UserCred, false)
  78. self.SetStageComplete(ctx, nil)
  79. }