2020-05-04 19:50:01 +02:00
|
|
|
package moby
|
|
|
|
|
|
|
|
import (
|
2020-05-22 16:15:57 +02:00
|
|
|
"bufio"
|
2020-05-04 19:50:01 +02:00
|
|
|
"context"
|
|
|
|
"io"
|
2020-05-22 16:15:57 +02:00
|
|
|
"strconv"
|
2020-05-16 12:13:51 +02:00
|
|
|
"time"
|
2020-05-04 19:50:01 +02:00
|
|
|
|
2020-05-22 16:15:57 +02:00
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
|
2020-05-14 22:54:57 +02:00
|
|
|
"github.com/docker/api/context/cloud"
|
|
|
|
|
2020-05-04 19:50:01 +02:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/client"
|
2020-05-13 15:20:27 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-05-04 19:50:01 +02:00
|
|
|
|
|
|
|
"github.com/docker/api/backend"
|
|
|
|
"github.com/docker/api/compose"
|
|
|
|
"github.com/docker/api/containers"
|
2020-05-13 15:20:27 +02:00
|
|
|
"github.com/docker/api/errdefs"
|
2020-05-04 19:50:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type mobyService struct {
|
|
|
|
apiClient *client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-05-28 17:37:59 +02:00
|
|
|
backend.Register("moby", "moby", service, cloud.NotImplementedCloudService)
|
2020-05-04 19:50:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 17:37:59 +02:00
|
|
|
func service(ctx context.Context) (backend.Service, error) {
|
2020-05-04 19:50:01 +02:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mobyService{
|
|
|
|
apiClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mobyService) ContainerService() containers.Service {
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mobyService) ComposeService() compose.Service {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-16 12:13:51 +02:00
|
|
|
func (ms *mobyService) List(ctx context.Context, all bool) ([]containers.Container, error) {
|
2020-05-04 19:50:01 +02:00
|
|
|
css, err := ms.apiClient.ContainerList(ctx, types.ContainerListOptions{
|
2020-05-16 12:13:51 +02:00
|
|
|
All: all,
|
2020-05-04 19:50:01 +02:00
|
|
|
})
|
2020-05-16 12:13:51 +02:00
|
|
|
|
2020-05-04 19:50:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []containers.Container
|
|
|
|
for _, container := range css {
|
|
|
|
result = append(result, containers.Container{
|
2020-05-16 12:13:51 +02:00
|
|
|
ID: container.ID,
|
|
|
|
Image: container.Image,
|
|
|
|
// TODO: `Status` is a human readable string ("Up 24 minutes"),
|
|
|
|
// we need to return the `State` instead but first we need to
|
|
|
|
// define an enum on the proto side with all the possible container
|
|
|
|
// statuses. We also need to add a `Created` property on the gRPC side.
|
2020-05-04 19:50:01 +02:00
|
|
|
Status: container.Status,
|
|
|
|
Command: container.Command,
|
2020-05-22 16:15:57 +02:00
|
|
|
Ports: toPorts(container.Ports),
|
2020-05-04 19:50:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mobyService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
2020-05-22 16:15:57 +02:00
|
|
|
exposedPorts, hostBindings, err := fromPorts(r.Ports)
|
2020-05-04 19:50:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:15:57 +02:00
|
|
|
containerConfig := &container.Config{
|
|
|
|
Image: r.Image,
|
|
|
|
Labels: r.Labels,
|
|
|
|
ExposedPorts: exposedPorts,
|
|
|
|
}
|
|
|
|
hostConfig := &container.HostConfig{
|
|
|
|
PortBindings: hostBindings,
|
|
|
|
}
|
|
|
|
|
|
|
|
created, err := ms.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, r.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
io, err := ms.apiClient.ImagePull(ctx, r.Image, types.ImagePullOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(io)
|
2020-05-22 17:05:46 +02:00
|
|
|
|
|
|
|
// Read the whole body, otherwise the pulling stops
|
2020-05-22 16:15:57 +02:00
|
|
|
for scanner.Scan() {
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = io.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
created, err = ms.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, r.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms.apiClient.ContainerStart(ctx, created.ID, types.ContainerStartOptions{})
|
2020-05-04 19:50:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 12:02:04 +02:00
|
|
|
func (ms *mobyService) Stop(ctx context.Context, containerID string, timeout *uint32) error {
|
|
|
|
var t *time.Duration
|
|
|
|
if timeout != nil {
|
|
|
|
timeoutValue := time.Duration(*timeout) * time.Second
|
|
|
|
t = &timeoutValue
|
|
|
|
}
|
|
|
|
return ms.apiClient.ContainerStop(ctx, containerID, t)
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 19:50:01 +02:00
|
|
|
func (ms *mobyService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
|
|
|
cec, err := ms.apiClient.ContainerExecCreate(ctx, name, types.ExecConfig{
|
|
|
|
Cmd: []string{command},
|
|
|
|
Tty: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := ms.apiClient.ContainerExecAttach(ctx, cec.ID, types.ExecStartCheck{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
|
|
|
readChannel := make(chan error, 10)
|
|
|
|
writeChannel := make(chan error, 10)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(writer, resp.Reader)
|
|
|
|
readChannel <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(resp.Conn, reader)
|
|
|
|
writeChannel <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-readChannel:
|
|
|
|
return err
|
|
|
|
case err := <-writeChannel:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mobyService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
|
|
|
|
r, err := ms.apiClient.ContainerLogs(ctx, containerName, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
2020-05-18 23:55:17 +02:00
|
|
|
Follow: request.Follow,
|
2020-05-04 19:50:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(request.Writer, r)
|
|
|
|
return err
|
|
|
|
}
|
2020-05-13 10:50:36 +02:00
|
|
|
|
|
|
|
func (ms *mobyService) Delete(ctx context.Context, containerID string, force bool) error {
|
2020-05-13 15:20:27 +02:00
|
|
|
err := ms.apiClient.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{
|
2020-05-13 10:50:36 +02:00
|
|
|
Force: force,
|
|
|
|
})
|
2020-05-13 15:20:27 +02:00
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return errors.Wrapf(errdefs.ErrNotFound, "container %q", containerID)
|
|
|
|
}
|
|
|
|
return err
|
2020-05-13 10:50:36 +02:00
|
|
|
}
|
2020-05-14 12:17:06 +02:00
|
|
|
|
2020-05-22 16:15:57 +02:00
|
|
|
func toPorts(ports []types.Port) []containers.Port {
|
2020-05-14 12:17:06 +02:00
|
|
|
result := []containers.Port{}
|
|
|
|
for _, port := range ports {
|
|
|
|
result = append(result, containers.Port{
|
|
|
|
ContainerPort: uint32(port.PrivatePort),
|
|
|
|
HostPort: uint32(port.PublicPort),
|
|
|
|
HostIP: port.IP,
|
|
|
|
Protocol: port.Type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-05-22 16:15:57 +02:00
|
|
|
|
|
|
|
func fromPorts(ports []containers.Port) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
|
|
|
|
var (
|
|
|
|
exposedPorts = make(map[nat.Port]struct{}, len(ports))
|
|
|
|
bindings = make(map[nat.Port][]nat.PortBinding)
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, port := range ports {
|
|
|
|
p, err := nat.NewPort(port.Protocol, strconv.Itoa(int(port.ContainerPort)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := exposedPorts[p]; !exists {
|
|
|
|
exposedPorts[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
portBinding := nat.PortBinding{
|
|
|
|
HostIP: port.HostIP,
|
|
|
|
HostPort: strconv.Itoa(int(port.HostPort)),
|
|
|
|
}
|
|
|
|
bslice, exists := bindings[p]
|
|
|
|
if !exists {
|
|
|
|
bslice = []nat.PortBinding{}
|
|
|
|
}
|
|
|
|
bindings[p] = append(bslice, portBinding)
|
|
|
|
}
|
|
|
|
|
|
|
|
return exposedPorts, bindings, nil
|
|
|
|
}
|