2017-07-27 22:12:29 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-07-28 00:18:44 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2017-07-27 22:12:29 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-10-03 21:36:26 +00:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-07-27 22:12:29 +00:00
|
|
|
docker "github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-07-25 23:10:28 +00:00
|
|
|
version = "1.21" // 1.24 is when server first started returning its version
|
2017-07-27 22:12:29 +00:00
|
|
|
defaultHeaders = map[string]string{"User-Agent": "engine-api-cli-1.0"}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client interface {
|
|
|
|
Info(ctx context.Context) (types.Info, error)
|
|
|
|
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
|
|
|
|
ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error)
|
|
|
|
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
|
2017-10-03 21:36:26 +00:00
|
|
|
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
|
|
|
|
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
|
|
|
|
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
|
2017-07-27 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEnvClient() (Client, error) {
|
2018-07-25 23:10:28 +00:00
|
|
|
client, err := docker.NewClientWithOpts(docker.FromEnv)
|
2017-07-27 22:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &SocketClient{client}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-28 00:18:44 +00:00
|
|
|
func NewClient(host string, tlsConfig *tls.Config) (Client, error) {
|
|
|
|
transport := &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
httpClient := &http.Client{Transport: transport}
|
|
|
|
|
2018-07-25 23:10:28 +00:00
|
|
|
client, err := docker.NewClientWithOpts(
|
|
|
|
docker.WithHTTPHeaders(defaultHeaders),
|
|
|
|
docker.WithHTTPClient(httpClient),
|
|
|
|
docker.WithVersion(version),
|
|
|
|
docker.WithHost(host))
|
2017-07-27 22:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-25 23:10:28 +00:00
|
|
|
|
2017-07-27 22:12:29 +00:00
|
|
|
return &SocketClient{client}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SocketClient struct {
|
|
|
|
client *docker.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SocketClient) Info(ctx context.Context) (types.Info, error) {
|
|
|
|
return c.client.Info(ctx)
|
|
|
|
}
|
|
|
|
func (c *SocketClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return c.client.ContainerList(ctx, options)
|
|
|
|
}
|
|
|
|
func (c *SocketClient) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) {
|
|
|
|
return c.client.ContainerStats(ctx, containerID, stream)
|
|
|
|
}
|
|
|
|
func (c *SocketClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
|
|
|
|
return c.client.ContainerInspect(ctx, containerID)
|
|
|
|
}
|
2017-10-03 21:36:26 +00:00
|
|
|
func (c *SocketClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return c.client.ServiceList(ctx, options)
|
|
|
|
}
|
|
|
|
func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return c.client.TaskList(ctx, options)
|
|
|
|
}
|
|
|
|
func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
|
|
|
|
return c.client.NodeList(ctx, options)
|
|
|
|
}
|