exec.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.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")
  80. } else {
  81. fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  82. }
  83. stdin = inStream
  84. stdout = outStream
  85. if !execStartCheck.Tty {
  86. stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  87. stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  88. }
  89. }
  90. // Now run the user process in container.
  91. // Maybe we should we pass ctx here if we're not detaching?
  92. if err := s.backend.ContainerExecStart(context.Background(), execName, stdin, stdout, stderr); err != nil {
  93. if execStartCheck.Detach {
  94. return err
  95. }
  96. stdout.Write([]byte(err.Error() + "\r\n"))
  97. logrus.Errorf("Error running exec in container: %v", err)
  98. }
  99. return nil
  100. }
  101. func (s *containerRouter) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  102. if err := httputils.ParseForm(r); err != nil {
  103. return err
  104. }
  105. height, err := strconv.Atoi(r.Form.Get("h"))
  106. if err != nil {
  107. return err
  108. }
  109. width, err := strconv.Atoi(r.Form.Get("w"))
  110. if err != nil {
  111. return err
  112. }
  113. return s.backend.ContainerExecResize(vars["name"], height, width)
  114. }