| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 | 
							- /*
 
-    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 compose
 
- import (
 
- 	"context"
 
- 	"fmt"
 
- 	"io"
 
- 	"github.com/docker/compose/v2/pkg/api"
 
- 	"github.com/compose-spec/compose-go/types"
 
- 	"github.com/docker/cli/cli/streams"
 
- 	moby "github.com/docker/docker/api/types"
 
- 	"github.com/docker/docker/pkg/ioutils"
 
- 	"github.com/docker/docker/pkg/stdcopy"
 
- 	"github.com/docker/docker/pkg/stringid"
 
- 	"github.com/moby/term"
 
- )
 
- func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
 
- 	containerID, err := s.prepareRun(ctx, project, opts)
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	if opts.Detach {
 
- 		err := s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
 
- 		if err != nil {
 
- 			return 0, err
 
- 		}
 
- 		fmt.Fprintln(opts.Stdout, containerID)
 
- 		return 0, nil
 
- 	}
 
- 	return s.runInteractive(ctx, containerID, opts)
 
- }
 
- func (s *composeService) runInteractive(ctx context.Context, containerID string, opts api.RunOptions) (int, error) {
 
- 	r, err := s.getEscapeKeyProxy(opts.Stdin)
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	stdin, stdout, err := s.getContainerStreams(ctx, containerID)
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	in := streams.NewIn(opts.Stdin)
 
- 	if in.IsTerminal() {
 
- 		state, err := term.SetRawTerminal(in.FD())
 
- 		if err != nil {
 
- 			return 0, err
 
- 		}
 
- 		defer term.RestoreTerminal(in.FD(), state) //nolint:errcheck
 
- 	}
 
- 	outputDone := make(chan error)
 
- 	inputDone := make(chan error)
 
- 	go func() {
 
- 		if opts.Tty {
 
- 			_, err := io.Copy(opts.Stdout, stdout) //nolint:errcheck
 
- 			outputDone <- err
 
- 		} else {
 
- 			_, err := stdcopy.StdCopy(opts.Stdout, opts.Stderr, stdout) //nolint:errcheck
 
- 			outputDone <- err
 
- 		}
 
- 		stdout.Close() //nolint:errcheck
 
- 	}()
 
- 	go func() {
 
- 		_, err := io.Copy(stdin, r)
 
- 		inputDone <- err
 
- 		stdin.Close() //nolint:errcheck
 
- 	}()
 
- 	err = s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	s.monitorTTySize(ctx, containerID, s.apiClient.ContainerResize)
 
- 	for {
 
- 		select {
 
- 		case err := <-outputDone:
 
- 			return s.terminateRun(ctx, containerID, opts, err)
 
- 		case err := <-inputDone:
 
- 			if _, ok := err.(term.EscapeError); ok {
 
- 				return 0, nil
 
- 			}
 
- 			if err != nil {
 
- 				return 0, err
 
- 			}
 
- 			// Wait for output to complete streaming
 
- 		case <-ctx.Done():
 
- 			return 0, ctx.Err()
 
- 		}
 
- 	}
 
- }
 
- func (s *composeService) terminateRun(ctx context.Context, containerID string, opts api.RunOptions, err error) (int, error) {
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	inspect, err := s.apiClient.ContainerInspect(ctx, containerID)
 
- 	if err != nil {
 
- 		return 0, err
 
- 	}
 
- 	exitCode := 0
 
- 	if inspect.State != nil {
 
- 		exitCode = inspect.State.ExitCode
 
- 	}
 
- 	if opts.AutoRemove {
 
- 		err = s.apiClient.ContainerRemove(ctx, containerID, moby.ContainerRemoveOptions{})
 
- 	}
 
- 	return exitCode, err
 
- }
 
- func (s *composeService) prepareRun(ctx context.Context, project *types.Project, opts api.RunOptions) (string, error) {
 
- 	if err := prepareVolumes(project); err != nil { // all dependencies already checked, but might miss service img
 
- 		return "", err
 
- 	}
 
- 	service, err := project.GetService(opts.Service)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	applyRunOptions(project, &service, opts)
 
- 	slug := stringid.GenerateRandomID()
 
- 	if service.ContainerName == "" {
 
- 		service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))
 
- 	}
 
- 	service.Scale = 1
 
- 	service.StdinOpen = true
 
- 	service.Restart = ""
 
- 	if service.Deploy != nil {
 
- 		service.Deploy.RestartPolicy = nil
 
- 	}
 
- 	service.Labels = service.Labels.Add(api.SlugLabel, slug)
 
- 	service.Labels = service.Labels.Add(api.OneoffLabel, "True")
 
- 	if err := s.ensureImagesExists(ctx, project, false); err != nil { // all dependencies already checked, but might miss service img
 
- 		return "", err
 
- 	}
 
- 	if !opts.NoDeps {
 
- 		if err := s.waitDependencies(ctx, project, service); err != nil {
 
- 			return "", err
 
- 		}
 
- 	}
 
- 	created, err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.Detach && opts.AutoRemove, opts.UseNetworkAliases)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	containerID := created.ID
 
- 	return containerID, nil
 
- }
 
- func (s *composeService) getEscapeKeyProxy(r io.ReadCloser) (io.ReadCloser, error) {
 
- 	var escapeKeys = []byte{16, 17}
 
- 	if s.configFile.DetachKeys != "" {
 
- 		customEscapeKeys, err := term.ToBytes(s.configFile.DetachKeys)
 
- 		if err != nil {
 
- 			return nil, err
 
- 		}
 
- 		escapeKeys = customEscapeKeys
 
- 	}
 
- 	return ioutils.NewReadCloserWrapper(term.NewEscapeProxy(r, escapeKeys), r.Close), nil
 
- }
 
- func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
 
- 	service.Tty = opts.Tty
 
- 	service.ContainerName = opts.Name
 
- 	if len(opts.Command) > 0 {
 
- 		service.Command = opts.Command
 
- 	}
 
- 	if len(opts.User) > 0 {
 
- 		service.User = opts.User
 
- 	}
 
- 	if len(opts.WorkingDir) > 0 {
 
- 		service.WorkingDir = opts.WorkingDir
 
- 	}
 
- 	if opts.Entrypoint != nil {
 
- 		service.Entrypoint = opts.Entrypoint
 
- 	}
 
- 	if len(opts.Environment) > 0 {
 
- 		env := types.NewMappingWithEquals(opts.Environment)
 
- 		projectEnv := env.Resolve(func(s string) (string, bool) {
 
- 			v, ok := project.Environment[s]
 
- 			return v, ok
 
- 		}).RemoveEmpty()
 
- 		service.Environment.OverrideBy(projectEnv)
 
- 	}
 
- 	for k, v := range opts.Labels {
 
- 		service.Labels.Add(k, v)
 
- 	}
 
- }
 
 
  |