forked from toolshed/abra
feat: very basic context management
Taken from this gist by github.com/agbaraka https://gist.github.com/agbaraka/654a218f8ea13b3da8a47d47595f5d05 There is no in-built way of dealing with contexts using the golang sdk. This means we have to make our own Dial helper borrowing from Docker CLI This means all Docker API calls are made within the ssh connection This uses `docker system dial-stdio`
This commit is contained in:
45
client/client.go
Normal file
45
client/client.go
Normal file
@ -0,0 +1,45 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
func NewClientWithContext(contextURL string) *client.Client {
|
||||
helper := newConnectionHelper(contextURL)
|
||||
httpClient := &http.Client{
|
||||
// No tls
|
||||
// No proxy
|
||||
Transport: &http.Transport{
|
||||
DialContext: helper.Dialer,
|
||||
},
|
||||
}
|
||||
|
||||
var clientOpts []client.Opt
|
||||
|
||||
clientOpts = append(clientOpts,
|
||||
client.WithHTTPClient(httpClient),
|
||||
client.WithHost(helper.Host),
|
||||
client.WithDialContext(helper.Dialer),
|
||||
)
|
||||
|
||||
// FIXME: Maybe don't have this variable here and load it beforehand
|
||||
version := os.Getenv("DOCKER_API_VERSION")
|
||||
|
||||
if version != "" {
|
||||
clientOpts = append(clientOpts, client.WithVersion(version))
|
||||
} else {
|
||||
clientOpts = append(clientOpts, client.WithAPIVersionNegotiation())
|
||||
}
|
||||
|
||||
cl, err := client.NewClientWithOpts(clientOpts...)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unable to create docker client")
|
||||
panic(err)
|
||||
}
|
||||
return cl
|
||||
}
|
12
client/connection.go
Normal file
12
client/connection.go
Normal file
@ -0,0 +1,12 @@
|
||||
package client
|
||||
|
||||
import "github.com/docker/cli/cli/connhelper"
|
||||
|
||||
func newConnectionHelper(daemonURL string) *connhelper.ConnectionHelper {
|
||||
helper, err := connhelper.GetConnectionHelper(daemonURL)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return helper
|
||||
}
|
Reference in New Issue
Block a user