From 5abc68ebddd71d2041c125d31dd42e159ce49c12 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 16 Nov 2020 12:11:52 +0100 Subject: [PATCH] Add compose service, run `compose up` through API Signed-off-by: Guillaume Tardif --- cli/cmd/serve.go | 2 ++ server/proxy/compose.go | 62 +++++++++++++++++++++++++++++++++++++++++ server/proxy/proxy.go | 2 ++ 3 files changed, 66 insertions(+) create mode 100644 server/proxy/compose.go diff --git a/cli/cmd/serve.go b/cli/cmd/serve.go index 84667ba87..d11e47a0e 100644 --- a/cli/cmd/serve.go +++ b/cli/cmd/serve.go @@ -23,6 +23,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + composev1 "github.com/docker/compose-cli/protos/compose/v1" containersv1 "github.com/docker/compose-cli/protos/containers/v1" contextsv1 "github.com/docker/compose-cli/protos/contexts/v1" streamsv1 "github.com/docker/compose-cli/protos/streams/v1" @@ -64,6 +65,7 @@ func runServe(ctx context.Context, opts serveOpts) error { p := proxy.New(ctx) + composev1.RegisterComposeServer(s, p) containersv1.RegisterContainersServer(s, p) contextsv1.RegisterContextsServer(s, p.ContextsProxy()) streamsv1.RegisterStreamingServer(s, p) diff --git a/server/proxy/compose.go b/server/proxy/compose.go new file mode 100644 index 000000000..be250213e --- /dev/null +++ b/server/proxy/compose.go @@ -0,0 +1,62 @@ +/* + Copyright 2020 Docker Compose CLI authors + + 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. +*/ + +package proxy + +import ( + "context" + + "github.com/compose-spec/compose-go/cli" + "github.com/docker/compose-cli/api/containers" + composev1 "github.com/docker/compose-cli/protos/compose/v1" + containersv1 "github.com/docker/compose-cli/protos/containers/v1" + "github.com/docker/compose-cli/server/proxy/streams" +) + +func (p *proxy) Up(ctx context.Context, request *composev1.ComposeUpRequest) (*composev1.ComposeUpResponse, error) { + options, err := cli.NewProjectOptions(request.Files, + cli.WithOsEnv, + cli.WithWorkingDirectory(request.WorkDir), + cli.WithName(request.ProjectName)) + if err != nil { + return nil, err + } + + project, err := cli.ProjectFromOptions(options) + if err != nil { + return nil, err + } + + return &composev1.ComposeUpResponse{}, Client(ctx).ComposeService().Up(ctx, project, true) +} + +func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest) (*composev1.ComposeDownResponse, error) { + err := Client(ctx).ComposeService().Down(ctx, "TODO") + if err != nil { + return nil, err + } + response := &composev1.ComposeDownResponse{} + return response, err +} + +func (p *proxy) ComposeLogs(request *containersv1.LogsRequest, stream containersv1.Containers_LogsServer) error { + return Client(stream.Context()).ContainerService().Logs(stream.Context(), request.GetContainerId(), containers.LogsRequest{ + Follow: request.Follow, + Writer: &streams.Log{ + Stream: stream, + }, + }) +} diff --git a/server/proxy/proxy.go b/server/proxy/proxy.go index 468b5b9a7..89f46b6ad 100644 --- a/server/proxy/proxy.go +++ b/server/proxy/proxy.go @@ -22,6 +22,7 @@ import ( "github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/config" + composev1 "github.com/docker/compose-cli/protos/compose/v1" containersv1 "github.com/docker/compose-cli/protos/containers/v1" contextsv1 "github.com/docker/compose-cli/protos/contexts/v1" streamsv1 "github.com/docker/compose-cli/protos/streams/v1" @@ -45,6 +46,7 @@ func Client(ctx context.Context) *client.Client { // Proxy implements the gRPC server and forwards the actions // to the right backend type Proxy interface { + composev1.ComposeServer containersv1.ContainersServer streamsv1.StreamingServer volumesv1.VolumesServer