qcloud.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. "yunion.io/x/pkg/util/osprofile"
  23. "yunion.io/x/pkg/util/rbacscope"
  24. "yunion.io/x/pkg/utils"
  25. api "yunion.io/x/onecloud/pkg/apis/compute"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  27. "yunion.io/x/onecloud/pkg/compute/models"
  28. "yunion.io/x/onecloud/pkg/httperrors"
  29. "yunion.io/x/onecloud/pkg/mcclient"
  30. )
  31. type SQcloudGuestDriver struct {
  32. SManagedVirtualizedGuestDriver
  33. }
  34. func init() {
  35. driver := SQcloudGuestDriver{}
  36. models.RegisterGuestDriver(&driver)
  37. }
  38. func (self *SQcloudGuestDriver) GetHypervisor() string {
  39. return api.HYPERVISOR_QCLOUD
  40. }
  41. func (self *SQcloudGuestDriver) GetProvider() string {
  42. return api.CLOUD_PROVIDER_QCLOUD
  43. }
  44. func (self *SQcloudGuestDriver) 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_QCLOUD
  49. keys.Brand = api.CLOUD_PROVIDER_QCLOUD
  50. keys.Hypervisor = api.HYPERVISOR_QCLOUD
  51. return keys
  52. }
  53. func (self *SQcloudGuestDriver) GetDefaultSysDiskBackend() string {
  54. return api.STORAGE_CLOUD_PREMIUM
  55. }
  56. func (self *SQcloudGuestDriver) GetMinimalSysDiskSizeGb() int {
  57. return 50
  58. }
  59. func (self *SQcloudGuestDriver) GetStorageTypes() []string {
  60. return []string{
  61. api.STORAGE_CLOUD_BASIC,
  62. api.STORAGE_CLOUD_PREMIUM,
  63. api.STORAGE_CLOUD_SSD,
  64. api.STORAGE_LOCAL_BASIC,
  65. api.STORAGE_LOCAL_SSD,
  66. api.STORAGE_CLOUD_HSSD,
  67. api.STORAGE_CLOUD_BSSD,
  68. api.STORAGE_CLOUD_TSSD,
  69. }
  70. }
  71. func (self *SQcloudGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) {
  72. return chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil
  73. }
  74. func (self *SQcloudGuestDriver) GetDetachDiskStatus() ([]string, error) {
  75. return []string{api.VM_READY, api.VM_RUNNING}, nil
  76. }
  77. func (self *SQcloudGuestDriver) GetAttachDiskStatus() ([]string, error) {
  78. return []string{api.VM_READY, api.VM_RUNNING}, nil
  79. }
  80. func (self *SQcloudGuestDriver) GetRebuildRootStatus() ([]string, error) {
  81. return []string{api.VM_READY, api.VM_RUNNING}, nil
  82. }
  83. func (self *SQcloudGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  84. return []string{api.VM_READY, api.VM_RUNNING}, nil
  85. }
  86. func (self *SQcloudGuestDriver) GetDeployStatus() ([]string, error) {
  87. return []string{api.VM_READY, api.VM_RUNNING}, nil
  88. }
  89. func (self *SQcloudGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  90. //https://cloud.tencent.com/document/product/362/5747
  91. if !utils.IsInStringArray(guest.Status, []string{api.VM_READY, api.VM_RUNNING, api.VM_START_RESIZE_DISK, api.VM_RESIZE_DISK}) {
  92. return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
  93. }
  94. if disk.DiskType == api.DISK_TYPE_SYS {
  95. return fmt.Errorf("Cannot resize system disk")
  96. }
  97. if utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_LOCAL_BASIC, api.STORAGE_LOCAL_SSD, api.STORAGE_LOCAL_PRO}) {
  98. return fmt.Errorf("Cannot resize %s disk", storage.StorageType)
  99. }
  100. if disk.DiskSize/1024%10 > 0 {
  101. return fmt.Errorf("Resize disk size must be an integer multiple of 10G")
  102. }
  103. return nil
  104. }
  105. func (self *SQcloudGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
  106. input, err := self.SManagedVirtualizedGuestDriver.ValidateCreateData(ctx, userCred, input)
  107. if err != nil {
  108. return nil, err
  109. }
  110. if len(input.Networks) > 2 {
  111. return nil, httperrors.NewInputParameterError("cannot support more than 1 nic")
  112. }
  113. sysDisk := input.Disks[0]
  114. if sysDisk.SizeMb < 50*1024 {
  115. return nil, fmt.Errorf("The system disk size must be more than 50GB")
  116. }
  117. switch sysDisk.Backend {
  118. case api.STORAGE_CLOUD_BASIC, api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_PREMIUM:
  119. if sysDisk.SizeMb > 1024*1024 {
  120. return nil, fmt.Errorf("The %s system disk size must be less than 1024GB", sysDisk.Backend)
  121. }
  122. case api.STORAGE_LOCAL_PRO: //https://cloud.tencent.com/document/product/362/2353
  123. return nil, fmt.Errorf("storage %s can not be system disk", sysDisk.Backend)
  124. }
  125. for i := 1; i < len(input.Disks); i++ {
  126. disk := input.Disks[i]
  127. switch disk.Backend {
  128. case api.STORAGE_CLOUD_BASIC:
  129. if disk.SizeMb < 10*1024 || disk.SizeMb > 16000*1024 {
  130. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 10GB ~ 16000GB", disk.Backend)
  131. }
  132. case api.STORAGE_CLOUD_PREMIUM:
  133. if disk.SizeMb < 10*1024 || disk.SizeMb > 32000*1024 {
  134. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 10GB ~ 32000GB", disk.Backend)
  135. }
  136. case api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_HSSD, api.STORAGE_CLOUD_BSSD, api.STORAGE_CLOUD_TSSD:
  137. if disk.SizeMb < 20*1024 || disk.SizeMb > 32000*1024 {
  138. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 20GB ~ 32000GB", disk.Backend)
  139. }
  140. case api.STORAGE_LOCAL_PRO:
  141. return nil, httperrors.NewInputParameterError("storage %s can not be data disk", disk.Backend)
  142. }
  143. if disk.SizeMb/1024%10 > 0 {
  144. return nil, httperrors.NewInputParameterError("Data disk size must be an integer multiple of 10G")
  145. }
  146. }
  147. return input, nil
  148. }
  149. func (qcloud *SQcloudGuestDriver) ValidateGuestChangeConfigInput(ctx context.Context, guest *models.SGuest, input api.ServerChangeConfigInput) (*api.ServerChangeConfigSettings, error) {
  150. confs, err := qcloud.SBaseGuestDriver.ValidateGuestChangeConfigInput(ctx, guest, input)
  151. if err != nil {
  152. return nil, errors.Wrap(err, "SBaseGuestDriver.ValidateGuestChangeConfigInput")
  153. }
  154. if confs.CpuChanged() || confs.MemChanged() {
  155. disk, err := guest.GetSystemDisk()
  156. if err != nil {
  157. return nil, httperrors.NewResourceNotFoundError("failed to found system disk error: %v", err)
  158. }
  159. storage, _ := disk.GetStorage()
  160. if storage == nil {
  161. return nil, httperrors.NewResourceNotFoundError("failed to found storage for disk %s(%s)", disk.Name, disk.Id)
  162. }
  163. // 腾讯云系统盘为本地存储,不支持调整配置
  164. if utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_LOCAL_BASIC, api.STORAGE_LOCAL_SSD, api.STORAGE_LOCAL_PRO}) {
  165. return nil, httperrors.NewUnsupportOperationError("The system disk is locally stored and does not support changing configuration")
  166. }
  167. }
  168. for _, newDisk := range confs.Create {
  169. switch newDisk.Backend {
  170. case api.STORAGE_CLOUD_BASIC:
  171. if newDisk.SizeMb < 10*1024 || newDisk.SizeMb > 16000*1024 {
  172. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 10GB ~ 16000GB", newDisk.Backend)
  173. }
  174. case api.STORAGE_CLOUD_PREMIUM:
  175. if newDisk.SizeMb < 10*1024 || newDisk.SizeMb > 32000*1024 {
  176. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 10GB ~ 32000GB", newDisk.Backend)
  177. }
  178. case api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_HSSD, api.STORAGE_CLOUD_BSSD, api.STORAGE_CLOUD_TSSD:
  179. if newDisk.SizeMb < 20*1024 || newDisk.SizeMb > 32000*1024 {
  180. return nil, httperrors.NewInputParameterError("The %s disk size must be in the range of 20GB ~ 32000GB", newDisk.Backend)
  181. }
  182. case api.STORAGE_LOCAL_BASIC, api.STORAGE_LOCAL_SSD, api.STORAGE_LOCAL_PRO:
  183. return nil, httperrors.NewUnsupportOperationError("Not support create local storage disks")
  184. case "": //这里Backend为空有可能会导致创建出来还是local storage,依然会出错,需要用户显式指定
  185. return nil, httperrors.NewInputParameterError("Please input new disk backend type")
  186. }
  187. if newDisk.SizeMb/1024%10 > 0 {
  188. return nil, httperrors.NewInputParameterError("Data disk size must be an integer multiple of 10G")
  189. }
  190. }
  191. return confs, nil
  192. }
  193. func (self *SQcloudGuestDriver) ValidateDetachDisk(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, disk *models.SDisk) error {
  194. storage, _ := disk.GetStorage()
  195. if storage == nil {
  196. return httperrors.NewResourceNotFoundError("failed to found storage for disk %s(%s)", disk.Name, disk.Id)
  197. }
  198. // 腾讯云本地盘不支持卸载
  199. if utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_LOCAL_BASIC, api.STORAGE_LOCAL_SSD}) {
  200. return httperrors.NewUnsupportOperationError("The disk is locally stored and does not support detach")
  201. }
  202. return nil
  203. }
  204. func (self *SQcloudGuestDriver) GetGuestInitialStateAfterCreate() string {
  205. return api.VM_RUNNING
  206. }
  207. func (self *SQcloudGuestDriver) GetGuestInitialStateAfterRebuild() string {
  208. return api.VM_RUNNING
  209. }
  210. func (self *SQcloudGuestDriver) IsSupportdDcryptPasswordFromSecretKey() bool {
  211. return false
  212. }
  213. func (self *SQcloudGuestDriver) IsSupportShutdownMode() bool {
  214. return true
  215. }
  216. func (self *SQcloudGuestDriver) GetUserDataType() string {
  217. return cloudprovider.CLOUD_SHELL
  218. }
  219. func (self *SQcloudGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  220. return cloudprovider.SInstanceCapability{
  221. Hypervisor: self.GetHypervisor(),
  222. Provider: self.GetProvider(),
  223. DefaultAccount: cloudprovider.SDefaultAccount{
  224. Linux: cloudprovider.SOsDefaultAccount{
  225. DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER,
  226. Changeable: false,
  227. DistAccounts: []cloudprovider.SDistDefaultAccount{
  228. {
  229. OsDistribution: "Ubuntu",
  230. DefaultAccount: "ubuntu",
  231. Changeable: false,
  232. },
  233. },
  234. },
  235. Windows: cloudprovider.SOsDefaultAccount{
  236. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  237. Changeable: false,
  238. },
  239. },
  240. Storages: cloudprovider.Storage{
  241. DataDisk: []cloudprovider.StorageInfo{
  242. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_BASIC, MaxSizeGb: 16000, MinSizeGb: 10, StepSizeGb: 10, Resizable: true},
  243. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_PREMIUM, MaxSizeGb: 16000, MinSizeGb: 50, StepSizeGb: 10, Resizable: true},
  244. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_SSD, MaxSizeGb: 16000, MinSizeGb: 100, StepSizeGb: 10, Resizable: true},
  245. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_HSSD, MaxSizeGb: 32000, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  246. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_BSSD, MaxSizeGb: 32000, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  247. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_TSSD, MaxSizeGb: 32000, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  248. },
  249. SysDisk: []cloudprovider.StorageInfo{
  250. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_BASIC, MaxSizeGb: 500, MinSizeGb: 50, StepSizeGb: 10, Resizable: false},
  251. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_PREMIUM, MaxSizeGb: 1024, MinSizeGb: 50, StepSizeGb: 10, Resizable: false},
  252. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_SSD, MaxSizeGb: 500, MinSizeGb: 50, StepSizeGb: 1, Resizable: false},
  253. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_BSSD, MaxSizeGb: 32000, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  254. cloudprovider.StorageInfo{StorageType: api.STORAGE_CLOUD_TSSD, MaxSizeGb: 32000, MinSizeGb: 20, StepSizeGb: 1, Resizable: true},
  255. },
  256. },
  257. }
  258. }
  259. func (self *SQcloudGuestDriver) GetDefaultAccount(osType, osDist, imageType string) string {
  260. userName := api.VM_DEFAULT_LINUX_LOGIN_USER
  261. if imageType == "system" {
  262. if osDist == "Ubuntu" {
  263. userName = "ubuntu"
  264. }
  265. }
  266. if strings.ToLower(osType) == strings.ToLower(osprofile.OS_TYPE_WINDOWS) {
  267. userName = api.VM_DEFAULT_WINDOWS_LOGIN_USER
  268. }
  269. return userName
  270. }
  271. func (self *SQcloudGuestDriver) AllowReconfigGuest() bool {
  272. return true
  273. }
  274. func (self *SQcloudGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
  275. months := bc.GetMonths()
  276. if (months >= 1 && months <= 12) || (months == 24) || (months == 36) {
  277. return true
  278. }
  279. return false
  280. }
  281. func (self *SQcloudGuestDriver) IsSupportPublicIp() bool {
  282. return true
  283. }
  284. func (self *SQcloudGuestDriver) IsSupportPublicipToEip() bool {
  285. return true
  286. }
  287. func (self *SQcloudGuestDriver) IsSupportSetAutoRenew() bool {
  288. return true
  289. }