compose/server/proxy/containers.go

120 lines
3.3 KiB
Go
Raw Normal View History

2020-05-16 12:13:51 +02:00
package proxy
import (
"context"
"errors"
2020-05-16 12:13:51 +02:00
"github.com/docker/api/containers"
containersv1 "github.com/docker/api/protos/containers/v1"
2020-06-06 22:46:35 +02:00
"github.com/docker/api/server/proxy/streams"
2020-05-16 12:13:51 +02:00
)
func portsToGrpc(ports []containers.Port) []*containersv1.Port {
var result []*containersv1.Port
2020-05-16 12:13:51 +02:00
for _, port := range ports {
result = append(result, &containersv1.Port{
2020-05-16 12:13:51 +02:00
ContainerPort: port.ContainerPort,
HostPort: port.HostPort,
HostIp: port.HostIP,
Protocol: port.Protocol,
})
}
return result
}
func (p *proxy) List(ctx context.Context, request *containersv1.ListRequest) (*containersv1.ListResponse, error) {
2020-05-16 12:13:51 +02:00
client := Client(ctx)
c, err := client.ContainerService().List(ctx, request.GetAll())
if err != nil {
return &containersv1.ListResponse{}, err
2020-05-16 12:13:51 +02:00
}
response := &containersv1.ListResponse{
Containers: []*containersv1.Container{},
2020-05-16 12:13:51 +02:00
}
for _, container := range c {
response.Containers = append(response.Containers, &containersv1.Container{
2020-05-16 12:13:51 +02:00
Id: container.ID,
Image: container.Image,
Command: container.Command,
Status: container.Status,
CpuTime: container.CPUTime,
Labels: container.Labels,
MemoryLimit: container.MemoryLimit,
MemoryUsage: container.MemoryUsage,
PidsCurrent: container.PidsCurrent,
PidsLimit: container.PidsLimit,
Ports: portsToGrpc(container.Ports),
})
}
return response, nil
}
func (p *proxy) Stop(ctx context.Context, request *containersv1.StopRequest) (*containersv1.StopResponse, error) {
2020-05-16 12:13:51 +02:00
c := Client(ctx)
timeoutValue := request.GetTimeout()
return &containersv1.StopResponse{}, c.ContainerService().Stop(ctx, request.Id, &timeoutValue)
2020-05-16 12:13:51 +02:00
}
func (p *proxy) Run(ctx context.Context, request *containersv1.RunRequest) (*containersv1.RunResponse, error) {
ports := []containers.Port{}
for _, p := range request.GetPorts() {
ports = append(ports, containers.Port{
ContainerPort: p.ContainerPort,
HostIP: p.HostIp,
HostPort: p.HostPort,
Protocol: p.Protocol,
})
}
err := Client(ctx).ContainerService().Run(ctx, containers.ContainerConfig{
ID: request.GetId(),
Image: request.GetImage(),
Labels: request.GetLabels(),
Ports: ports,
Volumes: request.GetVolumes(),
})
return &containersv1.RunResponse{}, err
2020-05-16 12:13:51 +02:00
}
func (p *proxy) Delete(ctx context.Context, request *containersv1.DeleteRequest) (*containersv1.DeleteResponse, error) {
2020-05-16 12:13:51 +02:00
err := Client(ctx).ContainerService().Delete(ctx, request.Id, request.Force)
if err != nil {
return &containersv1.DeleteResponse{}, err
2020-05-16 12:13:51 +02:00
}
return &containersv1.DeleteResponse{}, nil
2020-05-16 12:13:51 +02:00
}
func (p *proxy) Exec(ctx context.Context, request *containersv1.ExecRequest) (*containersv1.ExecResponse, error) {
p.mu.Lock()
stream, ok := p.streams[request.StreamId]
p.mu.Unlock()
if !ok {
return &containersv1.ExecResponse{}, errors.New("unknown stream id")
}
2020-06-06 22:46:35 +02:00
io := &streams.IO{
Stream: stream,
}
err := Client(ctx).ContainerService().Exec(ctx, request.GetId(), request.GetCommand(), io, io)
2020-05-16 12:13:51 +02:00
return &containersv1.ExecResponse{}, err
2020-05-16 12:13:51 +02:00
}
2020-05-18 23:55:17 +02:00
func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Containers_LogsServer) error {
2020-05-18 23:55:17 +02:00
ctx := stream.Context()
c := Client(ctx)
return c.ContainerService().Logs(ctx, request.GetContainerId(), containers.LogsRequest{
Follow: request.Follow,
2020-06-06 22:46:35 +02:00
Writer: &streams.Log{
Stream: stream,
},
2020-05-18 23:55:17 +02:00
})
}