mirror of https://github.com/docker/docs.git
Merge pull request #18398 from calavera/system_backend
Move docker system information to a dedicated router and backend.
This commit is contained in:
commit
51b0f23127
|
@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -1,10 +1,6 @@
|
||||||
package local
|
package local
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
"github.com/docker/docker/api/server/httputils"
|
"github.com/docker/docker/api/server/httputils"
|
||||||
dkrouter "github.com/docker/docker/api/server/router"
|
dkrouter "github.com/docker/docker/api/server/router"
|
||||||
"github.com/docker/docker/daemon"
|
"github.com/docker/docker/daemon"
|
||||||
|
@ -92,12 +88,7 @@ func (r *router) Routes() []dkrouter.Route {
|
||||||
func (r *router) initRoutes() {
|
func (r *router) initRoutes() {
|
||||||
r.routes = []dkrouter.Route{
|
r.routes = []dkrouter.Route{
|
||||||
// OPTIONS
|
// OPTIONS
|
||||||
NewOptionsRoute("/", optionsHandler),
|
|
||||||
// GET
|
// GET
|
||||||
NewGetRoute("/_ping", pingHandler),
|
|
||||||
NewGetRoute("/events", r.getEvents),
|
|
||||||
NewGetRoute("/info", r.getInfo),
|
|
||||||
NewGetRoute("/version", r.getVersion),
|
|
||||||
NewGetRoute("/images/json", r.getImagesJSON),
|
NewGetRoute("/images/json", r.getImagesJSON),
|
||||||
NewGetRoute("/images/search", r.getImagesSearch),
|
NewGetRoute("/images/search", r.getImagesSearch),
|
||||||
NewGetRoute("/images/get", r.getImagesGet),
|
NewGetRoute("/images/get", r.getImagesGet),
|
||||||
|
@ -105,7 +96,6 @@ func (r *router) initRoutes() {
|
||||||
NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
|
NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
|
||||||
NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
|
NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
|
||||||
// POST
|
// POST
|
||||||
NewPostRoute("/auth", r.postAuth),
|
|
||||||
NewPostRoute("/commit", r.postCommit),
|
NewPostRoute("/commit", r.postCommit),
|
||||||
NewPostRoute("/build", r.postBuild),
|
NewPostRoute("/build", r.postBuild),
|
||||||
NewPostRoute("/images/create", r.postImagesCreate),
|
NewPostRoute("/images/create", r.postImagesCreate),
|
||||||
|
@ -116,13 +106,3 @@ func (r *router) initRoutes() {
|
||||||
NewDeleteRoute("/images/{name:.*}", r.deleteImages),
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -1,51 +1,34 @@
|
||||||
package local
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/api"
|
"github.com/docker/docker/api"
|
||||||
"github.com/docker/docker/api/server/httputils"
|
"github.com/docker/docker/api/server/httputils"
|
||||||
"github.com/docker/docker/api/types"
|
"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/ioutils"
|
||||||
"github.com/docker/docker/pkg/jsonmessage"
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
"github.com/docker/docker/pkg/parsers/filters"
|
"github.com/docker/docker/pkg/parsers/filters"
|
||||||
"github.com/docker/docker/pkg/parsers/kernel"
|
|
||||||
"github.com/docker/docker/pkg/timeutils"
|
"github.com/docker/docker/pkg/timeutils"
|
||||||
"github.com/docker/docker/utils"
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *router) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||||
v := &types.Version{
|
w.WriteHeader(http.StatusOK)
|
||||||
Version: dockerversion.Version,
|
return nil
|
||||||
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 (s *router) getInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||||
info, err := s.daemon.SystemInfo()
|
_, 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 {
|
if err != nil {
|
||||||
return err
|
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)
|
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 {
|
if err := httputils.ParseForm(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -92,8 +82,8 @@ func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.R
|
||||||
|
|
||||||
enc := json.NewEncoder(output)
|
enc := json.NewEncoder(output)
|
||||||
|
|
||||||
buffered, l := s.daemon.SubscribeToEvents(since, sinceNano, ef)
|
buffered, l := s.backend.SubscribeToEvents(since, sinceNano, ef)
|
||||||
defer s.daemon.UnsubscribeFromEvents(l)
|
defer s.backend.UnsubscribeFromEvents(l)
|
||||||
|
|
||||||
for _, ev := range buffered {
|
for _, ev := range buffered {
|
||||||
if err := enc.Encode(ev); err != nil {
|
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,
|
||||||
|
})
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/docker/docker/api/server/router/container"
|
"github.com/docker/docker/api/server/router/container"
|
||||||
"github.com/docker/docker/api/server/router/local"
|
"github.com/docker/docker/api/server/router/local"
|
||||||
"github.com/docker/docker/api/server/router/network"
|
"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/api/server/router/volume"
|
||||||
"github.com/docker/docker/daemon"
|
"github.com/docker/docker/daemon"
|
||||||
"github.com/docker/docker/pkg/sockets"
|
"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.
|
// InitRouters initializes a list of routers for the server.
|
||||||
func (s *Server) InitRouters(d *daemon.Daemon) {
|
func (s *Server) InitRouters(d *daemon.Daemon) {
|
||||||
|
s.addRouter(container.NewRouter(d))
|
||||||
s.addRouter(local.NewRouter(d))
|
s.addRouter(local.NewRouter(d))
|
||||||
s.addRouter(network.NewRouter(d))
|
s.addRouter(network.NewRouter(d))
|
||||||
|
s.addRouter(system.NewRouter(d))
|
||||||
s.addRouter(volume.NewRouter(d))
|
s.addRouter(volume.NewRouter(d))
|
||||||
s.addRouter(container.NewRouter(d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// addRouter adds a new router to the server.
|
// addRouter adds a new router to the server.
|
||||||
|
|
|
@ -113,6 +113,25 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
|
||||||
return v, nil
|
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 {
|
func (daemon *Daemon) showPluginsInfo() types.PluginsInfo {
|
||||||
var pluginsInfo types.PluginsInfo
|
var pluginsInfo types.PluginsInfo
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue