host.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 aws
  15. import (
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SHost struct {
  24. multicloud.SHostBase
  25. zone *SZone
  26. }
  27. func (self *SHost) GetId() string {
  28. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  29. }
  30. func (self *SHost) GetName() string {
  31. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
  32. }
  33. func (self *SHost) GetGlobalId() string {
  34. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  35. }
  36. func (self *SHost) GetStatus() string {
  37. return api.HOST_STATUS_RUNNING
  38. }
  39. func (self *SHost) Refresh() error {
  40. return nil
  41. }
  42. func (self *SHost) IsEmulated() bool {
  43. return true
  44. }
  45. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  46. vms, err := self.zone.region.GetInstances(self.zone.ZoneName, "", nil)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "GetInstances")
  49. }
  50. ivms := make([]cloudprovider.ICloudVM, len(vms))
  51. for i := 0; i < len(vms); i += 1 {
  52. vms[i].host = self
  53. ivms[i] = &vms[i]
  54. }
  55. return ivms, nil
  56. }
  57. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  58. vms, err := self.zone.region.GetInstances(self.zone.ZoneName, "", []string{id})
  59. if err != nil {
  60. return nil, errors.Wrap(err, "GetInstances")
  61. }
  62. for i := range vms {
  63. if vms[i].InstanceId == id {
  64. vms[i].host = self
  65. return &vms[i], nil
  66. }
  67. }
  68. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  69. }
  70. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  71. return self.zone.GetIStorages()
  72. }
  73. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  74. return self.zone.GetIStorageById(id)
  75. }
  76. func (self *SHost) GetEnabled() bool {
  77. return true
  78. }
  79. func (self *SHost) GetHostStatus() string {
  80. return api.HOST_ONLINE
  81. }
  82. func (self *SHost) GetAccessIp() string {
  83. return ""
  84. }
  85. func (self *SHost) GetAccessMac() string {
  86. return ""
  87. }
  88. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  89. info := jsonutils.NewDict()
  90. info.Add(jsonutils.NewString(CLOUD_PROVIDER_AWS), "manufacture")
  91. return info
  92. }
  93. func (self *SHost) GetSN() string {
  94. return ""
  95. }
  96. func (self *SHost) GetCpuCount() int {
  97. return 0
  98. }
  99. func (self *SHost) GetNodeCount() int8 {
  100. return 0
  101. }
  102. func (self *SHost) GetCpuDesc() string {
  103. return ""
  104. }
  105. func (self *SHost) GetCpuMhz() int {
  106. return 0
  107. }
  108. func (self *SHost) GetMemSizeMB() int {
  109. return 0
  110. }
  111. func (self *SHost) GetStorageSizeMB() int64 {
  112. return 0
  113. }
  114. func (self *SHost) GetStorageType() string {
  115. return api.DISK_TYPE_HYBRID
  116. }
  117. func (self *SHost) GetHostType() string {
  118. return api.HOST_TYPE_AWS
  119. }
  120. func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
  121. inst, err := self.zone.region.GetInstance(instanceId)
  122. if err != nil {
  123. return nil, errors.Wrap(err, "GetInstance")
  124. }
  125. inst.host = self
  126. return inst, nil
  127. }
  128. func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  129. vm, err := self._createVM(desc.Name, desc.ExternalImageId, desc.SysDisk, desc.InstanceType,
  130. desc.ExternalNetworkId, desc.IpAddr, desc.Description, desc.Password, desc.DataDisks,
  131. desc.PublicKey, desc.ExternalSecgroupIds, desc.UserData, desc.Tags, desc.EnableMonitorAgent)
  132. if err != nil {
  133. return nil, errors.Wrap(err, "_createVM")
  134. }
  135. vm.host = self
  136. return vm, err
  137. }
  138. func (self *SHost) _createVM(name, imgId string, sysDisk cloudprovider.SDiskInfo, instanceType string,
  139. networkId, ipAddr, desc, passwd string,
  140. dataDisks []cloudprovider.SDiskInfo, publicKey string, secgroupIds []string, userData string,
  141. tags map[string]string, enableMonitorAgent bool,
  142. ) (*SInstance, error) {
  143. if len(instanceType) == 0 {
  144. return nil, fmt.Errorf("missing instance type params")
  145. }
  146. // 同步keypair
  147. var err error
  148. keypair := ""
  149. if len(publicKey) > 0 {
  150. keypair, err = self.zone.region.SyncKeypair(publicKey)
  151. if err != nil {
  152. return nil, err
  153. }
  154. }
  155. // 镜像及硬盘配置
  156. img, err := self.zone.region.GetImage(imgId)
  157. if err != nil {
  158. return nil, errors.Wrapf(err, "GetImage(%s)", imgId)
  159. }
  160. if img.Status != ImageStatusAvailable {
  161. return nil, fmt.Errorf("image not ready status: %s", img.Status)
  162. }
  163. if len(dataDisks) == 0 {
  164. dataDisks = []cloudprovider.SDiskInfo{}
  165. }
  166. if sysDisk.SizeGB < img.GetMinOsDiskSizeGb() {
  167. sysDisk.SizeGB = img.GetMinOsDiskSizeGb()
  168. }
  169. disks := append([]cloudprovider.SDiskInfo{sysDisk}, dataDisks...)
  170. instance, err := self.zone.region.CreateInstance(name, img, instanceType, networkId, secgroupIds, self.zone.ZoneName, desc, disks, ipAddr, keypair, userData, tags, enableMonitorAgent)
  171. if err != nil {
  172. return nil, errors.Wrapf(err, "CreateInstance")
  173. }
  174. return instance, nil
  175. }
  176. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  177. wires, err := host.zone.GetIWires()
  178. if err != nil {
  179. return nil, errors.Wrap(err, "GetIWires")
  180. }
  181. return cloudprovider.GetHostNetifs(host, wires), nil
  182. }
  183. func (self *SHost) GetIsMaintenance() bool {
  184. return false
  185. }
  186. func (self *SHost) GetVersion() string {
  187. return AWS_API_VERSION
  188. }