Browse Source

Add compose service, run `compose up` through API

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 5 years ago
parent
commit
5abc68ebdd
3 changed files with 66 additions and 0 deletions
  1. 2 0
      cli/cmd/serve.go
  2. 62 0
      server/proxy/compose.go
  3. 2 0
      server/proxy/proxy.go

+ 2 - 0
cli/cmd/serve.go

@@ -23,6 +23,7 @@ import (
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
+	composev1 "github.com/docker/compose-cli/protos/compose/v1"
 	containersv1 "github.com/docker/compose-cli/protos/containers/v1"
 	containersv1 "github.com/docker/compose-cli/protos/containers/v1"
 	contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
 	contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
 	streamsv1 "github.com/docker/compose-cli/protos/streams/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)
 	p := proxy.New(ctx)
 
 
+	composev1.RegisterComposeServer(s, p)
 	containersv1.RegisterContainersServer(s, p)
 	containersv1.RegisterContainersServer(s, p)
 	contextsv1.RegisterContextsServer(s, p.ContextsProxy())
 	contextsv1.RegisterContextsServer(s, p.ContextsProxy())
 	streamsv1.RegisterStreamingServer(s, p)
 	streamsv1.RegisterStreamingServer(s, p)

+ 62 - 0
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,
+		},
+	})
+}

+ 2 - 0
server/proxy/proxy.go

@@ -22,6 +22,7 @@ import (
 
 
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/config"
 	"github.com/docker/compose-cli/config"
+	composev1 "github.com/docker/compose-cli/protos/compose/v1"
 	containersv1 "github.com/docker/compose-cli/protos/containers/v1"
 	containersv1 "github.com/docker/compose-cli/protos/containers/v1"
 	contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
 	contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
 	streamsv1 "github.com/docker/compose-cli/protos/streams/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
 // Proxy implements the gRPC server and forwards the actions
 // to the right backend
 // to the right backend
 type Proxy interface {
 type Proxy interface {
+	composev1.ComposeServer
 	containersv1.ContainersServer
 	containersv1.ContainersServer
 	streamsv1.StreamingServer
 	streamsv1.StreamingServer
 	volumesv1.VolumesServer
 	volumesv1.VolumesServer