exec.go 3.9 KB

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