exec.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 mobycli
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "os/signal"
  20. "strings"
  21. apicontext "github.com/docker/api/context"
  22. "github.com/docker/api/context/store"
  23. )
  24. var delegatedContextTypes = []string{store.DefaultContextType}
  25. // ComDockerCli name of the classic cli binary
  26. const ComDockerCli = "com.docker.cli"
  27. // ExecIfDefaultCtxType delegates to com.docker.cli if on moby or AWS context (until there is an AWS backend)
  28. func ExecIfDefaultCtxType(ctx context.Context) {
  29. currentContext := apicontext.CurrentContext(ctx)
  30. s := store.ContextStore(ctx)
  31. currentCtx, err := s.Get(currentContext)
  32. // Only run original docker command if the current context is not ours.
  33. if err != nil || mustDelegateToMoby(currentCtx.Type()) {
  34. Exec()
  35. }
  36. }
  37. func mustDelegateToMoby(ctxType string) bool {
  38. for _, ctype := range delegatedContextTypes {
  39. if ctxType == ctype {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. // Exec delegates to com.docker.cli if on moby context
  46. func Exec() {
  47. cmd := exec.Command(ComDockerCli, os.Args[1:]...)
  48. cmd.Stdin = os.Stdin
  49. cmd.Stdout = os.Stdout
  50. cmd.Stderr = os.Stderr
  51. signals := make(chan os.Signal)
  52. childExit := make(chan bool)
  53. signal.Notify(signals) // catch all signals
  54. go func() {
  55. for {
  56. select {
  57. case sig := <-signals:
  58. if cmd.Process == nil {
  59. continue // can happen if receiving signal before the process is actually started
  60. }
  61. err := cmd.Process.Signal(sig)
  62. if err != nil {
  63. fmt.Printf("WARNING could not forward signal %s to %s : %s\n", sig.String(), ComDockerCli, err.Error())
  64. }
  65. case <-childExit:
  66. return
  67. }
  68. }
  69. }()
  70. err := cmd.Run()
  71. childExit <- true
  72. if err != nil {
  73. if exiterr, ok := err.(*exec.ExitError); ok {
  74. os.Exit(exiterr.ExitCode())
  75. }
  76. fmt.Fprintln(os.Stderr, err)
  77. os.Exit(1)
  78. }
  79. os.Exit(0)
  80. }
  81. // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
  82. func IsDefaultContextCommand(dockerCommand string) bool {
  83. cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
  84. b, e := cmd.CombinedOutput()
  85. if e != nil {
  86. fmt.Println(e)
  87. }
  88. output := string(b)
  89. contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)
  90. return contains
  91. }
  92. // ExecSilent executes a command and do redirect output to stdOut, return output
  93. func ExecSilent(ctx context.Context) ([]byte, error) {
  94. cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
  95. return cmd.CombinedOutput()
  96. }