Move docker system information to a dedicated router and backend.

Because I like the name `system` better than `local` :)

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-03 13:11:19 -05:00
parent d9755df2b3
commit 867f432985
7 changed files with 115 additions and 80 deletions

View file

@ -1,27 +0,0 @@
package local
import (
"encoding/json"
"net/http"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cliconfig"
"golang.org/x/net/context"
)
func (s *router) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
var config *cliconfig.AuthConfig
err := json.NewDecoder(r.Body).Decode(&config)
r.Body.Close()
if err != nil {
return err
}
status, err := s.daemon.AuthenticateToRegistry(config)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{
Status: status,
})
}

View file

@ -1,10 +1,6 @@
package local
import (
"net/http"
"golang.org/x/net/context"
"github.com/docker/docker/api/server/httputils"
dkrouter "github.com/docker/docker/api/server/router"
"github.com/docker/docker/daemon"
@ -92,12 +88,7 @@ func (r *router) Routes() []dkrouter.Route {
func (r *router) initRoutes() {
r.routes = []dkrouter.Route{
// OPTIONS
NewOptionsRoute("/", optionsHandler),
// GET
NewGetRoute("/_ping", pingHandler),
NewGetRoute("/events", r.getEvents),
NewGetRoute("/info", r.getInfo),
NewGetRoute("/version", r.getVersion),
NewGetRoute("/images/json", r.getImagesJSON),
NewGetRoute("/images/search", r.getImagesSearch),
NewGetRoute("/images/get", r.getImagesGet),
@ -105,7 +96,6 @@ func (r *router) initRoutes() {
NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
// POST
NewPostRoute("/auth", r.postAuth),
NewPostRoute("/commit", r.postCommit),
NewPostRoute("/build", r.postBuild),
NewPostRoute("/images/create", r.postImagesCreate),
@ -116,13 +106,3 @@ func (r *router) initRoutes() {
NewDeleteRoute("/images/{name:.*}", r.deleteImages),
}
}
func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
w.WriteHeader(http.StatusOK)
return nil
}
func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
_, err := w.Write([]byte{'O', 'K'})
return err
}

View file

@ -0,0 +1,18 @@
package system
import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/cliconfig"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/parsers/filters"
)
// Backend is the methods that need to be implemented to provide
// system specific functionality.
type Backend interface {
SystemInfo() (*types.Info, error)
SystemVersion() types.Version
SubscribeToEvents(since, sinceNano int64, ef filters.Args) ([]*jsonmessage.JSONMessage, chan interface{})
UnsubscribeFromEvents(chan interface{})
AuthenticateToRegistry(authConfig *cliconfig.AuthConfig) (string, error)
}

View file

@ -0,0 +1,37 @@
package system
import (
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/local"
)
// systemRouter is a Router that provides information about
// the Docker system overall. It gathers information about
// host, daemon and container events.
type systemRouter struct {
backend Backend
routes []router.Route
}
// NewRouter initializes a new systemRouter
func NewRouter(b Backend) router.Router {
r := &systemRouter{
backend: b,
}
r.routes = []router.Route{
local.NewOptionsRoute("/", optionsHandler),
local.NewGetRoute("/_ping", pingHandler),
local.NewGetRoute("/events", r.getEvents),
local.NewGetRoute("/info", r.getInfo),
local.NewGetRoute("/version", r.getVersion),
local.NewPostRoute("/auth", r.postAuth),
}
return r
}
// Routes return all the API routes dedicated to the docker system.
func (s *systemRouter) Routes() []router.Route {
return s.routes
}

View file

@ -1,51 +1,34 @@
package local
package system
import (
"encoding/json"
"net/http"
"runtime"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/cliconfig"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/parsers/filters"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/timeutils"
"github.com/docker/docker/utils"
"golang.org/x/net/context"
)
func (s *router) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
v := &types.Version{
Version: dockerversion.Version,
APIVersion: api.Version,
GitCommit: dockerversion.GitCommit,
GoVersion: runtime.Version(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
BuildTime: dockerversion.BuildTime,
}
version := httputils.VersionFromContext(ctx)
if version.GreaterThanOrEqualTo("1.19") {
v.Experimental = utils.ExperimentalBuild()
}
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
v.KernelVersion = kernelVersion.String()
}
return httputils.WriteJSON(w, http.StatusOK, v)
func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
w.WriteHeader(http.StatusOK)
return nil
}
func (s *router) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
info, err := s.daemon.SystemInfo()
func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
_, err := w.Write([]byte{'O', 'K'})
return err
}
func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
info, err := s.backend.SystemInfo()
if err != nil {
return err
}
@ -53,7 +36,14 @@ func (s *router) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Req
return httputils.WriteJSON(w, http.StatusOK, info)
}
func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
info := s.backend.SystemVersion()
info.APIVersion = api.Version
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 {
if err := httputils.ParseForm(r); err != nil {
return err
}
@ -92,8 +82,8 @@ func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.R
enc := json.NewEncoder(output)
buffered, l := s.daemon.SubscribeToEvents(since, sinceNano, ef)
defer s.daemon.UnsubscribeFromEvents(l)
buffered, l := s.backend.SubscribeToEvents(since, sinceNano, ef)
defer s.backend.UnsubscribeFromEvents(l)
for _, ev := range buffered {
if err := enc.Encode(ev); err != nil {
@ -124,3 +114,19 @@ func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.R
}
}
}
func (s *systemRouter) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
var config *cliconfig.AuthConfig
err := json.NewDecoder(r.Body).Decode(&config)
r.Body.Close()
if err != nil {
return err
}
status, err := s.backend.AuthenticateToRegistry(config)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, &types.AuthResponse{
Status: status,
})
}

View file

@ -13,6 +13,7 @@ import (
"github.com/docker/docker/api/server/router/container"
"github.com/docker/docker/api/server/router/local"
"github.com/docker/docker/api/server/router/network"
"github.com/docker/docker/api/server/router/system"
"github.com/docker/docker/api/server/router/volume"
"github.com/docker/docker/daemon"
"github.com/docker/docker/pkg/sockets"
@ -168,10 +169,11 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
// InitRouters initializes a list of routers for the server.
func (s *Server) InitRouters(d *daemon.Daemon) {
s.addRouter(container.NewRouter(d))
s.addRouter(local.NewRouter(d))
s.addRouter(network.NewRouter(d))
s.addRouter(system.NewRouter(d))
s.addRouter(volume.NewRouter(d))
s.addRouter(container.NewRouter(d))
}
// addRouter adds a new router to the server.

View file

@ -113,6 +113,25 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
return v, nil
}
// SystemVersion returns version information about the daemon.
func (daemon *Daemon) SystemVersion() types.Version {
v := types.Version{
Version: dockerversion.Version,
GitCommit: dockerversion.GitCommit,
GoVersion: runtime.Version(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
BuildTime: dockerversion.BuildTime,
Experimental: utils.ExperimentalBuild(),
}
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
v.KernelVersion = kernelVersion.String()
}
return v
}
func (daemon *Daemon) showPluginsInfo() types.PluginsInfo {
var pluginsInfo types.PluginsInfo