Skip to content

Commit 15483df

Browse files
committed
Add timeout to integration SSH check
1 parent eb5ba7e commit 15483df

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

integration/provision.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@ func (p packetProvisioner) waitForPublicIP(nodeID string) (*packet.Node, error)
435435
func waitForSSH(provisionedNodes provisionedNodes, sshKey string) error {
436436
nodes := provisionedNodes.allNodes()
437437
for _, n := range nodes {
438-
BlockUntilSSHOpen(n.PublicIP, n.SSHUser, sshKey)
438+
if open := WaitUntilSSHOpen(n.PublicIP, n.SSHUser, sshKey, 5 * time.Minute); !open {
439+
return fmt.Errorf("Timed out waiting for SSH at %q", n.PublicIP)
440+
}
439441
}
440442
return nil
441443
}

integration/ssh.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,28 @@ func scpFile(filePath string, destFilePath string, user, hostname, sshKey string
8484
return string(out), err
8585
}
8686

87-
// BlockUntilSSHOpen waits until the node with the given IP is accessible via SSH.
88-
func BlockUntilSSHOpen(publicIP, sshUser, sshKey string) {
87+
// WaitUntilSSHOpen waits up to the given timeout for a successful SSH connection to
88+
// the given node. If the connection is open, returns true. If the timeout is reached, returns false.
89+
func WaitUntilSSHOpen(publicIP, sshUser, sshKey string, timeout time.Duration) bool {
90+
tout := time.After(timeout)
91+
tick := time.Tick(3 * time.Second)
8992
for {
90-
cmd := exec.Command("ssh")
91-
cmd.Args = append(cmd.Args, "-i", sshKey)
92-
cmd.Args = append(cmd.Args, "-o", "ConnectTimeout=5")
93-
cmd.Args = append(cmd.Args, "-o", "BatchMode=yes")
94-
cmd.Args = append(cmd.Args, "-o", "StrictHostKeyChecking=no")
95-
cmd.Args = append(cmd.Args, fmt.Sprintf("%s@%s", sshUser, publicIP), "exit") // just call exit if we are able to connect
96-
if err := cmd.Run(); err == nil {
97-
// command succeeded
98-
fmt.Println()
99-
return
93+
select {
94+
case <-tout:
95+
return false
96+
case <-tick:
97+
cmd := exec.Command("ssh")
98+
cmd.Args = append(cmd.Args, "-i", sshKey)
99+
cmd.Args = append(cmd.Args, "-o", "ConnectTimeout=5")
100+
cmd.Args = append(cmd.Args, "-o", "BatchMode=yes")
101+
cmd.Args = append(cmd.Args, "-o", "StrictHostKeyChecking=no")
102+
cmd.Args = append(cmd.Args, fmt.Sprintf("%s@%s", sshUser, publicIP), "exit") // just call exit if we are able to connect
103+
if err := cmd.Run(); err == nil {
104+
// command succeeded
105+
fmt.Println()
106+
return true
107+
}
108+
fmt.Printf("?")
100109
}
101-
fmt.Printf("?")
102-
time.Sleep(3 * time.Second)
103110
}
104111
}

0 commit comments

Comments
 (0)