aws.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  28. "yunion.io/x/onecloud/pkg/compute/models"
  29. "yunion.io/x/onecloud/pkg/httperrors"
  30. "yunion.io/x/onecloud/pkg/mcclient"
  31. )
  32. type SAwsGuestDriver struct {
  33. SManagedVirtualizedGuestDriver
  34. }
  35. func init() {
  36. driver := SAwsGuestDriver{}
  37. models.RegisterGuestDriver(&driver)
  38. }
  39. func fetchAwsUserName(desc cloudprovider.SManagedVMCreateConfig) string {
  40. // 非公有云官方镜像
  41. if desc.ImageType != "system" {
  42. return "root"
  43. }
  44. // 公有云官方镜像
  45. dist := strings.ToLower(desc.OsDistribution)
  46. if strings.Contains(dist, "centos") {
  47. return "centos"
  48. } else if strings.Contains(dist, "ubuntu") {
  49. return "ubuntu"
  50. } else if strings.Contains(dist, "windows") {
  51. return "Administrator"
  52. } else if strings.Contains(dist, "debian") {
  53. return "admin"
  54. } else if strings.Contains(dist, "suse") {
  55. return "ec2-user"
  56. } else if strings.Contains(dist, "fedora") {
  57. return "ec2-user"
  58. } else if strings.Contains(dist, "rhel") || strings.Contains(dist, "redhat") {
  59. return "ec2-user"
  60. } else if strings.Contains(dist, "amazon linux") {
  61. return "ec2-user"
  62. } else {
  63. return "ec2-user"
  64. }
  65. }
  66. func (self *SAwsGuestDriver) IsNeedInjectPasswordByCloudInit() bool {
  67. return true
  68. }
  69. func (self *SAwsGuestDriver) IsWindowsUserDataTypeNeedEncode() bool {
  70. return true
  71. }
  72. func (self *SAwsGuestDriver) GetWindowsUserDataType() string {
  73. return cloudprovider.CLOUD_EC2
  74. }
  75. func (self *SAwsGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  76. return cloudprovider.SInstanceCapability{
  77. Hypervisor: self.GetHypervisor(),
  78. Provider: self.GetProvider(),
  79. DefaultAccount: cloudprovider.SDefaultAccount{
  80. Linux: cloudprovider.SOsDefaultAccount{
  81. DefaultAccount: api.VM_AWS_DEFAULT_LOGIN_USER,
  82. Changeable: false,
  83. },
  84. Windows: cloudprovider.SOsDefaultAccount{
  85. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  86. Changeable: false,
  87. },
  88. },
  89. Storages: cloudprovider.Storage{
  90. DataDisk: []cloudprovider.StorageInfo{
  91. cloudprovider.StorageInfo{StorageType: api.STORAGE_GP2_SSD, MaxSizeGb: 16384, MinSizeGb: 1, StepSizeGb: 1, Resizable: true},
  92. cloudprovider.StorageInfo{StorageType: api.STORAGE_GP3_SSD, MaxSizeGb: 16384, MinSizeGb: 1, StepSizeGb: 1, Resizable: true},
  93. cloudprovider.StorageInfo{StorageType: api.STORAGE_IO1_SSD, MaxSizeGb: 16384, MinSizeGb: 4, StepSizeGb: 1, Resizable: true},
  94. cloudprovider.StorageInfo{StorageType: api.STORAGE_ST1_HDD, MaxSizeGb: 16384, MinSizeGb: 500, StepSizeGb: 1, Resizable: true},
  95. cloudprovider.StorageInfo{StorageType: api.STORAGE_SC1_HDD, MaxSizeGb: 16384, MinSizeGb: 500, StepSizeGb: 1, Resizable: true},
  96. cloudprovider.StorageInfo{StorageType: api.STORAGE_STANDARD_HDD, MaxSizeGb: 1024, MinSizeGb: 1, StepSizeGb: 1, Resizable: true},
  97. },
  98. SysDisk: []cloudprovider.StorageInfo{
  99. cloudprovider.StorageInfo{StorageType: api.STORAGE_GP2_SSD, MaxSizeGb: 16384, MinSizeGb: 1, StepSizeGb: 1, Resizable: false},
  100. cloudprovider.StorageInfo{StorageType: api.STORAGE_IO1_SSD, MaxSizeGb: 16384, MinSizeGb: 4, StepSizeGb: 1, Resizable: false},
  101. cloudprovider.StorageInfo{StorageType: api.STORAGE_ST1_HDD, MaxSizeGb: 16384, MinSizeGb: 500, StepSizeGb: 1, Resizable: false},
  102. cloudprovider.StorageInfo{StorageType: api.STORAGE_SC1_HDD, MaxSizeGb: 16384, MinSizeGb: 500, StepSizeGb: 1, Resizable: false},
  103. cloudprovider.StorageInfo{StorageType: api.STORAGE_STANDARD_HDD, MaxSizeGb: 1024, MinSizeGb: 1, StepSizeGb: 1, Resizable: false},
  104. },
  105. },
  106. }
  107. }
  108. func (self *SAwsGuestDriver) GetDefaultAccount(osType, osDist, imageType string) string {
  109. if strings.ToLower(osType) == strings.ToLower(osprofile.OS_TYPE_WINDOWS) {
  110. return api.VM_DEFAULT_WINDOWS_LOGIN_USER
  111. }
  112. return api.VM_AWS_DEFAULT_LOGIN_USER
  113. }
  114. func (self *SAwsGuestDriver) GetHypervisor() string {
  115. return api.HYPERVISOR_AWS
  116. }
  117. func (self *SAwsGuestDriver) GetProvider() string {
  118. return api.CLOUD_PROVIDER_AWS
  119. }
  120. func (self *SAwsGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
  121. keys := models.SComputeResourceKeys{}
  122. keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
  123. keys.CloudEnv = api.CLOUD_ENV_PUBLIC_CLOUD
  124. keys.Provider = api.CLOUD_PROVIDER_AWS
  125. keys.Brand = api.CLOUD_PROVIDER_AWS
  126. keys.Hypervisor = api.HYPERVISOR_AWS
  127. return keys
  128. }
  129. func (self *SAwsGuestDriver) GetDefaultSysDiskBackend() string {
  130. return api.STORAGE_GP2_SSD
  131. }
  132. func (self *SAwsGuestDriver) GetMinimalSysDiskSizeGb() int {
  133. return 10
  134. }
  135. func (self *SAwsGuestDriver) GetStorageTypes() []string {
  136. return []string{
  137. api.STORAGE_GP2_SSD,
  138. api.STORAGE_GP3_SSD,
  139. api.STORAGE_IO1_SSD,
  140. api.STORAGE_IO2_SSD,
  141. api.STORAGE_ST1_HDD,
  142. api.STORAGE_SC1_HDD,
  143. api.STORAGE_STANDARD_HDD,
  144. }
  145. }
  146. func (self *SAwsGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) {
  147. return chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil
  148. }
  149. func (self *SAwsGuestDriver) GetDetachDiskStatus() ([]string, error) {
  150. return []string{api.VM_READY, api.VM_RUNNING}, nil
  151. }
  152. func (self *SAwsGuestDriver) GetAttachDiskStatus() ([]string, error) {
  153. return []string{api.VM_READY, api.VM_RUNNING}, nil
  154. }
  155. func (self *SAwsGuestDriver) GetRebuildRootStatus() ([]string, error) {
  156. return []string{api.VM_READY, api.VM_RUNNING}, nil
  157. }
  158. func (self *SAwsGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  159. return []string{api.VM_READY}, nil
  160. }
  161. func (self *SAwsGuestDriver) GetDeployStatus() ([]string, error) {
  162. return []string{api.VM_READY, api.VM_RUNNING}, nil
  163. }
  164. func (self *SAwsGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
  165. if len(input.Eip) > 0 || input.EipBw > 0 {
  166. // 未明确指定network时,由调度器进行调度,跳过support_eip检查
  167. if len(input.Networks) > 0 && len(input.Networks[0].Network) > 0 {
  168. inetwork, err := db.FetchByIdOrName(ctx, models.NetworkManager, userCred, input.Networks[0].Network)
  169. if err != nil {
  170. return nil, errors.Wrap(err, "SAwsGuestDriver.ValidateCreateData.Networks.FetchByIdOrName")
  171. }
  172. support_eip := inetwork.(*models.SNetwork).GetMetadataJson(ctx, "ext:support_eip", nil)
  173. if support_eip != nil {
  174. if ok, _ := support_eip.Bool(); !ok {
  175. return nil, httperrors.NewInputParameterError("network %s associated route table has no internet gateway attached.", inetwork.GetName())
  176. }
  177. }
  178. }
  179. }
  180. return self.SManagedVirtualizedGuestDriver.ValidateCreateData(ctx, userCred, input)
  181. }
  182. func (self *SAwsGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  183. // https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/stop-start.html
  184. if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY, api.VM_START_RESIZE_DISK, api.VM_RESIZE_DISK}) {
  185. return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
  186. }
  187. if disk.DiskType == api.DISK_TYPE_SYS && !utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_IO1_SSD, api.STORAGE_IO2_SSD, api.STORAGE_STANDARD_HDD, api.STORAGE_GP2_SSD, api.STORAGE_GP3_SSD}) {
  188. return fmt.Errorf("Cannot resize system disk with unsupported volumes type %s", storage.StorageType)
  189. }
  190. if !utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_GP2_SSD, api.STORAGE_GP3_SSD, api.STORAGE_IO1_SSD, api.STORAGE_IO2_SSD, api.STORAGE_ST1_HDD, api.STORAGE_SC1_HDD, api.STORAGE_STANDARD_HDD}) {
  191. return fmt.Errorf("Cannot resize %s disk", storage.StorageType)
  192. }
  193. return nil
  194. }
  195. func (self *SAwsGuestDriver) GetGuestInitialStateAfterCreate() string {
  196. return api.VM_RUNNING
  197. }
  198. func (self *SAwsGuestDriver) GetGuestInitialStateAfterRebuild() string {
  199. return api.VM_READY
  200. }
  201. func (self *SAwsGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
  202. return false
  203. }