Commit 19b6841e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/common: channels instead of unsafe read/write for communicate

parent dfbea5e6
...@@ -34,8 +34,7 @@ type StepConnectSSH struct { ...@@ -34,8 +34,7 @@ type StepConnectSSH struct {
// SSHWaitTimeout is the total timeout to wait for SSH to become available. // SSHWaitTimeout is the total timeout to wait for SSH to become available.
SSHWaitTimeout time.Duration SSHWaitTimeout time.Duration
cancel bool comm packer.Communicator
comm packer.Communicator
} }
func (s *StepConnectSSH) Run(state map[string]interface{}) multistep.StepAction { func (s *StepConnectSSH) Run(state map[string]interface{}) multistep.StepAction {
...@@ -44,10 +43,11 @@ func (s *StepConnectSSH) Run(state map[string]interface{}) multistep.StepAction ...@@ -44,10 +43,11 @@ func (s *StepConnectSSH) Run(state map[string]interface{}) multistep.StepAction
var comm packer.Communicator var comm packer.Communicator
var err error var err error
cancel := make(chan struct{})
waitDone := make(chan bool, 1) waitDone := make(chan bool, 1)
go func() { go func() {
ui.Say("Waiting for SSH to become available...") ui.Say("Waiting for SSH to become available...")
comm, err = s.waitForSSH(state) comm, err = s.waitForSSH(state, cancel)
waitDone <- true waitDone <- true
}() }()
...@@ -70,13 +70,13 @@ WaitLoop: ...@@ -70,13 +70,13 @@ WaitLoop:
break WaitLoop break WaitLoop
case <-timeout: case <-timeout:
ui.Error("Timeout waiting for SSH.") ui.Error("Timeout waiting for SSH.")
s.cancel = true close(cancel)
return multistep.ActionHalt return multistep.ActionHalt
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok { if _, ok := state[multistep.StateCancelled]; ok {
// The step sequence was cancelled, so cancel waiting for SSH // The step sequence was cancelled, so cancel waiting for SSH
// and just start the halting process. // and just start the halting process.
s.cancel = true close(cancel)
log.Println("Interrupt detected, quitting waiting for SSH.") log.Println("Interrupt detected, quitting waiting for SSH.")
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -89,16 +89,16 @@ WaitLoop: ...@@ -89,16 +89,16 @@ WaitLoop:
func (s *StepConnectSSH) Cleanup(map[string]interface{}) { func (s *StepConnectSSH) Cleanup(map[string]interface{}) {
} }
func (s *StepConnectSSH) waitForSSH(state map[string]interface{}) (packer.Communicator, error) { func (s *StepConnectSSH) waitForSSH(state map[string]interface{}, cancel <-chan struct{}) (packer.Communicator, error) {
handshakeAttempts := 0 handshakeAttempts := 0
var comm packer.Communicator var comm packer.Communicator
for { for {
time.Sleep(5 * time.Second) select {
case <-cancel:
if s.cancel {
log.Println("SSH wait cancelled. Exiting loop.") log.Println("SSH wait cancelled. Exiting loop.")
return nil, errors.New("SSH wait cancelled") return nil, errors.New("SSH wait cancelled")
case <-time.After(5 * time.Second):
} }
// First we request the TCP connection information // First we request the TCP connection information
...@@ -130,6 +130,7 @@ func (s *StepConnectSSH) waitForSSH(state map[string]interface{}) (packer.Commun ...@@ -130,6 +130,7 @@ func (s *StepConnectSSH) waitForSSH(state map[string]interface{}) (packer.Commun
SSHConfig: sshConfig, SSHConfig: sshConfig,
} }
log.Println("Attempting SSH connection...")
comm, err = ssh.New(config) comm, err = ssh.New(config)
if err != nil { if err != nil {
log.Printf("SSH handshake err: %s", err) log.Printf("SSH handshake err: %s", err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment