2021-07-16 08:32:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-07-18 05:34:22 +00:00
|
|
|
"errors"
|
2021-07-16 08:32:20 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2021-07-18 03:21:26 +00:00
|
|
|
dCliCommand "github.com/docker/cli/cli/command"
|
|
|
|
dCliConfig "github.com/docker/cli/cli/config"
|
|
|
|
dContext "github.com/docker/cli/cli/context"
|
|
|
|
dCliContextStore "github.com/docker/cli/cli/context/store"
|
|
|
|
dClient "github.com/docker/docker/client"
|
|
|
|
"github.com/moby/term"
|
2021-07-21 08:04:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-07-16 08:32:20 +00:00
|
|
|
)
|
|
|
|
|
2021-07-18 03:21:26 +00:00
|
|
|
func NewClientWithContext(contextURL string) *dClient.Client {
|
2021-07-16 08:32:20 +00:00
|
|
|
helper := newConnectionHelper(contextURL)
|
|
|
|
httpClient := &http.Client{
|
|
|
|
// No tls
|
|
|
|
// No proxy
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: helper.Dialer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-18 03:21:26 +00:00
|
|
|
var clientOpts []dClient.Opt
|
2021-07-16 08:32:20 +00:00
|
|
|
|
|
|
|
clientOpts = append(clientOpts,
|
2021-07-18 03:21:26 +00:00
|
|
|
dClient.WithHTTPClient(httpClient),
|
|
|
|
dClient.WithHost(helper.Host),
|
|
|
|
dClient.WithDialContext(helper.Dialer),
|
2021-07-16 08:32:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FIXME: Maybe don't have this variable here and load it beforehand
|
|
|
|
version := os.Getenv("DOCKER_API_VERSION")
|
|
|
|
|
|
|
|
if version != "" {
|
2021-07-18 03:21:26 +00:00
|
|
|
clientOpts = append(clientOpts, dClient.WithVersion(version))
|
2021-07-16 08:32:20 +00:00
|
|
|
} else {
|
2021-07-18 03:21:26 +00:00
|
|
|
clientOpts = append(clientOpts, dClient.WithAPIVersionNegotiation())
|
2021-07-16 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 03:21:26 +00:00
|
|
|
cl, err := dClient.NewClientWithOpts(clientOpts...)
|
2021-07-16 08:32:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-07-21 08:04:34 +00:00
|
|
|
logrus.Fatalf("unable to create Docker client: %s", err)
|
2021-07-16 08:32:20 +00:00
|
|
|
}
|
|
|
|
return cl
|
|
|
|
}
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-18 05:34:22 +00:00
|
|
|
func GetContextEndpoint(ctx dCliContextStore.Metadata) (string, error) {
|
2021-07-18 03:21:26 +00:00
|
|
|
// safe to use docker key hardcoded since abra doesn't use k8s... yet...
|
2021-07-18 05:34:22 +00:00
|
|
|
endpointmeta, ok := ctx.Endpoints["docker"].(dContext.EndpointMetaBase)
|
|
|
|
if !ok {
|
|
|
|
err := errors.New("context lacks Docker endpoint")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return endpointmeta.Host, nil
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultDockerContextStore() *dCliCommand.ContextStoreWithDefault {
|
|
|
|
// Grabbing the stderr from Docker commands
|
|
|
|
// Much easier to fit this into the code we are using to replicate docker cli commands
|
|
|
|
_, _, stderr := term.StdStreams()
|
|
|
|
// TODO: Look into custom docker configs in case users want that
|
|
|
|
dockerConfig := dCliConfig.LoadDefaultConfigFile(stderr)
|
|
|
|
contextDir := dCliConfig.ContextStoreDir()
|
|
|
|
storeConfig := dCliCommand.DefaultContextStoreConfig()
|
|
|
|
store := newContextStore(contextDir, storeConfig)
|
|
|
|
|
|
|
|
dockerContextStore := &dCliCommand.ContextStoreWithDefault{
|
|
|
|
Store: store,
|
|
|
|
Resolver: func() (*dCliCommand.DefaultContext, error) {
|
|
|
|
// nil for the Opts because it works without it and its a cli thing
|
|
|
|
return dCliCommand.ResolveDefaultContext(nil, dockerConfig, storeConfig, stderr)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return dockerContextStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContextStore(dir string, config dCliContextStore.Config) dCliContextStore.Store {
|
|
|
|
return dCliContextStore.New(dir, config)
|
|
|
|
}
|