service.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. _ "yunion.io/x/sqlchemy/backends"
  21. api "yunion.io/x/onecloud/pkg/apis/yunionconf"
  22. "yunion.io/x/onecloud/pkg/cloudcommon"
  23. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  27. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  28. "yunion.io/x/onecloud/pkg/mcclient/auth"
  29. "yunion.io/x/onecloud/pkg/yunionconf/models"
  30. "yunion.io/x/onecloud/pkg/yunionconf/options"
  31. "yunion.io/x/onecloud/pkg/yunionconf/policy"
  32. )
  33. func StartService() {
  34. opts := &options.Options
  35. baseOpts := &options.Options.BaseOptions
  36. commonOpts := &options.Options.CommonOptions
  37. dbOpts := &options.Options.DBOptions
  38. common_options.ParseOptions(opts, os.Args, "yunionconf.conf", api.SERVICE_TYPE)
  39. policy.Init()
  40. app_common.InitAuth(commonOpts, func() {
  41. log.Infof("Auth complete!!")
  42. })
  43. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange)
  44. cloudcommon.InitDB(dbOpts)
  45. app := app_common.InitApp(baseOpts, true).
  46. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  47. ctx := context.Background()
  48. session := auth.GetAdminSession(ctx, baseOpts.Region)
  49. notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
  50. })
  51. InitHandlers(app, opts.IsSlaveNode)
  52. db.AppDBInit(app)
  53. if db.CheckSync(opts.AutoSyncTable, opts.EnableDBChecksumTables, opts.DBChecksumSkipInit) {
  54. err := models.InitDB()
  55. if err == nil {
  56. app_common.ServeForeverWithCleanup(app, baseOpts, func() {
  57. cloudcommon.CloseDB()
  58. })
  59. } else {
  60. log.Errorf("InitDB fail: %s", err)
  61. }
  62. }
  63. }