exec.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package container
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "github.com/docker/docker/api/server/httputils"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/versions"
  11. "github.com/docker/docker/pkg/stdcopy"
  12. "github.com/sirupsen/logrus"
  13. "golang.org/x/net/context"
  14. )
  15. func (s *containerRouter) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  16. eConfig, err := s.backend.ContainerExecInspect(vars["id"])
  17. if err != nil {
  18. return err
  19. }
  20. return httputils.WriteJSON(w, http.StatusOK, eConfig)
  21. }
  22. type execCommandError struct{}
  23. func (execCommandError) Error() string {
  24. return "No exec command specified"
  25. }
  26. func (execCommandError) InvalidParameter() {}
  27. func (s *containerRouter) postContainerExecCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  28. if err := httputils.ParseForm(r); err != nil {
  29. return err
  30. }
  31. if err := httputils.CheckForJSON(r); err != nil {
  32. return err
  33. }
  34. name := vars["name"]
  35. execConfig := &types.ExecConfig{}
  36. if err := json.NewDecoder(r.Body).Decode(execConfig); err != nil {
  37. return err
  38. }
  39. if len(execConfig.Cmd) == 0 {
  40. return execCommandError{}
  41. }
  42. // Register an instance of Exec in container.
  43. id, err := s.backend.ContainerExecCreate(name, execConfig)
  44. if err != nil {
  45. logrus.Errorf("Error setting up exec command in container %s: %v", name, err)
  46. return err
  47. }
  48. return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{
  49. ID: id,
  50. })
  51. }
  52. // TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
  53. func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  54. if err := httputils.ParseForm(r); err != nil {
  55. return err
  56. }
  57. version := httputils.VersionFromContext(ctx)
  58. if versions.GreaterThan(version, "1.21") {
  59. if err := httputils.CheckForJSON(r); err != nil {
  60. return err
  61. }
  62. }
  63. var (
  64. execName = vars["name"]
  65. stdin, inStream io.ReadCloser
  66. stdout, stderr, outStream io.Writer
  67. )
  68. execStartCheck := &types.ExecStartCheck{}
  69. if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
  70. return err
  71. }
  72. if exists, err := s.backend.ExecExists(execName); !exists {
  73. return err
  74. }
  75. if !execStartCheck.Detach {
  76. var err error
  77. // Setting up the streaming http interface.
  78. inStream, outStream, err = httputils.HijackConnection(w)
  79. if err != nil {
  80. return err
  81. }
  82. defer httputils.CloseStreams(inStream, outStream)
  83. if _, ok := r.Header["Upgrade"]; ok {
  84. fmt.Fprint(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n")
  85. } else {
  86. fmt.Fprint(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n")
  87. }
  88. // copy headers that were removed as part of hijack
  89. if err := w.Header().WriteSubset(outStream, nil); err != nil {
  90. return err
  91. }
  92. fmt.Fprint(outStream, "\r\n")
  93. stdin = inStream
  94. stdout = outStream
  95. if !execStartCheck.Tty {
  96. stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  97. stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  98. }
  99. }
  100. // Now run the user process in container.
  101. // Maybe we should we pass ctx here if we're not detaching?
  102. if err := s.backend.ContainerExecStart(context.Background(), execName, stdin, stdout, stderr); err != nil {
  103. if execStartCheck.Detach {
  104. return err
  105. }
  106. stdout.Write([]byte(err.Error() + "\r\n"))
  107. logrus.Errorf("Error running exec in container: %v", err)
  108. }
  109. return nil
  110. }
  111. func (s *containerRouter) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  112. if err := httputils.ParseForm(r); err != nil {
  113. return err
  114. }
  115. height, err := strconv.Atoi(r.Form.Get("h"))
  116. if err != nil {
  117. return validationError{err}
  118. }
  119. width, err := strconv.Atoi(r.Form.Get("w"))
  120. if err != nil {
  121. return validationError{err}
  122. }
  123. return s.backend.ContainerExecResize(vars["name"], height, width)
  124. }