service.go 3.2 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. "os/signal"
  19. "sync"
  20. "syscall"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/appctx"
  24. "yunion.io/x/onecloud/pkg/apis"
  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/notifyclient"
  28. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  29. "yunion.io/x/onecloud/pkg/mcclient/auth"
  30. identity_modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  31. "yunion.io/x/onecloud/pkg/vpcagent/options"
  32. _ "yunion.io/x/onecloud/pkg/vpcagent/ovn"
  33. "yunion.io/x/onecloud/pkg/vpcagent/worker"
  34. )
  35. func updateDnsOptions(ctx context.Context, opts *options.Options) {
  36. regionConfig, err := identity_modules.ServicesV3.GetConfig(auth.GetAdminSession(ctx, opts.Region), apis.SERVICE_TYPE_REGION)
  37. if err != nil {
  38. log.Fatalf("fail to retrieve compute config")
  39. }
  40. if opts.DNSDomain == "" {
  41. opts.DNSDomain, _ = regionConfig.GetString("default", "dns_domain")
  42. }
  43. if opts.DNSServer == "" {
  44. opts.DNSServer, _ = regionConfig.GetString("default", "dns_server")
  45. }
  46. }
  47. func StartService() {
  48. opts := &options.Options{}
  49. commonOpts := &opts.CommonOptions
  50. {
  51. common_options.ParseOptions(opts, os.Args, "vpcagent.conf", "vpcagent")
  52. app_common.InitAuth(commonOpts, func() {
  53. log.Infof("auth finished ok")
  54. })
  55. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, apis.SERVICE_TYPE_VPCAGENT, "", options.OnOptionsChange)
  56. }
  57. if err := opts.ValidateThenInit(); err != nil {
  58. log.Fatalf("opts validate: %s", err)
  59. }
  60. app := app_common.InitApp(&opts.BaseOptions, true).
  61. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  62. ctx := context.Background()
  63. session := auth.GetAdminSession(ctx, commonOpts.Region)
  64. notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
  65. })
  66. w := worker.NewWorker(opts)
  67. if w == nil {
  68. log.Fatalf("new worker failed")
  69. }
  70. go func() {
  71. ctx := context.Background()
  72. ctx, cancelFunc := context.WithCancel(ctx)
  73. wg := &sync.WaitGroup{}
  74. ctx = context.WithValue(ctx, "wg", wg)
  75. ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_APPNAME, "vpcagent")
  76. if opts.DNSDomain == "" || opts.DNSServer == "" {
  77. updateDnsOptions(ctx, opts)
  78. }
  79. wg.Add(1)
  80. go w.Start(ctx, app, "vpcagent")
  81. go func() {
  82. sigChan := make(chan os.Signal)
  83. signal.Notify(sigChan, syscall.SIGINT)
  84. signal.Notify(sigChan, syscall.SIGTERM)
  85. sig := <-sigChan
  86. log.Infof("signal received: %s", sig)
  87. cancelFunc()
  88. }()
  89. wg.Wait()
  90. }()
  91. app_common.ServeForeverWithCleanup(app, &opts.BaseOptions, nil)
  92. }