hcs.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 guestdrivers
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/billing"
  23. "yunion.io/x/pkg/util/rbacscope"
  24. "yunion.io/x/pkg/utils"
  25. "yunion.io/x/sqlchemy"
  26. api "yunion.io/x/onecloud/pkg/apis/compute"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  29. "yunion.io/x/onecloud/pkg/compute/models"
  30. "yunion.io/x/onecloud/pkg/mcclient"
  31. )
  32. type SHCSGuestDriver struct {
  33. SManagedVirtualizedGuestDriver
  34. }
  35. func init() {
  36. driver := SHCSGuestDriver{}
  37. models.RegisterGuestDriver(&driver)
  38. }
  39. func (self *SHCSGuestDriver) GetHypervisor() string {
  40. return api.HYPERVISOR_HCS
  41. }
  42. func (self *SHCSGuestDriver) GetProvider() string {
  43. return api.CLOUD_PROVIDER_HCS
  44. }
  45. func (self *SHCSGuestDriver) DoScheduleSKUFilter() bool {
  46. return false
  47. }
  48. func (self *SHCSGuestDriver) DoScheduleStorageFilter() bool {
  49. return false
  50. }
  51. func (self *SHCSGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
  52. keys := models.SComputeResourceKeys{}
  53. keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
  54. keys.CloudEnv = api.CLOUD_ENV_PRIVATE_CLOUD
  55. keys.Provider = api.CLOUD_PROVIDER_HCS
  56. keys.Brand = api.CLOUD_PROVIDER_HCS
  57. keys.Hypervisor = api.HYPERVISOR_HCS
  58. return keys
  59. }
  60. func (self *SHCSGuestDriver) GetDefaultSysDiskBackend() string {
  61. return api.STORAGE_HUAWEI_SAS
  62. }
  63. func (self *SHCSGuestDriver) GetMinimalSysDiskSizeGb() int {
  64. return 40
  65. }
  66. func (self *SHCSGuestDriver) GetStorageTypes() []string {
  67. storages, _ := models.StorageManager.GetStorageTypesByProvider(self.GetProvider())
  68. return storages
  69. }
  70. func (self *SHCSGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) {
  71. return chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil
  72. }
  73. func (self *SHCSGuestDriver) GetDetachDiskStatus() ([]string, error) {
  74. return []string{api.VM_READY, api.VM_RUNNING}, nil
  75. }
  76. func (self *SHCSGuestDriver) GetAttachDiskStatus() ([]string, error) {
  77. return []string{api.VM_READY, api.VM_RUNNING}, nil
  78. }
  79. func (self *SHCSGuestDriver) GetRebuildRootStatus() ([]string, error) {
  80. return []string{api.VM_READY, api.VM_RUNNING}, nil
  81. }
  82. func (self *SHCSGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  83. return []string{api.VM_READY}, nil
  84. }
  85. func (self *SHCSGuestDriver) GetDeployStatus() ([]string, error) {
  86. return []string{api.VM_READY, api.VM_RUNNING}, nil
  87. }
  88. func (self *SHCSGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  89. if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) {
  90. return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
  91. }
  92. return nil
  93. }
  94. func (self *SHCSGuestDriver) GetGuestInitialStateAfterCreate() string {
  95. return api.VM_RUNNING
  96. }
  97. func (self *SHCSGuestDriver) GetGuestInitialStateAfterRebuild() string {
  98. return api.VM_RUNNING
  99. }
  100. func (self *SHCSGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  101. return cloudprovider.SInstanceCapability{
  102. Hypervisor: self.GetHypervisor(),
  103. Provider: self.GetProvider(),
  104. DefaultAccount: cloudprovider.SDefaultAccount{
  105. Linux: cloudprovider.SOsDefaultAccount{
  106. DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER,
  107. },
  108. Windows: cloudprovider.SOsDefaultAccount{
  109. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  110. },
  111. },
  112. Storages: cloudprovider.Storage{
  113. DataDisk: []cloudprovider.StorageInfo{
  114. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SSD, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  115. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SATA, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  116. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SAS, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  117. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_GPSSD, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  118. },
  119. SysDisk: []cloudprovider.StorageInfo{
  120. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SSD, MaxSizeGb: 1024, MinSizeGb: 40, StepSizeGb: 1, Resizable: false},
  121. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SATA, MaxSizeGb: 1024, MinSizeGb: 40, StepSizeGb: 1, Resizable: false},
  122. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_SAS, MaxSizeGb: 1024, MinSizeGb: 40, StepSizeGb: 1, Resizable: false},
  123. cloudprovider.StorageInfo{StorageType: api.STORAGE_HUAWEI_GPSSD, MaxSizeGb: 1024, MinSizeGb: 40, StepSizeGb: 1, Resizable: false},
  124. },
  125. },
  126. }
  127. }
  128. func (self *SHCSGuestDriver) RemoteDeployGuestSyncHost(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, host *models.SHost, iVM cloudprovider.ICloudVM) (cloudprovider.ICloudHost, error) {
  129. if hostId := iVM.GetIHostId(); len(hostId) > 0 {
  130. nh, err := db.FetchByExternalIdAndManagerId(models.HostManager, hostId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  131. return q.Equals("manager_id", host.ManagerId)
  132. })
  133. if err != nil {
  134. log.Debugf("failed to found new hostId(%s) for ivm %s(%s) error: %v", hostId, guest.Name, guest.Id, err)
  135. if errors.Cause(err) != sql.ErrNoRows {
  136. return nil, errors.Wrap(err, "FetchByExternalIdAndManagerId")
  137. }
  138. // HYPERVISOR_HCS VM被部署到一台全新的宿主机
  139. zone, err := host.GetZone()
  140. if err != nil {
  141. log.Warningf("host %s GetZone: %s", host.GetId(), err)
  142. } else {
  143. _host, err := models.HostManager.NewFromCloudHost(ctx, userCred, iVM.GetIHost(), host.GetCloudprovider(), zone)
  144. if err != nil {
  145. log.Warningf("NewFromCloudHost %s: %s", iVM.GetIHostId(), err)
  146. } else {
  147. host = _host
  148. }
  149. }
  150. } else {
  151. host = nh.(*models.SHost)
  152. }
  153. }
  154. if host.GetId() != guest.HostId {
  155. guest.OnScheduleToHost(ctx, userCred, host.GetId())
  156. }
  157. return host.GetIHost(ctx)
  158. }
  159. func (self *SHCSGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
  160. months := bc.GetMonths()
  161. if (months >= 1 && months <= 9) || (months == 12) || (months == 24) || (months == 36) {
  162. return true
  163. }
  164. return false
  165. }
  166. func (self *SHCSGuestDriver) IsNeedInjectPasswordByCloudInit() bool {
  167. return true
  168. }
  169. func (self *SHCSGuestDriver) IsSupportSetAutoRenew() bool {
  170. return false
  171. }