2015-12-03 13:11:19 -05:00
|
|
|
package system
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2015-07-28 14:35:24 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/events"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
|
|
|
timetypes "github.com/docker/engine-api/types/time"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
|
|
|
|
2015-12-03 13:11:19 -05:00
|
|
|
func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2015-12-03 13:11:19 -05:00
|
|
|
func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
_, err := w.Write([]byte{'O', 'K'})
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:11:19 -05:00
|
|
|
func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
info, err := s.backend.SystemInfo()
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, info)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:11:19 -05:00
|
|
|
func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
info := s.backend.SystemVersion()
|
2016-01-04 12:43:38 -05:00
|
|
|
info.APIVersion = api.DefaultVersion.String()
|
2015-12-03 13:11:19 -05:00
|
|
|
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-15 14:49:41 -05:00
|
|
|
since, sinceNano, err := timetypes.ParseTimestamps(r.Form.Get("since"), -1)
|
2015-09-23 14:51:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
2015-12-15 14:49:41 -05:00
|
|
|
until, untilNano, err := timetypes.ParseTimestamps(r.Form.Get("until"), -1)
|
2015-09-23 14:51:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
timer.Stop()
|
2015-10-29 13:51:36 -04:00
|
|
|
if until > 0 || untilNano > 0 {
|
|
|
|
dur := time.Unix(until, untilNano).Sub(time.Now())
|
2015-07-28 14:35:24 -04:00
|
|
|
timer = time.NewTimer(dur)
|
|
|
|
}
|
|
|
|
|
|
|
|
ef, err := filters.FromParam(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:11:28 -05:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
output := ioutils.NewWriteFlusher(w)
|
|
|
|
defer output.Close()
|
2015-12-19 09:43:10 -05:00
|
|
|
output.Flush()
|
2015-11-02 19:11:28 -05:00
|
|
|
|
|
|
|
enc := json.NewEncoder(output)
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2015-12-03 13:11:19 -05:00
|
|
|
buffered, l := s.backend.SubscribeToEvents(since, sinceNano, ef)
|
|
|
|
defer s.backend.UnsubscribeFromEvents(l)
|
2015-10-12 14:54:46 -04:00
|
|
|
|
2015-11-25 21:03:10 -05:00
|
|
|
for _, ev := range buffered {
|
|
|
|
if err := enc.Encode(ev); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeNotify <-chan bool
|
|
|
|
if closeNotifier, ok := w.(http.CloseNotifier); ok {
|
|
|
|
closeNotify = closeNotifier.CloseNotify()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-l:
|
2015-12-21 17:55:23 -05:00
|
|
|
jev, ok := ev.(events.Message)
|
2015-07-28 14:35:24 -04:00
|
|
|
if !ok {
|
2015-12-21 17:55:23 -05:00
|
|
|
logrus.Warnf("unexpected event message: %q", ev)
|
2015-07-28 14:35:24 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-11-25 21:03:10 -05:00
|
|
|
if err := enc.Encode(jev); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
return nil
|
|
|
|
case <-closeNotify:
|
|
|
|
logrus.Debug("Client disconnected, stop sending events")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-03 13:11:19 -05:00
|
|
|
|
|
|
|
func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-12-11 23:11:42 -05:00
|
|
|
var config *types.AuthConfig
|
2015-12-03 13:11:19 -05:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&config)
|
|
|
|
r.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-23 18:18:04 -05:00
|
|
|
status, token, err := s.backend.AuthenticateToRegistry(config)
|
2015-12-03 13:11:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{
|
2016-02-23 18:18:04 -05:00
|
|
|
Status: status,
|
|
|
|
IdentityToken: token,
|
2015-12-03 13:11:19 -05:00
|
|
|
})
|
|
|
|
}
|