huaweistack.go 7.0 KB

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