Reusing existing Create() and Start() API

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2020-12-16 15:25:31 +01:00
parent 27dc2e5be1
commit 76f36a69c6
8 changed files with 49 additions and 71 deletions

View File

@ -202,9 +202,6 @@ func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }
func (cs *aciComposeService) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
return "", errdefs.ErrNotImplemented return "", errdefs.ErrNotImplemented
} }
func (cs *aciComposeService) Run(ctx context.Context, container string, detach bool) error {
return errdefs.ErrNotImplemented
}

View File

@ -72,10 +72,6 @@ func (c *composeService) Convert(context.Context, *types.Project, compose.Conver
return nil, errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }
func (c *composeService) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
return "", errdefs.ErrNotImplemented return "", errdefs.ErrNotImplemented
} }
func (c *composeService) Run(ctx context.Context, container string, detach bool) error {
return errdefs.ErrNotImplemented
}

View File

@ -46,10 +46,8 @@ type Service interface {
List(ctx context.Context, projectName string) ([]Stack, error) List(ctx context.Context, projectName string) ([]Stack, error)
// 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)
// CreateOneOffContainer creates a service oneoff container and starts its dependencies // RunOneOffContainer creates a service oneoff container and starts its dependencies
CreateOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error) RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
// Run attaches to and starts a one-off container
Run(ctx context.Context, container string, detach bool) error
} }
// UpOptions group options of the Up API // UpOptions group options of the Up API
@ -74,6 +72,7 @@ type ConvertOptions struct {
type RunOptions struct { type RunOptions struct {
Name string Name string
Command []string Command []string
Detach bool
} }
// PortPublisher hold status about published port // PortPublisher hold status about published port

View File

@ -20,6 +20,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/compose-spec/compose-go/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
@ -72,17 +73,30 @@ func runRun(ctx context.Context, opts runOptions) error {
return err return err
} }
dependencies := []types.ServiceConfig{}
originalServices := project.Services
containerID, err := progress.Run(ctx, func(ctx context.Context) (string, error) { containerID, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
return c.ComposeService().CreateOneOffContainer(ctx, project, compose.RunOptions{ for _, service := range originalServices {
Name: opts.Name, if service.Name != opts.Name {
Command: opts.Command, dependencies = append(dependencies, service)
}) }
}
project.Services = types.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
} }
project.Services = originalServices
// start container and attach to container streams // start container and attach to container streams
err = c.ComposeService().Run(ctx, containerID, opts.Detach) containerID, err = c.ComposeService().RunOneOffContainer(ctx, project, compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach})
if err != nil { if err != nil {
return err return err
} }

View File

@ -163,9 +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) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
return "", errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run") return "", errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
} }
func (e ecsLocalSimulation) Run(ctx context.Context, container string, detach bool) error {
return errdefs.ErrNotImplemented
}

View File

@ -24,10 +24,6 @@ import (
"github.com/docker/compose-cli/errdefs" "github.com/docker/compose-cli/errdefs"
) )
func (b *ecsAPIService) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
return "", errdefs.ErrNotImplemented return "", errdefs.ErrNotImplemented
} }
func (b *ecsAPIService) Run(ctx context.Context, container string, detach bool) error {
return errdefs.ErrNotImplemented
}

View File

@ -182,10 +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) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (cs *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
return "", errdefs.ErrNotImplemented return "", errdefs.ErrNotImplemented
} }
func (cs *composeService) Run(ctx context.Context, container string, detach bool) error {
return errdefs.ErrNotImplemented
}

View File

@ -29,27 +29,27 @@ import (
moby "github.com/docker/docker/pkg/stringid" moby "github.com/docker/docker/pkg/stringid"
) )
func (s *composeService) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) { func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
originalServices := project.Services originalServices := project.Services
dependencies := []types.ServiceConfig{}
var requestedService types.ServiceConfig var requestedService types.ServiceConfig
for _, service := range originalServices { for _, service := range originalServices {
if service.Name != opts.Name { if service.Name == opts.Name {
dependencies = append(dependencies, service)
} else {
requestedService = service requestedService = service
} }
} }
project.Services = types.Services(dependencies)
if err := s.Create(ctx, project); err != nil {
return "", err
}
if err := s.Start(ctx, project, nil); err != nil {
return "", err
}
project.Services = originalServices project.Services = originalServices
updateOneOffServiceConfig(&requestedService, project.Name, opts) if len(opts.Command) > 0 {
requestedService.Command = opts.Command
}
requestedService.Scale = 1
requestedService.Tty = true
requestedService.StdinOpen = true
slug := moby.GenerateRandomID()
requestedService.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, requestedService.Name, moby.TruncateID(slug))
requestedService.Labels = requestedService.Labels.Add(slugLabel, slug)
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
@ -59,15 +59,13 @@ func (s *composeService) CreateOneOffContainer(ctx context.Context, project *typ
return "", err return "", err
} }
return requestedService.ContainerName, err containerID := requestedService.ContainerName
}
func (s *composeService) Run(ctx context.Context, container string, detach bool) error { if opts.Detach {
if detach { return containerID, s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
return s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{})
} }
cnx, err := s.apiClient.ContainerAttach(ctx, container, apitypes.ContainerAttachOptions{ cnx, err := s.apiClient.ContainerAttach(ctx, containerID, apitypes.ContainerAttachOptions{
Stream: true, Stream: true,
Stdin: true, Stdin: true,
Stdout: true, Stdout: true,
@ -75,7 +73,7 @@ func (s *composeService) Run(ctx context.Context, container string, detach bool)
Logs: true, Logs: true,
}) })
if err != nil { if err != nil {
return err return containerID, err
} }
defer cnx.Close() defer cnx.Close()
@ -102,32 +100,17 @@ func (s *composeService) Run(ctx context.Context, container string, detach bool)
}() }()
// start container // start container
err = s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{}) err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
if err != nil { if err != nil {
return err return containerID, err
} }
for { for {
select { select {
case err := <-readChannel: case err := <-readChannel:
return err return containerID, err
case err := <-writeChannel: case err := <-writeChannel:
return err return containerID, err
} }
} }
} }
func updateOneOffServiceConfig(service *types.ServiceConfig, projectName string, opts compose.RunOptions) {
if len(opts.Command) > 0 {
service.Command = opts.Command
}
slug := moby.GenerateRandomID()
service.Scale = 1
service.ContainerName = fmt.Sprintf("%s_%s_run_%s", projectName, service.Name, moby.TruncateID(slug))
service.Labels = types.Labels{
slugLabel: slug,
oneoffLabel: "True",
}
service.Tty = true
service.StdinOpen = true
}