2014-08-01 13:34:06 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-07 12:49:07 -04:00
|
|
|
"crypto/tls"
|
2015-01-21 19:55:05 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-03-08 16:03:37 -05:00
|
|
|
"runtime"
|
2015-05-05 00:18:28 -04:00
|
|
|
"strings"
|
2015-04-27 17:11:29 -04:00
|
|
|
"time"
|
2015-01-21 19:55:05 -05:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-29 17:47:30 -04:00
|
|
|
"github.com/docker/distribution/uuid"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-04-16 15:48:04 -04:00
|
|
|
apiserver "github.com/docker/docker/api/server"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
"github.com/docker/docker/api/server/router"
|
2016-02-10 15:16:59 -05:00
|
|
|
"github.com/docker/docker/api/server/router/build"
|
|
|
|
"github.com/docker/docker/api/server/router/container"
|
|
|
|
"github.com/docker/docker/api/server/router/image"
|
|
|
|
"github.com/docker/docker/api/server/router/network"
|
2016-06-13 22:52:49 -04:00
|
|
|
swarmrouter "github.com/docker/docker/api/server/router/swarm"
|
2016-02-10 15:16:59 -05:00
|
|
|
systemrouter "github.com/docker/docker/api/server/router/system"
|
|
|
|
"github.com/docker/docker/api/server/router/volume"
|
2016-04-13 13:21:00 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
2016-02-19 17:42:51 -05:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
2015-04-28 11:00:18 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2014-08-08 05:12:39 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2016-06-13 22:52:49 -04:00
|
|
|
"github.com/docker/docker/daemon/cluster"
|
2015-06-30 20:40:13 -04:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-11-09 13:32:46 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2015-05-05 00:18:28 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
2015-12-15 14:49:41 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2016-04-01 09:43:05 -04:00
|
|
|
"github.com/docker/docker/pkg/listeners"
|
2014-08-01 13:34:06 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-04-27 17:11:29 -04:00
|
|
|
"github.com/docker/docker/pkg/pidfile"
|
2014-08-06 04:12:22 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-06-11 14:29:29 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-08-20 11:31:24 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2016-03-28 14:22:23 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-05-21 13:48:36 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2014-08-01 13:34:06 -04:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
const (
|
|
|
|
daemonConfigFileFlag = "-config-file"
|
|
|
|
)
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// DaemonCli represents the daemon CLI.
|
|
|
|
type DaemonCli struct {
|
|
|
|
*daemon.Config
|
2016-04-21 17:51:28 -04:00
|
|
|
commonFlags *cliflags.CommonFlags
|
2016-02-19 17:42:51 -05:00
|
|
|
configFile *string
|
2016-04-22 20:16:14 -04:00
|
|
|
|
|
|
|
api *apiserver.Server
|
|
|
|
d *daemon.Daemon
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
func presentInHelp(usage string) string { return usage }
|
|
|
|
func absentFromHelp(string) string { return "" }
|
|
|
|
|
|
|
|
// NewDaemonCli returns a pre-configured daemon CLI
|
|
|
|
func NewDaemonCli() *DaemonCli {
|
|
|
|
// TODO(tiborvass): remove InstallFlags?
|
|
|
|
daemonConfig := new(daemon.Config)
|
2015-08-15 12:06:03 -04:00
|
|
|
daemonConfig.LogConfig.Config = make(map[string]string)
|
2015-09-28 19:22:57 -04:00
|
|
|
daemonConfig.ClusterOpts = make(map[string]string)
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
daemonConfig.V2Only = true
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
daemonConfig.InstallFlags(flag.CommandLine, presentInHelp)
|
|
|
|
configFile := flag.CommandLine.String([]string{daemonConfigFileFlag}, defaultDaemonConfigFile, "Daemon configuration file")
|
|
|
|
flag.CommandLine.Require(flag.Exact, 0)
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
return &DaemonCli{
|
2016-02-19 17:42:51 -05:00
|
|
|
Config: daemonConfig,
|
|
|
|
commonFlags: cliflags.InitCommonFlags(),
|
|
|
|
configFile: configFile,
|
2015-05-04 17:39:48 -04:00
|
|
|
}
|
2014-08-09 21:18:32 -04:00
|
|
|
}
|
|
|
|
|
2015-01-22 14:22:31 -05:00
|
|
|
func migrateKey() (err error) {
|
2015-01-21 19:55:05 -05:00
|
|
|
// Migrate trust key if exists at ~/.docker/key.json and owned by current user
|
2016-02-19 17:42:51 -05:00
|
|
|
oldPath := filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
|
|
|
newPath := filepath.Join(getDaemonConfDir(), cliflags.DefaultTrustKeyFile)
|
2015-03-29 15:48:52 -04:00
|
|
|
if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && currentUserIsOwner(oldPath) {
|
2015-01-22 14:22:31 -05:00
|
|
|
defer func() {
|
|
|
|
// Ensure old path is removed if no error occurred
|
|
|
|
if err == nil {
|
|
|
|
err = os.Remove(oldPath)
|
|
|
|
} else {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Warnf("Key migration failed, key file not removed at %s", oldPath)
|
2015-08-03 18:29:54 -04:00
|
|
|
os.Remove(newPath)
|
2015-01-22 14:22:31 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-11 14:29:29 -04:00
|
|
|
if err := system.MkdirAll(getDaemonConfDir(), os.FileMode(0644)); err != nil {
|
2015-01-22 14:22:31 -05:00
|
|
|
return fmt.Errorf("Unable to create daemon configuration directory: %s", err)
|
2015-01-21 19:55:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newFile, err := os.OpenFile(newPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating key file %q: %s", newPath, err)
|
|
|
|
}
|
|
|
|
defer newFile.Close()
|
|
|
|
|
|
|
|
oldFile, err := os.Open(oldPath)
|
|
|
|
if err != nil {
|
2015-01-22 14:22:31 -05:00
|
|
|
return fmt.Errorf("error opening key file %q: %s", oldPath, err)
|
2015-01-21 19:55:05 -05:00
|
|
|
}
|
2015-01-22 14:22:31 -05:00
|
|
|
defer oldFile.Close()
|
2015-01-21 19:55:05 -05:00
|
|
|
|
|
|
|
if _, err := io.Copy(newFile, oldFile); err != nil {
|
|
|
|
return fmt.Errorf("error copying key: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Infof("Migrated key from %s to %s", oldPath, newPath)
|
2015-01-21 19:55:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:16:14 -04:00
|
|
|
func (cli *DaemonCli) start() (err error) {
|
|
|
|
stopc := make(chan bool)
|
|
|
|
defer close(stopc)
|
|
|
|
|
2015-07-29 17:47:30 -04:00
|
|
|
// warn from uuid package when running the daemon
|
|
|
|
uuid.Loggerf = logrus.Warnf
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
flags := flag.CommandLine
|
|
|
|
cli.commonFlags.PostParse()
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
if cli.commonFlags.TrustKey == "" {
|
|
|
|
cli.commonFlags.TrustKey = filepath.Join(getDaemonConfDir(), cliflags.DefaultTrustKeyFile)
|
2015-05-21 13:48:36 -04:00
|
|
|
}
|
2016-02-19 17:42:51 -05:00
|
|
|
cliConfig, err := loadDaemonCliConfig(cli.Config, flags, cli.commonFlags, *cli.configFile)
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
cli.Config = cliConfig
|
|
|
|
|
|
|
|
if cli.Config.Debug {
|
|
|
|
utils.EnableDebug()
|
|
|
|
}
|
2015-05-21 13:48:36 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if utils.ExperimentalBuild() {
|
|
|
|
logrus.Warn("Running experimental build")
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-27 21:38:00 -04:00
|
|
|
|
2015-12-13 05:10:41 -05:00
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
|
|
TimestampFormat: jsonlog.RFC3339NanoFixed,
|
|
|
|
DisableColors: cli.Config.RawLogs,
|
|
|
|
})
|
2015-03-27 21:38:00 -04:00
|
|
|
|
2015-06-15 09:36:19 -04:00
|
|
|
if err := setDefaultUmask(); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("Failed to set umask: %v", err)
|
2015-06-15 09:36:19 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if len(cli.LogConfig.Config) > 0 {
|
|
|
|
if err := logger.ValidateLogOpts(cli.LogConfig.Type, cli.LogConfig.Config); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("Failed to set log opts: %v", err)
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if cli.Pidfile != "" {
|
|
|
|
pf, err := pidfile.New(cli.Pidfile)
|
2015-04-27 17:11:29 -04:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("Error starting daemon: %v", err)
|
2015-04-27 17:11:29 -04:00
|
|
|
}
|
|
|
|
defer func() {
|
2016-04-22 20:16:14 -04:00
|
|
|
if err := pf.Remove(); err != nil {
|
2015-04-27 17:11:29 -04:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
serverConfig := &apiserver.Config{
|
2016-04-08 19:22:39 -04:00
|
|
|
Logging: true,
|
|
|
|
SocketGroup: cli.Config.SocketGroup,
|
|
|
|
Version: dockerversion.Version,
|
2016-03-28 10:57:39 -04:00
|
|
|
EnableCors: cli.Config.EnableCors,
|
|
|
|
CorsHeaders: cli.Config.CorsHeaders,
|
2015-05-07 12:49:07 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
if cli.Config.TLS {
|
|
|
|
tlsOptions := tlsconfig.Options{
|
2016-01-22 13:14:48 -05:00
|
|
|
CAFile: cli.Config.CommonTLSOptions.CAFile,
|
|
|
|
CertFile: cli.Config.CommonTLSOptions.CertFile,
|
|
|
|
KeyFile: cli.Config.CommonTLSOptions.KeyFile,
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cli.Config.TLSVerify {
|
2015-05-05 00:18:28 -04:00
|
|
|
// server requires and verifies client's certificate
|
2015-12-10 18:35:10 -05:00
|
|
|
tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert
|
2015-05-07 12:49:07 -04:00
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
tlsConfig, err := tlsconfig.Server(tlsOptions)
|
2015-05-07 12:49:07 -04:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2015-05-07 12:49:07 -04:00
|
|
|
}
|
|
|
|
serverConfig.TLSConfig = tlsConfig
|
2015-08-21 09:28:49 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
if len(cli.Config.Hosts) == 0 {
|
|
|
|
cli.Config.Hosts = make([]string, 1)
|
2015-10-12 04:49:25 -04:00
|
|
|
}
|
2016-02-11 13:30:23 -05:00
|
|
|
|
|
|
|
api := apiserver.New(serverConfig)
|
2016-06-13 22:52:49 -04:00
|
|
|
cli.api = api
|
2016-02-11 13:30:23 -05:00
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
for i := 0; i < len(cli.Config.Hosts); i++ {
|
2015-08-21 09:28:49 -04:00
|
|
|
var err error
|
2016-01-30 21:45:49 -05:00
|
|
|
if cli.Config.Hosts[i], err = opts.ParseHost(cli.Config.TLS, cli.Config.Hosts[i]); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("error parsing -H %s : %v", cli.Config.Hosts[i], err)
|
2015-08-21 09:28:49 -04:00
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
protoAddr := cli.Config.Hosts[i]
|
2015-10-05 12:32:08 -04:00
|
|
|
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
|
|
|
|
if len(protoAddrParts) != 2 {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("bad format %s, expected PROTO://ADDR", protoAddr)
|
2015-10-05 12:32:08 -04:00
|
|
|
}
|
2016-04-09 02:49:33 -04:00
|
|
|
|
|
|
|
proto := protoAddrParts[0]
|
|
|
|
addr := protoAddrParts[1]
|
|
|
|
|
|
|
|
// It's a bad idea to bind to TCP without tlsverify.
|
|
|
|
if proto == "tcp" && (serverConfig.TLSConfig == nil || serverConfig.TLSConfig.ClientAuth != tls.RequireAndVerifyClientCert) {
|
|
|
|
logrus.Warn("[!] DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING [!]")
|
|
|
|
}
|
2016-04-12 11:21:20 -04:00
|
|
|
ls, err := listeners.Init(proto, addr, serverConfig.SocketGroup, serverConfig.TLSConfig)
|
2016-02-11 13:30:23 -05:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2016-02-11 13:30:23 -05:00
|
|
|
}
|
2016-04-12 11:21:20 -04:00
|
|
|
ls = wrapListeners(proto, ls)
|
2016-04-09 02:49:33 -04:00
|
|
|
// If we're binding to a TCP port, make sure that a container doesn't try to use it.
|
|
|
|
if proto == "tcp" {
|
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2016-04-09 02:49:33 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 13:30:23 -05:00
|
|
|
logrus.Debugf("Listener created for HTTP on %s (%s)", protoAddrParts[0], protoAddrParts[1])
|
2016-04-12 11:21:20 -04:00
|
|
|
api.Accept(protoAddrParts[1], ls...)
|
2015-10-05 12:32:08 -04:00
|
|
|
}
|
2015-04-16 15:48:04 -04:00
|
|
|
|
2015-05-07 18:35:12 -04:00
|
|
|
if err := migrateKey(); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2015-05-07 18:35:12 -04:00
|
|
|
}
|
2016-02-19 17:42:51 -05:00
|
|
|
cli.TrustKeyPath = cli.commonFlags.TrustKey
|
2015-05-07 18:35:12 -04:00
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
registryService := registry.NewService(cli.Config.ServiceOptions)
|
2016-03-24 14:42:03 -04:00
|
|
|
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), cli.getPlatformRemoteOptions()...)
|
2016-03-18 14:50:19 -04:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2016-06-13 02:10:37 -04:00
|
|
|
cli.api = api
|
|
|
|
signal.Trap(func() {
|
|
|
|
cli.stop()
|
|
|
|
<-stopc // wait for daemonCli.start() to return
|
|
|
|
})
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
d, err := daemon.NewDaemon(cli.Config, registryService, containerdRemote)
|
2015-05-07 18:35:12 -04:00
|
|
|
if err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("Error starting daemon: %v", err)
|
2015-05-07 18:35:12 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
name, _ := os.Hostname()
|
|
|
|
|
|
|
|
c, err := cluster.New(cluster.Config{
|
|
|
|
Root: cli.Config.Root,
|
|
|
|
Name: name,
|
|
|
|
Backend: d,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Error creating cluster component: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:35:12 -04:00
|
|
|
logrus.Info("Daemon has completed initialization")
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
2015-11-09 13:32:46 -05:00
|
|
|
"version": dockerversion.Version,
|
|
|
|
"commit": dockerversion.GitCommit,
|
2015-12-16 15:32:16 -05:00
|
|
|
"graphdriver": d.GraphDriverName(),
|
2015-05-07 18:35:12 -04:00
|
|
|
}).Info("Docker daemon")
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
cli.initMiddlewares(api, serverConfig)
|
2016-06-13 22:52:49 -04:00
|
|
|
initRouter(api, d, c)
|
2015-09-23 19:42:08 -04:00
|
|
|
|
2016-04-22 20:16:14 -04:00
|
|
|
cli.d = d
|
|
|
|
cli.setupConfigReloadTrap()
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2015-11-24 21:17:37 -05:00
|
|
|
// The serve API routine never exits unless an error occurs
|
|
|
|
// We need to start it as a goroutine and wait on it so
|
|
|
|
// daemon doesn't exit
|
|
|
|
serveAPIWait := make(chan error)
|
2015-12-10 18:35:10 -05:00
|
|
|
go api.Wait(serveAPIWait)
|
2015-11-24 21:17:37 -05:00
|
|
|
|
2015-11-25 14:05:31 -05:00
|
|
|
// after the daemon is done setting up we can notify systemd api
|
2015-09-23 19:42:08 -04:00
|
|
|
notifySystem()
|
2015-04-17 15:03:11 -04:00
|
|
|
|
2015-03-11 10:33:06 -04:00
|
|
|
// Daemon is fully initialized and handling API traffic
|
2015-04-27 17:11:29 -04:00
|
|
|
// Wait for serve API to complete
|
2015-03-11 10:33:06 -04:00
|
|
|
errAPI := <-serveAPIWait
|
2016-06-13 22:52:49 -04:00
|
|
|
c.Cleanup()
|
2015-09-29 13:51:40 -04:00
|
|
|
shutdownDaemon(d, 15)
|
2016-03-18 14:50:19 -04:00
|
|
|
containerdRemote.Cleanup()
|
2015-03-11 10:33:06 -04:00
|
|
|
if errAPI != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return fmt.Errorf("Shutting down due to ServeAPI error: %v", errAPI)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DaemonCli) reloadConfig() {
|
|
|
|
reload := func(config *daemon.Config) {
|
|
|
|
if err := cli.d.Reload(config); err != nil {
|
|
|
|
logrus.Errorf("Error reconfiguring the daemon: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config.IsValueSet("debug") {
|
|
|
|
debugEnabled := utils.IsDebugEnabled()
|
|
|
|
switch {
|
|
|
|
case debugEnabled && !config.Debug: // disable debug
|
|
|
|
utils.DisableDebug()
|
|
|
|
cli.api.DisableProfiler()
|
|
|
|
case config.Debug && !debugEnabled: // enable debug
|
|
|
|
utils.EnableDebug()
|
|
|
|
cli.api.EnableProfiler()
|
2015-04-27 17:11:29 -04:00
|
|
|
}
|
2016-04-22 20:16:14 -04:00
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
}
|
2015-03-11 10:33:06 -04:00
|
|
|
}
|
2016-04-22 20:16:14 -04:00
|
|
|
|
|
|
|
if err := daemon.ReloadConfiguration(*cli.configFile, flag.CommandLine, reload); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DaemonCli) stop() {
|
|
|
|
cli.api.Close()
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-29 15:48:52 -04:00
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
// shutdownDaemon just wraps daemon.Shutdown() to handle a timeout in case
|
|
|
|
// d.Shutdown() is waiting too long to kill container or worst it's
|
|
|
|
// blocked there
|
2015-09-29 13:51:40 -04:00
|
|
|
func shutdownDaemon(d *daemon.Daemon, timeout time.Duration) {
|
2015-04-27 17:11:29 -04:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-09-29 13:51:40 -04:00
|
|
|
d.Shutdown()
|
2015-04-27 17:11:29 -04:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
2015-08-07 18:24:18 -04:00
|
|
|
logrus.Debug("Clean shutdown succeeded")
|
2015-04-27 17:11:29 -04:00
|
|
|
case <-time.After(timeout * time.Second):
|
|
|
|
logrus.Error("Force shutdown daemon")
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-04-21 17:51:28 -04:00
|
|
|
func loadDaemonCliConfig(config *daemon.Config, flags *flag.FlagSet, commonConfig *cliflags.CommonFlags, configFile string) (*daemon.Config, error) {
|
2015-12-10 18:35:10 -05:00
|
|
|
config.Debug = commonConfig.Debug
|
|
|
|
config.Hosts = commonConfig.Hosts
|
|
|
|
config.LogLevel = commonConfig.LogLevel
|
|
|
|
config.TLS = commonConfig.TLS
|
|
|
|
config.TLSVerify = commonConfig.TLSVerify
|
2016-01-22 13:14:48 -05:00
|
|
|
config.CommonTLSOptions = daemon.CommonTLSOptions{}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
if commonConfig.TLSOptions != nil {
|
2016-01-22 13:14:48 -05:00
|
|
|
config.CommonTLSOptions.CAFile = commonConfig.TLSOptions.CAFile
|
|
|
|
config.CommonTLSOptions.CertFile = commonConfig.TLSOptions.CertFile
|
|
|
|
config.CommonTLSOptions.KeyFile = commonConfig.TLSOptions.KeyFile
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
2016-02-19 17:42:51 -05:00
|
|
|
c, err := daemon.MergeDaemonConfigurations(config, flags, configFile)
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
2016-02-19 17:42:51 -05:00
|
|
|
if flags.IsSet(daemonConfigFileFlag) || !os.IsNotExist(err) {
|
2015-12-10 18:35:10 -05:00
|
|
|
return nil, fmt.Errorf("unable to configure the Docker daemon with file %s: %v\n", configFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the merged configuration can be nil if the config file didn't exist.
|
|
|
|
// leave the current configuration as it is if when that happens.
|
|
|
|
if c != nil {
|
|
|
|
config = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:16:07 -05:00
|
|
|
// Regardless of whether the user sets it to true or false, if they
|
|
|
|
// specify TLSVerify at all then we need to turn on TLS
|
2016-02-19 17:42:51 -05:00
|
|
|
if config.IsValueSet(cliflags.TLSVerifyKey) {
|
2016-01-19 14:16:07 -05:00
|
|
|
config.TLS = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that the log level is the one set after merging configurations
|
2016-02-19 17:42:51 -05:00
|
|
|
cliflags.SetDaemonLogLevel(config.LogLevel)
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
2016-02-10 15:16:59 -05:00
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
func initRouter(s *apiserver.Server, d *daemon.Daemon, c *cluster.Cluster) {
|
2016-03-28 14:22:23 -04:00
|
|
|
decoder := runconfig.ContainerDecoder{}
|
|
|
|
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
routers := []router.Router{
|
2016-03-28 14:22:23 -04:00
|
|
|
container.NewRouter(d, decoder),
|
|
|
|
image.NewRouter(d, decoder),
|
2016-06-13 22:52:49 -04:00
|
|
|
systemrouter.NewRouter(d, c),
|
2016-02-10 15:16:59 -05:00
|
|
|
volume.NewRouter(d),
|
2016-04-13 13:21:00 -04:00
|
|
|
build.NewRouter(dockerfile.NewBuildManager(d)),
|
2016-06-13 22:52:49 -04:00
|
|
|
swarmrouter.NewRouter(c),
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
}
|
|
|
|
if d.NetworkControllerEnabled() {
|
2016-06-13 22:52:49 -04:00
|
|
|
routers = append(routers, network.NewRouter(d, c))
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s.InitRouter(utils.IsDebugEnabled(), routers...)
|
2016-02-10 15:16:59 -05:00
|
|
|
}
|
2016-04-08 19:22:39 -04:00
|
|
|
|
|
|
|
func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config) {
|
2016-04-19 10:56:54 -04:00
|
|
|
v := cfg.Version
|
2016-04-08 19:22:39 -04:00
|
|
|
|
|
|
|
vm := middleware.NewVersionMiddleware(v, api.DefaultVersion, api.MinVersion)
|
|
|
|
s.UseMiddleware(vm)
|
|
|
|
|
|
|
|
if cfg.EnableCors {
|
|
|
|
c := middleware.NewCORSMiddleware(cfg.CorsHeaders)
|
|
|
|
s.UseMiddleware(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
u := middleware.NewUserAgentMiddleware(v)
|
|
|
|
s.UseMiddleware(u)
|
|
|
|
|
|
|
|
if len(cli.Config.AuthorizationPlugins) > 0 {
|
|
|
|
authZPlugins := authorization.NewPlugins(cli.Config.AuthorizationPlugins)
|
|
|
|
handleAuthorization := authorization.NewMiddleware(authZPlugins)
|
|
|
|
s.UseMiddleware(handleAuthorization)
|
|
|
|
}
|
|
|
|
}
|