2015-11-04 17:38:05 -08:00
|
|
|
package container
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
2016-01-12 08:33:41 +01:00
|
|
|
"encoding/json"
|
2015-07-28 14:35:24 -04:00
|
|
|
"fmt"
|
2016-01-05 16:23:24 -05:00
|
|
|
"io"
|
2015-07-28 14:35:24 -04:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2015-08-04 13:51:48 -07:00
|
|
|
"syscall"
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-03-20 10:07:04 -07:00
|
|
|
"github.com/docker/docker/api"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-01-27 17:09:42 -05:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2017-03-30 20:01:41 -07:00
|
|
|
containerpkg "github.com/docker/docker/container"
|
2016-01-05 16:23:24 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-07-28 14:35:24 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/websocket"
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-27 17:09:42 -05:00
|
|
|
filter, err := filters.FromParam(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-01-27 17:09:42 -05:00
|
|
|
config := &types.ContainerListOptions{
|
2016-11-01 22:01:16 +08:00
|
|
|
All: httputils.BoolValue(r, "all"),
|
|
|
|
Size: httputils.BoolValue(r, "size"),
|
|
|
|
Since: r.Form.Get("since"),
|
|
|
|
Before: r.Form.Get("before"),
|
|
|
|
Filters: filter,
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if tmpLimit := r.Form.Get("limit"); tmpLimit != "" {
|
|
|
|
limit, err := strconv.Atoi(tmpLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Limit = limit
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
containers, err := s.backend.Containers(config)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, containers)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersStats(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
stream := httputils.BoolValueOrDefault(r, "stream", true)
|
2015-07-28 14:35:24 -04:00
|
|
|
if !stream {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
config := &backend.ContainerStatsConfig{
|
2015-07-28 14:35:24 -04:00
|
|
|
Stream: stream,
|
2015-12-19 09:43:10 -05:00
|
|
|
OutStream: w,
|
2015-12-30 18:20:41 +01:00
|
|
|
Version: string(httputils.VersionFromContext(ctx)),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-25 11:33:54 -07:00
|
|
|
return s.backend.ContainerStats(ctx, vars["name"], config)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:32:47 +02:00
|
|
|
// Args are validated before the stream starts because when it starts we're
|
|
|
|
// sending HTTP 200 by writing an empty chunk of data to tell the client that
|
|
|
|
// daemon is going to stream. By sending this initial HTTP 200 we can't report
|
|
|
|
// any error after the stream starts (i.e. container not found, wrong parameters)
|
|
|
|
// with the appropriate status code.
|
2015-09-23 19:42:08 -04:00
|
|
|
stdout, stderr := httputils.BoolValue(r, "stdout"), httputils.BoolValue(r, "stderr")
|
2015-07-28 14:35:24 -04:00
|
|
|
if !(stdout || stderr) {
|
|
|
|
return fmt.Errorf("Bad parameters: you must choose at least one stream")
|
|
|
|
}
|
|
|
|
|
2015-09-28 13:36:29 -07:00
|
|
|
containerName := vars["name"]
|
2017-03-20 10:07:04 -07:00
|
|
|
logsConfig := &types.ContainerLogsOptions{
|
|
|
|
Follow: httputils.BoolValue(r, "follow"),
|
|
|
|
Timestamps: httputils.BoolValue(r, "timestamps"),
|
|
|
|
Since: r.Form.Get("since"),
|
|
|
|
Tail: r.Form.Get("tail"),
|
|
|
|
ShowStdout: stdout,
|
|
|
|
ShowStderr: stderr,
|
|
|
|
Details: httputils.BoolValue(r, "details"),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 10:07:04 -07:00
|
|
|
// doesn't matter what version the client is on, we're using this internally only
|
|
|
|
// also do we need size? i'm thinkin no we don't
|
|
|
|
raw, err := s.backend.ContainerInspect(containerName, false, api.DefaultVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container, ok := raw.(*types.ContainerJSON)
|
|
|
|
if !ok {
|
|
|
|
// %T prints the type. handy!
|
|
|
|
return fmt.Errorf("expected container to be *types.ContainerJSON but got %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := s.backend.ContainerLogs(ctx, containerName, logsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 10:07:04 -07:00
|
|
|
// if has a tty, we're not muxing streams. if it doesn't, we are. simple.
|
|
|
|
// this is the point of no return for writing a response. once we call
|
|
|
|
// WriteLogStream, the response has been started and errors will be
|
|
|
|
// returned in band by WriteLogStream
|
|
|
|
httputils.WriteLogStream(ctx, w, msgs, logsConfig, !container.Config.Tty)
|
2015-07-28 14:35:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersExport(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
return s.backend.ContainerExport(vars["name"], w)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-07-28 14:35:24 -04:00
|
|
|
// If contentLength is -1, we can assumed chunked encoding
|
|
|
|
// or more technically that the length is unknown
|
|
|
|
// https://golang.org/src/pkg/net/http/request.go#L139
|
|
|
|
// net/http otherwise seems to swallow any headers related to chunked encoding
|
|
|
|
// including r.TransferEncoding
|
|
|
|
// allow a nil body for backwards compatibility
|
2016-05-07 18:05:26 +08:00
|
|
|
|
2016-07-06 09:13:59 +02:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
2015-12-18 13:36:17 -05:00
|
|
|
var hostConfig *container.HostConfig
|
2016-05-07 18:05:26 +08:00
|
|
|
// A non-nil json object is at least 7 characters.
|
|
|
|
if r.ContentLength > 7 || r.ContentLength == -1 {
|
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.24") {
|
2016-08-17 01:36:37 +03:00
|
|
|
return validationError{fmt.Errorf("starting container with non-empty request body was deprecated since v1.10 and removed in v1.12")}
|
2016-05-07 18:05:26 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:22:23 -04:00
|
|
|
c, err := s.decoder.DecodeHostConfig(r.Body)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hostConfig = c
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:52:00 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
checkpoint := r.Form.Get("checkpoint")
|
2016-09-19 12:01:16 -04:00
|
|
|
checkpointDir := r.Form.Get("checkpoint-dir")
|
2016-11-30 19:22:07 +01:00
|
|
|
if err := s.backend.ContainerStart(vars["name"], hostConfig, checkpoint, checkpointDir); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-05-12 10:52:00 -04:00
|
|
|
|
2015-07-28 14:35:24 -04:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersStop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:29:05 -07:00
|
|
|
var seconds *int
|
|
|
|
if tmpSeconds := r.Form.Get("t"); tmpSeconds != "" {
|
|
|
|
valSeconds, err := strconv.Atoi(tmpSeconds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
seconds = &valSeconds
|
|
|
|
}
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerStop(vars["name"], seconds); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
type errContainerIsRunning interface {
|
|
|
|
ContainerIsRunning() bool
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersKill(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-04 13:51:48 -07:00
|
|
|
var sig syscall.Signal
|
2015-07-28 14:35:24 -04:00
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
// If we have a signal, look at it. Otherwise, do nothing
|
|
|
|
if sigStr := r.Form.Get("signal"); sigStr != "" {
|
2015-08-04 13:51:48 -07:00
|
|
|
var err error
|
|
|
|
if sig, err = signal.ParseSignal(sigStr); err != nil {
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerKill(name, uint64(sig)); err != nil {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
var isStopped bool
|
|
|
|
if e, ok := err.(errContainerIsRunning); ok {
|
|
|
|
isStopped = !e.ContainerIsRunning()
|
|
|
|
}
|
2015-09-16 11:56:26 -07:00
|
|
|
|
2015-07-28 14:35:24 -04:00
|
|
|
// Return error that's not caused because the container is stopped.
|
|
|
|
// Return error if the container is not running and the api is >= 1.20
|
|
|
|
// to keep backwards compatibility.
|
2015-09-23 19:42:08 -04:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
2016-04-19 16:56:54 +02:00
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.20") || !isStopped {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("Cannot kill container %s: %v", name, err)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersRestart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:29:05 -07:00
|
|
|
var seconds *int
|
|
|
|
if tmpSeconds := r.Form.Get("t"); tmpSeconds != "" {
|
|
|
|
valSeconds, err := strconv.Atoi(tmpSeconds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
seconds = &valSeconds
|
|
|
|
}
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-06-06 20:29:05 -07:00
|
|
|
if err := s.backend.ContainerRestart(vars["name"], seconds); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersPause(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerPause(vars["name"]); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersUnpause(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerUnpause(vars["name"]); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersWait(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2017-03-30 20:01:41 -07:00
|
|
|
// Behavior changed in version 1.30 to handle wait condition and to
|
|
|
|
// return headers immediately.
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
legacyBehavior := versions.LessThan(version, "1.30")
|
|
|
|
|
|
|
|
// The wait condition defaults to "not-running".
|
|
|
|
waitCondition := containerpkg.WaitConditionNotRunning
|
|
|
|
if !legacyBehavior {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch container.WaitCondition(r.Form.Get("condition")) {
|
|
|
|
case container.WaitConditionNextExit:
|
|
|
|
waitCondition = containerpkg.WaitConditionNextExit
|
|
|
|
case container.WaitConditionRemoved:
|
|
|
|
waitCondition = containerpkg.WaitConditionRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: the context should get canceled if the client closes the
|
|
|
|
// connection since this handler has been wrapped by the
|
|
|
|
// router.WithCancel() wrapper.
|
|
|
|
waitC, err := s.backend.ContainerWait(ctx, vars["name"], waitCondition)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:01:41 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if !legacyBehavior {
|
|
|
|
// Write response header immediately.
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if flusher, ok := w.(http.Flusher); ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block on the result of the wait operation.
|
2017-03-30 13:52:40 -07:00
|
|
|
status := <-waitC
|
|
|
|
|
2017-03-30 20:01:41 -07:00
|
|
|
return json.NewEncoder(w).Encode(&container.ContainerWaitOKBody{
|
2017-03-30 13:52:40 -07:00
|
|
|
StatusCode: int64(status.ExitCode()),
|
2015-07-28 14:35:24 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersChanges(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
changes, err := s.backend.ContainerChanges(vars["name"])
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, changes)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) getContainersTop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
procList, err := s.backend.ContainerTop(vars["name"], r.Form.Get("ps_args"))
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, procList)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainerRename(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
|
|
|
newName := r.Form.Get("name")
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerRename(name, newName); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-28 19:19:26 +08:00
|
|
|
func (s *containerRouter) postContainerUpdate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-12 08:33:41 +01:00
|
|
|
var updateConfig container.UpdateConfig
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
if err := decoder.Decode(&updateConfig); err != nil {
|
2015-12-28 19:19:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-12 08:33:41 +01:00
|
|
|
hostConfig := &container.HostConfig{
|
2016-01-04 23:58:20 +08:00
|
|
|
Resources: updateConfig.Resources,
|
|
|
|
RestartPolicy: updateConfig.RestartPolicy,
|
2016-01-12 08:33:41 +01:00
|
|
|
}
|
|
|
|
|
2015-12-28 19:19:26 +08:00
|
|
|
name := vars["name"]
|
2016-11-30 19:22:07 +01:00
|
|
|
resp, err := s.backend.ContainerUpdate(name, hostConfig)
|
2015-12-28 19:19:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:25:14 +02:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, resp)
|
2015-12-28 19:19:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-22 18:06:09 -07:00
|
|
|
|
|
|
|
name := r.Form.Get("name")
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-03-28 14:22:23 -04:00
|
|
|
config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
2016-04-19 16:56:54 +02:00
|
|
|
adjustCPUShares := versions.LessThan(version, "1.19")
|
2015-07-28 14:35:24 -04:00
|
|
|
|
Don't use AutoRemove on older daemons
Docker 1.13 moves the `--rm` flag to the daemon,
through an AutoRemove option in HostConfig.
When using API 1.24 and under, AutoRemove should not be
used, even if the daemon is version 1.13 or above and
"supports" this feature.
This patch fixes a situation where an 1.13 client,
talking to an 1.13 daemon, but using the 1.24 API
version, still set the AutoRemove property.
As a result, both the client _and_ the daemon
were attempting to remove the container, resulting
in an error:
ERRO[0000] error removing container: Error response from daemon:
removal of container ce0976ad22495c7cbe9487752ea32721a282164862db036b2f3377bd07461c3a
is already in progress
In addition, the validation of conflicting options
is moved from `docker run` to `opts.parse()`, so
that conflicting options are also detected when
running `docker create` and `docker start` separately.
To resolve the issue, the `AutoRemove` option is now
always set to `false` both by the client and the
daemon, if API version 1.24 or under is used.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-01-13 01:05:39 +01:00
|
|
|
// When using API 1.24 and under, the client is responsible for removing the container
|
|
|
|
if hostConfig != nil && versions.LessThan(version, "1.25") {
|
|
|
|
hostConfig.AutoRemove = false
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:56:49 +01:00
|
|
|
ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{
|
2016-01-07 16:18:34 -08:00
|
|
|
Name: name,
|
|
|
|
Config: config,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
NetworkingConfig: networkingConfig,
|
|
|
|
AdjustCPUShares: adjustCPUShares,
|
2016-11-30 19:22:07 +01:00
|
|
|
})
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, ccr)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
2015-12-04 12:34:43 -08:00
|
|
|
config := &types.ContainerRmConfig{
|
2015-09-23 19:42:08 -04:00
|
|
|
ForceRemove: httputils.BoolValue(r, "force"),
|
|
|
|
RemoveVolume: httputils.BoolValue(r, "v"),
|
|
|
|
RemoveLink: httputils.BoolValue(r, "link"),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
if err := s.backend.ContainerRm(name, config); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
height, err := strconv.Atoi(r.Form.Get("h"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
width, err := strconv.Atoi(r.Form.Get("w"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
return s.backend.ContainerResize(vars["name"], height, width)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) postContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-01-03 23:03:39 +01:00
|
|
|
err := httputils.ParseForm(r)
|
|
|
|
if err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-17 12:57:57 -07:00
|
|
|
containerName := vars["name"]
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2015-12-16 01:41:46 -05:00
|
|
|
_, upgrade := r.Header["Upgrade"]
|
2016-01-03 23:03:39 +01:00
|
|
|
detachKeys := r.FormValue("detachKeys")
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
hijacker, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("error attaching to container %s, hijack connection missing", containerName)
|
2016-01-05 16:23:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
|
|
|
|
conn, _, err := hijacker.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set raw mode
|
|
|
|
conn.Write([]byte{})
|
|
|
|
|
|
|
|
if upgrade {
|
|
|
|
fmt.Fprintf(conn, "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(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
closer := func() error {
|
|
|
|
httputils.CloseStreams(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(conn, closer), conn, conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
attachConfig := &backend.ContainerAttachConfig{
|
|
|
|
GetStreams: setupStreams,
|
2016-01-03 23:03:39 +01:00
|
|
|
UseStdin: httputils.BoolValue(r, "stdin"),
|
|
|
|
UseStdout: httputils.BoolValue(r, "stdout"),
|
|
|
|
UseStderr: httputils.BoolValue(r, "stderr"),
|
|
|
|
Logs: httputils.BoolValue(r, "logs"),
|
|
|
|
Stream: httputils.BoolValue(r, "stream"),
|
2016-03-23 19:34:47 +08:00
|
|
|
DetachKeys: detachKeys,
|
2016-01-05 16:23:24 -05:00
|
|
|
MuxStreams: true,
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:34:47 +08:00
|
|
|
if err = s.backend.ContainerAttach(containerName, attachConfig); err != nil {
|
|
|
|
logrus.Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
|
|
|
|
// Remember to close stream if error happens
|
|
|
|
conn, _, errHijack := hijacker.Hijack()
|
|
|
|
if errHijack == nil {
|
|
|
|
statusCode := httputils.GetHTTPErrorStatusCode(err)
|
|
|
|
statusText := http.StatusText(statusCode)
|
|
|
|
fmt.Fprintf(conn, "HTTP/1.1 %d %s\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n%s\r\n", statusCode, statusText, err.Error())
|
|
|
|
httputils.CloseStreams(conn)
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("Error Hijacking: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-04 17:38:05 -08:00
|
|
|
func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-17 12:57:57 -07:00
|
|
|
containerName := vars["name"]
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-01-03 23:03:39 +01:00
|
|
|
var err error
|
|
|
|
detachKeys := r.FormValue("detachKeys")
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
done := make(chan struct{})
|
|
|
|
started := make(chan struct{})
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2017-01-25 19:07:27 -08:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
|
|
|
|
wsChan := make(chan *websocket.Conn)
|
|
|
|
h := func(conn *websocket.Conn) {
|
|
|
|
wsChan <- conn
|
|
|
|
<-done
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
srv := websocket.Server{Handler: h, Handshake: nil}
|
|
|
|
go func() {
|
|
|
|
close(started)
|
|
|
|
srv.ServeHTTP(w, r)
|
|
|
|
}()
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
conn := <-wsChan
|
2017-03-13 18:31:48 -07:00
|
|
|
// In case version 1.28 and above, a binary frame will be sent.
|
2017-01-25 19:07:27 -08:00
|
|
|
// See 28176 for details.
|
2017-03-13 18:31:48 -07:00
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.28") {
|
2017-01-25 19:07:27 -08:00
|
|
|
conn.PayloadType = websocket.BinaryFrame
|
|
|
|
}
|
2016-01-05 16:23:24 -05:00
|
|
|
return conn, conn, conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
attachConfig := &backend.ContainerAttachConfig{
|
|
|
|
GetStreams: setupStreams,
|
|
|
|
Logs: httputils.BoolValue(r, "logs"),
|
|
|
|
Stream: httputils.BoolValue(r, "stream"),
|
2016-03-23 19:34:47 +08:00
|
|
|
DetachKeys: detachKeys,
|
2016-01-05 16:23:24 -05:00
|
|
|
UseStdin: true,
|
|
|
|
UseStdout: true,
|
|
|
|
UseStderr: true,
|
|
|
|
MuxStreams: false, // TODO: this should be true since it's a single stream for both stdout and stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.backend.ContainerAttach(containerName, attachConfig)
|
|
|
|
close(done)
|
|
|
|
select {
|
|
|
|
case <-started:
|
|
|
|
logrus.Errorf("Error attaching websocket: %s", err)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
2016-08-23 16:25:43 -07:00
|
|
|
|
|
|
|
func (s *containerRouter) postContainersPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:46:37 -08:00
|
|
|
pruneFilters, err := filters.FromParam(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
2016-08-23 16:25:43 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 12:52:33 -07:00
|
|
|
pruneReport, err := s.backend.ContainersPrune(ctx, pruneFilters)
|
2016-08-23 16:25:43 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
|
|
|
|
}
|