Browse Source

Remove all docker debugging knowledge from the server.

It should be explicitly told whether to enable the profiler or not.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 years ago
parent
commit
e8f569b324
3 changed files with 37 additions and 35 deletions
  1. 4 2
      api/server/profiler.go
  2. 19 29
      api/server/server.go
  3. 14 4
      docker/daemon.go

+ 4 - 2
api/server/profiler.go

@@ -9,8 +9,10 @@ import (
 	"github.com/gorilla/mux"
 	"github.com/gorilla/mux"
 )
 )
 
 
-func profilerSetup(mainRouter *mux.Router, path string) {
-	var r = mainRouter.PathPrefix(path).Subrouter()
+const debugPathPrefix = "/debug/"
+
+func profilerSetup(mainRouter *mux.Router) {
+	var r = mainRouter.PathPrefix(debugPathPrefix).Subrouter()
 	r.HandleFunc("/vars", expVars)
 	r.HandleFunc("/vars", expVars)
 	r.HandleFunc("/pprof/", pprof.Index)
 	r.HandleFunc("/pprof/", pprof.Index)
 	r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
 	r.HandleFunc("/pprof/cmdline", pprof.Cmdline)

+ 19 - 29
api/server/server.go

@@ -72,8 +72,6 @@ func (s *Server) Close() {
 // serveAPI loops through all initialized servers and spawns goroutine
 // serveAPI loops through all initialized servers and spawns goroutine
 // with Server method for each. It sets createMux() as Handler also.
 // with Server method for each. It sets createMux() as Handler also.
 func (s *Server) serveAPI() error {
 func (s *Server) serveAPI() error {
-	s.initRouterSwapper()
-
 	var chErrors = make(chan error, len(s.servers))
 	var chErrors = make(chan error, len(s.servers))
 	for _, srv := range s.servers {
 	for _, srv := range s.servers {
 		srv.srv.Handler = s.routerSwapper
 		srv.srv.Handler = s.routerSwapper
@@ -149,24 +147,25 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
 	}
 	}
 }
 }
 
 
-// AddRouters initializes a list of routers for the server.
-func (s *Server) AddRouters(routers ...router.Router) {
+// InitRouter initializes the list of routers for the server.
+// This method also enables the Go profiler if enableProfiler is true.
+func (s *Server) InitRouter(enableProfiler bool, routers ...router.Router) {
 	for _, r := range routers {
 	for _, r := range routers {
-		s.addRouter(r)
+		s.routers = append(s.routers, r)
 	}
 	}
-}
 
 
-// addRouter adds a new router to the server.
-func (s *Server) addRouter(r router.Router) {
-	s.routers = append(s.routers, r)
+	m := s.createMux()
+	if enableProfiler {
+		profilerSetup(m)
+	}
+	s.routerSwapper = &routerSwapper{
+		router: m,
+	}
 }
 }
 
 
 // createMux initializes the main router the server uses.
 // createMux initializes the main router the server uses.
 func (s *Server) createMux() *mux.Router {
 func (s *Server) createMux() *mux.Router {
 	m := mux.NewRouter()
 	m := mux.NewRouter()
-	if utils.IsDebugEnabled() {
-		profilerSetup(m, "/debug/")
-	}
 
 
 	logrus.Debugf("Registering routers")
 	logrus.Debugf("Registering routers")
 	for _, apiRouter := range s.routers {
 	for _, apiRouter := range s.routers {
@@ -194,23 +193,14 @@ func (s *Server) Wait(waitChan chan error) {
 	waitChan <- nil
 	waitChan <- nil
 }
 }
 
 
-func (s *Server) initRouterSwapper() {
-	s.routerSwapper = &routerSwapper{
-		router: s.createMux(),
-	}
+// DisableProfiler reloads the server mux without adding the profiler routes.
+func (s *Server) DisableProfiler() {
+	s.routerSwapper.Swap(s.createMux())
 }
 }
 
 
-// Reload reads configuration changes and modifies the
-// server according to those changes.
-// Currently, only the --debug configuration is taken into account.
-func (s *Server) Reload(debug bool) {
-	debugEnabled := utils.IsDebugEnabled()
-	switch {
-	case debugEnabled && !debug: // disable debug
-		utils.DisableDebug()
-		s.routerSwapper.Swap(s.createMux())
-	case debug && !debugEnabled: // enable debug
-		utils.EnableDebug()
-		s.routerSwapper.Swap(s.createMux())
-	}
+// EnableProfiler reloads the server mux adding the profiler routes.
+func (s *Server) EnableProfiler() {
+	m := s.createMux()
+	profilerSetup(m)
+	s.routerSwapper.Swap(m)
 }
 }

+ 14 - 4
docker/daemon.go

@@ -282,14 +282,23 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
 		"graphdriver": d.GraphDriverName(),
 		"graphdriver": d.GraphDriverName(),
 	}).Info("Docker daemon")
 	}).Info("Docker daemon")
 
 
-	initRouters(api, d)
+	initRouter(api, d)
 
 
 	reload := func(config *daemon.Config) {
 	reload := func(config *daemon.Config) {
 		if err := d.Reload(config); err != nil {
 		if err := d.Reload(config); err != nil {
 			logrus.Errorf("Error reconfiguring the daemon: %v", err)
 			logrus.Errorf("Error reconfiguring the daemon: %v", err)
 			return
 			return
 		}
 		}
-		api.Reload(config.Debug)
+
+		debugEnabled := utils.IsDebugEnabled()
+		switch {
+		case debugEnabled && !config.Debug: // disable debug
+			utils.DisableDebug()
+			api.DisableProfiler()
+		case config.Debug && !debugEnabled: // enable debug
+			utils.EnableDebug()
+			api.EnableProfiler()
+		}
 	}
 	}
 
 
 	setupConfigReloadTrap(*configFile, cli.flags, reload)
 	setupConfigReloadTrap(*configFile, cli.flags, reload)
@@ -386,8 +395,9 @@ func loadDaemonCliConfig(config *daemon.Config, daemonFlags *flag.FlagSet, commo
 	return config, nil
 	return config, nil
 }
 }
 
 
-func initRouters(s *apiserver.Server, d *daemon.Daemon) {
-	s.AddRouters(container.NewRouter(d),
+func initRouter(s *apiserver.Server, d *daemon.Daemon) {
+	s.InitRouter(utils.IsDebugEnabled(),
+		container.NewRouter(d),
 		image.NewRouter(d),
 		image.NewRouter(d),
 		network.NewRouter(d),
 		network.NewRouter(d),
 		systemrouter.NewRouter(d),
 		systemrouter.NewRouter(d),