Merge pull request #16497 from MHBauer/attach-refactor-new

refactor attach to not use internal data structures
This commit is contained in:
Jess Frazelle 2015-09-23 16:52:39 -07:00
commit ddd0c47412
2 changed files with 19 additions and 10 deletions

View File

@ -444,10 +444,10 @@ func (s *Server) postContainersAttach(ctx context.Context, w http.ResponseWriter
if vars == nil { if vars == nil {
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
containerName := vars["name"]
cont, err := s.daemon.Get(vars["name"]) if !s.daemon.Exists(containerName) {
if err != nil { return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
return err
} }
inStream, outStream, err := hijackServer(w) inStream, outStream, err := hijackServer(w)
@ -472,7 +472,7 @@ func (s *Server) postContainersAttach(ctx context.Context, w http.ResponseWriter
Stream: boolValue(r, "stream"), Stream: boolValue(r, "stream"),
} }
if err := s.daemon.ContainerAttachWithLogs(cont, attachWithLogsConfig); err != nil { if err := s.daemon.ContainerAttachWithLogs(containerName, attachWithLogsConfig); err != nil {
fmt.Fprintf(outStream, "Error attaching: %s\n", err) fmt.Fprintf(outStream, "Error attaching: %s\n", err)
} }
@ -486,10 +486,10 @@ func (s *Server) wsContainersAttach(ctx context.Context, w http.ResponseWriter,
if vars == nil { if vars == nil {
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
containerName := vars["name"]
cont, err := s.daemon.Get(vars["name"]) if !s.daemon.Exists(containerName) {
if err != nil { return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
return err
} }
h := websocket.Handler(func(ws *websocket.Conn) { h := websocket.Handler(func(ws *websocket.Conn) {
@ -503,7 +503,7 @@ func (s *Server) wsContainersAttach(ctx context.Context, w http.ResponseWriter,
Stream: boolValue(r, "stream"), Stream: boolValue(r, "stream"),
} }
if err := s.daemon.ContainerWsAttachWithLogs(cont, wsAttachWithLogsConfig); err != nil { if err := s.daemon.ContainerWsAttachWithLogs(containerName, wsAttachWithLogsConfig); err != nil {
logrus.Errorf("Error attaching websocket: %s", err) logrus.Errorf("Error attaching websocket: %s", err)
} }
}) })

View File

@ -15,7 +15,12 @@ type ContainerAttachWithLogsConfig struct {
} }
// 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(container *Container, c *ContainerAttachWithLogsConfig) error { func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
container, err := daemon.Get(prefixOrName)
if err != nil {
return err
}
var errStream io.Writer var errStream io.Writer
if !container.Config.Tty { if !container.Config.Tty {
@ -50,6 +55,10 @@ type ContainerWsAttachWithLogsConfig struct {
} }
// ContainerWsAttachWithLogs websocket connection // ContainerWsAttachWithLogs websocket connection
func (daemon *Daemon) ContainerWsAttachWithLogs(container *Container, c *ContainerWsAttachWithLogsConfig) error { func (daemon *Daemon) ContainerWsAttachWithLogs(prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
container, err := daemon.Get(prefixOrName)
if err != nil {
return err
}
return container.attachWithLogs(c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream) return container.attachWithLogs(c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream)
} }