syncworker.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. "yunion.io/x/onecloud/pkg/keystone/driver"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. var (
  27. syncWorker *appsrv.SWorkerManager
  28. )
  29. func InitSyncWorkers() {
  30. syncWorker = appsrv.NewWorkerManager(
  31. "identityProviderSyncWorkerManager",
  32. 1,
  33. 2048,
  34. true,
  35. )
  36. }
  37. type syncTask struct {
  38. ctx context.Context
  39. userCred mcclient.TokenCredential
  40. idp *SIdentityProvider
  41. }
  42. func (t *syncTask) Run() {
  43. t.idp.SetSyncStatus(t.ctx, t.userCred, api.IdentitySyncStatusSyncing)
  44. defer t.idp.SetSyncStatus(t.ctx, t.userCred, api.IdentitySyncStatusIdle)
  45. conf, err := GetConfigs(t.idp, true, nil, nil)
  46. if err != nil {
  47. log.Errorf("GetConfig for idp %s fail %s", t.idp.Name, err)
  48. t.idp.MarkDisconnected(t.ctx, t.userCred, err)
  49. return
  50. }
  51. driver, err := driver.GetDriver(t.idp.Driver, t.idp.Id, t.idp.Name, t.idp.Template, t.idp.TargetDomainId, conf)
  52. if err != nil {
  53. log.Errorf("GetDriver for idp %s fail %s", t.idp.Name, err)
  54. t.idp.MarkDisconnected(t.ctx, t.userCred, err)
  55. return
  56. }
  57. err = driver.Probe(t.ctx)
  58. if err != nil {
  59. log.Errorf("Probe for idp %s fail %s", t.idp.Name, err)
  60. t.idp.MarkDisconnected(t.ctx, t.userCred, err)
  61. return
  62. }
  63. t.idp.MarkConnected(t.ctx, t.userCred)
  64. err = driver.Sync(t.ctx)
  65. if err != nil {
  66. logclient.AddActionLogWithContext(t.ctx, t.idp, logclient.ACT_SYNC_CONF, err, t.userCred, false)
  67. return
  68. }
  69. }
  70. func (t *syncTask) Dump() string {
  71. return fmt.Sprintf("idp %s", jsonutils.Marshal(t.idp).String())
  72. }
  73. func submitIdpSyncTask(ctx context.Context, userCred mcclient.TokenCredential, idp *SIdentityProvider) {
  74. idp.SetSyncStatus(ctx, userCred, api.IdentitySyncStatusQueued)
  75. task := &syncTask{
  76. ctx: ctx,
  77. userCred: userCred,
  78. idp: idp,
  79. }
  80. syncWorker.Run(task, nil, nil)
  81. }