h3c.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 SH3CGuestDriver struct {
  33. SManagedVirtualizedGuestDriver
  34. }
  35. func init() {
  36. driver := SH3CGuestDriver{}
  37. models.RegisterGuestDriver(&driver)
  38. }
  39. func (self *SH3CGuestDriver) GetHypervisor() string {
  40. return api.HYPERVISOR_H3C
  41. }
  42. func (self *SH3CGuestDriver) GetProvider() string {
  43. return api.CLOUD_PROVIDER_H3C
  44. }
  45. func (self *SH3CGuestDriver) DoScheduleSKUFilter() bool {
  46. return false
  47. }
  48. func (self *SH3CGuestDriver) DoScheduleStorageFilter() bool {
  49. return false
  50. }
  51. func (self *SH3CGuestDriver) 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_H3C
  56. keys.Brand = api.CLOUD_PROVIDER_H3C
  57. keys.Hypervisor = api.HYPERVISOR_H3C
  58. return keys
  59. }
  60. func (self *SH3CGuestDriver) GetDefaultSysDiskBackend() string {
  61. return api.STORAGE_LOCAL
  62. }
  63. func (self *SH3CGuestDriver) GetMinimalSysDiskSizeGb() int {
  64. return 40
  65. }
  66. func (self *SH3CGuestDriver) GetStorageTypes() []string {
  67. storages, _ := models.StorageManager.GetStorageTypesByProvider(self.GetProvider())
  68. return storages
  69. }
  70. func (self *SH3CGuestDriver) 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 *SH3CGuestDriver) GetDetachDiskStatus() ([]string, error) {
  74. return []string{api.VM_READY, api.VM_RUNNING}, nil
  75. }
  76. func (self *SH3CGuestDriver) GetAttachDiskStatus() ([]string, error) {
  77. return []string{api.VM_READY, api.VM_RUNNING}, nil
  78. }
  79. func (self *SH3CGuestDriver) GetRebuildRootStatus() ([]string, error) {
  80. return []string{api.VM_READY, api.VM_RUNNING}, nil
  81. }
  82. func (self *SH3CGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  83. return []string{api.VM_READY}, nil
  84. }
  85. func (self *SH3CGuestDriver) GetDeployStatus() ([]string, error) {
  86. return []string{api.VM_READY, api.VM_RUNNING}, nil
  87. }
  88. func (self *SH3CGuestDriver) 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 *SH3CGuestDriver) GetGuestInitialStateAfterCreate() string {
  95. return api.VM_RUNNING
  96. }
  97. func (self *SH3CGuestDriver) GetGuestInitialStateAfterRebuild() string {
  98. return api.VM_RUNNING
  99. }
  100. func (self *SH3CGuestDriver) 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. }
  113. }
  114. func (self *SH3CGuestDriver) RemoteDeployGuestSyncHost(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, host *models.SHost, iVM cloudprovider.ICloudVM) (cloudprovider.ICloudHost, error) {
  115. if hostId := iVM.GetIHostId(); len(hostId) > 0 {
  116. nh, err := db.FetchByExternalIdAndManagerId(models.HostManager, hostId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  117. return q.Equals("manager_id", host.ManagerId)
  118. })
  119. if err != nil {
  120. log.Debugf("failed to found new hostId(%s) for ivm %s(%s) error: %v", hostId, guest.Name, guest.Id, err)
  121. if errors.Cause(err) != sql.ErrNoRows {
  122. return nil, errors.Wrap(err, "FetchByExternalIdAndManagerId")
  123. }
  124. // HYPERVISOR_H3C VM被部署到一台全新的宿主机
  125. zone, err := host.GetZone()
  126. if err != nil {
  127. log.Warningf("host %s GetZone: %s", host.GetId(), err)
  128. } else {
  129. _host, err := models.HostManager.NewFromCloudHost(ctx, userCred, iVM.GetIHost(), host.GetCloudprovider(), zone)
  130. if err != nil {
  131. log.Warningf("NewFromCloudHost %s: %s", iVM.GetIHostId(), err)
  132. } else {
  133. host = _host
  134. }
  135. }
  136. } else {
  137. host = nh.(*models.SHost)
  138. }
  139. }
  140. if host.GetId() != guest.HostId {
  141. guest.OnScheduleToHost(ctx, userCred, host.GetId())
  142. }
  143. return host.GetIHost(ctx)
  144. }
  145. func (self *SH3CGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
  146. return false
  147. }
  148. func (self *SH3CGuestDriver) IsNeedInjectPasswordByCloudInit() bool {
  149. return true
  150. }
  151. func (self *SH3CGuestDriver) IsSupportSetAutoRenew() bool {
  152. return false
  153. }