host.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 huawei
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  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) GetId() string {
  30. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  31. }
  32. func (self *SHost) GetName() string {
  33. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
  34. }
  35. func (self *SHost) GetGlobalId() string {
  36. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  37. }
  38. func (self *SHost) GetStatus() string {
  39. return api.HOST_STATUS_RUNNING
  40. }
  41. func (self *SHost) Refresh() error {
  42. return nil
  43. }
  44. func (self *SHost) IsEmulated() bool {
  45. return true
  46. }
  47. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  48. vms, err := self.zone.region.GetInstances("")
  49. if err != nil {
  50. return nil, err
  51. }
  52. ret := []cloudprovider.ICloudVM{}
  53. for i := range vms {
  54. if vms[i].OSEXTAZAvailabilityZone == self.zone.GetId() {
  55. vms[i].host = self
  56. ret = append(ret, &vms[i])
  57. }
  58. }
  59. return ret, nil
  60. }
  61. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  62. vm, err := self.zone.region.GetInstance(id)
  63. if err != nil {
  64. return nil, err
  65. }
  66. vm.host = self
  67. return vm, err
  68. }
  69. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  70. return self.zone.GetIStorages()
  71. }
  72. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  73. return self.zone.GetIStorageById(id)
  74. }
  75. func (self *SHost) GetEnabled() bool {
  76. return true
  77. }
  78. func (self *SHost) GetHostStatus() string {
  79. return api.HOST_ONLINE
  80. }
  81. func (self *SHost) GetAccessIp() string {
  82. return ""
  83. }
  84. func (self *SHost) GetAccessMac() string {
  85. return ""
  86. }
  87. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  88. info := jsonutils.NewDict()
  89. info.Add(jsonutils.NewString(CLOUD_PROVIDER_HUAWEI), "manufacture")
  90. return info
  91. }
  92. func (self *SHost) GetSN() string {
  93. return ""
  94. }
  95. func (self *SHost) GetCpuCount() int {
  96. return 0
  97. }
  98. func (self *SHost) GetNodeCount() int8 {
  99. return 0
  100. }
  101. func (self *SHost) GetCpuDesc() string {
  102. return ""
  103. }
  104. func (self *SHost) GetCpuMhz() int {
  105. return 0
  106. }
  107. func (self *SHost) GetMemSizeMB() int {
  108. return 0
  109. }
  110. func (self *SHost) GetStorageSizeMB() int64 {
  111. return 0
  112. }
  113. func (self *SHost) GetStorageType() string {
  114. return api.DISK_TYPE_HYBRID
  115. }
  116. func (self *SHost) GetHostType() string {
  117. return api.HOST_TYPE_HUAWEI
  118. }
  119. func (self *SHost) GetIsMaintenance() bool {
  120. return false
  121. }
  122. func (self *SHost) GetVersion() string {
  123. return HUAWEI_API_VERSION
  124. }
  125. func (self *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  126. vmId, err := self._createVM(opts)
  127. if err != nil {
  128. return nil, err
  129. }
  130. cloudprovider.Wait(time.Second*5, time.Minute, func() (bool, error) {
  131. _, err := self.zone.region.GetInstance(vmId)
  132. if err == nil {
  133. return true, nil
  134. }
  135. if errors.Cause(err) == cloudprovider.ErrNotFound {
  136. return false, nil
  137. }
  138. return false, err
  139. })
  140. vm, err := self.zone.region.GetInstance(vmId)
  141. if err != nil {
  142. return nil, err
  143. }
  144. vm.host = self
  145. return vm, nil
  146. }
  147. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  148. wires, err := host.zone.GetIWires()
  149. if err != nil {
  150. return nil, errors.Wrap(err, "GetIWires")
  151. }
  152. return cloudprovider.GetHostNetifs(host, wires), nil
  153. }
  154. func (self *SHost) _createVM(opts *cloudprovider.SManagedVMCreateConfig) (string, error) {
  155. // 同步keypair
  156. keypair := ""
  157. var err error
  158. if len(opts.PublicKey) > 0 {
  159. keypair, err = self.zone.region.syncKeypair(opts.PublicKey)
  160. if err != nil {
  161. return "", err
  162. }
  163. }
  164. // 镜像及硬盘配置
  165. img, err := self.zone.region.GetImage(opts.ExternalImageId)
  166. if err != nil {
  167. return "", errors.Wrapf(err, "GetImage")
  168. }
  169. if img.SizeGB > opts.SysDisk.SizeGB {
  170. opts.SysDisk.SizeGB = img.SizeGB
  171. }
  172. // 创建实例
  173. if len(opts.InstanceType) > 0 {
  174. log.Debugf("Try instancetype : %s", opts.InstanceType)
  175. vmId, err := self.zone.region.CreateInstance(keypair, self.zone.ZoneName, opts)
  176. if err != nil {
  177. return "", errors.Wrapf(err, "CreateInstance")
  178. }
  179. return vmId, nil
  180. }
  181. // 匹配实例类型
  182. instanceTypes, err := self.zone.region.GetMatchInstanceTypes(opts.Cpu, opts.MemoryMB, self.zone.GetId())
  183. if err != nil {
  184. return "", err
  185. }
  186. if len(instanceTypes) == 0 {
  187. return "", fmt.Errorf("instance type %dC%dMB not avaiable", opts.Cpu, opts.MemoryMB)
  188. }
  189. var vmId string
  190. for _, instType := range instanceTypes {
  191. opts.InstanceType = instType.Name
  192. log.Debugf("Try instancetype : %s", opts.InstanceType)
  193. vmId, err = self.zone.region.CreateInstance(keypair, self.zone.ZoneName, opts)
  194. if err != nil {
  195. log.Errorf("Failed for %s: %s", opts.InstanceType, err)
  196. }
  197. return vmId, nil
  198. }
  199. return "", errors.Wrapf(err, "CreateInstance")
  200. }