host_system.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "os"
  16. "time"
  17. "github.com/google/uuid"
  18. "github.com/vmware/govmomi/simulator/esx"
  19. "github.com/vmware/govmomi/vim25/methods"
  20. "github.com/vmware/govmomi/vim25/mo"
  21. "github.com/vmware/govmomi/vim25/soap"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. var (
  25. hostPortUnique = os.Getenv("VCSIM_HOST_PORT_UNIQUE") == "true"
  26. )
  27. type HostSystem struct {
  28. mo.HostSystem
  29. }
  30. func NewHostSystem(host mo.HostSystem) *HostSystem {
  31. if hostPortUnique { // configure unique port for each host
  32. port := &esx.HostSystem.Summary.Config.Port
  33. *port++
  34. host.Summary.Config.Port = *port
  35. }
  36. now := time.Now()
  37. hs := &HostSystem{
  38. HostSystem: host,
  39. }
  40. hs.Name = hs.Summary.Config.Name
  41. hs.Summary.Runtime = &hs.Runtime
  42. hs.Summary.Runtime.BootTime = &now
  43. id := uuid.New().String()
  44. hardware := *host.Summary.Hardware
  45. hs.Summary.Hardware = &hardware
  46. hs.Summary.Hardware.Uuid = id
  47. info := *esx.HostHardwareInfo
  48. info.SystemInfo.Uuid = id
  49. hs.Hardware = &info
  50. config := []struct {
  51. ref **types.ManagedObjectReference
  52. obj mo.Reference
  53. }{
  54. {&hs.ConfigManager.DatastoreSystem, &HostDatastoreSystem{Host: &hs.HostSystem}},
  55. {&hs.ConfigManager.NetworkSystem, NewHostNetworkSystem(&hs.HostSystem)},
  56. {&hs.ConfigManager.AdvancedOption, NewOptionManager(nil, esx.Setting)},
  57. {&hs.ConfigManager.FirewallSystem, NewHostFirewallSystem(&hs.HostSystem)},
  58. }
  59. for _, c := range config {
  60. ref := Map.Put(c.obj).Reference()
  61. *c.ref = &ref
  62. }
  63. return hs
  64. }
  65. func (h *HostSystem) event() types.HostEvent {
  66. return types.HostEvent{
  67. Event: types.Event{
  68. Datacenter: datacenterEventArgument(h),
  69. ComputeResource: h.eventArgumentParent(),
  70. Host: h.eventArgument(),
  71. },
  72. }
  73. }
  74. func (h *HostSystem) eventArgument() *types.HostEventArgument {
  75. return &types.HostEventArgument{
  76. Host: h.Self,
  77. EntityEventArgument: types.EntityEventArgument{Name: h.Name},
  78. }
  79. }
  80. func (h *HostSystem) eventArgumentParent() *types.ComputeResourceEventArgument {
  81. parent := hostParent(&h.HostSystem)
  82. return &types.ComputeResourceEventArgument{
  83. ComputeResource: parent.Self,
  84. EntityEventArgument: types.EntityEventArgument{Name: parent.Name},
  85. }
  86. }
  87. func hostParent(host *mo.HostSystem) *mo.ComputeResource {
  88. switch parent := Map.Get(*host.Parent).(type) {
  89. case *mo.ComputeResource:
  90. return parent
  91. case *ClusterComputeResource:
  92. return &parent.ComputeResource
  93. default:
  94. return nil
  95. }
  96. }
  97. func addComputeResource(s *types.ComputeResourceSummary, h *HostSystem) {
  98. s.TotalCpu += h.Summary.Hardware.CpuMhz
  99. s.TotalMemory += h.Summary.Hardware.MemorySize
  100. s.NumCpuCores += h.Summary.Hardware.NumCpuCores
  101. s.NumCpuThreads += h.Summary.Hardware.NumCpuThreads
  102. s.EffectiveCpu += h.Summary.Hardware.CpuMhz
  103. s.EffectiveMemory += h.Summary.Hardware.MemorySize
  104. s.NumHosts++
  105. s.NumEffectiveHosts++
  106. s.OverallStatus = types.ManagedEntityStatusGreen
  107. }
  108. // CreateDefaultESX creates a standalone ESX
  109. // Adds objects of type: Datacenter, Network, ComputeResource, ResourcePool and HostSystem
  110. func CreateDefaultESX(f *Folder) {
  111. dc := NewDatacenter(f)
  112. host := NewHostSystem(esx.HostSystem)
  113. summary := new(types.ComputeResourceSummary)
  114. addComputeResource(summary, host)
  115. cr := &mo.ComputeResource{Summary: summary}
  116. cr.EnvironmentBrowser = newEnvironmentBrowser()
  117. cr.Self = *host.Parent
  118. cr.Name = host.Name
  119. cr.Host = append(cr.Host, host.Reference())
  120. Map.PutEntity(cr, host)
  121. pool := NewResourcePool()
  122. cr.ResourcePool = &pool.Self
  123. Map.PutEntity(cr, pool)
  124. pool.Owner = cr.Self
  125. Map.Get(dc.HostFolder).(*Folder).putChild(cr)
  126. }
  127. // CreateStandaloneHost uses esx.HostSystem as a template, applying the given spec
  128. // and creating the ComputeResource parent and ResourcePool sibling.
  129. func CreateStandaloneHost(f *Folder, spec types.HostConnectSpec) (*HostSystem, types.BaseMethodFault) {
  130. if spec.HostName == "" {
  131. return nil, &types.NoHost{}
  132. }
  133. pool := NewResourcePool()
  134. host := NewHostSystem(esx.HostSystem)
  135. host.Summary.Config.Name = spec.HostName
  136. host.Name = host.Summary.Config.Name
  137. host.Runtime.ConnectionState = types.HostSystemConnectionStateDisconnected
  138. summary := new(types.ComputeResourceSummary)
  139. addComputeResource(summary, host)
  140. cr := &mo.ComputeResource{
  141. ConfigurationEx: &types.ComputeResourceConfigInfo{
  142. VmSwapPlacement: string(types.VirtualMachineConfigInfoSwapPlacementTypeVmDirectory),
  143. },
  144. Summary: summary,
  145. EnvironmentBrowser: newEnvironmentBrowser(),
  146. }
  147. Map.PutEntity(cr, Map.NewEntity(host))
  148. host.Summary.Host = &host.Self
  149. Map.PutEntity(cr, Map.NewEntity(pool))
  150. cr.Name = host.Name
  151. cr.Host = append(cr.Host, host.Reference())
  152. cr.ResourcePool = &pool.Self
  153. f.putChild(cr)
  154. pool.Owner = cr.Self
  155. return host, nil
  156. }
  157. func (h *HostSystem) DestroyTask(ctx *Context, req *types.Destroy_Task) soap.HasFault {
  158. task := CreateTask(h, "destroy", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  159. if len(h.Vm) > 0 {
  160. return nil, &types.ResourceInUse{}
  161. }
  162. ctx.postEvent(&types.HostRemovedEvent{HostEvent: h.event()})
  163. f := Map.getEntityParent(h, "Folder").(*Folder)
  164. f.removeChild(h.Reference())
  165. return nil, nil
  166. })
  167. return &methods.Destroy_TaskBody{
  168. Res: &types.Destroy_TaskResponse{
  169. Returnval: task.Run(),
  170. },
  171. }
  172. }
  173. func (h *HostSystem) EnterMaintenanceModeTask(spec *types.EnterMaintenanceMode_Task) soap.HasFault {
  174. task := CreateTask(h, "enterMaintenanceMode", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  175. h.Runtime.InMaintenanceMode = true
  176. return nil, nil
  177. })
  178. return &methods.EnterMaintenanceMode_TaskBody{
  179. Res: &types.EnterMaintenanceMode_TaskResponse{
  180. Returnval: task.Run(),
  181. },
  182. }
  183. }
  184. func (h *HostSystem) ExitMaintenanceModeTask(spec *types.ExitMaintenanceMode_Task) soap.HasFault {
  185. task := CreateTask(h, "exitMaintenanceMode", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  186. h.Runtime.InMaintenanceMode = false
  187. return nil, nil
  188. })
  189. return &methods.ExitMaintenanceMode_TaskBody{
  190. Res: &types.ExitMaintenanceMode_TaskResponse{
  191. Returnval: task.Run(),
  192. },
  193. }
  194. }