host.go 7.8 KB

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