Add compose service, run `compose up` through API

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-11-16 12:11:52 +01:00
parent 0ae42dea04
commit 5abc68ebdd
3 changed files with 66 additions and 0 deletions

View File

@ -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)

62
server/proxy/compose.go Normal file
View File

@ -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,
},
})
}

View File

@ -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