agent.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 esxi
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/agent"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/workmanager"
  26. "yunion.io/x/onecloud/pkg/esxi/options"
  27. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  28. "yunion.io/x/onecloud/pkg/hostman/storageman"
  29. "yunion.io/x/onecloud/pkg/httperrors"
  30. "yunion.io/x/onecloud/pkg/mcclient"
  31. "yunion.io/x/onecloud/pkg/mcclient/auth"
  32. "yunion.io/x/onecloud/pkg/util/netutils2"
  33. )
  34. const (
  35. _ = compute.AgentTypeEsxi
  36. )
  37. var (
  38. EsxiAgent *SEsxiAgent
  39. )
  40. type SEsxiAgent struct {
  41. agent.SBaseAgent
  42. agentImageCache *storageman.SAgentImageCacheManager
  43. AgentStorage *storageman.SAgentStorage
  44. ListenNic netutils2.SNetInterface
  45. }
  46. func NewEsxiAgent() (*SEsxiAgent, error) {
  47. agent := &SEsxiAgent{}
  48. err := agent.Init(agent, options.Options.ListenInterface, options.Options.ImageCachePath)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return agent, nil
  53. }
  54. func (ea *SEsxiAgent) GetAgentType() string {
  55. return string(compute.AgentTypeEsxi)
  56. }
  57. func (ea *SEsxiAgent) GetAccessIP() (net.IP, error) {
  58. return ea.GetListenIP()
  59. }
  60. func (ea *SEsxiAgent) GetListenIP() (net.IP, error) {
  61. return ea.FindListenIP(options.Options.ListenAddress)
  62. }
  63. func (ea *SEsxiAgent) GetPort() int {
  64. return options.Options.Port
  65. }
  66. func (ea *SEsxiAgent) GetEnableSsl() bool {
  67. return options.Options.EnableSsl
  68. }
  69. func (ea *SEsxiAgent) GetZoneId() string {
  70. return options.Options.Zone
  71. }
  72. func (ea *SEsxiAgent) GetAdminSession() *mcclient.ClientSession {
  73. return auth.GetAdminSession(context.TODO(), options.Options.Region)
  74. }
  75. func (ea *SEsxiAgent) TuneSystem() error {
  76. return nil
  77. }
  78. func (ea *SEsxiAgent) StartService() error {
  79. ea.DoOnline(ea.GetAdminSession())
  80. return nil
  81. }
  82. func (ea *SEsxiAgent) StopService() error {
  83. return nil
  84. }
  85. func (ea *SEsxiAgent) Start() error {
  86. err := ea.SBaseAgent.Start()
  87. if err != nil {
  88. return err
  89. }
  90. // add agent image cache
  91. ea.agentImageCache = storageman.NewAgentImageCacheManager(ea.CacheManager)
  92. ea.AgentStorage = storageman.NewAgentStorage(&storageman.SStorageManager{LocalStorageImagecacheManager: ea.CacheManager},
  93. ea, options.Options.AgentTempPath)
  94. cronManager := cronman.InitCronJobManager(false, options.Options.CronJobWorkerCount, options.Options.TimeZone)
  95. err = cronManager.AddJobEveryFewDays(
  96. "CleanRecycleDiskFiles", 1, 3, 0, 0, storageman.CleanRecycleDiskfiles, false)
  97. if err != nil {
  98. return err
  99. }
  100. cronManager.Start()
  101. return nil
  102. }
  103. func Start(app *appsrv.Application) error {
  104. var err error
  105. if EsxiAgent != nil {
  106. log.Warningf("Global EsxiAgent already start")
  107. return nil
  108. }
  109. EsxiAgent, err = NewEsxiAgent()
  110. if err != nil {
  111. return err
  112. }
  113. err = EsxiAgent.Start()
  114. if err != nil {
  115. return err
  116. }
  117. EsxiAgent.AddImageCacheHandler("", app)
  118. return nil
  119. }
  120. func (agent *SEsxiAgent) AddImageCacheHandler(prefix string, app *appsrv.Application) {
  121. app.AddHandler("POST",
  122. fmt.Sprintf("%s/disks/image_cache", prefix),
  123. auth.Authenticate(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  124. performImageCache(ctx, w, r, agent.agentImageCache.PrefetchImageCache)
  125. }))
  126. app.AddHandler("DELETE",
  127. fmt.Sprintf("%s/disks/image_cache", prefix),
  128. auth.Authenticate(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  129. performImageCache(ctx, w, r, agent.agentImageCache.DeleteImageCache)
  130. }))
  131. }
  132. func performImageCache(
  133. ctx context.Context,
  134. w http.ResponseWriter,
  135. r *http.Request,
  136. performTask workmanager.DelayTaskFunc,
  137. ) {
  138. _, _, body := appsrv.FetchEnv(ctx, w, r)
  139. disk, err := body.Get("disk")
  140. if err != nil {
  141. httperrors.MissingParameterError(ctx, w, "disk")
  142. return
  143. }
  144. hostutils.DelayTask(ctx, performTask, disk)
  145. hostutils.ResponseOk(ctx, w)
  146. }
  147. func Stop() error {
  148. if EsxiAgent != nil {
  149. log.Infof("EsxiAgent stop...")
  150. tmpAgent := EsxiAgent
  151. EsxiAgent = nil
  152. tmpAgent.Stop()
  153. }
  154. return nil
  155. }