cloudpods-pod.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/cloudinit"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. "yunion.io/x/pkg/utils"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/compute/options"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. )
  30. type SCloudpodsPodGuestDriver struct {
  31. SManagedVirtualizedGuestDriver
  32. }
  33. func init() {
  34. driver := SCloudpodsPodGuestDriver{}
  35. models.RegisterGuestDriver(&driver)
  36. }
  37. func (self *SCloudpodsPodGuestDriver) DoScheduleCPUFilter() bool { return true }
  38. func (self *SCloudpodsPodGuestDriver) DoScheduleMemoryFilter() bool { return true }
  39. func (self *SCloudpodsPodGuestDriver) DoScheduleSKUFilter() bool { return false }
  40. func (self *SCloudpodsPodGuestDriver) DoScheduleStorageFilter() bool { return true }
  41. func (self *SCloudpodsPodGuestDriver) GetHypervisor() string {
  42. return api.HYPERVISOR_POD
  43. }
  44. func (self *SCloudpodsPodGuestDriver) GetProvider() string {
  45. return api.CLOUD_PROVIDER_CLOUDPODS
  46. }
  47. func (self *SCloudpodsPodGuestDriver) GetGuestInitialStateAfterCreate() string {
  48. return api.VM_READY
  49. }
  50. func (self *SCloudpodsPodGuestDriver) DoGuestCreateDisksTask(ctx context.Context, guest *models.SGuest, task taskman.ITask) error {
  51. subtask, err := taskman.TaskManager.NewTask(ctx, "CloudpodsGuestCreateDiskTask", guest, task.GetUserCred(), task.GetParams(), task.GetTaskId(), "", nil)
  52. if err != nil {
  53. return errors.Wrapf(err, "NewTask")
  54. }
  55. return subtask.ScheduleRun(nil)
  56. }
  57. func (self *SCloudpodsPodGuestDriver) GetDetachDiskStatus() ([]string, error) {
  58. return []string{api.VM_READY, api.VM_RUNNING}, nil
  59. }
  60. func (self *SCloudpodsPodGuestDriver) GetAttachDiskStatus() ([]string, error) {
  61. return []string{api.VM_READY, api.VM_RUNNING}, nil
  62. }
  63. func (self *SCloudpodsPodGuestDriver) GetRebuildRootStatus() ([]string, error) {
  64. return []string{api.VM_READY, api.VM_RUNNING}, nil
  65. }
  66. func (self *SCloudpodsPodGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  67. return []string{api.VM_READY, api.VM_RUNNING}, nil
  68. }
  69. func (self *SCloudpodsPodGuestDriver) GetDeployStatus() ([]string, error) {
  70. return []string{api.VM_READY, api.VM_ADMIN}, nil
  71. }
  72. func (self *SCloudpodsPodGuestDriver) IsSupportCdrom(guest *models.SGuest) (bool, error) {
  73. return true, nil
  74. }
  75. func (self *SCloudpodsPodGuestDriver) IsSupportFloppy(guest *models.SGuest) (bool, error) {
  76. return false, nil
  77. }
  78. func (self *SCloudpodsPodGuestDriver) IsSupportMigrate() bool {
  79. return true
  80. }
  81. func (self *SCloudpodsPodGuestDriver) IsSupportLiveMigrate() bool {
  82. return true
  83. }
  84. func (self *SCloudpodsPodGuestDriver) GetUserDataType() string {
  85. return cloudprovider.CLOUD_SHELL_WITHOUT_ENCRYPT
  86. }
  87. func (self *SCloudpodsPodGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  88. if guest.GetDiskIndex(disk.Id) <= 0 && guest.Status == api.VM_RUNNING {
  89. return httperrors.NewUnsupportOperationError("Cann't online resize root disk")
  90. }
  91. if !utils.IsInStringArray(guest.Status, []string{api.VM_READY, api.VM_RUNNING}) {
  92. return httperrors.NewServerStatusError("Cannot resize disk when guest in status %s", guest.Status)
  93. }
  94. return nil
  95. }
  96. func (self *SCloudpodsPodGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
  97. keys := models.SComputeResourceKeys{}
  98. keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
  99. keys.CloudEnv = api.CLOUD_ENV_PRIVATE_CLOUD
  100. keys.Provider = api.CLOUD_PROVIDER_CLOUDPODS
  101. keys.Brand = brand
  102. keys.Hypervisor = api.HYPERVISOR_DEFAULT
  103. return keys
  104. }
  105. func (self *SCloudpodsPodGuestDriver) GetDefaultSysDiskBackend() string {
  106. return api.STORAGE_LOCAL
  107. }
  108. func (self *SCloudpodsPodGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  109. return cloudprovider.SInstanceCapability{
  110. Hypervisor: self.GetHypervisor(),
  111. Provider: self.GetProvider(),
  112. DefaultAccount: cloudprovider.SDefaultAccount{
  113. Linux: cloudprovider.SOsDefaultAccount{
  114. DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER,
  115. Changeable: false,
  116. },
  117. Windows: cloudprovider.SOsDefaultAccount{
  118. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  119. Changeable: false,
  120. },
  121. },
  122. Storages: cloudprovider.Storage{
  123. SysDisk: []cloudprovider.StorageInfo{
  124. {StorageType: api.STORAGE_LOCAL, MinSizeGb: options.Options.LocalSysDiskMinSizeGB, MaxSizeGb: options.Options.LocalSysDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  125. {StorageType: api.STORAGE_RBD, MinSizeGb: options.Options.LocalSysDiskMinSizeGB, MaxSizeGb: options.Options.LocalSysDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  126. {StorageType: api.STORAGE_NFS, MinSizeGb: options.Options.LocalSysDiskMinSizeGB, MaxSizeGb: options.Options.LocalSysDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  127. {StorageType: api.STORAGE_GPFS, MinSizeGb: options.Options.LocalSysDiskMinSizeGB, MaxSizeGb: options.Options.LocalSysDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  128. },
  129. DataDisk: []cloudprovider.StorageInfo{
  130. {StorageType: api.STORAGE_LOCAL, MinSizeGb: options.Options.LocalDataDiskMinSizeGB, MaxSizeGb: options.Options.LocalDataDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  131. {StorageType: api.STORAGE_RBD, MinSizeGb: options.Options.LocalDataDiskMinSizeGB, MaxSizeGb: options.Options.LocalDataDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  132. {StorageType: api.STORAGE_NFS, MinSizeGb: options.Options.LocalDataDiskMinSizeGB, MaxSizeGb: options.Options.LocalDataDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  133. {StorageType: api.STORAGE_GPFS, MinSizeGb: options.Options.LocalDataDiskMinSizeGB, MaxSizeGb: options.Options.LocalDataDiskMaxSizeGB, StepSizeGb: 1, Resizable: true},
  134. },
  135. },
  136. }
  137. }
  138. func (self *SCloudpodsPodGuestDriver) GetMinimalSysDiskSizeGb() int {
  139. return options.Options.DefaultDiskSizeMB / 1024
  140. }
  141. func (self *SCloudpodsPodGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
  142. if len(input.UserData) > 0 {
  143. _, err := cloudinit.ParseUserData(input.UserData)
  144. if err != nil {
  145. return nil, err
  146. }
  147. }
  148. return input, nil
  149. }