system_routes.go 4.4 KB

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