@@ -84,21 +84,28 @@ func scpFile(filePath string, destFilePath string, user, hostname, sshKey string
84
84
return string (out ), err
85
85
}
86
86
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 )
89
92
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 ("?" )
100
109
}
101
- fmt .Printf ("?" )
102
- time .Sleep (3 * time .Second )
103
110
}
104
111
}
0 commit comments