Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

Allow retrying the initial SSH connection #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

coledave
Copy link
Contributor

Although the server is provisioned OpenSSH isn't always ready in time to receive the initial dial(), this causes the build to fail with a "Connection refused on host x.x.x.x:22" error.

I only know the basics of Go but this works for me, happy to amend as requested if it means getting it merged.

@bradrydzewski
Copy link
Contributor

bradrydzewski commented Feb 16, 2020

thanks for sending a pull request. Instead of putting all the retry logic inside the Run function inline, it would be better to wrap dial with a helper function like this:

-	client, err := dial(
+	client, err := retryDial(
		spec.ip,
		spec.Server.User,	
		e.privatekey,
	)

the dial with retry function could also accept a context so that if / when a user cancels a build the runner stops waiting for ssh to initialize.

// helper function configures and dials the ssh server and retries if
// there is an error connecting.
func retryDial(ctx context.Context, server, username, privatekey string) (*ssh.Client, error) {
	var client *ssh.Client
	var err error
	for i := 0;; i++ {
		client, err := dial(server, username, privatekey)
		if err == nil || i == 5 {
			break
		}
		select {
		case <- ctx.Done():
			return nil, ctx.Err()
		case <- time.After(time.Second * 10):
		}
	}
	return client, err
}

@CLAassistant
Copy link

CLA assistant check
All committers have signed the CLA.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants