system_routes.go 4.2 KB

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