From fa8fa69e7cb43a3185a558681ed4098097d7de6e Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 23 Dec 2015 10:06:59 +0100 Subject: [PATCH] Wait for machine to start/stop Signed-off-by: David Gageot --- drivers/hyperv/hyperv.go | 57 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/drivers/hyperv/hyperv.go b/drivers/hyperv/hyperv.go index 19bf16a1b1..a19858aeab 100644 --- a/drivers/hyperv/hyperv.go +++ b/drivers/hyperv/hyperv.go @@ -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 @@ -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 @@ -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 = "" @@ -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 = ""