summaryrefslogtreecommitdiff
path: root/cmd/podman
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-04-11 13:09:41 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-04-13 18:43:44 +0000
commit5e03cec7ec83f8ff8b31a89a6180dda203b04d9c (patch)
treeb32484a6c5d15f430fca12f07db974e354636ccc /cmd/podman
parentb8394600d855a88b38f01feeadf5a63e703183cd (diff)
downloadpodman-5e03cec7ec83f8ff8b31a89a6180dda203b04d9c.tar.gz
podman-5e03cec7ec83f8ff8b31a89a6180dda203b04d9c.tar.bz2
podman-5e03cec7ec83f8ff8b31a89a6180dda203b04d9c.zip
Changes to attach to enable per-stream attaching
This allows us to attach to attach to just stdout or stderr or stdin, or any combination of these. Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #608 Approved by: baude
Diffstat (limited to 'cmd/podman')
-rw-r--r--cmd/podman/attach.go9
-rw-r--r--cmd/podman/run.go39
-rw-r--r--cmd/podman/start.go9
-rw-r--r--cmd/podman/utils.go87
4 files changed, 138 insertions, 6 deletions
diff --git a/cmd/podman/attach.go b/cmd/podman/attach.go
index 20c1c306d..4b16d013c 100644
--- a/cmd/podman/attach.go
+++ b/cmd/podman/attach.go
@@ -1,6 +1,8 @@
package main
import (
+ "os"
+
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
"github.com/urfave/cli"
@@ -71,7 +73,12 @@ func attachCmd(c *cli.Context) error {
ProxySignals(ctr)
}
- if err := ctr.Attach(c.Bool("no-stdin"), c.String("detach-keys")); err != nil {
+ inputStream := os.Stdin
+ if c.Bool("no-stdin") {
+ inputStream = nil
+ }
+
+ if err := attachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys")); err != nil {
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
}
diff --git a/cmd/podman/run.go b/cmd/podman/run.go
index 4966316c5..b8d6e0968 100644
--- a/cmd/podman/run.go
+++ b/cmd/podman/run.go
@@ -144,9 +144,42 @@ func runCmd(c *cli.Context) error {
return nil
}
- // TODO: that "false" should probably be linked to -i
- // Handle this when we split streams to allow attaching just stdin/out/err
- attachChan, err := ctr.StartAndAttach(false, c.String("detach-keys"))
+ outputStream := os.Stdout
+ errorStream := os.Stderr
+ inputStream := os.Stdin
+
+ // If -i is not set, clear stdin
+ if !c.Bool("interactive") {
+ inputStream = nil
+ }
+
+ // If attach is set, clear stdin/stdout/stderr and only attach requested
+ if c.IsSet("attach") {
+ outputStream = nil
+ errorStream = nil
+ inputStream = nil
+
+ attachTo := c.StringSlice("attach")
+ for _, stream := range attachTo {
+ switch strings.ToLower(stream) {
+ case "stdout":
+ outputStream = os.Stdout
+ case "stderr":
+ errorStream = os.Stderr
+ case "stdin":
+ inputStream = os.Stdin
+ default:
+ return errors.Wrapf(libpod.ErrInvalidArg, "invalid stream %q for --attach - must be one of stdin, stdout, or stderr", stream)
+ }
+ }
+
+ // If --interactive is set, restore stdin
+ if c.Bool("interactive") {
+ inputStream = os.Stdin
+ }
+ }
+
+ attachChan, err := startAttachCtr(ctr, outputStream, errorStream, inputStream, c.String("detach-keys"))
if err != nil {
// This means the command did not exist
exitCode = 127
diff --git a/cmd/podman/start.go b/cmd/podman/start.go
index 1a57a538b..14b03ac7c 100644
--- a/cmd/podman/start.go
+++ b/cmd/podman/start.go
@@ -95,7 +95,7 @@ func startCmd(c *cli.Context) error {
if c.Bool("interactive") && !ctr.Config().Stdin {
return errors.Errorf("the container was not created with the interactive option")
}
- noStdIn := c.Bool("interactive")
+
tty, err := strconv.ParseBool(ctr.Spec().Annotations["io.kubernetes.cri-o.TTY"])
if err != nil {
return errors.Wrapf(err, "unable to parse annotations in %s", ctr.ID())
@@ -105,7 +105,12 @@ func startCmd(c *cli.Context) error {
// We only get a terminal session if both a tty was specified in the spec and
// -a on the command-line was given.
if attach && tty {
- attachChan, err := ctr.StartAndAttach(noStdIn, c.String("detach-keys"))
+ inputStream := os.Stdin
+ if !c.Bool("interactive") {
+ inputStream = nil
+ }
+
+ attachChan, err := startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.String("detach-keys"))
if err != nil {
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
diff --git a/cmd/podman/utils.go b/cmd/podman/utils.go
index 8b59e1344..502424403 100644
--- a/cmd/podman/utils.go
+++ b/cmd/podman/utils.go
@@ -1,9 +1,18 @@
package main
import (
+ "os"
+ gosignal "os/signal"
+
"github.com/containers/storage"
+ "github.com/docker/docker/pkg/signal"
+ "github.com/docker/docker/pkg/term"
+ "github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
+ "github.com/sirupsen/logrus"
"github.com/urfave/cli"
+ "golang.org/x/crypto/ssh/terminal"
+ "k8s.io/client-go/tools/remotecommand"
)
// Generate a new libpod runtime configured by command line options
@@ -54,3 +63,81 @@ func getRuntime(c *cli.Context) (*libpod.Runtime, error) {
return libpod.NewRuntime(options...)
}
+
+// Attach to a container
+func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) error {
+ resize := make(chan remotecommand.TerminalSize)
+
+ haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
+
+ // Check if we are attached to a terminal. If we are, generate resize
+ // events, and set the terminal to raw mode
+ if haveTerminal {
+ logrus.Debugf("Handling terminal attach")
+
+ resizeTty(resize)
+
+ oldTermState, err := term.SaveState(os.Stdin.Fd())
+ if err != nil {
+ return errors.Wrapf(err, "unable to save terminal state")
+ }
+
+ term.SetRawTerminal(os.Stdin.Fd())
+
+ defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
+ }
+
+ return ctr.Attach(stdout, stderr, stdin, detachKeys, resize)
+}
+
+// Start and attach to a container
+func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string) (<-chan error, error) {
+ resize := make(chan remotecommand.TerminalSize)
+
+ haveTerminal := terminal.IsTerminal(int(os.Stdin.Fd()))
+
+ // Check if we are attached to a terminal. If we are, generate resize
+ // events, and set the terminal to raw mode
+ if haveTerminal && ctr.Spec().Process.Terminal {
+ logrus.Debugf("Handling terminal attach")
+
+ resizeTty(resize)
+
+ oldTermState, err := term.SaveState(os.Stdin.Fd())
+ if err != nil {
+ return nil, errors.Wrapf(err, "unable to save terminal state")
+ }
+
+ term.SetRawTerminal(os.Stdin.Fd())
+
+ defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState)
+ }
+
+ return ctr.StartAndAttach(stdout, stderr, stdin, detachKeys, resize)
+}
+
+// Helper for prepareAttach - set up a goroutine to generate terminal resize events
+func resizeTty(resize chan remotecommand.TerminalSize) {
+ sigchan := make(chan os.Signal, 1)
+ gosignal.Notify(sigchan, signal.SIGWINCH)
+ sendUpdate := func() {
+ winsize, err := term.GetWinsize(os.Stdin.Fd())
+ if err != nil {
+ logrus.Warnf("Could not get terminal size %v", err)
+ return
+ }
+ resize <- remotecommand.TerminalSize{
+ Width: winsize.Width,
+ Height: winsize.Height,
+ }
+ }
+ go func() {
+ defer close(resize)
+ // Update the terminal size immediately without waiting
+ // for a SIGWINCH to get the correct initial size.
+ sendUpdate()
+ for range sigchan {
+ sendUpdate()
+ }
+ }()
+}