system_routes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package system
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/api"
  9. "github.com/docker/docker/api/errors"
  10. "github.com/docker/docker/api/server/httputils"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/events"
  13. "github.com/docker/docker/api/types/filters"
  14. timetypes "github.com/docker/docker/api/types/time"
  15. "github.com/docker/docker/api/types/versions"
  16. "github.com/docker/docker/pkg/ioutils"
  17. "golang.org/x/net/context"
  18. )
  19. func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  20. w.WriteHeader(http.StatusOK)
  21. return nil
  22. }
  23. func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  24. _, err := w.Write([]byte{'O', 'K'})
  25. return err
  26. }
  27. func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  28. info, err := s.backend.SystemInfo()
  29. if err != nil {
  30. return err
  31. }
  32. if s.clusterProvider != nil {
  33. info.Swarm = s.clusterProvider.Info()
  34. }
  35. if versions.LessThan("1.25", httputils.VersionFromContext(ctx)) {
  36. // TODO: handle this conversion in engine-api
  37. type oldInfo struct {
  38. *types.Info
  39. ExecutionDriver string
  40. }
  41. return httputils.WriteJSON(w, http.StatusOK, &oldInfo{Info: info, ExecutionDriver: "<not supported>"})
  42. }
  43. return httputils.WriteJSON(w, http.StatusOK, info)
  44. }
  45. func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  46. info := s.backend.SystemVersion()
  47. info.APIVersion = api.DefaultVersion
  48. return httputils.WriteJSON(w, http.StatusOK, info)
  49. }
  50. func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  51. if err := httputils.ParseForm(r); err != nil {
  52. return err
  53. }
  54. since, err := eventTime(r.Form.Get("since"))
  55. if err != nil {
  56. return err
  57. }
  58. until, err := eventTime(r.Form.Get("until"))
  59. if err != nil {
  60. return err
  61. }
  62. var (
  63. timeout <-chan time.Time
  64. onlyPastEvents bool
  65. )
  66. if !until.IsZero() {
  67. if until.Before(since) {
  68. return errors.NewBadRequestError(fmt.Errorf("`since` time (%s) cannot be after `until` time (%s)", r.Form.Get("since"), r.Form.Get("until")))
  69. }
  70. now := time.Now()
  71. onlyPastEvents = until.Before(now)
  72. if !onlyPastEvents {
  73. dur := until.Sub(now)
  74. timeout = time.NewTimer(dur).C
  75. }
  76. }
  77. ef, err := filters.FromParam(r.Form.Get("filters"))
  78. if err != nil {
  79. return err
  80. }
  81. w.Header().Set("Content-Type", "application/json")
  82. output := ioutils.NewWriteFlusher(w)
  83. defer output.Close()
  84. output.Flush()
  85. enc := json.NewEncoder(output)
  86. buffered, l := s.backend.SubscribeToEvents(since, until, ef)
  87. defer s.backend.UnsubscribeFromEvents(l)
  88. for _, ev := range buffered {
  89. if err := enc.Encode(ev); err != nil {
  90. return err
  91. }
  92. }
  93. if onlyPastEvents {
  94. return nil
  95. }
  96. for {
  97. select {
  98. case ev := <-l:
  99. jev, ok := ev.(events.Message)
  100. if !ok {
  101. logrus.Warnf("unexpected event message: %q", ev)
  102. continue
  103. }
  104. if err := enc.Encode(jev); err != nil {
  105. return err
  106. }
  107. case <-timeout:
  108. return nil
  109. case <-ctx.Done():
  110. logrus.Debug("Client context cancelled, stop sending events")
  111. return nil
  112. }
  113. }
  114. }
  115. func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  116. var config *types.AuthConfig
  117. err := json.NewDecoder(r.Body).Decode(&config)
  118. r.Body.Close()
  119. if err != nil {
  120. return err
  121. }
  122. status, token, err := s.backend.AuthenticateToRegistry(ctx, config)
  123. if err != nil {
  124. return err
  125. }
  126. return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{
  127. Status: status,
  128. IdentityToken: token,
  129. })
  130. }
  131. func eventTime(formTime string) (time.Time, error) {
  132. t, tNano, err := timetypes.ParseTimestamps(formTime, -1)
  133. if err != nil {
  134. return time.Time{}, err
  135. }
  136. if t == -1 {
  137. return time.Time{}, nil
  138. }
  139. return time.Unix(t, tNano), nil
  140. }