esxi_agent_service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "os"
  17. "path/filepath"
  18. mcesxi "yunion.io/x/cloudmux/pkg/multicloud/esxi"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/appsrv"
  21. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  23. options_common "yunion.io/x/onecloud/pkg/cloudcommon/options"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/service"
  25. "yunion.io/x/onecloud/pkg/esxi"
  26. "yunion.io/x/onecloud/pkg/esxi/handler"
  27. "yunion.io/x/onecloud/pkg/esxi/options"
  28. "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver"
  29. "yunion.io/x/onecloud/pkg/hostman/hostdeployer/deployclient"
  30. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  31. )
  32. type SExsiAgentService struct {
  33. service.SServiceBase
  34. }
  35. func New() *SExsiAgentService {
  36. return &SExsiAgentService{}
  37. }
  38. func (s *SExsiAgentService) StartService() {
  39. options_common.ParseOptions(&options.Options, os.Args, "esxiagent.conf", "esxiagent")
  40. if len(options.Options.ImageCachePath) == 0 {
  41. options.Options.ImageCachePath = filepath.Join(options.Options.EsxiAgentPath, "image_cache")
  42. log.Infof("No cachepath, use default %s", options.Options.ImageCachePath)
  43. err := os.MkdirAll(options.Options.ImageCachePath, 0760)
  44. if err != nil {
  45. log.Fatalf("fail to create ImageCachePath %s", options.Options.ImageCachePath)
  46. }
  47. }
  48. if len(options.Options.AgentTempPath) == 0 {
  49. options.Options.AgentTempPath = filepath.Join(options.Options.EsxiAgentPath, "agent_tmp")
  50. log.Infof("No agent temp path, use default %s", options.Options.AgentTempPath)
  51. err := os.MkdirAll(options.Options.AgentTempPath, 0760)
  52. if err != nil {
  53. log.Fatalf("fail to create AgentTempPath %s", options.Options.AgentTempPath)
  54. }
  55. }
  56. // init lockman
  57. s.InitLockman()
  58. app_common.InitAuth(&options.Options.CommonOptions, func() {
  59. log.Infof("auth complete")
  60. })
  61. options_common.StartOptionManager(&options.Options.CommonOptions, options.Options.ConfigSyncPeriodSeconds, "", "", options_common.OnCommonOptionsChange)
  62. fsdriver.Init("")
  63. deployclient.Init(options.Options.DeployServerSocketPath)
  64. hostutils.InitWorkerManagerWithCount(options.Options.HostDelayTaskWorkerCount)
  65. app := app_common.InitApp(&options.Options.BaseOptions, false)
  66. handler.InitHandlers(app)
  67. mcesxi.InitEsxiConfig(options.Options.EsxiOptions)
  68. s.startAgent(app)
  69. app_common.ServeForeverWithCleanup(app, &options.Options.BaseOptions, func() {
  70. esxi.Stop()
  71. })
  72. }
  73. func (s *SExsiAgentService) startAgent(app *appsrv.Application) {
  74. err := esxi.Start(app)
  75. if err != nil {
  76. log.Fatalf("Start agent error: %v", err)
  77. }
  78. }
  79. func (s *SExsiAgentService) InitLockman() {
  80. log.Infof("using inmemory lockman")
  81. lm := lockman.NewInMemoryLockManager()
  82. lockman.Init(lm)
  83. }