Skip to content

Commit

Permalink
Wait for machine to start/stop
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Dec 23, 2015
1 parent 8b31854 commit fa8fa69
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,36 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {
return d.VSwitch, nil
}

func (d *Driver) wait() error {
// waitForIP waits until the host has a valid IP
func (d *Driver) waitForIP() (string, error) {
log.Infof("Waiting for host to start...")

for {
ip, _ := d.GetIP()
if ip != "" {
break
return ip, nil
}

time.Sleep(1 * time.Second)
}
}

return nil
// waitStopped waits until the host is stopped
func (d *Driver) waitStopped() error {
log.Infof("Waiting for host to stop...")

for {
s, err := d.GetState()
if err != nil {
return err
}

if s != state.Running {
return nil
}

time.Sleep(1 * time.Second)
}
}

// Start starts an host
Expand All @@ -267,14 +284,14 @@ func (d *Driver) Start() error {
return err
}

if err := d.wait(); err != nil {
ip, err := d.waitForIP()
if err != nil {
return err
}

var err error
d.IPAddress, err = d.GetIP()
d.IPAddress = ip

return err
return nil
}

// Stop stops an host
Expand All @@ -283,17 +300,8 @@ func (d *Driver) Stop() error {
return err
}

for {
s, err := d.GetState()
if err != nil {
return err
}

if s != state.Running {
break
}

time.Sleep(1 * time.Second)
if err := d.waitStopped(); err != nil {
return err
}

d.IPAddress = ""
Expand Down Expand Up @@ -333,17 +341,8 @@ func (d *Driver) Kill() error {
return err
}

for {
s, err := d.GetState()
if err != nil {
return err
}

if s != state.Running {
break
}

time.Sleep(1 * time.Second)
if err := d.waitStopped(); err != nil {
return err
}

d.IPAddress = ""
Expand Down

0 comments on commit fa8fa69

Please sign in to comment.