exec.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package local
  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/pkg/stdcopy"
  12. "github.com/docker/docker/runconfig"
  13. "golang.org/x/net/context"
  14. )
  15. func (s *router) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  16. if vars == nil {
  17. return fmt.Errorf("Missing parameter 'id'")
  18. }
  19. eConfig, err := s.daemon.ContainerExecInspect(vars["id"])
  20. if err != nil {
  21. return err
  22. }
  23. return httputils.WriteJSON(w, http.StatusOK, eConfig)
  24. }
  25. func (s *router) postContainerExecCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  26. if err := httputils.ParseForm(r); err != nil {
  27. return err
  28. }
  29. if err := httputils.CheckForJSON(r); err != nil {
  30. return err
  31. }
  32. name := vars["name"]
  33. execConfig := &runconfig.ExecConfig{}
  34. if err := json.NewDecoder(r.Body).Decode(execConfig); err != nil {
  35. return err
  36. }
  37. execConfig.Container = name
  38. if len(execConfig.Cmd) == 0 {
  39. return fmt.Errorf("No exec command specified")
  40. }
  41. // Register an instance of Exec in container.
  42. id, err := s.daemon.ContainerExecCreate(execConfig)
  43. if err != nil {
  44. logrus.Errorf("Error setting up exec command in container %s: %s", name, err)
  45. return err
  46. }
  47. return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerExecCreateResponse{
  48. ID: id,
  49. })
  50. }
  51. // TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
  52. func (s *router) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  53. if err := httputils.ParseForm(r); err != nil {
  54. return err
  55. }
  56. var (
  57. execName = vars["name"]
  58. stdin, inStream io.ReadCloser
  59. stdout, stderr, outStream io.Writer
  60. )
  61. execStartCheck := &types.ExecStartCheck{}
  62. if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
  63. return err
  64. }
  65. if exists, err := s.daemon.ExecExists(execName); !exists {
  66. return err
  67. }
  68. if !execStartCheck.Detach {
  69. var err error
  70. // Setting up the streaming http interface.
  71. inStream, outStream, err = httputils.HijackConnection(w)
  72. if err != nil {
  73. return err
  74. }
  75. defer httputils.CloseStreams(inStream, outStream)
  76. if _, ok := r.Header["Upgrade"]; ok {
  77. 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")
  78. } else {
  79. fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  80. }
  81. stdin = inStream
  82. stdout = outStream
  83. if !execStartCheck.Tty {
  84. stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  85. stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  86. }
  87. } else {
  88. outStream = w
  89. }
  90. // Now run the user process in container.
  91. if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
  92. if execStartCheck.Detach {
  93. return err
  94. }
  95. logrus.Errorf("Error running exec in container: %v\n", err)
  96. }
  97. return nil
  98. }
  99. func (s *router) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  100. if err := httputils.ParseForm(r); err != nil {
  101. return err
  102. }
  103. if vars == nil {
  104. return fmt.Errorf("Missing parameter")
  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.daemon.ContainerExecResize(vars["name"], height, width)
  115. }