Only set the terminal in raw mode for commands which need it

The raw mode is actually only needed when you attach to a container.
Having it enabled all the time can be a pain, e.g: if docker crashes
your terminal will end up in a broken state.

Since we are currently missing a real API for the docker daemon to
negotiate this kind of options, this changeset actually enable the raw
mode on the login (because it outputs a password), run and attach
commands.

This "optional raw mode" is implemented by passing a more complicated
interface than io.Writer as the stdout argument of each command. This
interface (DockerConn) exposes a method which allows the command to set
the terminal in raw mode or not.

Finally, the code added by this changeset will be deprecated by a real
API for the docker daemon.
This commit is contained in:
Louis Opter
2013-04-08 15:58:09 -07:00
parent 4e5001b46a
commit 7d0ab3858e
4 changed files with 238 additions and 56 deletions
+74 -22
View File
@@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/rcli"
"github.com/dotcloud/docker/term"
@@ -56,30 +57,82 @@ func daemon() error {
return rcli.ListenAndServe("tcp", "127.0.0.1:4242", service)
}
func runCommand(args []string) error {
var oldState *term.State
var err error
if term.IsTerminal(int(os.Stdin.Fd())) && os.Getenv("NORAW") == "" {
oldState, err = term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return err
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
term.Restore(int(os.Stdin.Fd()), oldState)
log.Printf("\nSIGINT received\n")
os.Exit(0)
}
}()
func setRawTerminal() (*term.State, error) {
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return nil, err
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
term.Restore(int(os.Stdin.Fd()), oldState)
log.Printf("\nSIGINT received\n")
os.Exit(0)
}
}()
return oldState, err
}
func restoreTerminal(state *term.State) {
term.Restore(int(os.Stdin.Fd()), state)
}
type DockerLocalConn struct {
file *os.File
savedState *term.State
}
func newDockerLocalConn(output *os.File) *DockerLocalConn {
return &DockerLocalConn{file: output}
}
func (c *DockerLocalConn) Read(b []byte) (int, error) { return c.file.Read(b) }
func (c *DockerLocalConn) Write(b []byte) (int, error) { return c.file.Write(b) }
func (c *DockerLocalConn) Close() error {
if c.savedState != nil {
restoreTerminal(c.savedState)
c.savedState = nil
}
return c.file.Close()
}
func (c *DockerLocalConn) CloseWrite() error { return nil }
func (c *DockerLocalConn) CloseRead() error { return nil }
func (c *DockerLocalConn) GetOptions() *rcli.DockerConnOptions { return nil }
func (c *DockerLocalConn) SetOptionRawTerminal() {
if state, err := setRawTerminal(); err != nil {
fmt.Fprintf(
os.Stderr,
"Can't set the terminal in raw mode: %v",
err.Error(),
)
} else {
c.savedState = state
}
}
func runCommand(args []string) error {
// FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
// CloseWrite(), which we need to cleanly signal that stdin is closed without
// closing the connection.
// See http://code.google.com/p/go/issues/detail?id=3345
if conn, err := rcli.Call("tcp", "127.0.0.1:4242", args...); err == nil {
options := conn.GetOptions()
if options.RawTerminal &&
term.IsTerminal(int(os.Stdin.Fd())) &&
os.Getenv("NORAW") == "" {
if oldState, err := setRawTerminal(); err != nil {
return err
} else {
defer restoreTerminal(oldState)
}
}
receiveStdout := docker.Go(func() error {
_, err := io.Copy(os.Stdout, conn)
return err
@@ -104,12 +157,11 @@ func runCommand(args []string) error {
if err != nil {
return err
}
if err := rcli.LocalCall(service, os.Stdin, os.Stdout, args...); err != nil {
dockerConn := newDockerLocalConn(os.Stdout)
defer dockerConn.Close()
if err := rcli.LocalCall(service, os.Stdin, dockerConn, args...); err != nil {
return err
}
}
if oldState != nil {
term.Restore(int(os.Stdin.Fd()), oldState)
}
return nil
}