system_routes.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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) getDiskUsage(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  51. du, err := s.backend.SystemDiskUsage()
  52. if err != nil {
  53. return err
  54. }
  55. return httputils.WriteJSON(w, http.StatusOK, du)
  56. }
  57. func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  58. if err := httputils.ParseForm(r); err != nil {
  59. return err
  60. }
  61. since, err := eventTime(r.Form.Get("since"))
  62. if err != nil {
  63. return err
  64. }
  65. until, err := eventTime(r.Form.Get("until"))
  66. if err != nil {
  67. return err
  68. }
  69. var (
  70. timeout <-chan time.Time
  71. onlyPastEvents bool
  72. )
  73. if !until.IsZero() {
  74. if until.Before(since) {
  75. return errors.NewBadRequestError(fmt.Errorf("`since` time (%s) cannot be after `until` time (%s)", r.Form.Get("since"), r.Form.Get("until")))
  76. }
  77. now := time.Now()
  78. onlyPastEvents = until.Before(now)
  79. if !onlyPastEvents {
  80. dur := until.Sub(now)
  81. timeout = time.NewTimer(dur).C
  82. }
  83. }
  84. ef, err := filters.FromParam(r.Form.Get("filters"))
  85. if err != nil {
  86. return err
  87. }
  88. w.Header().Set("Content-Type", "application/json")
  89. output := ioutils.NewWriteFlusher(w)
  90. defer output.Close()
  91. output.Flush()
  92. enc := json.NewEncoder(output)
  93. buffered, l := s.backend.SubscribeToEvents(since, until, ef)
  94. defer s.backend.UnsubscribeFromEvents(l)
  95. for _, ev := range buffered {
  96. if err := enc.Encode(ev); err != nil {
  97. return err
  98. }
  99. }
  100. if onlyPastEvents {
  101. return nil
  102. }
  103. for {
  104. select {
  105. case ev := <-l:
  106. jev, ok := ev.(events.Message)
  107. if !ok {
  108. logrus.Warnf("unexpected event message: %q", ev)
  109. continue
  110. }
  111. if err := enc.Encode(jev); err != nil {
  112. return err
  113. }
  114. case <-timeout:
  115. return nil
  116. case <-ctx.Done():
  117. logrus.Debug("Client context cancelled, stop sending events")
  118. return nil
  119. }
  120. }
  121. }
  122. func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  123. var config *types.AuthConfig
  124. err := json.NewDecoder(r.Body).Decode(&config)
  125. r.Body.Close()
  126. if err != nil {
  127. return err
  128. }
  129. status, token, err := s.backend.AuthenticateToRegistry(ctx, config)
  130. if err != nil {
  131. return err
  132. }
  133. return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{
  134. Status: status,
  135. IdentityToken: token,
  136. })
  137. }
  138. func eventTime(formTime string) (time.Time, error) {
  139. t, tNano, err := timetypes.ParseTimestamps(formTime, -1)
  140. if err != nil {
  141. return time.Time{}, err
  142. }
  143. if t == -1 {
  144. return time.Time{}, nil
  145. }
  146. return time.Unix(t, tNano), nil
  147. }