Skip to content

Commit

Permalink
feat: Introduce retries in ssh connection in the ssh client
Browse files Browse the repository at this point in the history
Signed-off-by: aerosouund <aerosound161@gmail.com>
  • Loading branch information
aerosouund committed Aug 17, 2024
1 parent 0b0b7b1 commit 1ff08d2
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cluster-provision/gocli/pkg/libssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/bramvdbogaerde/go-scp"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -219,9 +220,25 @@ func (s *SSHClientImpl) executeCommand(cmd string, outWriter, errWriter io.Write
}

func (s *SSHClientImpl) initClient() error {
client, err := ssh.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(s.sshPort)), s.config)
if err != nil {
return fmt.Errorf("Failed to connect to SSH server: %v", err)
var (
client *ssh.Client
established bool
err error
)

for i := 0; i < 5; i++ {
client, err = ssh.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(s.sshPort)), s.config)
if err != nil {
logrus.Infof("Attempt %d. Error establishing connection: %s, sleeping 6 seconds", i+1, err.Error())
time.Sleep(time.Second * 6)
} else {
established = true
break
}
}

if !established {
return fmt.Errorf("Error establishing connection after 10 attempts")
}

conn, err := client.Dial("tcp", fmt.Sprintf("192.168.66.10%d:22", s.nodeIdx))
Expand Down

0 comments on commit 1ff08d2

Please sign in to comment.