system_routes.go 4.9 KB

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