exec.go 3.6 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/pkg/stdcopy"
  11. "github.com/docker/engine-api/types"
  12. "golang.org/x/net/context"
  13. )
  14. func (s *containerRouter) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  15. eConfig, err := s.backend.ContainerExecInspect(vars["id"])
  16. if err != nil {
  17. return err
  18. }
  19. return httputils.WriteJSON(w, http.StatusOK, eConfig)
  20. }
  21. func (s *containerRouter) postContainerExecCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  22. if err := httputils.ParseForm(r); err != nil {
  23. return err
  24. }
  25. if err := httputils.CheckForJSON(r); err != nil {
  26. return err
  27. }
  28. name := vars["name"]
  29. execConfig := &types.ExecConfig{}
  30. if err := json.NewDecoder(r.Body).Decode(execConfig); err != nil {
  31. return err
  32. }
  33. execConfig.Container = name
  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(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.ContainerExecCreateResponse{
  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 version.GreaterThan("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. if err := s.backend.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
  92. if execStartCheck.Detach {
  93. return err
  94. }
  95. stdout.Write([]byte(err.Error()))
  96. logrus.Errorf("Error running exec in container: %v\n", err)
  97. return 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. }