mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
Pass reader Writer as options to backend, remove hardcoded os.Stdout
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
bad0d41d90
commit
32d5644937
@ -202,6 +202,6 @@ func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project
|
|||||||
return nil, errdefs.ErrNotImplemented
|
return nil, errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
return "", errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,6 @@ func (c *composeService) Convert(context.Context, *types.Project, compose.Conver
|
|||||||
return nil, errdefs.ErrNotImplemented
|
return nil, errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
return "", errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
)
|
)
|
||||||
@ -47,7 +48,7 @@ type Service interface {
|
|||||||
// Convert translate compose model into backend's native format
|
// Convert translate compose model into backend's native format
|
||||||
Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
|
Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
|
||||||
// RunOneOffContainer creates a service oneoff container and starts its dependencies
|
// RunOneOffContainer creates a service oneoff container and starts its dependencies
|
||||||
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
|
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpOptions group options of the Up API
|
// UpOptions group options of the Up API
|
||||||
@ -68,12 +69,14 @@ type ConvertOptions struct {
|
|||||||
Format string
|
Format string
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunOptions holds all flags for compose run
|
// RunOptions options to execute compose run
|
||||||
type RunOptions struct {
|
type RunOptions struct {
|
||||||
Name string
|
Name string
|
||||||
Command []string
|
Command []string
|
||||||
Detach bool
|
Detach bool
|
||||||
AutoRemove bool
|
AutoRemove bool
|
||||||
|
Writer io.Writer
|
||||||
|
Reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// PortPublisher hold status about published port
|
// PortPublisher hold status about published port
|
||||||
|
@ -18,11 +18,12 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"os"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/api/client"
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/compose-cli/progress"
|
"github.com/docker/compose-cli/progress"
|
||||||
)
|
)
|
||||||
@ -74,20 +75,7 @@ func runRun(ctx context.Context, opts runOptions) error {
|
|||||||
|
|
||||||
originalServices := project.Services
|
originalServices := project.Services
|
||||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||||
dependencies := types.Services{}
|
return "", startDependencies(ctx, c, project, opts.Name)
|
||||||
for _, service := range originalServices {
|
|
||||||
if service.Name != opts.Name {
|
|
||||||
dependencies = append(dependencies, service)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
project.Services = dependencies
|
|
||||||
if err := c.ComposeService().Create(ctx, project); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -95,13 +83,32 @@ func runRun(ctx context.Context, opts runOptions) error {
|
|||||||
|
|
||||||
project.Services = originalServices
|
project.Services = originalServices
|
||||||
// start container and attach to container streams
|
// start container and attach to container streams
|
||||||
runOpts := compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach, AutoRemove: opts.Remove}
|
runOpts := compose.RunOptions{
|
||||||
containerID, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
|
Name: opts.Name,
|
||||||
if err != nil {
|
Command: opts.Command,
|
||||||
|
Detach: opts.Detach,
|
||||||
|
AutoRemove: opts.Remove,
|
||||||
|
Writer: os.Stdout,
|
||||||
|
Reader: os.Stdin,
|
||||||
|
}
|
||||||
|
return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error {
|
||||||
|
originalServices := project.Services
|
||||||
|
dependencies := types.Services{}
|
||||||
|
for _, service := range originalServices {
|
||||||
|
if service.Name != requestedService {
|
||||||
|
dependencies = append(dependencies, service)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
project.Services = dependencies
|
||||||
|
if err := c.ComposeService().Create(ctx, project); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if opts.Detach {
|
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
|
||||||
fmt.Printf("%s", containerID)
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -163,6 +163,6 @@ func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compo
|
|||||||
func (e ecsLocalSimulation) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
|
func (e ecsLocalSimulation) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
|
||||||
return e.compose.List(ctx, projectName)
|
return e.compose.List(ctx, projectName)
|
||||||
}
|
}
|
||||||
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
return "", errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
|
return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,6 @@ import (
|
|||||||
"github.com/docker/compose-cli/errdefs"
|
"github.com/docker/compose-cli/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
return "", errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
@ -182,6 +182,6 @@ func (cs *composeService) Logs(ctx context.Context, projectName string, consumer
|
|||||||
func (cs *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
|
func (cs *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
|
||||||
return nil, errdefs.ErrNotImplemented
|
return nil, errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
func (cs *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (cs *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
return "", errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
@ -229,7 +229,7 @@ func (s *composeService) restartContainer(ctx context.Context, container moby.Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container, autoRemove bool) error {
|
func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container, autoRemove bool) error {
|
||||||
containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container, autoRemove)
|
containerConfig, hostConfig, networkingConfig, err := getCreateOptions(project, service, number, container, autoRemove)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
|
func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
|
||||||
hash, err := jsonHash(s)
|
hash, err := jsonHash(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
|
@ -19,7 +19,6 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
@ -30,7 +29,7 @@ import (
|
|||||||
moby "github.com/docker/docker/pkg/stringid"
|
moby "github.com/docker/docker/pkg/stringid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
|
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
||||||
originalServices := project.Services
|
originalServices := project.Services
|
||||||
var requestedService types.ServiceConfig
|
var requestedService types.ServiceConfig
|
||||||
for _, service := range originalServices {
|
for _, service := range originalServices {
|
||||||
@ -53,16 +52,20 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
|
|||||||
requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
|
requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
|
||||||
|
|
||||||
if err := s.waitDependencies(ctx, project, requestedService); err != nil {
|
if err := s.waitDependencies(ctx, project, requestedService); err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
|
if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
containerID := requestedService.ContainerName
|
containerID := requestedService.ContainerName
|
||||||
|
|
||||||
if opts.Detach {
|
if opts.Detach {
|
||||||
return containerID, s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
|
err := s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintln(opts.Writer, containerID)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
|
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
|
||||||
@ -72,20 +75,16 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
|
|||||||
All: true,
|
All: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
oneoffContainer := containers[0]
|
oneoffContainer := containers[0]
|
||||||
eg := errgroup.Group{}
|
eg := errgroup.Group{}
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return s.attachContainerStreams(ctx, oneoffContainer, true, os.Stdin, os.Stdout)
|
return s.attachContainerStreams(ctx, oneoffContainer, true, opts.Reader, opts.Writer)
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{}); err != nil {
|
if err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{}); err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
err = eg.Wait()
|
return eg.Wait()
|
||||||
return containerID, err
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user