Skip to content

Commit

Permalink
Merge pull request #10 from AntonioMeireles/grrr
Browse files Browse the repository at this point in the history
assorted bug fixing and cleanups
  • Loading branch information
AntonioMeireles committed Dec 2, 2015
2 parents 95c2fd2 + 50e4fa7 commit a117e5d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
27 changes: 14 additions & 13 deletions halt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 16 additions & 24 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/blang/semver"
"github.com/mitchellh/go-ps"
Expand Down Expand Up @@ -312,43 +311,36 @@ 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
}

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()
Expand Down
42 changes: 29 additions & 13 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit a117e5d

Please sign in to comment.