From 8b9eddf6f695723923b4a068d3f862dc69f1a4a0 Mon Sep 17 00:00:00 2001 From: thebsdbox Date: Wed, 10 Jan 2018 19:44:18 +0000 Subject: [PATCH] Added capability to find a host if `-hostname` isn't specified Will return the list of all hosts that are part of a vCenter DataCenter and choose one at random to use. Signed-off-by: Dan Finneran --- src/cmd/linuxkit/run_vcenter.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cmd/linuxkit/run_vcenter.go b/src/cmd/linuxkit/run_vcenter.go index b524bd41e..e19e0c7ca 100644 --- a/src/cmd/linuxkit/run_vcenter.go +++ b/src/cmd/linuxkit/run_vcenter.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "math/rand" "net/url" "os" "path" @@ -208,10 +209,21 @@ func vCenterConnect(ctx context.Context, newVM vmConfig) (*govmomi.Client, *obje } } - // Set the host that the VM will be created on - hs, err := f.HostSystemOrDefault(ctx, *newVM.vSphereHost) - if err != nil { - log.Fatalf("vSphere host [%s], could not be found", *newVM.vSphereHost) + // Check to see if a host has been specified + var hs *object.HostSystem + if *newVM.vSphereHost != "" { + // Find the selected host + hs, err = f.HostSystem(ctx, *newVM.vSphereHost) + if err != nil { + log.Fatalf("vSphere host [%s], could not be found", *newVM.vSphereHost) + } + } else { + allHosts, err := f.HostSystemList(ctx, "*/*") + if err != nil || len(allHosts) == 0 { + log.Fatalf("No vSphere hosts could be found on vCenter server") + } + // Select a host from the list off all available hosts at random + hs = allHosts[rand.Int()%len(allHosts)] } var rp *object.ResourcePool