exec.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/pkg/stdcopy"
  11. "github.com/docker/docker/utils"
  12. "github.com/docker/engine-api/types"
  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. execConfig.Container = name
  35. if len(execConfig.Cmd) == 0 {
  36. return fmt.Errorf("No exec command specified")
  37. }
  38. // Register an instance of Exec in container.
  39. id, err := s.backend.ContainerExecCreate(execConfig)
  40. if err != nil {
  41. logrus.Errorf("Error setting up exec command in container %s: %s", name, utils.GetErrorMessage(err))
  42. return err
  43. }
  44. return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerExecCreateResponse{
  45. ID: id,
  46. })
  47. }
  48. // TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
  49. func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  50. if err := httputils.ParseForm(r); err != nil {
  51. return err
  52. }
  53. version := httputils.VersionFromContext(ctx)
  54. if version.GreaterThan("1.21") {
  55. if err := httputils.CheckForJSON(r); err != nil {
  56. return err
  57. }
  58. }
  59. var (
  60. execName = vars["name"]
  61. stdin, inStream io.ReadCloser
  62. stdout, stderr, outStream io.Writer
  63. )
  64. execStartCheck := &types.ExecStartCheck{}
  65. if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
  66. return err
  67. }
  68. if exists, err := s.backend.ExecExists(execName); !exists {
  69. return err
  70. }
  71. if !execStartCheck.Detach {
  72. var err error
  73. // Setting up the streaming http interface.
  74. inStream, outStream, err = httputils.HijackConnection(w)
  75. if err != nil {
  76. return err
  77. }
  78. defer httputils.CloseStreams(inStream, outStream)
  79. if _, ok := r.Header["Upgrade"]; ok {
  80. 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")
  81. } else {
  82. fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  83. }
  84. stdin = inStream
  85. stdout = outStream
  86. if !execStartCheck.Tty {
  87. stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  88. stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  89. }
  90. } else {
  91. outStream = w
  92. }
  93. // Now run the user process in container.
  94. if err := s.backend.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
  95. if execStartCheck.Detach {
  96. return err
  97. }
  98. logrus.Errorf("Error running exec in container: %v\n", utils.GetErrorMessage(err))
  99. }
  100. return nil
  101. }
  102. func (s *containerRouter) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  103. if err := httputils.ParseForm(r); err != nil {
  104. return err
  105. }
  106. height, err := strconv.Atoi(r.Form.Get("h"))
  107. if err != nil {
  108. return err
  109. }
  110. width, err := strconv.Atoi(r.Form.Get("w"))
  111. if err != nil {
  112. return err
  113. }
  114. return s.backend.ContainerExecResize(vars["name"], height, width)
  115. }