service.go 3.1 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. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/utils"
  21. api "yunion.io/x/onecloud/pkg/apis/baremetal"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/baremetal"
  24. "yunion.io/x/onecloud/pkg/baremetal/handler"
  25. o "yunion.io/x/onecloud/pkg/baremetal/options"
  26. "yunion.io/x/onecloud/pkg/baremetal/tasks"
  27. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  29. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  30. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  31. "yunion.io/x/onecloud/pkg/cloudcommon/service"
  32. "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver"
  33. _ "yunion.io/x/onecloud/pkg/util/redfish/loader"
  34. )
  35. type BaremetalService struct {
  36. service.SServiceBase
  37. }
  38. func New() *BaremetalService {
  39. return &BaremetalService{}
  40. }
  41. func (s *BaremetalService) StartService() {
  42. common_options.ParseOptions(&o.Options, os.Args, "baremetal.conf", "baremetal")
  43. if len(o.Options.CachePath) == 0 {
  44. o.Options.CachePath = filepath.Join(filepath.Dir(o.Options.BaremetalsPath), "bm_image_cache")
  45. log.Infof("No cachepath, use default %s", o.Options.CachePath)
  46. }
  47. if len(o.Options.BootIsoPath) == 0 {
  48. o.Options.BootIsoPath = filepath.Join(filepath.Dir(o.Options.BaremetalsPath), "bm_boot_iso")
  49. log.Infof("No BootIsoPath, use default %s", o.Options.BootIsoPath)
  50. err := os.MkdirAll(o.Options.BootIsoPath, os.FileMode(0760))
  51. if err != nil {
  52. log.Fatalf("fail to create BootIsoPath %s", o.Options.BootIsoPath)
  53. }
  54. }
  55. if !utils.IsInStringArray(o.Options.BootLoader, []string{o.BOOT_LOADER_GRUB, o.BOOT_LOADER_SYSLINUX}) {
  56. log.Fatalf("invalid boot_loader option: %q", o.Options.BootLoader)
  57. }
  58. app_common.InitAuth(&o.Options.CommonOptions, func() {
  59. log.Infof("auth complete")
  60. })
  61. fsdriver.Init("")
  62. app := app_common.InitApp(&o.Options.BaseOptions, false)
  63. common_options.StartOptionManager(&o.Options, o.Options.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange)
  64. handler.InitHandlers(app)
  65. s.startAgent(app)
  66. cron := cronman.InitCronJobManager(false, o.Options.CronJobWorkerCount, o.Options.TimeZone)
  67. cron.AddJobAtIntervals("BaremetalCronJobs", 10*time.Second, baremetal.DoCronJobs)
  68. cron.Start()
  69. defer cron.Stop()
  70. app_common.ServeForeverWithCleanup(app, &o.Options.BaseOptions, func() {
  71. tasks.OnStop()
  72. baremetal.Stop()
  73. })
  74. }
  75. func (s *BaremetalService) startAgent(app *appsrv.Application) {
  76. // init lockman
  77. lm := lockman.NewInMemoryLockManager()
  78. lockman.Init(lm)
  79. err := baremetal.Start(app)
  80. if err != nil {
  81. log.Fatalf("Start agent error: %v", err)
  82. }
  83. }