shellout.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os/exec"
  17. "github.com/compose-spec/compose-go/v2/types"
  18. "github.com/docker/cli/cli-plugins/manager"
  19. "github.com/docker/cli/cli/context/docker"
  20. "go.opentelemetry.io/otel"
  21. "go.opentelemetry.io/otel/propagation"
  22. )
  23. // prepareShellOut prepare a shell-out command to be ran by Compose
  24. func (s *composeService) prepareShellOut(gctx context.Context, project *types.Project, cmd *exec.Cmd) error {
  25. // exec command with same environment Compose is running
  26. env := types.NewMapping(project.Environment.Values())
  27. // remove DOCKER_CLI_PLUGIN... variable so a docker-cli plugin will detect it run standalone
  28. delete(env, manager.ReexecEnvvar)
  29. // propagate opentelemetry context to child process, see https://github.com/open-telemetry/oteps/blob/main/text/0258-env-context-baggage-carriers.md
  30. carrier := propagation.MapCarrier{}
  31. otel.GetTextMapPropagator().Inject(gctx, &carrier)
  32. env.Merge(types.Mapping(carrier))
  33. env["DOCKER_CONTEXT"] = s.dockerCli.CurrentContext()
  34. metadata, err := s.dockerCli.ContextStore().GetMetadata(s.dockerCli.CurrentContext())
  35. if err != nil {
  36. return err
  37. }
  38. endpoint, err := docker.EndpointFromContext(metadata)
  39. if err != nil {
  40. return err
  41. }
  42. actualHost := s.dockerCli.DockerEndpoint().Host
  43. if endpoint.Host != actualHost {
  44. // We are running with `--host` or `DOCKER_HOST` which overrides selected context
  45. env["DOCKER_HOST"] = actualHost
  46. }
  47. cmd.Env = env.Values()
  48. return nil
  49. }