host.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 hcso
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. "yunion.io/x/pkg/util/osprofile"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type SHost struct {
  28. multicloud.SHostBase
  29. zone *SZone
  30. vms []SInstance
  31. // 华为私有云没有直接列出host的接口,所有账号下的host都是通过VM反向解析出来的
  32. // 当账号下没有虚拟机时,如果没有host,会导致调度找不到可用的HOST。
  33. // 因此,为了避免上述情况始终会在每个zone下返回一台虚拟的host
  34. IsFake bool
  35. projectId string
  36. Id string
  37. Name string
  38. }
  39. func (self *SHost) GetId() string {
  40. return self.Id
  41. }
  42. func (self *SHost) GetName() string {
  43. if len(self.Name) > 0 {
  44. return self.Name
  45. }
  46. return self.Id
  47. }
  48. func (self *SHost) GetGlobalId() string {
  49. return self.Id
  50. }
  51. func (self *SHost) GetStatus() string {
  52. return api.HOST_STATUS_RUNNING
  53. }
  54. func (self *SHost) Refresh() error {
  55. _, err := self.getVMs()
  56. return errors.Wrap(err, "getVMs")
  57. }
  58. func (self *SHost) IsEmulated() bool {
  59. return self.IsFake
  60. }
  61. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  62. var vms []SInstance
  63. var err error
  64. if self.vms != nil {
  65. vms = self.vms
  66. } else {
  67. vms, err = self.getVMs()
  68. if err != nil {
  69. return nil, err
  70. }
  71. }
  72. ret := make([]cloudprovider.ICloudVM, len(vms))
  73. for i := range vms {
  74. vm := vms[i]
  75. vm.host = self
  76. ret[i] = &vm
  77. }
  78. return ret, nil
  79. }
  80. func (self *SHost) getVMs() ([]SInstance, error) {
  81. vms, err := self.zone.region.GetInstances()
  82. if err != nil {
  83. return nil, errors.Wrap(err, "GetInstances")
  84. }
  85. ret := []SInstance{}
  86. for i := range vms {
  87. vm := vms[i]
  88. if vm.OSEXTAZAvailabilityZone == self.GetId() && vm.HostID == self.GetId() {
  89. vm.host = self
  90. ret = append(ret, vm)
  91. }
  92. }
  93. self.vms = ret
  94. return ret, nil
  95. }
  96. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  97. vm, err := self.zone.region.GetInstanceByID(id)
  98. if vm.HostID != self.GetId() {
  99. return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetInstanceByID")
  100. }
  101. vm.host = self
  102. return &vm, err
  103. }
  104. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  105. return self.zone.GetIStorages()
  106. }
  107. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  108. return self.zone.GetIStorageById(id)
  109. }
  110. func (self *SHost) GetEnabled() bool {
  111. return true
  112. }
  113. func (self *SHost) GetHostStatus() string {
  114. return api.HOST_ONLINE
  115. }
  116. func (self *SHost) GetAccessIp() string {
  117. return ""
  118. }
  119. func (self *SHost) GetAccessMac() string {
  120. return ""
  121. }
  122. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  123. info := jsonutils.NewDict()
  124. info.Add(jsonutils.NewString(CLOUD_PROVIDER_HUAWEI), "manufacture")
  125. info.Add(jsonutils.NewString(self.GetId()), "id")
  126. info.Add(jsonutils.NewString(self.GetName()), "name")
  127. return info
  128. }
  129. func (self *SHost) GetSN() string {
  130. return ""
  131. }
  132. func (self *SHost) GetCpuCount() int {
  133. return 0
  134. }
  135. func (self *SHost) GetNodeCount() int8 {
  136. return 0
  137. }
  138. func (self *SHost) GetCpuDesc() string {
  139. return ""
  140. }
  141. func (self *SHost) GetCpuMhz() int {
  142. return 0
  143. }
  144. func (self *SHost) GetMemSizeMB() int {
  145. return 0
  146. }
  147. func (self *SHost) GetStorageSizeMB() int64 {
  148. return 0
  149. }
  150. func (self *SHost) GetStorageType() string {
  151. return api.DISK_TYPE_HYBRID
  152. }
  153. func (self *SHost) GetHostType() string {
  154. return api.HOST_TYPE_HCSO
  155. }
  156. func (self *SHost) GetIsMaintenance() bool {
  157. return false
  158. }
  159. func (self *SHost) GetVersion() string {
  160. return HUAWEI_API_VERSION
  161. }
  162. func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
  163. instance, err := self.zone.region.GetInstanceByID(instanceId)
  164. if err != nil {
  165. return nil, err
  166. }
  167. if instance.HostID != self.GetId() {
  168. return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetInstanceByID")
  169. }
  170. instance.host = self
  171. return &instance, nil
  172. }
  173. func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  174. vmId, err := self._createVM(
  175. desc.Name, desc.ExternalImageId, desc.SysDisk,
  176. desc.Cpu, desc.MemoryMB, desc.InstanceType,
  177. desc.ExternalNetworkId, desc.IpAddr,
  178. desc.Description, desc.Account,
  179. desc.Password, desc.DataDisks,
  180. desc.PublicKey, desc.ExternalSecgroupIds,
  181. desc.UserData, desc.BillingCycle, desc.ProjectId, desc.Tags)
  182. if err != nil {
  183. return nil, err
  184. }
  185. // VM实际调度到的host, 可能不是当前host.因此需要改写host信息
  186. vm, err := self.zone.region.GetIVMById(vmId)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return vm, err
  191. }
  192. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  193. wires, err := host.zone.GetIWires()
  194. if err != nil {
  195. return nil, errors.Wrap(err, "GetIWires")
  196. }
  197. return cloudprovider.GetHostNetifs(host, wires), nil
  198. }
  199. func (self *SHost) _createVM(name string, imgId string, sysDisk cloudprovider.SDiskInfo, cpu int, memMB int, instanceType string,
  200. networkId string, ipAddr string, desc string, account string, passwd string,
  201. diskSizes []cloudprovider.SDiskInfo, publicKey string, secgroupIds []string,
  202. userData string, bc *billing.SBillingCycle, projectId string, tags map[string]string) (string, error) {
  203. net := self.zone.getNetworkById(networkId)
  204. if net == nil {
  205. return "", fmt.Errorf("invalid network ID %s", networkId)
  206. }
  207. if net.wire == nil {
  208. log.Errorf("network's wire is empty")
  209. return "", fmt.Errorf("network's wire is empty")
  210. }
  211. if net.wire.vpc == nil {
  212. log.Errorf("wire's vpc is empty")
  213. return "", fmt.Errorf("wire's vpc is empty")
  214. }
  215. // 同步keypair
  216. var err error
  217. keypair := ""
  218. if len(publicKey) > 0 {
  219. keypair, err = self.zone.region.syncKeypair(publicKey)
  220. if err != nil {
  221. return "", err
  222. }
  223. }
  224. // 镜像及硬盘配置
  225. img, err := self.zone.region.GetImage(imgId)
  226. if err != nil {
  227. log.Errorf("getiamge %s fail %s", imgId, err)
  228. return "", err
  229. }
  230. if img.Status != ImageStatusActive {
  231. log.Errorf("image %s status %s", imgId, img.Status)
  232. return "", fmt.Errorf("image not ready")
  233. }
  234. // passwd, windows机型直接使用密码比较方便
  235. if strings.ToLower(img.Platform) == strings.ToLower(osprofile.OS_TYPE_WINDOWS) && len(passwd) > 0 {
  236. keypair = ""
  237. }
  238. if strings.ToLower(img.Platform) == strings.ToLower(osprofile.OS_TYPE_WINDOWS) {
  239. if u, err := updateWindowsUserData(userData, img.OSVersion, account, passwd); err == nil {
  240. userData = u
  241. } else {
  242. return "", errors.Wrap(err, "SHost.CreateVM.updateWindowsUserData")
  243. }
  244. }
  245. disks := make([]SDisk, len(diskSizes)+1)
  246. disks[0].SizeGB = img.SizeGB
  247. if sysDisk.SizeGB > 0 && sysDisk.SizeGB > img.SizeGB {
  248. disks[0].SizeGB = sysDisk.SizeGB
  249. }
  250. disks[0].VolumeType = sysDisk.StorageType
  251. for i, dataDisk := range diskSizes {
  252. disks[i+1].SizeGB = dataDisk.SizeGB
  253. disks[i+1].VolumeType = dataDisk.StorageType
  254. }
  255. // 创建实例
  256. if len(instanceType) > 0 {
  257. log.Debugf("Try instancetype : %s", instanceType)
  258. vmId, err := self.zone.region.CreateInstance(name, imgId, instanceType, networkId, secgroupIds, net.VpcID, self.zone.GetId(), desc, disks, ipAddr, keypair, publicKey, passwd, userData, bc, projectId, tags)
  259. if err != nil {
  260. log.Errorf("Failed for %s: %s", instanceType, err)
  261. return "", fmt.Errorf("create %s failed:%s", instanceType, ErrMessage(err))
  262. } else {
  263. return vmId, nil
  264. }
  265. }
  266. // 匹配实例类型
  267. instanceTypes, err := self.zone.region.GetMatchInstanceTypes(cpu, memMB, self.zone.GetId())
  268. if err != nil {
  269. return "", err
  270. }
  271. if len(instanceTypes) == 0 {
  272. return "", fmt.Errorf("instance type %dC%dMB not avaiable", cpu, memMB)
  273. }
  274. var vmId string
  275. for _, instType := range instanceTypes {
  276. instanceTypeId := instType.Name
  277. log.Debugf("Try instancetype : %s", instanceTypeId)
  278. vmId, err = self.zone.region.CreateInstance(name, imgId, instanceTypeId, networkId, secgroupIds, net.VpcID, self.zone.GetId(), desc, disks, ipAddr, keypair, publicKey, passwd, userData, bc, projectId, tags)
  279. if err != nil {
  280. log.Errorf("Failed for %s: %s", instanceTypeId, err)
  281. } else {
  282. return vmId, nil
  283. }
  284. }
  285. return "", fmt.Errorf("create failed: %s", ErrMessage(err))
  286. }