host.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 cloudpods
  15. import (
  16. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/gotypes"
  22. "yunion.io/x/pkg/tristate"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  25. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. )
  27. type SHost struct {
  28. multicloud.SHostBase
  29. zone *SZone
  30. api.HostDetails
  31. }
  32. func (host *SHost) GetGlobalId() string {
  33. return host.Id
  34. }
  35. func (host *SHost) GetId() string {
  36. return host.Id
  37. }
  38. func (host *SHost) GetName() string {
  39. return host.Name
  40. }
  41. func (host *SHost) GetStatus() string {
  42. return host.Status
  43. }
  44. func (host *SHost) GetAccessIp() string {
  45. return host.AccessIp
  46. }
  47. func (host *SHost) GetOvnVersion() string {
  48. return host.OvnVersion
  49. }
  50. func (host *SHost) Refresh() error {
  51. h, err := host.zone.region.GetHost(host.Id)
  52. if err != nil {
  53. return err
  54. }
  55. return jsonutils.Update(host, h)
  56. }
  57. func (host *SHost) getIWires() ([]cloudprovider.ICloudWire, error) {
  58. wires, err := host.zone.region.GetWires("", host.Id)
  59. if err != nil {
  60. return nil, err
  61. }
  62. ret := []cloudprovider.ICloudWire{}
  63. for i := range wires {
  64. vpc, _ := host.zone.region.GetVpc(wires[i].VpcId)
  65. wires[i].vpc = vpc
  66. ret = append(ret, &wires[i])
  67. }
  68. return ret, nil
  69. }
  70. func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  71. storages, err := host.zone.region.GetStorages("", host.Id)
  72. if err != nil {
  73. return nil, err
  74. }
  75. ret := []cloudprovider.ICloudStorage{}
  76. for i := range storages {
  77. storages[i].region = host.zone.region
  78. ret = append(ret, &storages[i])
  79. }
  80. return ret, nil
  81. }
  82. func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  83. storage, err := host.zone.region.GetStorage(id)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return storage, nil
  88. }
  89. func (host *SHost) GetEnabled() bool {
  90. return host.Enabled != nil && *host.Enabled
  91. }
  92. func (host *SHost) GetHostStatus() string {
  93. return host.HostStatus
  94. }
  95. func (host *SHost) GetAccessMac() string {
  96. return host.AccessMac
  97. }
  98. func (host *SHost) GetSysInfo() jsonutils.JSONObject {
  99. return jsonutils.Marshal(host.SysInfo)
  100. }
  101. func (host *SHost) GetSN() string {
  102. return host.SN
  103. }
  104. func (host *SHost) GetCpuCount() int {
  105. return host.CpuCount
  106. }
  107. func (host *SHost) GetNodeCount() int8 {
  108. return int8(host.NodeCount)
  109. }
  110. func (host *SHost) GetCpuDesc() string {
  111. return host.CpuDesc
  112. }
  113. func (host *SHost) GetCpuMhz() int {
  114. return host.CpuMhz
  115. }
  116. func (host *SHost) GetCpuCmtbound() float32 {
  117. return host.CpuCmtbound
  118. }
  119. func (host *SHost) GetMemSizeMB() int {
  120. return host.MemSize
  121. }
  122. func (host *SHost) GetMemCmtbound() float32 {
  123. return host.MemCmtbound
  124. }
  125. func (host *SHost) GetReservedMemoryMb() int {
  126. return host.MemReserved
  127. }
  128. func (host *SHost) GetStorageSizeMB() int64 {
  129. return host.StorageSize
  130. }
  131. func (host *SHost) GetStorageDriver() string {
  132. return host.StorageDriver
  133. }
  134. func (host *SHost) GetStorageInfo() jsonutils.JSONObject {
  135. if !gotypes.IsNil(host.Spec) {
  136. ret, _ := host.Spec.Get("storage_info")
  137. return ret
  138. }
  139. return host.StorageInfo
  140. }
  141. func (host *SHost) GetStorageType() string {
  142. return host.StorageType
  143. }
  144. func (host *SHost) GetHostType() string {
  145. return host.HostType
  146. }
  147. func (host *SHost) GetIsMaintenance() bool {
  148. return host.IsMaintenance
  149. }
  150. func (host *SHost) GetVersion() string {
  151. return host.Version
  152. }
  153. func (host *SHost) GetIpmiInfo() jsonutils.JSONObject {
  154. return host.zone.region.GetIpmiInfo(host.Id)
  155. }
  156. type SHostNic struct {
  157. host *SHost
  158. nic *types.SNic
  159. }
  160. func (hn *SHostNic) GetDriver() string {
  161. return ""
  162. }
  163. func (hn *SHostNic) GetDevice() string {
  164. return hn.nic.Interface
  165. }
  166. func (hn *SHostNic) GetMac() string {
  167. return hn.nic.Mac
  168. }
  169. func (hn *SHostNic) GetIndex() int8 {
  170. return 0
  171. }
  172. func (hn *SHostNic) IsLinkUp() tristate.TriState {
  173. if hn.nic.LinkUp {
  174. return tristate.True
  175. }
  176. return tristate.False
  177. }
  178. func (hn *SHostNic) GetMtu() int32 {
  179. return int32(hn.nic.Mtu)
  180. }
  181. func (hn *SHostNic) GetNicType() string {
  182. return string(hn.nic.Type)
  183. }
  184. func (hn *SHostNic) GetVlanId() int {
  185. return hn.nic.VlanId
  186. }
  187. func (hn *SHostNic) GetBridge() string {
  188. return hn.nic.Bridge
  189. }
  190. func (hn *SHostNic) GetIpAddr() string {
  191. return hn.nic.IpAddr
  192. }
  193. func (hn *SHostNic) GetIWire() cloudprovider.ICloudWire {
  194. wires, err := hn.host.getIWires()
  195. if err != nil {
  196. log.Errorf("SHostNic.GetIWire fail %s", err)
  197. return nil
  198. }
  199. for i := range wires {
  200. if wires[i].GetId() == hn.nic.WireId {
  201. return wires[i]
  202. }
  203. }
  204. return nil
  205. }
  206. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  207. nics := []SHostNic{}
  208. for i := range host.NicInfo {
  209. nics = append(nics, SHostNic{
  210. host: host,
  211. nic: host.NicInfo[i],
  212. })
  213. }
  214. ret := []cloudprovider.ICloudHostNetInterface{}
  215. for i := range nics {
  216. ret = append(ret, &nics[i])
  217. }
  218. return ret, nil
  219. }
  220. func (host *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  221. hypervisor := api.HYPERVISOR_KVM
  222. if host.HostType == api.HOST_TYPE_ESXI {
  223. hypervisor = api.HYPERVISOR_ESXI
  224. }
  225. ins, err := host.zone.region.CreateInstance(host.Id, hypervisor, opts)
  226. if err != nil {
  227. return nil, err
  228. }
  229. ins.host = host
  230. return ins, nil
  231. }
  232. func (host *SHost) Start() error {
  233. _, err := host.zone.region.perform(&modules.Hosts, host.Id, "start", nil)
  234. return err
  235. }
  236. func (host *SHost) Stop() error {
  237. _, err := host.zone.region.perform(&modules.Hosts, host.Id, "stop", nil)
  238. return err
  239. }
  240. func (host *SHost) CreateBaremetalServer(opts *api.ServerCreateInput) (cloudprovider.ICloudVM, error) {
  241. vm := &SInstance{host: host}
  242. opts.PreferHost = host.Id
  243. err := host.zone.region.create(&modules.Servers, opts, vm)
  244. if err != nil {
  245. return nil, err
  246. }
  247. return vm, nil
  248. }
  249. func (region *SRegion) GetHost(id string) (*SHost, error) {
  250. host := &SHost{}
  251. err := region.cli.get(&modules.Hosts, id, nil, host)
  252. if err != nil {
  253. return nil, errors.Wrap(err, "get")
  254. }
  255. return host, nil
  256. }
  257. func (region *SRegion) GetIpmiInfo(hostId string) jsonutils.JSONObject {
  258. resp, _ := modules.Hosts.GetSpecific(region.cli.s, hostId, "ipmi", nil)
  259. return resp
  260. }
  261. func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  262. host, err := zone.region.GetHost(id)
  263. if err != nil {
  264. return nil, err
  265. }
  266. host.zone = zone
  267. return host, nil
  268. }
  269. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  270. hosts, err := zone.region.GetHosts(zone.Id)
  271. if err != nil {
  272. return nil, errors.Wrapf(err, "GetHosts")
  273. }
  274. ret := []cloudprovider.ICloudHost{}
  275. for i := range hosts {
  276. hosts[i].zone = zone
  277. ret = append(ret, &hosts[i])
  278. }
  279. return ret, nil
  280. }
  281. func (region *SRegion) GetHosts(zoneId string) ([]SHost, error) {
  282. params := map[string]interface{}{}
  283. if len(zoneId) > 0 {
  284. params["zone_id"] = zoneId
  285. }
  286. params["filter"] = "host_type.in('hypervisor', 'baremetal', 'container')"
  287. ret := []SHost{}
  288. err := region.list(&modules.Hosts, params, &ret)
  289. if err != nil {
  290. return nil, errors.Wrap(err, "list")
  291. }
  292. return ret, nil
  293. }
  294. func (host *SHost) GetIsolateDevices() ([]cloudprovider.IsolateDevice, error) {
  295. devs, err := host.zone.region.GetIsolatedDevices(host.Id, "")
  296. if err != nil {
  297. return nil, err
  298. }
  299. ret := []cloudprovider.IsolateDevice{}
  300. for i := range devs {
  301. ret = append(ret, &devs[i])
  302. }
  303. return ret, nil
  304. }