Files
docker-machine/ssh/ssh.go
Nathan LeClaire 29999e9b10 Add a few improvements to stability
This improves on a few issues which are not frequent but do sometimes
come up:

1. Check that the daemon is up before attempting to do anything
   docker-ey in provisioning Ubuntu
2. Set a max retries for SSH so that if a request fails it attempts
   again before giving up

Also have annotated a few of the SSH options slightly better.

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
2015-04-06 13:00:09 -07:00

78 lines
1.7 KiB
Go

package ssh
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
log "github.com/Sirupsen/logrus"
)
func GetSSHCommand(host string, port int, user string, sshKey string, args ...string) *exec.Cmd {
defaultSSHArgs := []string{
"-o", "IdentitiesOnly=yes",
"-o", "StrictHostKeyChecking=no", // don't bother checking in ~/.ssh/known_hosts
"-o", "UserKnownHostsFile=/dev/null", // don't write anything to ~/.ssh/known_hosts
"-o", "ConnectionAttempts=30", // retry 30 times if SSH connection fails
"-o", "LogLevel=quiet", // suppress "Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts."
"-p", fmt.Sprintf("%d", port),
"-i", sshKey,
fmt.Sprintf("%s@%s", user, host),
}
sshArgs := append(defaultSSHArgs, args...)
cmd := exec.Command("ssh", sshArgs...)
cmd.Stderr = os.Stderr
if os.Getenv("DEBUG") != "" {
cmd.Stdout = os.Stdout
}
log.Debugf("executing: %v", strings.Join(cmd.Args, " "))
return cmd
}
func GenerateSSHKey(path string) error {
if _, err := exec.LookPath("ssh-keygen"); err != nil {
return fmt.Errorf("ssh-keygen not found in the path, please install ssh-keygen")
}
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return err
}
cmd := exec.Command("ssh-keygen", "-t", "rsa", "-N", "", "-f", path)
if os.Getenv("DEBUG") != "" {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
log.Debugf("executing: %v %v\n", cmd.Path, strings.Join(cmd.Args, " "))
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func WaitForTCP(addr string) error {
for {
conn, err := net.Dial("tcp", addr)
if err != nil {
continue
}
defer conn.Close()
if _, err = conn.Read(make([]byte, 1)); err != nil {
continue
}
break
}
return nil
}