2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-16 12:13:51 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-05 16:28:18 +02:00
|
|
|
"errors"
|
2020-05-16 12:13:51 +02:00
|
|
|
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/containers"
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/formatter"
|
|
|
|
containersv1 "github.com/docker/compose-cli/protos/containers/v1"
|
|
|
|
"github.com/docker/compose-cli/server/proxy/streams"
|
2020-05-16 12:13:51 +02:00
|
|
|
)
|
|
|
|
|
2020-06-05 16:28:18 +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 {
|
2020-06-05 16:28:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) List(ctx context.Context, request *containersv1.ListRequest) (*containersv1.ListResponse, error) {
|
2020-06-08 18:34:32 +02:00
|
|
|
containerList, err := Client(ctx).ContainerService().List(ctx, request.GetAll())
|
2020-05-16 12:13:51 +02:00
|
|
|
if err != nil {
|
2020-06-05 16:28:18 +02:00
|
|
|
return &containersv1.ListResponse{}, err
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
response := &containersv1.ListResponse{
|
|
|
|
Containers: []*containersv1.Container{},
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
2020-06-08 18:34:32 +02:00
|
|
|
for _, container := range containerList {
|
2020-06-15 10:38:37 +02:00
|
|
|
response.Containers = append(response.Containers, toGrpcContainer(container))
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:43:57 +02:00
|
|
|
func (p *proxy) Start(ctx context.Context, request *containersv1.StartRequest) (*containersv1.StartResponse, error) {
|
|
|
|
return &containersv1.StartResponse{}, Client(ctx).ContainerService().Start(ctx, request.Id)
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Stop(ctx context.Context, request *containersv1.StopRequest) (*containersv1.StopResponse, error) {
|
2020-05-18 12:02:04 +02:00
|
|
|
timeoutValue := request.GetTimeout()
|
2020-06-08 18:34:32 +02:00
|
|
|
return &containersv1.StopResponse{}, Client(ctx).ContainerService().Stop(ctx, request.Id, &timeoutValue)
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-09-01 16:06:07 +02:00
|
|
|
func (p *proxy) Kill(ctx context.Context, request *containersv1.KillRequest) (*containersv1.KillResponse, error) {
|
|
|
|
signal := request.GetSignal()
|
|
|
|
return &containersv1.KillResponse{}, Client(ctx).ContainerService().Kill(ctx, request.Id, signal)
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Run(ctx context.Context, request *containersv1.RunRequest) (*containersv1.RunResponse, error) {
|
2020-06-24 09:59:53 +02:00
|
|
|
return &containersv1.RunResponse{}, Client(ctx).ContainerService().Run(ctx, grpcContainerToContainerConfig(request))
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 10:53:06 +02:00
|
|
|
func (p *proxy) Inspect(ctx context.Context, request *containersv1.InspectRequest) (*containersv1.InspectResponse, error) {
|
|
|
|
c, err := Client(ctx).ContainerService().Inspect(ctx, request.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response := &containersv1.InspectResponse{
|
2020-06-15 10:38:37 +02:00
|
|
|
Container: toGrpcContainer(c),
|
2020-06-12 10:53:06 +02:00
|
|
|
}
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Delete(ctx context.Context, request *containersv1.DeleteRequest) (*containersv1.DeleteResponse, error) {
|
2020-08-11 15:46:29 +02:00
|
|
|
return &containersv1.DeleteResponse{}, Client(ctx).ContainerService().Delete(ctx, request.Id, containers.DeleteRequest{
|
|
|
|
Force: request.Force,
|
|
|
|
})
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +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,
|
|
|
|
}
|
2020-05-16 12:13:51 +02:00
|
|
|
|
2020-07-08 14:31:27 +02:00
|
|
|
return &containersv1.ExecResponse{}, Client(ctx).ContainerService().Exec(ctx, request.GetId(), containers.ExecRequest{
|
|
|
|
Stdin: io,
|
|
|
|
Stdout: io,
|
|
|
|
Command: request.GetCommand(),
|
|
|
|
Tty: request.GetTty(),
|
|
|
|
})
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
2020-05-18 23:55:17 +02:00
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Containers_LogsServer) error {
|
2020-06-08 18:34:32 +02:00
|
|
|
return Client(stream.Context()).ContainerService().Logs(stream.Context(), request.GetContainerId(), containers.LogsRequest{
|
2020-05-18 23:55:17 +02:00
|
|
|
Follow: request.Follow,
|
2020-06-06 22:46:35 +02:00
|
|
|
Writer: &streams.Log{
|
|
|
|
Stream: stream,
|
|
|
|
},
|
2020-05-18 23:55:17 +02:00
|
|
|
})
|
|
|
|
}
|
2020-06-15 10:38:37 +02:00
|
|
|
|
|
|
|
func toGrpcContainer(c containers.Container) *containersv1.Container {
|
|
|
|
return &containersv1.Container{
|
2020-08-05 09:39:10 +02:00
|
|
|
Id: c.ID,
|
|
|
|
Image: c.Image,
|
|
|
|
Status: c.Status,
|
|
|
|
Command: c.Command,
|
|
|
|
CpuTime: c.CPUTime,
|
|
|
|
MemoryUsage: c.MemoryUsage,
|
|
|
|
MemoryLimit: c.MemoryLimit,
|
|
|
|
Platform: c.Platform,
|
|
|
|
PidsCurrent: c.PidsCurrent,
|
|
|
|
PidsLimit: c.PidsLimit,
|
|
|
|
Labels: c.Labels,
|
|
|
|
Ports: portsToGrpc(c.Ports),
|
|
|
|
CpuLimit: uint64(c.CPULimit),
|
|
|
|
RestartPolicyCondition: c.RestartPolicyCondition,
|
2020-06-24 09:59:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func grpcContainerToContainerConfig(request *containersv1.RunRequest) containers.ContainerConfig {
|
|
|
|
var 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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return containers.ContainerConfig{
|
2020-08-05 09:39:10 +02:00
|
|
|
ID: request.GetId(),
|
|
|
|
Image: request.GetImage(),
|
|
|
|
Ports: ports,
|
|
|
|
Labels: request.GetLabels(),
|
|
|
|
Volumes: request.GetVolumes(),
|
|
|
|
MemLimit: formatter.MemBytes(request.GetMemoryLimit()),
|
|
|
|
CPULimit: float64(request.GetCpuLimit()),
|
|
|
|
RestartPolicyCondition: request.RestartPolicyCondition,
|
2020-06-15 10:38:37 +02:00
|
|
|
}
|
|
|
|
}
|