hostdrivers.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type IHostDriver interface {
  26. GetHostType() string
  27. GetProvider() string
  28. GetHypervisor() string
  29. RequestBaremetalUnmaintence(ctx context.Context, userCred mcclient.TokenCredential, baremetal *SHost, task taskman.ITask) error
  30. RequestBaremetalMaintence(ctx context.Context, userCred mcclient.TokenCredential, baremetal *SHost, task taskman.ITask) error
  31. RequestSyncBaremetalHostStatus(ctx context.Context, userCred mcclient.TokenCredential, baremetal *SHost, task taskman.ITask) error
  32. RequestSyncBaremetalHostConfig(ctx context.Context, userCred mcclient.TokenCredential, baremetal *SHost, task taskman.ITask) error
  33. CheckAndSetCacheImage(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, storagecache *SStoragecache, task taskman.ITask) error
  34. RequestUncacheImage(ctx context.Context, host *SHost, storageCache *SStoragecache, task taskman.ITask, deactivateImage bool) error
  35. ValidateUpdateDisk(ctx context.Context, userCred mcclient.TokenCredential, input *api.DiskUpdateInput) (*api.DiskUpdateInput, error)
  36. ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *SDisk, snapshot *SSnapshot, guests []SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error)
  37. ValidateDiskSize(storage *SStorage, sizeGb int) error
  38. RequestPrepareSaveDiskOnHost(ctx context.Context, host *SHost, disk *SDisk, imageId string, task taskman.ITask) error
  39. RequestSaveUploadImageOnHost(ctx context.Context, host *SHost, disk *SDisk, imageId string, task taskman.ITask, data jsonutils.JSONObject) error
  40. // create disk
  41. RequestAllocateDiskOnStorage(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, storage *SStorage, disk *SDisk, task taskman.ITask, input api.DiskAllocateInput) error
  42. RequestRebuildDiskOnStorage(ctx context.Context, host *SHost, storage *SStorage, disk *SDisk, task taskman.ITask, input api.DiskAllocateInput) error
  43. RequestRemoteUpdateDisk(ctx context.Context, userCred mcclient.TokenCredential, storage *SStorage, disk *SDisk, replaceTags bool) error
  44. // delete disk
  45. RequestDeallocateDiskOnHost(ctx context.Context, host *SHost, storage *SStorage, disk *SDisk, cleanSnapshots bool, task taskman.ITask) error
  46. RequestDeallocateBackupDiskOnHost(ctx context.Context, host *SHost, storage *SStorage, disk *SDisk, task taskman.ITask) error
  47. // resize disk
  48. RequestResizeDiskOnHost(ctx context.Context, host *SHost, storage *SStorage, disk *SDisk, size int64, task taskman.ITask) error
  49. RequestDiskSrcMigratePrepare(ctx context.Context, host *SHost, disk *SDisk, task taskman.ITask) (jsonutils.JSONObject, error)
  50. RequestDiskMigrate(ctx context.Context, targetHost *SHost, targetStorage *SStorage, disk *SDisk, task taskman.ITask, body *jsonutils.JSONDict) error
  51. RequestDeleteSnapshotsWithStorage(ctx context.Context, host *SHost, snapshot *SSnapshot, task taskman.ITask, snapshotIds []string) error
  52. RequestDeleteSnapshotWithoutGuest(ctx context.Context, host *SHost, snapshot *SSnapshot, params *jsonutils.JSONDict, task taskman.ITask) error
  53. RequestResetDisk(ctx context.Context, host *SHost, disk *SDisk, params *jsonutils.JSONDict, task taskman.ITask) error
  54. RequestCleanUpDiskSnapshots(ctx context.Context, host *SHost, disk *SDisk, params *jsonutils.JSONDict, task taskman.ITask) error
  55. PrepareConvert(host *SHost, image, raid string, data jsonutils.JSONObject) (*api.ServerCreateInput, error)
  56. PrepareUnconvert(host *SHost) error
  57. FinishUnconvert(ctx context.Context, userCred mcclient.TokenCredential, host *SHost) error
  58. FinishConvert(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, guest *SGuest, hostType string) error
  59. ConvertFailed(host *SHost) error
  60. GetRaidScheme(host *SHost, raid string) (string, error)
  61. IsReachStoragecacheCapacityLimit(host *SHost, cachedImages []SCachedimage) bool
  62. GetStoragecacheQuota(host *SHost) int
  63. ValidateAttachStorage(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, storage *SStorage, input api.HostStorageCreateInput) (api.HostStorageCreateInput, error)
  64. RequestAttachStorage(ctx context.Context, hoststorage *SHoststorage, host *SHost, storage *SStorage, task taskman.ITask) error
  65. RequestDetachStorage(ctx context.Context, host *SHost, storage *SStorage, task taskman.ITask) error
  66. RequestSyncOnHost(ctx context.Context, host *SHost, task taskman.ITask) error
  67. RequestProbeIsolatedDevices(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, input jsonutils.JSONObject) (*jsonutils.JSONArray, error)
  68. RequestUploadGuestsStatus(ctx context.Context, host *SHost, guests []SGuest, task taskman.ITask) error
  69. }
  70. var hostDrivers map[string]IHostDriver
  71. func init() {
  72. hostDrivers = make(map[string]IHostDriver)
  73. }
  74. func RegisterHostDriver(driver IHostDriver) {
  75. key := fmt.Sprintf("%s-%s", driver.GetHostType(), driver.GetProvider())
  76. hostDrivers[key] = driver
  77. }
  78. func GetHostDriver(hostType, provider string) (IHostDriver, error) {
  79. key := fmt.Sprintf("%s-%s", hostType, provider)
  80. driver, ok := hostDrivers[key]
  81. if ok {
  82. return driver, nil
  83. }
  84. return nil, errors.Wrapf(errors.ErrNotFound, "host type: %s provider: %s", hostType, provider)
  85. }
  86. func Hypervisors2HostTypes(hypervisors []string) []string {
  87. ret := []string{}
  88. for _, driver := range hostDrivers {
  89. if !utils.IsInStringArray(driver.GetHypervisor(), hypervisors) {
  90. continue
  91. }
  92. if utils.IsInStringArray(driver.GetHostType(), ret) {
  93. continue
  94. }
  95. ret = append(ret, driver.GetHostType())
  96. }
  97. return ret
  98. }