From c58d5ab3aff1a1af092193747b94bb3ea261de77 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 12 Jun 2013 00:54:25 -0700 Subject: [PATCH] builder/virtualbox: time out on SSH handshake for retry --- builder/virtualbox/builder_test.go | 4 ++-- builder/virtualbox/step_wait_for_ssh.go | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/builder/virtualbox/builder_test.go b/builder/virtualbox/builder_test.go index 1682f0113..a0558ae35 100644 --- a/builder/virtualbox/builder_test.go +++ b/builder/virtualbox/builder_test.go @@ -9,8 +9,8 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ - "iso_md5": "foo", - "iso_url": "http://www.google.com/", + "iso_md5": "foo", + "iso_url": "http://www.google.com/", "ssh_username": "foo", } } diff --git a/builder/virtualbox/step_wait_for_ssh.go b/builder/virtualbox/step_wait_for_ssh.go index e9069da16..4ce7c97ff 100644 --- a/builder/virtualbox/step_wait_for_ssh.go +++ b/builder/virtualbox/step_wait_for_ssh.go @@ -115,10 +115,25 @@ func (s *stepWaitForSSH) waitForSSH(state map[string]interface{}) (packer.Commun }, } - comm, err = ssh.New(nc, sshConfig) - if err != nil { - log.Printf("SSH connection fail: %s", err) - nc.Close() + sshConnectSuccess := make(chan bool, 1) + go func() { + comm, err = ssh.New(nc, sshConfig) + if err != nil { + log.Printf("SSH connection fail: %s", err) + sshConnectSuccess <- false + return + } + + sshConnectSuccess <- true + }() + + select { + case success := <-sshConnectSuccess: + if !success { + continue + } + case <-time.After(5 * time.Second): + log.Printf("SSH handshake timeout. Trying again.") continue }