forked from toolshed/abra
refactor!: drop all SSH opts / config handling
See coop-cloud/organising#601 See coop-cloud/organising#482
This commit is contained in:
parent
43990b6fae
commit
aa3910f8df
@ -53,7 +53,7 @@ func cleanUp(domainName string) {
|
||||
// Docker manages SSH connection details. These are stored to disk in
|
||||
// ~/.docker. Abra can manage this completely for the user, so it's an
|
||||
// implementation detail.
|
||||
func newContext(c *cli.Context, domainName, username, port string) error {
|
||||
func newContext(c *cli.Context, domainName string) error {
|
||||
store := contextPkg.NewDefaultDockerContextStore()
|
||||
contexts, err := store.Store.List()
|
||||
if err != nil {
|
||||
@ -67,9 +67,9 @@ func newContext(c *cli.Context, domainName, username, port string) error {
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("creating context with domain %s, username %s and port %s", domainName, username, port)
|
||||
logrus.Debugf("creating context with domain %s", domainName)
|
||||
|
||||
if err := client.CreateContext(domainName, username, port); err != nil {
|
||||
if err := client.CreateContext(domainName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -158,12 +158,7 @@ developer machine.
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
hostConfig, err := sshPkg.GetHostConfig(domainName)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := newContext(c, domainName, hostConfig.User, hostConfig.Port); err != nil {
|
||||
if err := newContext(c, domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -14,19 +14,16 @@ import (
|
||||
|
||||
type Context = contextStore.Metadata
|
||||
|
||||
func CreateContext(contextName string, user string, port string) error {
|
||||
host := contextName
|
||||
if user != "" {
|
||||
host = fmt.Sprintf("%s@%s", user, host)
|
||||
}
|
||||
if port != "" {
|
||||
host = fmt.Sprintf("%s:%s", host, port)
|
||||
}
|
||||
host = fmt.Sprintf("ssh://%s", host)
|
||||
// CreateContext creates a new Docker context.
|
||||
func CreateContext(contextName string) error {
|
||||
host := fmt.Sprintf("ssh://%s", contextName)
|
||||
|
||||
if err := createContext(contextName, host); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Debugf("created the %s context", contextName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -2,73 +2,14 @@ package ssh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// HostConfig is a SSH host config.
|
||||
type HostConfig struct {
|
||||
Host string
|
||||
IdentityFile string
|
||||
Port string
|
||||
User string
|
||||
}
|
||||
|
||||
// String presents a human friendly output for the HostConfig.
|
||||
func (h HostConfig) String() string {
|
||||
return fmt.Sprintf(
|
||||
"{host: %s, username: %s, port: %s, identityfile: %s}",
|
||||
h.Host,
|
||||
h.User,
|
||||
h.Port,
|
||||
h.IdentityFile,
|
||||
)
|
||||
}
|
||||
|
||||
// GetHostConfig retrieves a ~/.ssh/config config for a host using /usr/bin/ssh
|
||||
// directly. We therefore maintain consistent interop with this standard
|
||||
// tooling. This is useful because SSH confuses a lot of people and having to
|
||||
// learn how two tools (`ssh` and `abra`) handle SSH connection details instead
|
||||
// of one (just `ssh`) is Not Cool. Here's to less bug reports on this topic!
|
||||
func GetHostConfig(hostname string) (HostConfig, error) {
|
||||
var hostConfig HostConfig
|
||||
|
||||
out, err := exec.Command("ssh", "-G", hostname).Output()
|
||||
if err != nil {
|
||||
return hostConfig, err
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
entries := strings.Split(line, " ")
|
||||
for idx, entry := range entries {
|
||||
if entry == "hostname" {
|
||||
hostConfig.Host = entries[idx+1]
|
||||
}
|
||||
if entry == "user" {
|
||||
hostConfig.User = entries[idx+1]
|
||||
}
|
||||
if entry == "port" {
|
||||
hostConfig.Port = entries[idx+1]
|
||||
}
|
||||
if entry == "identityfile" {
|
||||
if hostConfig.IdentityFile == "" {
|
||||
hostConfig.IdentityFile = entries[idx+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("retrieved ssh config for %s: %s", hostname, hostConfig.String())
|
||||
|
||||
return hostConfig, nil
|
||||
}
|
||||
|
||||
// Fatal is a error output wrapper which aims to make SSH failures easier to
|
||||
// parse through re-wording.
|
||||
func Fatal(hostname string, err error) error {
|
||||
out := err.Error()
|
||||
|
||||
if strings.Contains(out, "Host key verification failed.") {
|
||||
return fmt.Errorf("SSH host key verification failed for %s", hostname)
|
||||
} else if strings.Contains(out, "Could not resolve hostname") {
|
||||
@ -79,7 +20,7 @@ func Fatal(hostname string, err error) error {
|
||||
return fmt.Errorf("ssh auth: permission denied for %s", hostname)
|
||||
} else if strings.Contains(out, "Network is unreachable") {
|
||||
return fmt.Errorf("unable to connect to %s, network is unreachable?", hostname)
|
||||
} else {
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ import (
|
||||
// GetConnectionHelper returns Docker-specific connection helper for the given URL.
|
||||
// GetConnectionHelper returns nil without error when no helper is registered for the scheme.
|
||||
//
|
||||
// ssh://<user>@<host> URL requires Docker 18.09 or later on the remote host.
|
||||
// ssh://<host> URL requires Docker 18.09 or later on the remote host.
|
||||
func GetConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
|
||||
return getConnectionHelper(daemonURL, []string{"-o ConnectTimeout=60"})
|
||||
return getConnectionHelper(daemonURL)
|
||||
}
|
||||
|
||||
func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.ConnectionHelper, error) {
|
||||
func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
|
||||
url, err := url.Parse(daemonURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -35,7 +35,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
|
||||
|
||||
return &connhelper.ConnectionHelper{
|
||||
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return New(ctx, "ssh", append(sshFlags, ctxConnDetails.Args("docker", "system", "dial-stdio")...)...)
|
||||
return New(ctx, "ssh", ctxConnDetails.Args("docker", "system", "dial-stdio")...)
|
||||
},
|
||||
Host: "http://docker.example.com",
|
||||
}, nil
|
||||
@ -45,6 +45,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// NewConnectionHelper creates new connection helper for a remote docker daemon.
|
||||
func NewConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
|
||||
helper, err := GetConnectionHelper(daemonURL)
|
||||
if err != nil {
|
||||
@ -73,6 +74,7 @@ func getDockerEndpoint(host string) (docker.Endpoint, error) {
|
||||
return ep, nil
|
||||
}
|
||||
|
||||
// GetDockerEndpointMetadataAndTLS retrieves the docker endpoint and TLS info for a remote host.
|
||||
func GetDockerEndpointMetadataAndTLS(host string) (docker.EndpointMeta, *dCliContextStore.EndpointTLSData, error) {
|
||||
ep, err := getDockerEndpoint(host)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user