aliyun.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "fmt"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. "yunion.io/x/pkg/util/rbacscope"
  23. "yunion.io/x/pkg/utils"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/util/logclient"
  30. )
  31. type SAliyunGuestDriver struct {
  32. SManagedVirtualizedGuestDriver
  33. }
  34. func init() {
  35. driver := SAliyunGuestDriver{}
  36. models.RegisterGuestDriver(&driver)
  37. }
  38. func (self *SAliyunGuestDriver) GetHypervisor() string {
  39. return api.HYPERVISOR_ALIYUN
  40. }
  41. func (self *SAliyunGuestDriver) GetProvider() string {
  42. return api.CLOUD_PROVIDER_ALIYUN
  43. }
  44. func (self *SAliyunGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
  45. keys := models.SComputeResourceKeys{}
  46. keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
  47. keys.CloudEnv = api.CLOUD_ENV_PUBLIC_CLOUD
  48. keys.Provider = api.CLOUD_PROVIDER_ALIYUN
  49. keys.Brand = api.CLOUD_PROVIDER_ALIYUN
  50. keys.Hypervisor = api.HYPERVISOR_ALIYUN
  51. return keys
  52. }
  53. func (self *SAliyunGuestDriver) GetDefaultSysDiskBackend() string {
  54. return api.STORAGE_CLOUD_EFFICIENCY
  55. }
  56. func (self *SAliyunGuestDriver) GetMinimalSysDiskSizeGb() int {
  57. return 20
  58. }
  59. func (self *SAliyunGuestDriver) GetStorageTypes() []string {
  60. return []string{
  61. api.STORAGE_CLOUD_EFFICIENCY,
  62. api.STORAGE_CLOUD_SSD,
  63. api.STORAGE_CLOUD_ESSD_PL0,
  64. api.STORAGE_CLOUD_ESSD,
  65. api.STORAGE_CLOUD_ESSD_PL2,
  66. api.STORAGE_CLOUD_ESSD_PL3,
  67. api.STORAGE_CLOUD_ESSD_ENTRY,
  68. api.STORAGE_CLOUD_AUTO,
  69. api.STORAGE_PUBLIC_CLOUD,
  70. api.STORAGE_EPHEMERAL_SSD,
  71. }
  72. }
  73. func (self *SAliyunGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) {
  74. return chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil
  75. }
  76. func (self *SAliyunGuestDriver) GetDetachDiskStatus() ([]string, error) {
  77. return []string{api.VM_READY, api.VM_RUNNING}, nil
  78. }
  79. func (self *SAliyunGuestDriver) GetAttachDiskStatus() ([]string, error) {
  80. return []string{api.VM_READY, api.VM_RUNNING}, nil
  81. }
  82. func (self *SAliyunGuestDriver) GetRebuildRootStatus() ([]string, error) {
  83. return []string{api.VM_READY, api.VM_RUNNING}, nil
  84. }
  85. func (self *SAliyunGuestDriver) IsAllowSaveImageOnRunning() bool {
  86. return true
  87. }
  88. func (self *SAliyunGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  89. return []string{api.VM_READY}, nil
  90. }
  91. func (self *SAliyunGuestDriver) GetDeployStatus() ([]string, error) {
  92. return []string{api.VM_READY, api.VM_RUNNING}, nil
  93. }
  94. func (self *SAliyunGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  95. if !utils.IsInStringArray(guest.Status, []string{api.VM_READY, api.VM_RUNNING, api.VM_START_RESIZE_DISK, api.VM_RESIZE_DISK}) {
  96. return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
  97. }
  98. return nil
  99. }
  100. func (self *SAliyunGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
  101. input, err := self.SManagedVirtualizedGuestDriver.ValidateCreateData(ctx, userCred, input)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if len(input.Networks) > 2 {
  106. return nil, httperrors.NewInputParameterError("cannot support more than 1 nic")
  107. }
  108. for i, disk := range input.Disks {
  109. minGB := -1
  110. maxGB := -1
  111. switch disk.Backend {
  112. case api.STORAGE_CLOUD_EFFICIENCY, api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_ESSD:
  113. minGB = 20
  114. maxGB = 32768
  115. case api.STORAGE_CLOUD_ESSD_PL2:
  116. minGB = 461
  117. maxGB = 32768
  118. case api.STORAGE_CLOUD_ESSD_PL3:
  119. minGB = 1261
  120. maxGB = 32768
  121. case api.STORAGE_PUBLIC_CLOUD:
  122. minGB = 5
  123. maxGB = 2000
  124. case api.STORAGE_EPHEMERAL_SSD:
  125. minGB = 5
  126. maxGB = 800
  127. case api.STORAGE_CLOUD_AUTO, api.STORAGE_CLOUD_ESSD_PL0:
  128. minGB = 40
  129. maxGB = 32768
  130. case api.STORAGE_CLOUD_ESSD_ENTRY:
  131. minGB = 10
  132. maxGB = 32768
  133. }
  134. if i == 0 && (disk.SizeMb < 20*1024 || disk.SizeMb > 2048*1024) {
  135. return nil, httperrors.NewInputParameterError("The system disk size must be in the range of 20GB ~ 2048GB")
  136. }
  137. if disk.SizeMb < minGB*1024 || disk.SizeMb > maxGB*1024 {
  138. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of %dGB ~ %dGB", disk.Backend, minGB, maxGB)
  139. }
  140. }
  141. if input.EipBw > 100 {
  142. return nil, httperrors.NewInputParameterError("%s requires that the eip bandwidth must be less than 100Mbps", self.GetHypervisor())
  143. }
  144. return input, nil
  145. }
  146. func (self *SAliyunGuestDriver) GetGuestInitialStateAfterCreate() string {
  147. return api.VM_RUNNING
  148. }
  149. func (self *SAliyunGuestDriver) GetGuestInitialStateAfterRebuild() string {
  150. return api.VM_READY
  151. }
  152. func (self *SAliyunGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  153. return cloudprovider.SInstanceCapability{
  154. Hypervisor: self.GetHypervisor(),
  155. Provider: self.GetProvider(),
  156. DefaultAccount: cloudprovider.SDefaultAccount{
  157. Linux: cloudprovider.SOsDefaultAccount{
  158. DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER,
  159. Changeable: false,
  160. },
  161. Windows: cloudprovider.SOsDefaultAccount{
  162. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  163. Changeable: false,
  164. },
  165. },
  166. Storages: cloudprovider.Storage{
  167. DataDisk: []cloudprovider.StorageInfo{
  168. {StorageType: api.STORAGE_CLOUD_EFFICIENCY, MaxSizeGb: 32768, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  169. {StorageType: api.STORAGE_CLOUD_SSD, MaxSizeGb: 32768, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  170. {StorageType: api.STORAGE_CLOUD_ESSD_PL0, MaxSizeGb: 32768, MinSizeGb: 40, StepSizeGb: 1, Resizable: true},
  171. {StorageType: api.STORAGE_CLOUD_ESSD, MaxSizeGb: 32768, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  172. {StorageType: api.STORAGE_CLOUD_ESSD_PL2, MaxSizeGb: 32768, MinSizeGb: 461, StepSizeGb: 1, Resizable: true},
  173. {StorageType: api.STORAGE_CLOUD_ESSD_PL3, MaxSizeGb: 32768, MinSizeGb: 1261, StepSizeGb: 1, Resizable: true},
  174. {StorageType: api.STORAGE_CLOUD_ESSD_ENTRY, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  175. {StorageType: api.STORAGE_CLOUD_AUTO, MaxSizeGb: 32768, MinSizeGb: 40, StepSizeGb: 1, Resizable: true},
  176. {StorageType: api.STORAGE_PUBLIC_CLOUD, MaxSizeGb: 2000, MinSizeGb: 5, StepSizeGb: 1, Resizable: true},
  177. {StorageType: api.STORAGE_EPHEMERAL_SSD, MaxSizeGb: 800, MinSizeGb: 5, StepSizeGb: 1, Resizable: true},
  178. },
  179. SysDisk: []cloudprovider.StorageInfo{
  180. {StorageType: api.STORAGE_CLOUD_EFFICIENCY, MaxSizeGb: 500, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  181. {StorageType: api.STORAGE_CLOUD_SSD, MaxSizeGb: 500, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  182. {StorageType: api.STORAGE_CLOUD_ESSD_PL0, MaxSizeGb: 32768, MinSizeGb: 40, StepSizeGb: 1, Resizable: true},
  183. {StorageType: api.STORAGE_CLOUD_ESSD, MaxSizeGb: 500, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  184. {StorageType: api.STORAGE_CLOUD_ESSD_PL2, MaxSizeGb: 32768, MinSizeGb: 461, StepSizeGb: 1, Resizable: true},
  185. {StorageType: api.STORAGE_CLOUD_ESSD_PL3, MaxSizeGb: 32768, MinSizeGb: 1261, StepSizeGb: 1, Resizable: true},
  186. {StorageType: api.STORAGE_CLOUD_ESSD_ENTRY, MaxSizeGb: 32768, MinSizeGb: 10, StepSizeGb: 1, Resizable: true},
  187. {StorageType: api.STORAGE_CLOUD_AUTO, MaxSizeGb: 32768, MinSizeGb: 40, StepSizeGb: 1, Resizable: true},
  188. },
  189. },
  190. }
  191. }
  192. func (self *SAliyunGuestDriver) AllowReconfigGuest() bool {
  193. return true
  194. }
  195. func (self *SAliyunGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
  196. weeks := bc.GetWeeks()
  197. if weeks >= 1 && weeks <= 4 {
  198. return true
  199. }
  200. months := bc.GetMonths()
  201. if (months >= 1 && months <= 10) || (months == 12) || (months == 24) || (months == 36) || (months == 48) || (months == 60) {
  202. return true
  203. }
  204. return false
  205. }
  206. func (self *SAliyunGuestDriver) IsSupportShutdownMode() bool {
  207. return true
  208. }
  209. func (self *SAliyunGuestDriver) IsSupportPublicipToEip() bool {
  210. return true
  211. }
  212. func (self *SAliyunGuestDriver) IsSupportSetAutoRenew() bool {
  213. return true
  214. }
  215. func (self *SAliyunGuestDriver) IsSupportPublicIp() bool {
  216. return true
  217. }
  218. func (self *SAliyunGuestDriver) RemoteActionAfterGuestCreated(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, host *models.SHost, iVM cloudprovider.ICloudVM, desc *cloudprovider.SManagedVMCreateConfig) {
  219. if desc.PublicIpBw > 0 {
  220. publicIp, err := iVM.AllocatePublicIpAddress()
  221. if err != nil {
  222. logclient.AddSimpleActionLog(guest, logclient.ACT_ALLOCATE, errors.Wrapf(err, "iVM.AllocatePublicIpAddress"), userCred, false)
  223. return
  224. }
  225. log.Infof("AllocatePublicIpAddress for instance %s %s", guest.Name, publicIp)
  226. }
  227. }