cloud_provider_sync_info_task.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/appsrv"
  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 CloudProviderSyncInfoTask struct {
  26. taskman.STask
  27. }
  28. var syncLocalTaskWorkerMan *appsrv.SWorkerManager
  29. func InitCloudproviderSyncWorkers(count int) {
  30. syncWorker := appsrv.NewWorkerManager("CloudProviderSyncInfoTaskWorkerManager", count, 512, true)
  31. taskman.RegisterTaskAndWorker(CloudProviderSyncInfoTask{}, syncWorker)
  32. syncLocalTaskWorkerMan = appsrv.NewWorkerManager("CloudProviderSyncLocalTaskWorkerManager", count, 512, false)
  33. }
  34. func getAction(params *jsonutils.JSONDict) string {
  35. fullSync := jsonutils.QueryBoolean(params, "full_sync", false)
  36. if !fullSync {
  37. syncRangeJson, _ := params.Get("sync_range")
  38. if syncRangeJson != nil {
  39. fullSync = jsonutils.QueryBoolean(syncRangeJson, "full_sync", false)
  40. }
  41. }
  42. action := ""
  43. if fullSync {
  44. action = logclient.ACT_CLOUD_FULLSYNC
  45. } else {
  46. action = logclient.ACT_CLOUD_SYNC
  47. }
  48. return action
  49. }
  50. func (self *CloudProviderSyncInfoTask) GetSyncRange(ctx context.Context) models.SSyncRange {
  51. syncRange := models.SSyncRange{}
  52. syncRangeJson, _ := self.Params.Get("sync_range")
  53. if syncRangeJson != nil {
  54. syncRangeJson.Unmarshal(&syncRange)
  55. }
  56. syncRange.Normalize(ctx)
  57. return syncRange
  58. }
  59. func (self *CloudProviderSyncInfoTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  60. provider := obj.(*models.SCloudprovider)
  61. self.SetStage("OnSyncCloudProviderPreInfoComplete", nil)
  62. syncRange := self.GetSyncRange(ctx)
  63. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  64. return nil, models.SyncCloudproviderResources(ctx, self.GetUserCred(), provider, &syncRange)
  65. })
  66. }
  67. func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderPreInfoComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  68. provider := obj.(*models.SCloudprovider)
  69. syncRange := self.GetSyncRange(ctx)
  70. db.OpsLog.LogEvent(provider, db.ACT_SYNCING_HOST, "", self.UserCred)
  71. self.SetStage("OnSyncCloudProviderInfoComplete", nil)
  72. taskman.LocalTaskRunWithWorkers(self, func() (jsonutils.JSONObject, error) {
  73. provider.SyncCallSyncCloudproviderRegions(ctx, self.UserCred, syncRange)
  74. return nil, nil
  75. }, syncLocalTaskWorkerMan)
  76. }
  77. func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderPreInfoCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  78. log.Errorf("faild to sync provider quotas %s", body.String())
  79. self.OnSyncCloudProviderPreInfoComplete(ctx, obj, body)
  80. }
  81. func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderInfoComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  82. provider := obj.(*models.SCloudprovider)
  83. db.OpsLog.LogEvent(provider, db.ACT_SYNC_HOST_COMPLETE, "", self.UserCred)
  84. logclient.AddActionLogWithStartable(self, provider, getAction(self.Params), body, self.UserCred, true)
  85. self.SetStageComplete(ctx, nil)
  86. }
  87. func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderInfoCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  88. provider := obj.(*models.SCloudprovider)
  89. db.OpsLog.LogEvent(provider, db.ACT_SYNC_HOST_FAILED, "", self.UserCred)
  90. logclient.AddActionLogWithStartable(self, provider, getAction(self.Params), body, self.UserCred, false)
  91. self.SetStageFailed(ctx, nil)
  92. }