1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move holdHijackConnection to the container package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-08-29 13:36:29 -04:00
parent 31d8c27e70
commit 6af6a3a538
9 changed files with 53 additions and 43 deletions

View file

@ -57,31 +57,12 @@ func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
return cli.configFile
}
func (cli *DockerCli) setRawTerminal() error {
if err := cli.in.setRawTerminal(); err != nil {
return err
}
return cli.out.setRawTerminal()
}
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
cli.in.restoreTerminal()
cli.out.restoreTerminal()
// WARNING: DO NOT REMOVE THE OS CHECK !!!
// For some reason this Close call blocks on darwin..
// As the client exists right after, simply discard the close
// until we find a better solution.
if in != nil && runtime.GOOS != "darwin" {
return in.Close()
}
return nil
}
// Initialize the dockerCli runs initialization that must happen after command
// line flags are parsed.
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) (err error) {
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
cli.configFile = LoadDefaultConfigFile(cli.err)
var err error
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
if err != nil {
return err

View file

@ -110,7 +110,7 @@ func runAttach(dockerCli *client.DockerCli, opts *attachOptions) error {
logrus.Debugf("Error monitoring TTY size: %s", err)
}
}
if err := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil {
if err := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil {
return err
}

View file

@ -10,9 +10,8 @@ import (
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cli"
"github.com/docker/docker/pkg/promise"
apiclient "github.com/docker/docker/client"
"github.com/docker/engine-api/types"
"github.com/docker/docker/pkg/promise"
"github.com/spf13/cobra"
)
@ -127,7 +126,7 @@ func runExec(dockerCli *client.DockerCli, opts *execOptions, container string, e
}
defer resp.Close()
errCh = promise.Go(func() error {
return dockerCli.HoldHijackedConnection(ctx, execConfig.Tty, in, out, stderr, resp)
return holdHijackedConnection(ctx, dockerCli, execConfig.Tty, in, out, stderr, resp)
})
if execConfig.Tty && dockerCli.In().IsTerminal() {

View file

@ -1,30 +1,36 @@
package client
package container
import (
"io"
"runtime"
"sync"
"golang.org/x/net/context"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"golang.org/x/net/context"
)
// HoldHijackedConnection handles copying input to and output from streams to the
type streams interface {
In() *client.InStream
Out() *client.OutStream
}
// holdHijackedConnection handles copying input to and output from streams to the
// connection
func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
func holdHijackedConnection(ctx context.Context, streams streams, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
var (
err error
restoreOnce sync.Once
)
if inputStream != nil && tty {
if err := cli.setRawTerminal(); err != nil {
if err := setRawTerminal(streams); err != nil {
return err
}
defer func() {
restoreOnce.Do(func() {
cli.restoreTerminal(inputStream)
restoreTerminal(streams, inputStream)
})
}()
}
@ -39,7 +45,7 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu
// so any following print messages will be in normal type.
if inputStream != nil {
restoreOnce.Do(func() {
cli.restoreTerminal(inputStream)
restoreTerminal(streams, inputStream)
})
}
} else {
@ -59,7 +65,7 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu
// so any following print messages will be in normal type.
if tty {
restoreOnce.Do(func() {
cli.restoreTerminal(inputStream)
restoreTerminal(streams, inputStream)
})
}
logrus.Debug("[hijack] End of stdin")
@ -93,3 +99,23 @@ func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inpu
return nil
}
func setRawTerminal(streams streams) error {
if err := streams.In().SetRawTerminal(); err != nil {
return err
}
return streams.Out().SetRawTerminal()
}
func restoreTerminal(streams streams, in io.Closer) error {
streams.In().RestoreTerminal()
streams.Out().RestoreTerminal()
// WARNING: DO NOT REMOVE THE OS CHECK !!!
// For some reason this Close call blocks on darwin..
// As the client exists right after, simply discard the close
// until we find a better solution.
if in != nil && runtime.GOOS != "darwin" {
return in.Close()
}
return nil
}

View file

@ -203,7 +203,7 @@ func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions,
defer resp.Close()
errCh = promise.Go(func() error {
errHijack := dockerCli.HoldHijackedConnection(ctx, config.Tty, in, out, cerr, resp)
errHijack := holdHijackedConnection(ctx, dockerCli, config.Tty, in, out, cerr, resp)
if errHijack == nil {
return errAttach
}

View file

@ -95,7 +95,7 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error {
}
defer resp.Close()
cErr := promise.Go(func() error {
errHijack := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp)
errHijack := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp)
if errHijack == nil {
return errAttach
}

View file

@ -9,13 +9,13 @@ import (
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/types"
apiclient "github.com/docker/docker/client"
"github.com/docker/docker/pkg/signal"
apiclient "github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
// ResizeTtyTo resizes tty to specific height and width
// resizeTtyTo resizes tty to specific height and width
func resizeTtyTo(ctx context.Context, client apiclient.ContainerAPIClient, id string, height, width int, isExec bool) {
if height == 0 && width == 0 {
return

View file

@ -36,7 +36,8 @@ func (i *InStream) IsTerminal() bool {
return i.isTerminal
}
func (i *InStream) setRawTerminal() (err error) {
// SetRawTerminal sets raw mode on the input terminal
func (i *InStream) SetRawTerminal() (err error) {
if os.Getenv("NORAW") != "" || !i.isTerminal {
return nil
}
@ -44,7 +45,8 @@ func (i *InStream) setRawTerminal() (err error) {
return err
}
func (i *InStream) restoreTerminal() {
// RestoreTerminal restores normal mode to the terminal
func (i *InStream) RestoreTerminal() {
if i.state != nil {
term.RestoreTerminal(i.fd, i.state)
}

View file

@ -31,7 +31,8 @@ func (o *OutStream) IsTerminal() bool {
return o.isTerminal
}
func (o *OutStream) setRawTerminal() (err error) {
// SetRawTerminal sets raw mode on the output terminal
func (o *OutStream) SetRawTerminal() (err error) {
if os.Getenv("NORAW") != "" || !o.isTerminal {
return nil
}
@ -39,7 +40,8 @@ func (o *OutStream) setRawTerminal() (err error) {
return err
}
func (o *OutStream) restoreTerminal() {
// RestoreTerminal restores normal mode to the terminal
func (o *OutStream) RestoreTerminal() {
if o.state != nil {
term.RestoreTerminal(o.fd, o.state)
}