Check for IP to be assigned before returning from Start()

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2015-06-11 12:47:26 -07:00
parent 6d26992101
commit e91c28fad6
2 changed files with 36 additions and 21 deletions

View File

@ -368,6 +368,20 @@ func (d *Driver) Create() error {
return nil
}
func (d *Driver) hostOnlyIpAvailable() bool {
ip, err := d.GetIP()
if err != nil {
log.Debug("ERROR getting IP: %s", err)
return false
}
if ip != "" {
log.Debugf("IP is %s", ip)
return true
}
log.Debug("Strangely, there was no error attempting to get the IP, but it was still empty.")
return false
}
func (d *Driver) Start() error {
s, err := d.GetState()
if err != nil {
@ -398,11 +412,18 @@ func (d *Driver) Start() error {
log.Infof("VM not in restartable state")
}
// Wait for SSH over NAT to be available before returning to user
if err := drivers.WaitForSSH(d); err != nil {
return err
}
// Bail if we don't get an IP from DHCP after a given number of seconds.
if err := utils.WaitForSpecific(d.hostOnlyIpAvailable, 5, 4*time.Second); err != nil {
return err
}
d.IPAddress, err = d.GetIP()
return err
}
@ -512,6 +533,7 @@ func (d *Driver) GetIP() (string, error) {
}
log.Debugf("SSH returned: %s\nEND SSH\n", output)
// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
lines := strings.Split(output, "\n")
for _, line := range lines {

View File

@ -172,8 +172,13 @@ func (h *Host) CreateSSHShell() error {
return client.Shell()
}
func (h *Host) Start() error {
if err := h.Driver.Start(); err != nil {
func (h *Host) runActionForState(action func() error, desiredState state.State) error {
if drivers.MachineInState(h.Driver, desiredState)() {
log.Debug("Machine already in state %s, returning", desiredState)
return nil
}
if err := action(); err != nil {
return err
}
@ -181,31 +186,19 @@ func (h *Host) Start() error {
return err
}
return utils.WaitFor(drivers.MachineInState(h.Driver, state.Running))
return utils.WaitFor(drivers.MachineInState(h.Driver, desiredState))
}
func (h *Host) Start() error {
return h.runActionForState(h.Driver.Start, state.Running)
}
func (h *Host) Stop() error {
if err := h.Driver.Stop(); err != nil {
return err
}
if err := h.SaveConfig(); err != nil {
return err
}
return utils.WaitFor(drivers.MachineInState(h.Driver, state.Stopped))
return h.runActionForState(h.Driver.Stop, state.Stopped)
}
func (h *Host) Kill() error {
if err := h.Driver.Stop(); err != nil {
return err
}
if err := h.SaveConfig(); err != nil {
return err
}
return utils.WaitFor(drivers.MachineInState(h.Driver, state.Stopped))
return h.runActionForState(h.Driver.Kill, state.Stopped)
}
func (h *Host) Restart() error {