service.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 service
  15. import (
  16. "context"
  17. "os"
  18. "time"
  19. _ "yunion.io/x/cloudmux/pkg/multicloud/loader"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. _ "yunion.io/x/sqlchemy/backends"
  23. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  24. "yunion.io/x/onecloud/pkg/cloudcommon"
  25. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  29. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  30. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  31. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  32. _ "yunion.io/x/onecloud/pkg/cloudid/drivers"
  33. "yunion.io/x/onecloud/pkg/cloudid/models"
  34. "yunion.io/x/onecloud/pkg/cloudid/options"
  35. "yunion.io/x/onecloud/pkg/cloudid/policy"
  36. "yunion.io/x/onecloud/pkg/cloudid/saml"
  37. _ "yunion.io/x/onecloud/pkg/cloudid/tasks"
  38. "yunion.io/x/onecloud/pkg/mcclient/auth"
  39. )
  40. func StartService() {
  41. opts := &options.Options
  42. dbOpts := &opts.DBOptions
  43. baseOpts := &opts.BaseOptions
  44. commonOpts := &opts.CommonOptions
  45. common_options.ParseOptions(opts, os.Args, "cloudid.conf", api.SERVICE_TYPE)
  46. policy.Init()
  47. app_common.InitAuth(commonOpts, func() {
  48. log.Infof("Auth complete!!")
  49. })
  50. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange)
  51. app := app_common.InitApp(baseOpts, true).
  52. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  53. ctx := context.Background()
  54. session := auth.GetAdminSession(ctx, commonOpts.Region)
  55. notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
  56. })
  57. cloudcommon.InitDB(dbOpts)
  58. InitHandlers(app, opts.IsSlaveNode)
  59. db.EnsureAppSyncDB(app, dbOpts, models.InitDB)
  60. defer cloudcommon.CloseDB()
  61. err := saml.InitSAML(app, api.SAML_IDP_PREFIX)
  62. if err != nil {
  63. log.Errorf("SAML initialization fail %s", err)
  64. return
  65. }
  66. if !opts.IsSlaveNode {
  67. models.CloudaccountManager.StartWatchSAMLInRegion()
  68. if err != nil {
  69. log.Fatalf("StartWatchSAMLInRegion error: %v", err)
  70. }
  71. models.CloudproviderManager.StartWatchInRegion()
  72. if err != nil {
  73. log.Fatalf("StartWatchInRegion error: %v", err)
  74. }
  75. err := taskman.TaskManager.InitializeData()
  76. if err != nil {
  77. log.Fatalf("TaskManager.InitializeData fail %s", err)
  78. }
  79. cron := cronman.InitCronJobManager(true, options.Options.CronJobWorkerCount, options.Options.TimeZone)
  80. cron.AddJobAtIntervalsWithStartRun("SyncCloudaccountResources", time.Duration(opts.CloudIdResourceSyncIntervalHours)*time.Hour, models.CloudaccountManager.SyncCloudaccountResources, true)
  81. cron.AddJobAtIntervalsWithStartRun("SyncCloudproviderResources", time.Duration(opts.CloudIdResourceSyncIntervalHours)*time.Hour, models.CloudproviderManager.SyncCloudproviderResources, true)
  82. cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false)
  83. cron.AddJobAtIntervalsWithStartRun("TaskCleanupJob", time.Duration(options.Options.TaskArchiveIntervalMinutes)*time.Minute, taskman.TaskManager.TaskCleanupJob, true)
  84. cron.Start()
  85. defer cron.Stop()
  86. }
  87. app_common.ServeForever(app, baseOpts)
  88. }