host.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 aliyun
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/billing"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SHost struct {
  26. multicloud.SHostBase
  27. zone *SZone
  28. }
  29. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  30. return self.zone.GetIStorages()
  31. }
  32. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  33. return self.zone.GetIStorageById(id)
  34. }
  35. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  36. vms, err := self.zone.region.GetInstances(self.zone.ZoneId, nil)
  37. if err != nil {
  38. return nil, err
  39. }
  40. ret := make([]cloudprovider.ICloudVM, len(vms))
  41. for i := 0; i < len(vms); i += 1 {
  42. vms[i].host = self
  43. ret[i] = &vms[i]
  44. }
  45. return ret, nil
  46. }
  47. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  48. vms, err := self.zone.region.GetInstances(self.zone.ZoneId, []string{id})
  49. if err != nil {
  50. return nil, err
  51. }
  52. for i := range vms {
  53. if vms[i].InstanceId == id {
  54. vms[i].host = self
  55. return &vms[i], nil
  56. }
  57. }
  58. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  59. }
  60. func (self *SHost) GetId() string {
  61. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  62. }
  63. func (self *SHost) GetName() string {
  64. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
  65. }
  66. func (self *SHost) GetGlobalId() string {
  67. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  68. }
  69. func (self *SHost) IsEmulated() bool {
  70. return true
  71. }
  72. func (self *SHost) GetStatus() string {
  73. return api.HOST_STATUS_RUNNING
  74. }
  75. func (self *SHost) Refresh() error {
  76. return nil
  77. }
  78. func (self *SHost) GetHostStatus() string {
  79. return api.HOST_ONLINE
  80. }
  81. func (self *SHost) GetEnabled() bool {
  82. return true
  83. }
  84. func (self *SHost) GetAccessIp() string {
  85. return ""
  86. }
  87. func (self *SHost) GetAccessMac() string {
  88. return ""
  89. }
  90. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  91. info := jsonutils.NewDict()
  92. info.Add(jsonutils.NewString(CLOUD_PROVIDER_ALIYUN), "manufacture")
  93. return info
  94. }
  95. func (self *SHost) GetSN() string {
  96. return ""
  97. }
  98. func (self *SHost) GetCpuCount() int {
  99. return 0
  100. }
  101. func (self *SHost) GetNodeCount() int8 {
  102. return 0
  103. }
  104. func (self *SHost) GetCpuDesc() string {
  105. return ""
  106. }
  107. func (self *SHost) GetCpuMhz() int {
  108. return 0
  109. }
  110. func (self *SHost) GetMemSizeMB() int {
  111. return 0
  112. }
  113. func (self *SHost) GetStorageSizeMB() int64 {
  114. return 0
  115. }
  116. func (self *SHost) GetStorageType() string {
  117. return api.DISK_TYPE_HYBRID
  118. }
  119. func (self *SHost) GetHostType() string {
  120. return api.HOST_TYPE_ALIYUN
  121. }
  122. func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
  123. inst, err := self.zone.region.GetInstance(instanceId)
  124. if err != nil {
  125. return nil, err
  126. }
  127. inst.host = self
  128. return inst, nil
  129. }
  130. func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  131. vmId, err := self._createVM(desc.Name, desc.Hostname, desc.ExternalImageId, desc.SysDisk, desc.Cpu, desc.MemoryMB,
  132. desc.InstanceType, desc.ExternalNetworkId, desc.IpAddr, desc.Description, desc.Password,
  133. desc.DataDisks, desc.PublicKey, desc.ExternalSecgroupIds, desc.UserData, desc.BillingCycle,
  134. desc.ProjectId, desc.OsType, desc.Tags, desc.SPublicIpInfo)
  135. if err != nil {
  136. return nil, err
  137. }
  138. vm, err := self.GetInstanceById(vmId)
  139. if err != nil {
  140. return nil, errors.Wrapf(err, "GetInstanceById")
  141. }
  142. return vm, nil
  143. }
  144. func (self *SHost) _createVM(name, hostname string, imgId string,
  145. sysDisk cloudprovider.SDiskInfo, cpu int, memMB int, instanceType string,
  146. vswitchId string, ipAddr string, desc string, passwd string,
  147. dataDisks []cloudprovider.SDiskInfo, publicKey string, secgroupIds []string,
  148. userData string, bc *billing.SBillingCycle, projectId, osType string,
  149. tags map[string]string, publicIp cloudprovider.SPublicIpInfo,
  150. ) (string, error) {
  151. net := self.zone.getNetworkById(vswitchId)
  152. if net == nil {
  153. return "", fmt.Errorf("invalid switch ID %s", vswitchId)
  154. }
  155. if net.wire == nil {
  156. log.Errorf("vsiwtch's wire is empty")
  157. return "", fmt.Errorf("vsiwtch's wire is empty")
  158. }
  159. if net.wire.vpc == nil {
  160. log.Errorf("vsiwtch's wire' vpc is empty")
  161. return "", fmt.Errorf("vsiwtch's wire's vpc is empty")
  162. }
  163. var err error
  164. keypair := ""
  165. if len(publicKey) > 0 {
  166. keypair, err = self.zone.region.syncKeypair(publicKey)
  167. if err != nil {
  168. return "", err
  169. }
  170. }
  171. img, err := self.zone.region.GetImage(imgId)
  172. if err != nil {
  173. log.Errorf("GetImage fail %s", err)
  174. return "", err
  175. }
  176. if img.Status != ImageStatusAvailable {
  177. log.Errorf("image %s status %s", imgId, img.Status)
  178. return "", fmt.Errorf("image not ready")
  179. }
  180. disks := make([]SDisk, len(dataDisks)+1)
  181. disks[0].Size = img.Size
  182. if sysDisk.SizeGB > 0 && sysDisk.SizeGB > img.Size {
  183. disks[0].Size = sysDisk.SizeGB
  184. }
  185. storage, err := self.zone.getStorageByCategory(sysDisk.StorageType)
  186. if err != nil {
  187. return "", fmt.Errorf("Storage %s not avaiable: %s", sysDisk.StorageType, err)
  188. }
  189. disks[0].Category = storage.storageType
  190. for i, dataDisk := range dataDisks {
  191. disks[i+1].Size = dataDisk.SizeGB
  192. storage, err := self.zone.getStorageByCategory(dataDisk.StorageType)
  193. if err != nil {
  194. return "", fmt.Errorf("Storage %s not avaiable: %s", dataDisk.StorageType, err)
  195. }
  196. disks[i+1].Category = storage.storageType
  197. }
  198. if len(instanceType) > 0 {
  199. log.Debugf("Try instancetype : %s", instanceType)
  200. vmId, err := self.zone.region.CreateInstance(name, hostname, imgId, instanceType, secgroupIds, self.zone.ZoneId, desc, passwd, disks, vswitchId, ipAddr, keypair, userData, bc, projectId, osType, tags, publicIp)
  201. if err != nil {
  202. log.Errorf("Failed for %s: %s", instanceType, err)
  203. return "", fmt.Errorf("Failed to create specification %s.%s", instanceType, err.Error())
  204. }
  205. return vmId, nil
  206. }
  207. instanceTypes, err := self.zone.region.GetMatchInstanceTypes(cpu, memMB, 0, self.zone.ZoneId)
  208. if err != nil {
  209. return "", err
  210. }
  211. if len(instanceTypes) == 0 {
  212. return "", fmt.Errorf("instance type %dC%dMB not avaiable", cpu, memMB)
  213. }
  214. var vmId string
  215. for _, instType := range instanceTypes {
  216. instanceTypeId := instType.InstanceTypeId
  217. log.Debugf("Try instancetype : %s", instanceTypeId)
  218. vmId, err = self.zone.region.CreateInstance(name, hostname, imgId, instanceTypeId, secgroupIds, self.zone.ZoneId, desc, passwd, disks, vswitchId, ipAddr, keypair, userData, bc, projectId, osType, tags, publicIp)
  219. if err != nil {
  220. log.Errorf("Failed for %s: %s", instanceTypeId, err)
  221. } else {
  222. return vmId, nil
  223. }
  224. }
  225. return "", fmt.Errorf("Failed to create, %s", err.Error())
  226. }
  227. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  228. wires, err := host.zone.GetIWires()
  229. if err != nil {
  230. return nil, errors.Wrap(err, "GetIWires")
  231. }
  232. return cloudprovider.GetHostNetifs(host, wires), nil
  233. }
  234. func (host *SHost) GetIsMaintenance() bool {
  235. return false
  236. }
  237. func (host *SHost) GetVersion() string {
  238. return ALIYUN_API_VERSION
  239. }