system_routes.go 4.7 KB

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