exec.go 4.1 KB

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