system_routes.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 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()
  66. if err != nil {
  67. return err
  68. }
  69. return httputils.WriteJSON(w, http.StatusOK, du)
  70. }
  71. func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  72. if err := httputils.ParseForm(r); err != nil {
  73. return err
  74. }
  75. since, err := eventTime(r.Form.Get("since"))
  76. if err != nil {
  77. return err
  78. }
  79. until, err := eventTime(r.Form.Get("until"))
  80. if err != nil {
  81. return err
  82. }
  83. var (
  84. timeout <-chan time.Time
  85. onlyPastEvents bool
  86. )
  87. if !until.IsZero() {
  88. if until.Before(since) {
  89. return errors.NewBadRequestError(fmt.Errorf("`since` time (%s) cannot be after `until` time (%s)", r.Form.Get("since"), r.Form.Get("until")))
  90. }
  91. now := time.Now()
  92. onlyPastEvents = until.Before(now)
  93. if !onlyPastEvents {
  94. dur := until.Sub(now)
  95. timeout = time.NewTimer(dur).C
  96. }
  97. }
  98. ef, err := filters.FromParam(r.Form.Get("filters"))
  99. if err != nil {
  100. return err
  101. }
  102. w.Header().Set("Content-Type", "application/json")
  103. output := ioutils.NewWriteFlusher(w)
  104. defer output.Close()
  105. output.Flush()
  106. enc := json.NewEncoder(output)
  107. buffered, l := s.backend.SubscribeToEvents(since, until, ef)
  108. defer s.backend.UnsubscribeFromEvents(l)
  109. for _, ev := range buffered {
  110. if err := enc.Encode(ev); err != nil {
  111. return err
  112. }
  113. }
  114. if onlyPastEvents {
  115. return nil
  116. }
  117. for {
  118. select {
  119. case ev := <-l:
  120. jev, ok := ev.(events.Message)
  121. if !ok {
  122. logrus.Warnf("unexpected event message: %q", ev)
  123. continue
  124. }
  125. if err := enc.Encode(jev); err != nil {
  126. return err
  127. }
  128. case <-timeout:
  129. return nil
  130. case <-ctx.Done():
  131. logrus.Debug("Client context cancelled, stop sending events")
  132. return nil
  133. }
  134. }
  135. }
  136. func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  137. var config *types.AuthConfig
  138. err := json.NewDecoder(r.Body).Decode(&config)
  139. r.Body.Close()
  140. if err != nil {
  141. return err
  142. }
  143. status, token, err := s.backend.AuthenticateToRegistry(ctx, config)
  144. if err != nil {
  145. return err
  146. }
  147. return httputils.WriteJSON(w, http.StatusOK, &registry.AuthenticateOKBody{
  148. Status: status,
  149. IdentityToken: token,
  150. })
  151. }
  152. func eventTime(formTime string) (time.Time, error) {
  153. t, tNano, err := timetypes.ParseTimestamps(formTime, -1)
  154. if err != nil {
  155. return time.Time{}, err
  156. }
  157. if t == -1 {
  158. return time.Time{}, nil
  159. }
  160. return time.Unix(t, tNano), nil
  161. }