Merge pull request #423 from docker/sig_proxy

Fix signal passing to mobycli
This commit is contained in:
Anca Iordache 2020-08-05 15:17:16 +02:00 committed by GitHub
commit 6c66f38870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 23 deletions

View File

@ -70,7 +70,8 @@ $ docker context create my-context --description "some description" --docker "ho
Use: "create CONTEXT", Use: "create CONTEXT",
Short: "Create new context", Short: "Create new context",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
}, },
Long: longHelp, Long: longHelp,
} }

View File

@ -27,7 +27,8 @@ func inspectCommand() *cobra.Command {
Use: "inspect", Use: "inspect",
Short: "Display detailed information on one or more contexts", Short: "Display detailed information on one or more contexts",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
}, },
} }
// flags matching delegated command in moby cli // flags matching delegated command in moby cli

View File

@ -69,7 +69,8 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return err return err
} }
if opts.format != "" { if opts.format != "" {
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
} }
ctx := cmd.Context() ctx := cmd.Context()

View File

@ -56,7 +56,8 @@ func runLogin(cmd *cobra.Command, args []string) error {
backend := args[0] backend := args[0]
return errors.New("unknown backend type for cloud login: " + backend) return errors.New("unknown backend type for cloud login: " + backend)
} }
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
} }
func cloudLogin(cmd *cobra.Command, backendType string, params interface{}) error { func cloudLogin(cmd *cobra.Command, backendType string, params interface{}) error {

View File

@ -21,5 +21,6 @@ func Command() *cobra.Command {
} }
func runLogout(cmd *cobra.Command, args []string) error { func runLogout(cmd *cobra.Command, args []string) error {
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
} }

View File

@ -51,7 +51,8 @@ func runVersion(cmd *cobra.Command, version string) error {
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info // we don't want to fail on error, there is an error if the engine is not available but it displays client version info
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
if versionResult == nil { if versionResult == nil {
return mobycli.ExecCmd(cmd) mobycli.Exec()
return nil
} }
var s string = string(versionResult) var s string = string(versionResult)
fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+displayedVersion+"\n Version:", 1)) fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+displayedVersion+"\n Version:", 1))

View File

@ -151,7 +151,7 @@ func main() {
// --host and --version should immediately be forwarded to the original cli // --host and --version should immediately be forwarded to the original cli
if opts.Host != "" || opts.Version { if opts.Host != "" || opts.Version {
mobycli.Exec(ctx) mobycli.Exec()
} }
if opts.Config == "" { if opts.Config == "" {

View File

@ -21,10 +21,9 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"strings" "strings"
"github.com/spf13/cobra"
apicontext "github.com/docker/api/context" apicontext "github.com/docker/api/context"
"github.com/docker/api/context/store" "github.com/docker/api/context/store"
) )
@ -43,7 +42,7 @@ func ExecIfDefaultCtxType(ctx context.Context) {
currentCtx, err := s.Get(currentContext) currentCtx, err := s.Get(currentContext)
// Only run original docker command if the current context is not ours. // Only run original docker command if the current context is not ours.
if err != nil || mustDelegateToMoby(currentCtx.Type()) { if err != nil || mustDelegateToMoby(currentCtx.Type()) {
Exec(ctx) Exec()
} }
} }
@ -57,12 +56,32 @@ func mustDelegateToMoby(ctxType string) bool {
} }
// Exec delegates to com.docker.cli if on moby context // Exec delegates to com.docker.cli if on moby context
func Exec(ctx context.Context) { func Exec() {
cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...) cmd := exec.Command(ComDockerCli, os.Args[1:]...)
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
signals := make(chan os.Signal)
childExit := make(chan bool)
signal.Notify(signals) // catch all signals
go func() {
for {
select {
case sig := <-signals:
err := cmd.Process.Signal(sig)
if err != nil {
fmt.Printf("WARNING could not forward signal %s to %s : %s\n", sig.String(), ComDockerCli, err.Error())
}
case <-childExit:
return
}
}
}()
err := cmd.Run()
childExit <- true
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok { if exiterr, ok := err.(*exec.ExitError); ok {
os.Exit(exiterr.ExitCode()) os.Exit(exiterr.ExitCode())
} }
@ -72,14 +91,6 @@ func Exec(ctx context.Context) {
os.Exit(0) os.Exit(0)
} }
// ExecCmd delegates the cli command to com.docker.cli. The error is never
// returned (process will exit with docker classic exit code), the return type
// is to make it easier to use with cobra commands
func ExecCmd(command *cobra.Command) error {
Exec(command.Context())
return nil
}
// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help) // IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
func IsDefaultContextCommand(dockerCommand string) bool { func IsDefaultContextCommand(dockerCommand string) bool {
cmd := exec.Command(ComDockerCli, dockerCommand, "--help") cmd := exec.Command(ComDockerCli, dockerCommand, "--help")

View File

@ -24,6 +24,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/docker/api/cli/mobycli"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -50,9 +52,10 @@ RUN sleep 100`), 0644)).To(Succeed())
_, err := ctx.Exec() _, err := ctx.Exec()
errs <- err errs <- err
}() }()
mobyBuild := mobycli.ComDockerCli + " build --no-cache -t " + imageName
err := WaitFor(time.Second, 10*time.Second, errs, func() bool { err := WaitFor(time.Second, 10*time.Second, errs, func() bool {
out := s.ListProcessesCommand().ExecOrDie() out := s.ListProcessesCommand().ExecOrDie()
return strings.Contains(out, imageName) return strings.Contains(out, mobyBuild)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
log.Println("Killing docker process") log.Println("Killing docker process")
@ -60,7 +63,7 @@ RUN sleep 100`), 0644)).To(Succeed())
close(shutdown) close(shutdown)
err = WaitFor(time.Second, 12*time.Second, nil, func() bool { err = WaitFor(time.Second, 12*time.Second, nil, func() bool {
out := s.ListProcessesCommand().ExecOrDie() out := s.ListProcessesCommand().ExecOrDie()
return !strings.Contains(out, imageName) return !strings.Contains(out, mobyBuild)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })