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

Merge pull request #18715 from calavera/remove_is_paused_from_interface

Remove `IsPaused` from backend interface.
This commit is contained in:
Arnaud Porterie 2015-12-28 11:25:11 -08:00
commit baee7ae045
4 changed files with 54 additions and 37 deletions

View file

@ -45,7 +45,6 @@ type stateBackend interface {
ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error) ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error)
ContainerWait(name string, timeout time.Duration) (int, error) ContainerWait(name string, timeout time.Duration) (int, error)
Exists(id string) bool Exists(id string) bool
IsPaused(id string) bool
} }
// monitorBackend includes functions to implement to provide containers monitoring functionality. // monitorBackend includes functions to implement to provide containers monitoring functionality.

View file

@ -425,29 +425,11 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo
} }
containerName := vars["name"] containerName := vars["name"]
if !s.backend.Exists(containerName) { _, upgrade := r.Header["Upgrade"]
return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
}
if s.backend.IsPaused(containerName) {
return derr.ErrorCodePausedContainer.WithArgs(containerName)
}
inStream, outStream, err := httputils.HijackConnection(w)
if err != nil {
return err
}
defer httputils.CloseStreams(inStream, outStream)
if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
attachWithLogsConfig := &daemon.ContainerAttachWithLogsConfig{ attachWithLogsConfig := &daemon.ContainerAttachWithLogsConfig{
InStream: inStream, Hijacker: w.(http.Hijacker),
OutStream: outStream, Upgrade: upgrade,
UseStdin: httputils.BoolValue(r, "stdin"), UseStdin: httputils.BoolValue(r, "stdin"),
UseStdout: httputils.BoolValue(r, "stdout"), UseStdout: httputils.BoolValue(r, "stdout"),
UseStderr: httputils.BoolValue(r, "stderr"), UseStderr: httputils.BoolValue(r, "stderr"),
@ -455,11 +437,7 @@ func (s *containerRouter) postContainersAttach(ctx context.Context, w http.Respo
Stream: httputils.BoolValue(r, "stream"), Stream: httputils.BoolValue(r, "stream"),
} }
if err := s.backend.ContainerAttachWithLogs(containerName, attachWithLogsConfig); err != nil { return s.backend.ContainerAttachWithLogs(containerName, attachWithLogsConfig)
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
} }
func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -1,53 +1,84 @@
package daemon package daemon
import ( import (
"fmt"
"io" "io"
"net/http"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/container" "github.com/docker/docker/container"
"github.com/docker/docker/daemon/logger" "github.com/docker/docker/daemon/logger"
derr "github.com/docker/docker/errors"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
) )
// ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs. // ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs.
type ContainerAttachWithLogsConfig struct { type ContainerAttachWithLogsConfig struct {
InStream io.ReadCloser Hijacker http.Hijacker
OutStream io.Writer Upgrade bool
UseStdin, UseStdout, UseStderr bool UseStdin bool
Logs, Stream bool UseStdout bool
UseStderr bool
Logs bool
Stream bool
} }
// ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig. // ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error { func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
if c.Hijacker == nil {
return derr.ErrorCodeNoHijackConnection.WithArgs(prefixOrName)
}
container, err := daemon.GetContainer(prefixOrName) container, err := daemon.GetContainer(prefixOrName)
if err != nil {
return derr.ErrorCodeNoSuchContainer.WithArgs(prefixOrName)
}
if container.IsPaused() {
return derr.ErrorCodePausedContainer.WithArgs(prefixOrName)
}
conn, _, err := c.Hijacker.Hijack()
if err != nil { if err != nil {
return err return err
} }
defer conn.Close()
// Flush the options to make sure the client sets the raw mode
conn.Write([]byte{})
inStream := conn.(io.ReadCloser)
outStream := conn.(io.Writer)
if c.Upgrade {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
var errStream io.Writer var errStream io.Writer
if !container.Config.Tty { if !container.Config.Tty {
errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr) errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout) outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} else { } else {
errStream = c.OutStream errStream = outStream
} }
var stdin io.ReadCloser var stdin io.ReadCloser
var stdout, stderr io.Writer var stdout, stderr io.Writer
if c.UseStdin { if c.UseStdin {
stdin = c.InStream stdin = inStream
} }
if c.UseStdout { if c.UseStdout {
stdout = c.OutStream stdout = outStream
} }
if c.UseStderr { if c.UseStderr {
stderr = errStream stderr = errStream
} }
return daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream) if err := daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
}
return nil
} }
// ContainerWsAttachWithLogsConfig attach with websockets, since all // ContainerWsAttachWithLogsConfig attach with websockets, since all

View file

@ -33,4 +33,13 @@ var (
Description: "Docker's networking stack is disabled for this platform", Description: "Docker's networking stack is disabled for this platform",
HTTPStatusCode: http.StatusNotFound, HTTPStatusCode: http.StatusNotFound,
}) })
// ErrorCodeNoHijackConnection is generated when a request tries to attach to a container
// but the connection to hijack is not provided.
ErrorCodeNoHijackConnection = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "HIJACK_CONNECTION_MISSING",
Message: "error attaching to container %s, hijack connection missing",
Description: "The caller didn't provide a connection to hijack",
HTTPStatusCode: http.StatusBadRequest,
})
) )