diff options
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/container_api.go | 35 | ||||
-rw-r--r-- | libpod/container_attach.go | 18 |
2 files changed, 42 insertions, 11 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go index 2c5ed9dbe..38c3faeef 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -4,17 +4,20 @@ import ( "encoding/json" "io/ioutil" "os" + gosignal "os/signal" "path/filepath" "strconv" "time" "github.com/docker/docker/daemon/caps" + "github.com/docker/docker/pkg/signal" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/term" "github.com/pkg/errors" "github.com/projectatomic/libpod/libpod/driver" "github.com/projectatomic/libpod/pkg/inspect" "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh/terminal" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/remotecommand" ) @@ -345,6 +348,31 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e return waitErr } +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() + } + }() +} + // Attach attaches to a container // Returns fully qualified URL of streaming server for the container func (c *Container) Attach(noStdin bool, keys string, attached chan<- bool) error { @@ -373,8 +401,11 @@ func (c *Container) Attach(noStdin bool, keys string, attached chan<- bool) erro } resize := make(chan remotecommand.TerminalSize) - defer close(resize) - + if terminal.IsTerminal(int(os.Stdin.Fd())) { + resizeTty(resize) + } else { + defer close(resize) + } err = c.attachContainerSocket(resize, noStdin, detachKeys, attached) return err } diff --git a/libpod/container_attach.go b/libpod/container_attach.go index 86bf7ee14..25e8bcdac 100644 --- a/libpod/container_attach.go +++ b/libpod/container_attach.go @@ -56,17 +56,17 @@ func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSi term.SetRawTerminal(inputStream.Fd()) } - controlPath := filepath.Join(c.bundlePath(), "ctl") - controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0) - if err != nil { - return errors.Wrapf(err, "failed to open container ctl file") - } - defer controlFile.Close() - kubecontainer.HandleResizing(resize, func(size remotecommand.TerminalSize) { - logrus.Debugf("Received a resize event: %+v", size) - _, err := fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width) + controlPath := filepath.Join(c.bundlePath(), "ctl") + controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0) if err != nil { + logrus.Debugf("Could not open ctl file: %v", err) + return + } + defer controlFile.Close() + + logrus.Debugf("Received a resize event: %+v", size) + if _, err = fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width); err != nil { logrus.Warnf("Failed to write to control file to resize terminal: %v", err) } }) |