mirror of
https://github.com/rancher/rke.git
synced 2025-06-26 15:34:53 +00:00
122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package hosts
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"net"
|
|
|
|
"github.com/docker/docker/client"
|
|
"github.com/rancher/rke/docker"
|
|
"github.com/rancher/rke/log"
|
|
"github.com/sirupsen/logrus"
|
|
"golang.org/x/crypto/ssh"
|
|
"golang.org/x/crypto/ssh/agent"
|
|
)
|
|
|
|
const (
|
|
DockerAPIVersion = "1.24"
|
|
K8sVersion = "1.8"
|
|
)
|
|
|
|
func (h *Host) TunnelUp(ctx context.Context, dialerFactory DialerFactory) error {
|
|
if h.DClient != nil {
|
|
return nil
|
|
}
|
|
log.Infof(ctx, "[dialer] Setup tunnel for host [%s]", h.Address)
|
|
httpClient, err := h.newHTTPClient(dialerFactory)
|
|
if err != nil {
|
|
return fmt.Errorf("Can't establish dialer connection: %v", err)
|
|
}
|
|
// set Docker client
|
|
logrus.Debugf("Connecting to Docker API for host [%s]", h.Address)
|
|
h.DClient, err = client.NewClient("unix:///var/run/docker.sock", DockerAPIVersion, httpClient, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("Can't initiate NewClient: %v", err)
|
|
}
|
|
return checkDockerVersion(ctx, h)
|
|
}
|
|
|
|
func (h *Host) TunnelUpLocal(ctx context.Context) error {
|
|
var err error
|
|
if h.DClient != nil {
|
|
return nil
|
|
}
|
|
// set Docker client
|
|
logrus.Debugf("Connecting to Docker API for host [%s]", h.Address)
|
|
h.DClient, err = client.NewEnvClient()
|
|
if err != nil {
|
|
return fmt.Errorf("Can't initiate NewClient: %v", err)
|
|
}
|
|
return checkDockerVersion(ctx, h)
|
|
}
|
|
|
|
func checkDockerVersion(ctx context.Context, h *Host) error {
|
|
info, err := h.DClient.Info(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("Can't retrieve Docker Info: %v", err)
|
|
}
|
|
logrus.Debugf("Docker Info found: %#v", info)
|
|
h.DockerInfo = info
|
|
isvalid, err := docker.IsSupportedDockerVersion(info, K8sVersion)
|
|
if err != nil {
|
|
return fmt.Errorf("Error while determining supported Docker version [%s]: %v", info.ServerVersion, err)
|
|
}
|
|
|
|
if !isvalid && !h.IgnoreDockerVersion {
|
|
return fmt.Errorf("Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
|
|
} else if !isvalid {
|
|
log.Warnf(ctx, "Unsupported Docker version found [%s], supported versions are %v", info.ServerVersion, docker.K8sDockerVersions[K8sVersion])
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parsePrivateKey(keyBuff string) (ssh.Signer, error) {
|
|
return ssh.ParsePrivateKey([]byte(keyBuff))
|
|
}
|
|
|
|
func parsePrivateKeyWithPassPhrase(keyBuff string, passphrase []byte) (ssh.Signer, error) {
|
|
return ssh.ParsePrivateKeyWithPassphrase([]byte(keyBuff), passphrase)
|
|
}
|
|
|
|
func getSSHConfig(username, sshPrivateKeyString string, useAgentAuth bool) (*ssh.ClientConfig, error) {
|
|
config := &ssh.ClientConfig{
|
|
User: username,
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
}
|
|
|
|
// Kind of a double check now.
|
|
if useAgentAuth {
|
|
if sshAgentSock := os.Getenv("SSH_AUTH_SOCK"); sshAgentSock != "" {
|
|
sshAgent, err := net.Dial("unix", sshAgentSock)
|
|
if err != nil {
|
|
return config, fmt.Errorf("Cannot connect to SSH Auth socket %q: %s", sshAgentSock, err)
|
|
}
|
|
|
|
config.Auth = append(config.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
|
|
|
|
logrus.Debugf("using %q SSH_AUTH_SOCK", sshAgentSock)
|
|
return config, nil
|
|
}
|
|
}
|
|
|
|
signer, err := parsePrivateKey(sshPrivateKeyString)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
config.Auth = append(config.Auth, ssh.PublicKeys(signer))
|
|
|
|
return config, nil
|
|
}
|
|
|
|
func privateKeyPath(sshKeyPath string) string {
|
|
if sshKeyPath[:2] == "~/" {
|
|
sshKeyPath = filepath.Join(os.Getenv("HOME"), sshKeyPath[2:])
|
|
}
|
|
buff, _ := ioutil.ReadFile(sshKeyPath)
|
|
return string(buff)
|
|
}
|