service.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 host_health
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. execlient "yunion.io/x/executor/client"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/signalutils"
  23. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/service"
  25. "yunion.io/x/onecloud/pkg/hostman/options"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. "yunion.io/x/onecloud/pkg/util/procutils"
  28. "yunion.io/x/onecloud/pkg/util/sysutils"
  29. )
  30. type SHostHealthService struct {
  31. *service.SServiceBase
  32. }
  33. func (host *SHostHealthService) InitService() {
  34. options.Init()
  35. isRoot := sysutils.IsRootPermission()
  36. if !isRoot {
  37. log.Fatalf("host service must running with root permissions")
  38. }
  39. if len(options.HostOptions.DeployServerSocketPath) == 0 {
  40. log.Fatalf("missing deploy server socket path")
  41. }
  42. // options.HostOptions.EnableRbac = false // disable rbac
  43. // init base option for pid file
  44. host.SServiceBase.O = &options.HostOptions.BaseOptions
  45. log.Infof("exec socket path: %s", options.HostOptions.ExecutorSocketPath)
  46. if options.HostOptions.EnableRemoteExecutor {
  47. execlient.Init(options.HostOptions.ExecutorSocketPath)
  48. execlient.SetTimeoutSeconds(options.HostOptions.ExecutorConnectTimeoutSeconds)
  49. procutils.SetRemoteExecutor()
  50. }
  51. }
  52. func (host *SHostHealthService) RunService() {
  53. hn, err := os.Hostname()
  54. if err != nil {
  55. log.Fatalf("fail to get hostname %s", err)
  56. }
  57. app_common.InitAuth(&options.HostOptions.CommonOptions, func() {
  58. log.Infof("Auth complete!!")
  59. if err := host.initEtcdConfig(); err != nil {
  60. log.Fatalln("Init etcd config:", err)
  61. }
  62. if len(options.HostOptions.EtcdEndpoints) > 0 {
  63. _, err := InitHostHealthManager(hn)
  64. if err != nil {
  65. log.Fatalf("Init host health manager failed %s", err)
  66. }
  67. }
  68. })
  69. signalutils.SetDumpStackSignal()
  70. signalutils.StartTrap()
  71. select {}
  72. }
  73. func writeFile(dir, file string, data []byte) (string, error) {
  74. p := filepath.Join(dir, file)
  75. return p, ioutil.WriteFile(p, data, 0600)
  76. }
  77. func (host *SHostHealthService) initEtcdConfig() error {
  78. etcdEndpoint, err := app_common.FetchEtcdServiceInfo()
  79. if err != nil {
  80. if errors.Cause(err) == httperrors.ErrNotFound {
  81. return nil
  82. }
  83. return errors.Wrap(err, "fetch etcd service info")
  84. }
  85. if etcdEndpoint == nil {
  86. return nil
  87. }
  88. if len(options.HostOptions.EtcdEndpoints) == 0 {
  89. options.HostOptions.EtcdEndpoints = []string{etcdEndpoint.Url}
  90. if len(etcdEndpoint.CertId) > 0 {
  91. dir, err := ioutil.TempDir("", "etcd-cluster-tls")
  92. if err != nil {
  93. return errors.Wrap(err, "create dir etcd cluster tls")
  94. }
  95. options.HostOptions.EtcdCert, err = writeFile(dir, "etcd.crt", []byte(etcdEndpoint.Certificate))
  96. if err != nil {
  97. return errors.Wrap(err, "write file certificate")
  98. }
  99. options.HostOptions.EtcdKey, err = writeFile(dir, "etcd.key", []byte(etcdEndpoint.PrivateKey))
  100. if err != nil {
  101. return errors.Wrap(err, "write file private key")
  102. }
  103. options.HostOptions.EtcdCacert, err = writeFile(dir, "etcd-ca.crt", []byte(etcdEndpoint.CaCertificate))
  104. if err != nil {
  105. return errors.Wrap(err, "write file cacert")
  106. }
  107. options.HostOptions.EtcdUseTLS = true
  108. }
  109. }
  110. return nil
  111. }
  112. func (host *SHostHealthService) OnExitService() {}
  113. func StartService() {
  114. var srv = &SHostHealthService{}
  115. srv.SServiceBase = &service.SServiceBase{
  116. Service: srv,
  117. }
  118. srv.StartService()
  119. }