cloud_account_sync_task.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 cloudaccount
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type CloudAccountSyncInfoTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(CloudAccountSyncInfoTask{})
  33. }
  34. func (self *CloudAccountSyncInfoTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  35. cloudaccount := obj.(*models.SCloudaccount)
  36. if cloudaccount.Provider == api.CLOUD_PROVIDER_VMWARE {
  37. cloudaccount.SetStatus(ctx, self.UserCred, api.CLOUD_PROVIDER_SYNC_NETWORK, "StartSyncVMwareNetworkTask")
  38. zone, _ := self.Params.GetString("zone")
  39. err := cloudaccount.PrepareEsxiHostNetwork(ctx, self.UserCred, zone)
  40. if err != nil {
  41. d := jsonutils.NewDict()
  42. d.Set("error", jsonutils.NewString(err.Error()))
  43. db.OpsLog.LogEvent(cloudaccount, db.ACT_SYNC_NETWORK_FAILED, d, self.UserCred)
  44. cloudaccount.SetStatus(ctx, self.UserCred, api.CLOUD_PROVIDER_SYNC_NETWORK_FAILED, "sync network failed")
  45. logclient.AddActionLogWithStartable(self, cloudaccount, logclient.ACT_CLOUDACCOUNT_SYNC_NETWORK, d, self.UserCred, false)
  46. cloudaccount.MarkEndSync(self.UserCred, false)
  47. self.SetStageFailed(ctx, d)
  48. return
  49. } else {
  50. cloudaccount.SetStatus(ctx, self.UserCred, api.CLOUD_PROVIDER_INIT, "sync network sucess")
  51. logclient.AddActionLogWithStartable(self, cloudaccount, logclient.ACT_CLOUDACCOUNT_SYNC_NETWORK, cloudaccount.GetShortDesc(ctx), self.UserCred, true)
  52. }
  53. }
  54. db.OpsLog.LogEvent(cloudaccount, db.ACT_SYNCING_HOST, "", self.UserCred)
  55. self.SetStage("OnCloudaccountSyncReady", nil)
  56. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  57. // do sync
  58. err := cloudaccount.SyncCallSyncAccountTask(ctx, self.UserCred)
  59. if err != nil {
  60. if errors.Cause(err) == httperrors.ErrConflict {
  61. log.Errorf("account %s(%s) alread in syncing", cloudaccount.Name, cloudaccount.Provider)
  62. }
  63. // 进入同步任务前已经mark sync, 这里需要清理下状态
  64. cloudaccount.MarkEndSyncWithLock(ctx, self.UserCred, false)
  65. return nil, errors.Wrap(err, "SyncCallSyncAccountTask")
  66. }
  67. return nil, nil
  68. })
  69. }
  70. func (self *CloudAccountSyncInfoTask) OnCloudaccountSyncReadyFailed(ctx context.Context, obj db.IStandaloneModel, err jsonutils.JSONObject) {
  71. cloudaccount := obj.(*models.SCloudaccount)
  72. if !strings.Contains(err.String(), "ConflictError") {
  73. db.OpsLog.LogEvent(cloudaccount, db.ACT_SYNC_HOST_FAILED, err, self.UserCred)
  74. logclient.AddActionLogWithStartable(self, cloudaccount, logclient.ACT_CLOUD_SYNC, err, self.UserCred, false)
  75. }
  76. self.SetStageFailed(ctx, err)
  77. }
  78. func (self *CloudAccountSyncInfoTask) GetSyncRange(ctx context.Context) models.SSyncRange {
  79. syncRange := models.SSyncRange{}
  80. syncRangeJson, _ := self.Params.Get("sync_range")
  81. if syncRangeJson != nil {
  82. syncRangeJson.Unmarshal(&syncRange)
  83. } else {
  84. syncRange.FullSync = true
  85. syncRange.DeepSync = true
  86. }
  87. return syncRange
  88. }
  89. func (self *CloudAccountSyncInfoTask) OnCloudaccountSyncReady(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  90. cloudaccount := obj.(*models.SCloudaccount)
  91. syncRange := self.GetSyncRange(ctx)
  92. if !syncRange.NeedSyncInfo() {
  93. self.OnCloudaccountSyncComplete(ctx, obj, nil)
  94. return
  95. }
  96. cloudproviders := cloudaccount.GetEnabledCloudproviders()
  97. if len(cloudproviders) > 0 {
  98. self.SetStage("OnCloudaccountSyncComplete", nil)
  99. for i := range cloudproviders {
  100. cloudproviders[i].StartSyncCloudProviderInfoTask(ctx, self.UserCred, &syncRange, self.GetId())
  101. }
  102. } else {
  103. self.OnCloudaccountSyncComplete(ctx, obj, nil)
  104. }
  105. }
  106. func (self *CloudAccountSyncInfoTask) OnCloudaccountSyncComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  107. cloudaccount := obj.(*models.SCloudaccount)
  108. syncRange := self.GetSyncRange(ctx)
  109. cloudaccount.MarkEndSyncWithLock(ctx, self.UserCred, syncRange.NeedSyncInfo())
  110. db.OpsLog.LogEvent(cloudaccount, db.ACT_SYNC_HOST_COMPLETE, "", self.UserCred)
  111. self.SetStageComplete(ctx, nil)
  112. logclient.AddActionLogWithStartable(self, cloudaccount, logclient.ACT_CLOUD_SYNC, "", self.UserCred, true)
  113. }
  114. func (self *CloudAccountSyncInfoTask) OnCloudaccountSyncCompleteFailed(ctx context.Context, obj db.IStandaloneModel, err jsonutils.JSONObject) {
  115. cloudaccount := obj.(*models.SCloudaccount)
  116. syncRange := self.GetSyncRange(ctx)
  117. cloudaccount.MarkEndSyncWithLock(ctx, self.UserCred, syncRange.NeedSyncInfo())
  118. db.OpsLog.LogEvent(cloudaccount, db.ACT_SYNC_HOST_FAILED, err, self.UserCred)
  119. self.SetStageFailed(ctx, err)
  120. logclient.AddActionLogWithStartable(self, cloudaccount, logclient.ACT_CLOUD_SYNC, err, self.UserCred, false)
  121. }