cloudpods-baremetal.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 hostdrivers
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. "yunion.io/x/onecloud/pkg/mcclient/cloudpods"
  27. )
  28. type SCloudpodsBaremetalHostDriver struct {
  29. SManagedVirtualizationHostDriver
  30. }
  31. func init() {
  32. driver := SCloudpodsBaremetalHostDriver{}
  33. models.RegisterHostDriver(&driver)
  34. }
  35. func (self *SCloudpodsBaremetalHostDriver) GetHostType() string {
  36. return api.HOST_TYPE_BAREMETAL
  37. }
  38. func (self *SCloudpodsBaremetalHostDriver) GetHypervisor() string {
  39. return api.HYPERVISOR_BAREMETAL
  40. }
  41. func (self *SCloudpodsBaremetalHostDriver) GetProvider() string {
  42. return api.CLOUD_PROVIDER_CLOUDPODS
  43. }
  44. func (self *SCloudpodsBaremetalHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
  45. return nil
  46. }
  47. func (driver *SCloudpodsBaremetalHostDriver) GetStoragecacheQuota(host *models.SHost) int {
  48. return -1
  49. }
  50. func (driver *SCloudpodsBaremetalHostDriver) RequestBaremetalUnmaintence(ctx context.Context, userCred mcclient.TokenCredential, baremetal *models.SHost, task taskman.ITask) error {
  51. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  52. iHost, err := baremetal.GetIHost(ctx)
  53. if err != nil {
  54. return nil, errors.Wrapf(err, "GetIHost")
  55. }
  56. h := iHost.(*cloudpods.SHost)
  57. err = h.Stop()
  58. if err != nil {
  59. return nil, err
  60. }
  61. err = cloudprovider.Wait(time.Second*10, time.Minute*10, func() (bool, error) {
  62. err = iHost.Refresh()
  63. if err != nil {
  64. return false, errors.Wrapf(err, "Refresh")
  65. }
  66. status := iHost.GetStatus()
  67. log.Debugf("expect baremetal host status %s current is: %s", api.HOST_STATUS_READY, status)
  68. if status != api.HOST_STATUS_READY {
  69. return false, nil
  70. }
  71. return true, nil
  72. })
  73. if err != nil {
  74. return nil, errors.Wrapf(err, "Wait status")
  75. }
  76. return nil, nil
  77. })
  78. return nil
  79. }
  80. func (driver *SCloudpodsBaremetalHostDriver) RequestBaremetalMaintence(ctx context.Context, userCred mcclient.TokenCredential, baremetal *models.SHost, task taskman.ITask) error {
  81. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  82. iHost, err := baremetal.GetIHost(ctx)
  83. if err != nil {
  84. return nil, errors.Wrapf(err, "GetIHost")
  85. }
  86. h := iHost.(*cloudpods.SHost)
  87. err = h.Start()
  88. if err != nil {
  89. return nil, err
  90. }
  91. err = cloudprovider.Wait(time.Second*10, time.Minute*10, func() (bool, error) {
  92. err = iHost.Refresh()
  93. if err != nil {
  94. return false, errors.Wrapf(err, "Refresh")
  95. }
  96. status := iHost.GetStatus()
  97. log.Debugf("expect baremetal host status %s current is: %s", api.HOST_STATUS_RUNNING, status)
  98. if status != api.HOST_STATUS_RUNNING {
  99. return false, nil
  100. }
  101. return true, nil
  102. })
  103. if err != nil {
  104. return nil, errors.Wrapf(err, "Wait status")
  105. }
  106. return nil, nil
  107. })
  108. return nil
  109. }
  110. func (driver *SCloudpodsBaremetalHostDriver) RequestSyncBaremetalHostStatus(ctx context.Context, userCred mcclient.TokenCredential, baremetal *models.SHost, task taskman.ITask) error {
  111. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  112. iHost, err := baremetal.GetIHost(ctx)
  113. if err != nil {
  114. return nil, errors.Wrapf(err, "GetIHost")
  115. }
  116. return nil, baremetal.SyncWithCloudHost(ctx, userCred, iHost)
  117. })
  118. return nil
  119. }
  120. func (driver *SCloudpodsBaremetalHostDriver) RequestSyncBaremetalHostConfig(ctx context.Context, userCred mcclient.TokenCredential, baremetal *models.SHost, task taskman.ITask) error {
  121. return driver.RequestSyncBaremetalHostStatus(ctx, userCred, baremetal, task)
  122. }