Skip to content

Commit

Permalink
Support for SSH connection
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Sep 13, 2021
1 parent a7487c9 commit aa529c0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 35 deletions.
43 changes: 42 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cmd

import (
"net/http"
"net/url"
"os"

dockerClient "github.com/docker/docker/client"
"github.com/heroku/color"
"github.com/matejvasek/sshdialer"
"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -133,7 +139,42 @@ func initConfig() (config.Config, string, error) {
}

func initClient(logger logging.Logger, cfg config.Config) (pack.Client, error) {
client, err := pack.NewClient(pack.WithLogger(logger), pack.WithExperimental(cfg.Experimental), pack.WithRegistryMirrors(cfg.RegistryMirrors))
dockerClientOpts := []dockerClient.Opt{
dockerClient.WithVersion("1.38"),
}

usesSSH := false
if dockerHost, ok := os.LookupEnv("DOCKER_HOST"); ok {
if _url, err := url.Parse(dockerHost); err == nil && _url.Scheme == "ssh" {
usesSSH = true
dialer, err := sshdialer.CreateDialContext(_url, "", "")
if err != nil {
return pack.Client{}, err
}

httpClient := &http.Client{
// No tls
// No proxy
Transport: &http.Transport{
DialContext: dialer,
},
}
dockerClientOpts = append(dockerClientOpts,
dockerClient.WithHTTPClient(httpClient),
dockerClient.WithHost("http://example.com/"),
dockerClient.WithDialContext(dialer),
)
}
}
if !usesSSH {
dockerClientOpts = append(dockerClientOpts, dockerClient.FromEnv)
}

dc, err := dockerClient.NewClientWithOpts(dockerClientOpts...)
if err != nil {
return pack.Client{}, err
}
client, err := pack.NewClient(pack.WithLogger(logger), pack.WithExperimental(cfg.Experimental), pack.WithRegistryMirrors(cfg.RegistryMirrors), pack.WithDockerClient(dc))
if err != nil {
return pack.Client{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ require (
github.com/google/go-containerregistry v0.6.0
github.com/google/go-github/v30 v30.1.0
github.com/heroku/color v0.0.6
github.com/matejvasek/sshdialer v0.0.0-20210913130443-7a6a7b860dfe
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf // indirect
github.com/onsi/gomega v1.16.0
github.com/opencontainers/image-spec v1.0.1
github.com/pelletier/go-toml v1.9.3
Expand Down
Loading

0 comments on commit aa529c0

Please sign in to comment.