diff --git a/halt.go b/halt.go index 1e70358..7845344 100644 --- a/halt.go +++ b/halt.go @@ -87,22 +87,23 @@ func (vm VMInfo) halt() (err error) { } } } - // wait until it's _really_ dead - defer func() { - leftover := filepath.Join(SessionContext.runDir, vm.UUID) - - for range time.Tick(500 * time.Millisecond) { + // wait until it's _really_ dead, but not forever + for { + select { + case <-time.After(3 * time.Second): + return fmt.Errorf("VM didn't shutdown normally after 3s (!)... ") + case <-time.Tick(100 * time.Millisecond): if _, ee := os.FindProcess(vm.Pid); ee == nil { - break + if e := + os.RemoveAll(filepath.Join(SessionContext.runDir, + vm.UUID)); e != nil { + log.Println(e.Error()) + } + log.Printf("successfully halted '%s'\n", vm.Name) + return } } - if e := os.RemoveAll(leftover); e != nil { - log.Println(e.Error()) - } - log.Printf("successfully halted '%s'\n", vm.Name) - }() - - return + } } func init() { diff --git a/helpers.go b/helpers.go index 763ce86..4bc7de6 100644 --- a/helpers.go +++ b/helpers.go @@ -37,7 +37,6 @@ import ( "strconv" "strings" "sync" - "time" "github.com/blang/semver" "github.com/mitchellh/go-ps" @@ -312,23 +311,23 @@ func normalizeVersion(version string) string { } func (vm *VMInfo) isActive() bool { - clean := func() { - staled := filepath.Join(SessionContext.runDir, vm.UUID) - if SessionContext.debug { - log.Println("removing staled", staled) - } - - if e := os.RemoveAll(staled); e != nil { - log.Println(e) - } - } + // clean := func() { + // staled := filepath.Join(SessionContext.runDir, vm.UUID) + // if SessionContext.debug { + // log.Println("removing staled", staled) + // } + // + // if e := os.RemoveAll(staled); e != nil { + // log.Println(e) + // } + // } if vm.Pid < 1 { - clean() + // clean() return false } if p, _ := ps.FindProcess(vm.Pid); p == nil || !strings.HasPrefix(p.Executable(), "corectl") { - clean() + // clean() return false } return true @@ -336,19 +335,12 @@ func (vm *VMInfo) isActive() bool { func (vm *VMInfo) metadataService() (endpoint string, err error) { var ( - free net.Listener - runOnce sync.Once - freePort = func() (net.Listener, error) { - defer recover() - return net.Listen("tcp", "localhost:0") - } + free net.Listener + runOnce sync.Once ) - // workaround OSX and/or golang weirdness... - for range time.Tick(500 * time.Millisecond) { - if free, err = freePort(); err == nil { - break - } + if free, err = net.Listen("tcp", "127.0.0.1:0"); err != nil { + return } mux := http.NewServeMux() diff --git a/run.go b/run.go index 252c9e1..8fcf00a 100644 --- a/run.go +++ b/run.go @@ -172,23 +172,39 @@ func bootVM(vipre *viper.Viper) (err error) { go func() { wg.Add(1) - defer func() { - if r := recover(); r == nil { - wg.Done() - } - }() + defer wg.Done() + if err != nil { return } - for range time.Tick(500 * time.Millisecond) { - vm.Pid = c.Process.Pid - break - } - for range time.Tick(500 * time.Millisecond) { - var e error - if vm.PublicIP, e = - uuid2ip.GuestIPfromMAC(vm.MacAddress); e == nil { + for { + select { + case <-time.After(2 * time.Second): + log.Println("Unable to grab VM's pid after 2s (!)... " + + "Aborting") + return + case <-time.Tick(100 * time.Millisecond): + vm.Pid = c.Process.Pid + } + if vm.Pid > 0 { + break + } + } + for { + select { + case <-time.After(5 * time.Second): + log.Println("Unable to grab VM's IP after 5s (!)... " + + "Aborting") + return + case <-time.Tick(250 * time.Millisecond): + var e error + if vm.PublicIP, e = + uuid2ip.GuestIPfromMAC(vm.MacAddress); e == nil { + break + } + } + if vm.PublicIP != "" { break } }