2015-09-23 19:42:08 -04:00
|
|
|
package local
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-04 13:51:48 -07:00
|
|
|
"syscall"
|
2015-07-28 14:35:24 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-16 11:56:26 -07:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2015-07-28 14:35:24 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/daemon"
|
2015-09-17 11:54:14 -07:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-07-28 14:35:24 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2015-09-16 11:56:26 -07:00
|
|
|
"github.com/docker/docker/utils"
|
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-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &daemon.ContainersConfig{
|
2015-09-23 19:42:08 -04:00
|
|
|
All: httputils.BoolValue(r, "all"),
|
|
|
|
Size: httputils.BoolValue(r, "size"),
|
2015-07-28 14:35:24 -04:00
|
|
|
Since: r.Form.Get("since"),
|
|
|
|
Before: r.Form.Get("before"),
|
|
|
|
Filters: r.Form.Get("filters"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpLimit := r.Form.Get("limit"); tmpLimit != "" {
|
|
|
|
limit, err := strconv.Atoi(tmpLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Limit = limit
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
containers, err := s.daemon.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-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersStats(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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
|
|
|
var out io.Writer
|
|
|
|
if !stream {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
out = w
|
|
|
|
} else {
|
|
|
|
out = ioutils.NewWriteFlusher(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeNotifier <-chan bool
|
|
|
|
if notifier, ok := w.(http.CloseNotifier); ok {
|
|
|
|
closeNotifier = notifier.CloseNotify()
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &daemon.ContainerStatsConfig{
|
|
|
|
Stream: stream,
|
|
|
|
OutStream: out,
|
|
|
|
Stop: closeNotifier,
|
2015-09-23 19:42:08 -04:00
|
|
|
Version: httputils.VersionFromContext(ctx),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
return s.daemon.ContainerStats(vars["name"], config)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var since time.Time
|
|
|
|
if r.Form.Get("since") != "" {
|
|
|
|
s, err := strconv.ParseInt(r.Form.Get("since"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
since = time.Unix(s, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeNotifier <-chan bool
|
|
|
|
if notifier, ok := w.(http.CloseNotifier); ok {
|
|
|
|
closeNotifier = notifier.CloseNotify()
|
|
|
|
}
|
|
|
|
|
2015-09-28 13:36:29 -07:00
|
|
|
containerName := vars["name"]
|
|
|
|
|
|
|
|
if !s.daemon.Exists(containerName) {
|
|
|
|
return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
outStream := ioutils.NewWriteFlusher(w)
|
|
|
|
// write an empty chunk of data (this is to ensure that the
|
2015-08-07 23:24:18 +01:00
|
|
|
// HTTP Response is sent immediately, even if the container has
|
2015-07-28 14:35:24 -04:00
|
|
|
// not yet produced any data)
|
|
|
|
outStream.Write(nil)
|
|
|
|
|
|
|
|
logsConfig := &daemon.ContainerLogsConfig{
|
2015-09-23 19:42:08 -04:00
|
|
|
Follow: httputils.BoolValue(r, "follow"),
|
|
|
|
Timestamps: httputils.BoolValue(r, "timestamps"),
|
2015-07-28 14:35:24 -04:00
|
|
|
Since: since,
|
|
|
|
Tail: r.Form.Get("tail"),
|
|
|
|
UseStdout: stdout,
|
|
|
|
UseStderr: stderr,
|
|
|
|
OutStream: outStream,
|
|
|
|
Stop: closeNotifier,
|
|
|
|
}
|
|
|
|
|
2015-09-28 13:36:29 -07:00
|
|
|
if err := s.daemon.ContainerLogs(containerName, logsConfig); err != nil {
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 11:02:56 -04:00
|
|
|
// The client may be expecting all of the data we're sending to
|
|
|
|
// be multiplexed, so send it through OutStream, which will
|
|
|
|
// have been set up to handle that if needed.
|
2015-09-16 11:56:26 -07:00
|
|
|
fmt.Fprintf(logsConfig.OutStream, "Error running logs job: %s\n", utils.GetErrorMessage(err))
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersExport(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-29 13:51:40 -04:00
|
|
|
return s.daemon.ContainerExport(vars["name"], w)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) 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
|
|
|
|
var hostConfig *runconfig.HostConfig
|
|
|
|
if r.Body != nil && (r.ContentLength > 0 || r.ContentLength == -1) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := runconfig.DecodeHostConfig(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hostConfig = c
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerStart(vars["name"], hostConfig); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersStop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
seconds, _ := strconv.Atoi(r.Form.Get("t"))
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerStop(vars["name"], seconds); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersKill(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerKill(name, uint64(sig)); err != nil {
|
2015-09-16 11:56:26 -07:00
|
|
|
theErr, isDerr := err.(errcode.ErrorCoder)
|
|
|
|
isStopped := isDerr && theErr.ErrorCode() == derr.ErrorCodeNotRunning
|
|
|
|
|
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)
|
2015-07-28 14:35:24 -04:00
|
|
|
if version.GreaterThanOrEqualTo("1.20") || !isStopped {
|
|
|
|
return fmt.Errorf("Cannot kill container %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersRestart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout, _ := strconv.Atoi(r.Form.Get("t"))
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerRestart(vars["name"], timeout); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersPause(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerPause(vars["name"]); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersUnpause(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerUnpause(vars["name"]); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersWait(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-29 13:51:40 -04:00
|
|
|
status, err := s.daemon.ContainerWait(vars["name"], -1*time.Second)
|
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, &types.ContainerWaitResponse{
|
2015-07-28 14:35:24 -04:00
|
|
|
StatusCode: status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersChanges(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-29 13:51:40 -04:00
|
|
|
changes, err := s.daemon.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-09-23 19:42:08 -04:00
|
|
|
func (s *router) getContainersTop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
procList, err := s.daemon.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-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainerRename(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerRename(name, newName); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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
|
|
|
|
|
|
|
config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
version := httputils.VersionFromContext(ctx)
|
2015-08-06 08:15:14 +08:00
|
|
|
adjustCPUShares := version.LessThan("1.19")
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2015-09-09 19:23:06 -07:00
|
|
|
ccr, err := s.daemon.ContainerCreate(&daemon.ContainerCreateConfig{
|
|
|
|
Name: name,
|
|
|
|
Config: config,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
AdjustCPUShares: adjustCPUShares,
|
|
|
|
})
|
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-09-23 19:42:08 -04:00
|
|
|
func (s *router) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
|
|
|
config := &daemon.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-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerRm(name, config); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
// Force a 404 for the empty string
|
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "prefix can't be empty") {
|
|
|
|
return fmt.Errorf("no such id: \"\"")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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-09-29 13:51:40 -04:00
|
|
|
return s.daemon.ContainerResize(vars["name"], height, width)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) postContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if !s.daemon.Exists(containerName) {
|
2015-09-17 12:57:57 -07:00
|
|
|
return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
inStream, outStream, err := httputils.HijackConnection(w)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
defer httputils.CloseStreams(inStream, outStream)
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
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{
|
|
|
|
InStream: inStream,
|
|
|
|
OutStream: outStream,
|
2015-09-23 19:42:08 -04: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"),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerAttachWithLogs(containerName, attachWithLogsConfig); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *router) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
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
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if !s.daemon.Exists(containerName) {
|
2015-09-17 12:57:57 -07:00
|
|
|
return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
h := websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
wsAttachWithLogsConfig := &daemon.ContainerWsAttachWithLogsConfig{
|
|
|
|
InStream: ws,
|
|
|
|
OutStream: ws,
|
|
|
|
ErrStream: ws,
|
2015-09-23 19:42:08 -04:00
|
|
|
Logs: httputils.BoolValue(r, "logs"),
|
|
|
|
Stream: httputils.BoolValue(r, "stream"),
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := s.daemon.ContainerWsAttachWithLogs(containerName, wsAttachWithLogsConfig); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
logrus.Errorf("Error attaching websocket: %s", err)
|
|
|
|
}
|
|
|
|
})
|
2015-07-19 21:13:11 +02:00
|
|
|
ws := websocket.Server{Handler: h, Handshake: nil}
|
|
|
|
ws.ServeHTTP(w, r)
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|