main.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 main
  15. import (
  16. "context"
  17. "os"
  18. "os/signal"
  19. "sync"
  20. "syscall"
  21. "yunion.io/x/log"
  22. api "yunion.io/x/onecloud/pkg/apis/cloudproxy"
  23. common_app "yunion.io/x/onecloud/pkg/cloudcommon/app"
  24. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  25. "yunion.io/x/onecloud/pkg/cloudproxy/agent/worker"
  26. "yunion.io/x/onecloud/pkg/cloudproxy/options"
  27. "yunion.io/x/onecloud/pkg/cloudproxy/service"
  28. "yunion.io/x/onecloud/pkg/util/atexit"
  29. )
  30. func main() {
  31. defer atexit.Handle()
  32. wg := &sync.WaitGroup{}
  33. ctx := context.Background()
  34. ctx = context.WithValue(ctx, "wg", wg)
  35. ctx, cancelFunc := context.WithCancel(ctx)
  36. var (
  37. opts = options.Get()
  38. )
  39. common_app.InitAuth(&opts.CommonOptions, func() {
  40. log.Infof("Auth complete")
  41. })
  42. common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange)
  43. wg.Add(1)
  44. go func() {
  45. defer wg.Done()
  46. service.StartService()
  47. }()
  48. if opts.EnableProxyAgent {
  49. const d = "10m"
  50. log.Infof("set proxy_agent_init_wait to %s", d)
  51. opts.Options.ProxyAgentInitWait = d
  52. if err := opts.Options.ValidateThenInit(); err != nil {
  53. log.Fatalf("proxy agent options validation: %v", err)
  54. }
  55. worker := worker.NewWorker(&opts.CommonOptions, &opts.Options)
  56. go func() {
  57. worker.Start(ctx)
  58. pid := os.Getpid()
  59. p, err := os.FindProcess(pid)
  60. if err != nil {
  61. log.Fatalf("find process of my pid %d: %v", pid, err)
  62. }
  63. p.Signal(syscall.SIGTERM)
  64. }()
  65. }
  66. go func() {
  67. sigChan := make(chan os.Signal)
  68. signal.Notify(sigChan, syscall.SIGINT)
  69. signal.Notify(sigChan, syscall.SIGTERM)
  70. sig := <-sigChan
  71. log.Infof("signal received: %s", sig)
  72. cancelFunc()
  73. }()
  74. wg.Wait()
  75. }