diff --git a/api.go b/api.go index c24b969c35..ea7a6f1461 100644 --- a/api.go +++ b/api.go @@ -97,9 +97,9 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) ([ in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ContainerExport(name, out); err != nil { fmt.Fprintf(out, "Error: %s\n", err) @@ -117,9 +117,9 @@ func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, err if viz { in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ImagesViz(out); err != nil { fmt.Fprintf(out, "Error: %s\n", err) @@ -269,9 +269,9 @@ func postImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, er in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if image != "" { //pull registry := r.Form.Get("registry") @@ -317,9 +317,9 @@ func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) ([]by in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ImageInsert(name, url, path, out); err != nil { fmt.Fprintf(out, "Error: %s\n", err) @@ -340,9 +340,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ImagePush(name, registry, out); err != nil { fmt.Fprintln(out, "Error: %s\n", err) @@ -354,9 +354,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) { in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ImageCreateFromFile(in, out); err != nil { fmt.Fprintln(out, "Error: %s\n", err) @@ -487,9 +487,9 @@ func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) ( in, out, err := hijackServer(w) if err != nil { - defer in.Close() return nil, err } + defer in.Close() fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n") if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, in, out); err != nil { diff --git a/commands.go b/commands.go index 9a3bfaab9d..a19a37d5ff 100644 --- a/commands.go +++ b/commands.go @@ -1140,7 +1140,6 @@ func CmdRun(args ...string) error { return err } } - if !config.AttachStdout && !config.AttachStderr { fmt.Println(out.Id) } diff --git a/server.go b/server.go index 5fb4191130..7f91bf0196 100644 --- a/server.go +++ b/server.go @@ -497,67 +497,67 @@ func (srv *Server) ContainerWait(name string) (int, error) { } func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error { - if container := srv.runtime.Get(name); container != nil { - //logs - if logs { - if stdout { - cLog, err := container.ReadLog("stdout") - if err != nil { - Debugf(err.Error()) - } else if _, err := io.Copy(out, cLog); err != nil { - Debugf(err.Error()) - } - } - if stderr { - cLog, err := container.ReadLog("stderr") - if err != nil { - Debugf(err.Error()) - } else if _, err := io.Copy(out, cLog); err != nil { - Debugf(err.Error()) - } - } - } - - //stream - if stream { - if container.State.Ghost { - return fmt.Errorf("Impossible to attach to a ghost container") - } - - var ( - cStdin io.ReadCloser - cStdout, cStderr io.Writer - cStdinCloser io.Closer - ) - - if stdin { - r, w := io.Pipe() - go func() { - defer w.Close() - defer Debugf("Closing buffered stdin pipe") - io.Copy(w, in) - }() - cStdin = r - cStdinCloser = in - } - if stdout { - cStdout = out - } - if stderr { - cStderr = out - } - - <-container.Attach(cStdin, cStdinCloser, cStdout, cStderr) - - // If we are in stdinonce mode, wait for the process to end - // otherwise, simply return - if container.Config.StdinOnce && !container.Config.Tty { - container.Wait() - } - } - } else { + container := srv.runtime.Get(name) + if container == nil { return fmt.Errorf("No such container: %s", name) } + //logs + if logs { + if stdout { + cLog, err := container.ReadLog("stdout") + if err != nil { + Debugf(err.Error()) + } else if _, err := io.Copy(out, cLog); err != nil { + Debugf(err.Error()) + } + } + if stderr { + cLog, err := container.ReadLog("stderr") + if err != nil { + Debugf(err.Error()) + } else if _, err := io.Copy(out, cLog); err != nil { + Debugf(err.Error()) + } + } + } + + //stream + if stream { + if container.State.Ghost { + return fmt.Errorf("Impossible to attach to a ghost container") + } + + var ( + cStdin io.ReadCloser + cStdout, cStderr io.Writer + cStdinCloser io.Closer + ) + + if stdin { + r, w := io.Pipe() + go func() { + defer w.Close() + defer Debugf("Closing buffered stdin pipe") + io.Copy(w, in) + }() + cStdin = r + cStdinCloser = in + } + if stdout { + cStdout = out + } + if stderr { + cStderr = out + } + + <-container.Attach(cStdin, cStdinCloser, cStdout, cStderr) + + // If we are in stdinonce mode, wait for the process to end + // otherwise, simply return + if container.Config.StdinOnce && !container.Config.Tty { + container.Wait() + } + } return nil }