system_routes.go 4.8 KB

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