Although having a request ID available throughout the codebase is very valuable, the impact of requiring a Context as an argument to every function in the codepath of an API request, is too significant and was not properly understood at the time of the review. Furthermore, mixing API-layer code with non-API-layer code makes the latter usable only by API-layer code (one that has a notion of Context). This reverts commit de4164043546d2b9ee3bf323dbc41f4979c84480, reversing changes made to 7daeecd42d7bb112bfe01532c8c9a962bb0c7967. Signed-off-by: Tibor Vass <tibor@docker.com> Conflicts: api/server/container.go builder/internals.go daemon/container_unix.go daemon/create.go Upstream-commit: b08f071e18043abe8ce15f56826d38dd26bedb78 Component: engine
128 lines
3.3 KiB
Go
128 lines
3.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/context"
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
"github.com/docker/docker/runconfig"
|
|
)
|
|
|
|
func (s *Server) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if vars == nil {
|
|
return fmt.Errorf("Missing parameter 'id'")
|
|
}
|
|
|
|
eConfig, err := s.daemon.ContainerExecInspect(vars["id"])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return writeJSON(w, http.StatusOK, eConfig)
|
|
}
|
|
|
|
func (s *Server) postContainerExecCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := parseForm(r); err != nil {
|
|
return err
|
|
}
|
|
if err := checkForJSON(r); err != nil {
|
|
return err
|
|
}
|
|
name := vars["name"]
|
|
|
|
execConfig := &runconfig.ExecConfig{}
|
|
if err := json.NewDecoder(r.Body).Decode(execConfig); err != nil {
|
|
return err
|
|
}
|
|
execConfig.Container = name
|
|
|
|
if len(execConfig.Cmd) == 0 {
|
|
return fmt.Errorf("No exec command specified")
|
|
}
|
|
|
|
// Register an instance of Exec in container.
|
|
id, err := s.daemon.ContainerExecCreate(execConfig)
|
|
if err != nil {
|
|
logrus.Errorf("Error setting up exec command in container %s: %s", name, err)
|
|
return err
|
|
}
|
|
|
|
return writeJSON(w, http.StatusCreated, &types.ContainerExecCreateResponse{
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
|
|
func (s *Server) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := parseForm(r); err != nil {
|
|
return err
|
|
}
|
|
var (
|
|
execName = vars["name"]
|
|
stdin, inStream io.ReadCloser
|
|
stdout, stderr, outStream io.Writer
|
|
)
|
|
|
|
execStartCheck := &types.ExecStartCheck{}
|
|
if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !execStartCheck.Detach {
|
|
var err error
|
|
// Setting up the streaming http interface.
|
|
inStream, outStream, err = hijackServer(w)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer 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")
|
|
}
|
|
|
|
stdin = inStream
|
|
stdout = outStream
|
|
if !execStartCheck.Tty {
|
|
stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
|
|
stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
|
}
|
|
} else {
|
|
outStream = w
|
|
}
|
|
|
|
// Now run the user process in container.
|
|
if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
|
|
fmt.Fprintf(outStream, "Error running exec in container: %v\n", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := parseForm(r); err != nil {
|
|
return err
|
|
}
|
|
if vars == nil {
|
|
return fmt.Errorf("Missing parameter")
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
return s.daemon.ContainerExecResize(vars["name"], height, width)
|
|
}
|