2014-08-01 17:34:06 +00:00
|
|
|
// +build daemon
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-07 16:49:07 +00:00
|
|
|
"crypto/tls"
|
2015-01-22 00:55:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-05-05 04:18:28 +00:00
|
|
|
"strings"
|
2015-04-27 21:11:29 +00:00
|
|
|
"time"
|
2015-01-22 00:55:05 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-29 21:47:30 +00:00
|
|
|
"github.com/docker/distribution/uuid"
|
2015-04-16 19:48:04 +00:00
|
|
|
apiserver "github.com/docker/docker/api/server"
|
2015-02-04 21:22:38 +00:00
|
|
|
"github.com/docker/docker/autogen/dockerversion"
|
2015-05-05 04:18:28 +00:00
|
|
|
"github.com/docker/docker/cli"
|
2015-04-28 15:00:18 +00:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-09-10 22:01:18 +00:00
|
|
|
"github.com/docker/docker/context"
|
2014-08-08 09:12:39 +00:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-07-01 00:40:13 +00:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-05-05 04:18:28 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2014-08-01 17:34:06 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-04-27 21:11:29 +00:00
|
|
|
"github.com/docker/docker/pkg/pidfile"
|
2014-08-06 08:12:22 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-06-11 18:29:29 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-03-28 01:38:00 +00:00
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2015-05-07 16:49:07 +00:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2014-08-20 15:31:24 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2015-05-21 17:48:36 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-01 17:34:06 +00:00
|
|
|
)
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
const daemonUsage = " docker daemon [ --help | ... ]\n"
|
|
|
|
|
2014-08-10 01:18:32 +00:00
|
|
|
var (
|
2015-05-05 04:18:28 +00:00
|
|
|
flDaemon = flag.Bool([]string{"#d", "#-daemon"}, false, "Enable daemon mode (deprecated; use docker daemon)")
|
|
|
|
daemonCli cli.Handler = NewDaemonCli()
|
2014-08-10 01:18:32 +00:00
|
|
|
)
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
// TODO: remove once `-d` is retired
|
|
|
|
func handleGlobalDaemonFlag() {
|
|
|
|
// This block makes sure that if the deprecated daemon flag `--daemon` is absent,
|
|
|
|
// then all daemon-specific flags are absent as well.
|
|
|
|
if !*flDaemon && daemonFlags != nil {
|
|
|
|
flag.CommandLine.Visit(func(fl *flag.Flag) {
|
|
|
|
for _, name := range fl.Names {
|
|
|
|
name := strings.TrimPrefix(name, "#")
|
|
|
|
if daemonFlags.Lookup(name) != nil {
|
|
|
|
// daemon flag was NOT specified, but daemon-specific flags were
|
|
|
|
// so let's error out
|
|
|
|
fmt.Fprintf(os.Stderr, "docker: the daemon flag '-%s' must follow the 'docker daemon' command.\n", name)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if *flDaemon {
|
|
|
|
daemonCli.(*DaemonCli).CmdDaemon(flag.Args()...)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func presentInHelp(usage string) string { return usage }
|
|
|
|
func absentFromHelp(string) string { return "" }
|
|
|
|
|
|
|
|
// NewDaemonCli returns a pre-configured daemon CLI
|
|
|
|
func NewDaemonCli() *DaemonCli {
|
|
|
|
daemonFlags = cli.Subcmd("daemon", nil, "Enable daemon mode", true)
|
|
|
|
|
|
|
|
// TODO(tiborvass): remove InstallFlags?
|
|
|
|
daemonConfig := new(daemon.Config)
|
2015-08-15 16:06:03 +00:00
|
|
|
daemonConfig.LogConfig.Config = make(map[string]string)
|
2015-05-05 04:18:28 +00:00
|
|
|
daemonConfig.InstallFlags(daemonFlags, presentInHelp)
|
|
|
|
daemonConfig.InstallFlags(flag.CommandLine, absentFromHelp)
|
|
|
|
registryOptions := new(registry.Options)
|
|
|
|
registryOptions.InstallFlags(daemonFlags, presentInHelp)
|
|
|
|
registryOptions.InstallFlags(flag.CommandLine, absentFromHelp)
|
|
|
|
daemonFlags.Require(flag.Exact, 0)
|
|
|
|
|
|
|
|
return &DaemonCli{
|
|
|
|
Config: daemonConfig,
|
|
|
|
registryOptions: registryOptions,
|
2015-05-04 21:39:48 +00:00
|
|
|
}
|
2014-08-10 01:18:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 19:22:31 +00:00
|
|
|
func migrateKey() (err error) {
|
2015-01-22 00:55:05 +00:00
|
|
|
// Migrate trust key if exists at ~/.docker/key.json and owned by current user
|
2015-04-28 15:00:18 +00:00
|
|
|
oldPath := filepath.Join(cliconfig.ConfigDir(), defaultTrustKeyFile)
|
2015-01-22 00:55:05 +00:00
|
|
|
newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile)
|
2015-03-29 19:48:52 +00:00
|
|
|
if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && currentUserIsOwner(oldPath) {
|
2015-01-22 19:22:31 +00:00
|
|
|
defer func() {
|
|
|
|
// Ensure old path is removed if no error occurred
|
|
|
|
if err == nil {
|
|
|
|
err = os.Remove(oldPath)
|
|
|
|
} else {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Warnf("Key migration failed, key file not removed at %s", oldPath)
|
2015-08-03 22:29:54 +00:00
|
|
|
os.Remove(newPath)
|
2015-01-22 19:22:31 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-11 18:29:29 +00:00
|
|
|
if err := system.MkdirAll(getDaemonConfDir(), os.FileMode(0644)); err != nil {
|
2015-01-22 19:22:31 +00:00
|
|
|
return fmt.Errorf("Unable to create daemon configuration directory: %s", err)
|
2015-01-22 00:55:05 +00: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 19:22:31 +00:00
|
|
|
return fmt.Errorf("error opening key file %q: %s", oldPath, err)
|
2015-01-22 00:55:05 +00:00
|
|
|
}
|
2015-01-22 19:22:31 +00:00
|
|
|
defer oldFile.Close()
|
2015-01-22 00:55:05 +00:00
|
|
|
|
|
|
|
if _, err := io.Copy(newFile, oldFile); err != nil {
|
|
|
|
return fmt.Errorf("error copying key: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Infof("Migrated key from %s to %s", oldPath, newPath)
|
2015-01-22 00:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
// DaemonCli represents the daemon CLI.
|
|
|
|
type DaemonCli struct {
|
|
|
|
*daemon.Config
|
|
|
|
registryOptions *registry.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGlobalFlag() (globalFlag *flag.Flag) {
|
|
|
|
defer func() {
|
|
|
|
if x := recover(); x != nil {
|
|
|
|
switch f := x.(type) {
|
|
|
|
case *flag.Flag:
|
|
|
|
globalFlag = f
|
|
|
|
default:
|
|
|
|
panic(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
visitor := func(f *flag.Flag) { panic(f) }
|
|
|
|
commonFlags.FlagSet.Visit(visitor)
|
|
|
|
clientFlags.FlagSet.Visit(visitor)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdDaemon is the daemon command, called the raw arguments after `docker daemon`.
|
|
|
|
func (cli *DaemonCli) CmdDaemon(args ...string) error {
|
2015-09-10 22:01:18 +00:00
|
|
|
// This may need to be made even more global - it all depends
|
|
|
|
// on whether we want the CLI to have a context object too.
|
|
|
|
// For now we'll leave it as a daemon-side object only.
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2015-07-29 21:47:30 +00:00
|
|
|
// warn from uuid package when running the daemon
|
|
|
|
uuid.Loggerf = logrus.Warnf
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
if *flDaemon {
|
|
|
|
// allow legacy forms `docker -D -d` and `docker -d -D`
|
|
|
|
logrus.Warn("please use 'docker daemon' instead.")
|
|
|
|
} else if !commonFlags.FlagSet.IsEmpty() || !clientFlags.FlagSet.IsEmpty() {
|
|
|
|
// deny `docker -D daemon`
|
|
|
|
illegalFlag := getGlobalFlag()
|
|
|
|
fmt.Fprintf(os.Stderr, "invalid flag '-%s'.\nSee 'docker daemon --help'.\n", illegalFlag.Names[0])
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
// allow new form `docker daemon -D`
|
|
|
|
flag.Merge(daemonFlags, commonFlags.FlagSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemonFlags.ParseFlags(args, true)
|
|
|
|
commonFlags.PostParse()
|
|
|
|
|
|
|
|
if len(commonFlags.Hosts) == 0 {
|
|
|
|
commonFlags.Hosts = []string{opts.DefaultHost}
|
|
|
|
}
|
|
|
|
if commonFlags.TrustKey == "" {
|
|
|
|
commonFlags.TrustKey = filepath.Join(getDaemonConfDir(), defaultTrustKeyFile)
|
2015-05-21 17:48:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
if utils.ExperimentalBuild() {
|
|
|
|
logrus.Warn("Running experimental build")
|
2014-08-01 17:34:06 +00:00
|
|
|
}
|
2015-03-28 01:38:00 +00:00
|
|
|
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: timeutils.RFC3339NanoFixed})
|
|
|
|
|
2015-06-15 13:36:19 +00:00
|
|
|
if err := setDefaultUmask(); err != nil {
|
|
|
|
logrus.Fatalf("Failed to set umask: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
if len(cli.LogConfig.Config) > 0 {
|
|
|
|
if err := logger.ValidateLogOpts(cli.LogConfig.Type, cli.LogConfig.Config); err != nil {
|
2015-07-01 00:40:13 +00:00
|
|
|
logrus.Fatalf("Failed to set log opts: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
var pfile *pidfile.PIDFile
|
2015-05-05 04:18:28 +00:00
|
|
|
if cli.Pidfile != "" {
|
|
|
|
pf, err := pidfile.New(cli.Pidfile)
|
2015-04-27 21:11:29 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Error starting daemon: %v", err)
|
|
|
|
}
|
|
|
|
pfile = pf
|
|
|
|
defer func() {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-08-20 15:31:24 +00:00
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
serverConfig := &apiserver.Config{
|
2015-07-16 21:14:58 +00:00
|
|
|
Logging: true,
|
|
|
|
Version: dockerversion.VERSION,
|
2015-05-07 16:49:07 +00:00
|
|
|
}
|
2015-05-05 04:18:28 +00:00
|
|
|
serverConfig = setPlatformServerConfig(serverConfig, cli.Config)
|
2015-05-07 16:49:07 +00:00
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
if commonFlags.TLSOptions != nil {
|
|
|
|
if !commonFlags.TLSOptions.InsecureSkipVerify {
|
|
|
|
// server requires and verifies client's certificate
|
|
|
|
commonFlags.TLSOptions.ClientAuth = tls.RequireAndVerifyClientCert
|
2015-05-07 16:49:07 +00:00
|
|
|
}
|
2015-05-05 04:18:28 +00:00
|
|
|
tlsConfig, err := tlsconfig.Server(*commonFlags.TLSOptions)
|
2015-05-07 16:49:07 +00:00
|
|
|
if err != nil {
|
2015-07-30 18:15:41 +00:00
|
|
|
logrus.Fatal(err)
|
2015-05-07 16:49:07 +00:00
|
|
|
}
|
|
|
|
serverConfig.TLSConfig = tlsConfig
|
2015-04-16 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 22:01:18 +00:00
|
|
|
api := apiserver.New(ctx, serverConfig)
|
2015-04-17 21:32:18 +00:00
|
|
|
|
2015-04-16 19:48:04 +00:00
|
|
|
// The serve API routine never exits unless an error occurs
|
2015-03-11 14:33:06 +00:00
|
|
|
// We need to start it as a goroutine and wait on it so
|
|
|
|
// daemon doesn't exit
|
|
|
|
serveAPIWait := make(chan error)
|
|
|
|
go func() {
|
2015-07-21 05:15:44 +00:00
|
|
|
if err := api.ServeAPI(commonFlags.Hosts); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("ServeAPI error: %v", err)
|
2015-03-11 14:33:06 +00:00
|
|
|
serveAPIWait <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
serveAPIWait <- nil
|
|
|
|
}()
|
2015-03-06 20:44:31 +00:00
|
|
|
|
2015-05-07 22:35:12 +00:00
|
|
|
if err := migrateKey(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2015-05-05 04:18:28 +00:00
|
|
|
cli.TrustKeyPath = commonFlags.TrustKey
|
2015-05-07 22:35:12 +00:00
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
registryService := registry.NewService(cli.registryOptions)
|
2015-09-10 22:01:18 +00:00
|
|
|
d, err := daemon.NewDaemon(ctx, cli.Config, registryService)
|
2015-05-07 22:35:12 +00:00
|
|
|
if err != nil {
|
|
|
|
if pfile != nil {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.Fatalf("Error starting daemon: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Info("Daemon has completed initialization")
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"version": dockerversion.VERSION,
|
|
|
|
"commit": dockerversion.GITCOMMIT,
|
2015-09-10 22:01:18 +00:00
|
|
|
"execdriver": d.ExecutionDriver(ctx).Name(),
|
|
|
|
"graphdriver": d.GraphDriver(ctx).String(),
|
2015-05-07 22:35:12 +00:00
|
|
|
}).Info("Docker daemon")
|
|
|
|
|
2015-04-27 21:11:29 +00:00
|
|
|
signal.Trap(func() {
|
|
|
|
api.Close()
|
|
|
|
<-serveAPIWait
|
2015-09-10 22:01:18 +00:00
|
|
|
shutdownDaemon(ctx, d, 15)
|
2015-04-27 21:11:29 +00:00
|
|
|
if pfile != nil {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-04-17 19:03:11 +00:00
|
|
|
|
|
|
|
// after the daemon is done setting up we can tell the api to start
|
2015-04-17 21:32:18 +00:00
|
|
|
// accepting connections with specified daemon
|
2015-09-10 22:01:18 +00:00
|
|
|
api.AcceptConnections(ctx, d)
|
2015-04-17 19:03:11 +00:00
|
|
|
|
2015-03-11 14:33:06 +00:00
|
|
|
// Daemon is fully initialized and handling API traffic
|
2015-04-27 21:11:29 +00:00
|
|
|
// Wait for serve API to complete
|
2015-03-11 14:33:06 +00:00
|
|
|
errAPI := <-serveAPIWait
|
2015-09-10 22:01:18 +00:00
|
|
|
shutdownDaemon(ctx, d, 15)
|
2015-03-11 14:33:06 +00:00
|
|
|
if errAPI != nil {
|
2015-04-27 21:11:29 +00:00
|
|
|
if pfile != nil {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Fatalf("Shutting down due to ServeAPI error: %v", errAPI)
|
2015-03-11 14:33:06 +00:00
|
|
|
}
|
2015-05-05 04:18:28 +00:00
|
|
|
return nil
|
2014-08-01 17:34:06 +00:00
|
|
|
}
|
2015-03-29 19:48:52 +00:00
|
|
|
|
2015-04-27 21:11:29 +00: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-10 22:01:18 +00:00
|
|
|
func shutdownDaemon(ctx context.Context, d *daemon.Daemon, timeout time.Duration) {
|
2015-04-27 21:11:29 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-09-10 22:01:18 +00:00
|
|
|
d.Shutdown(ctx)
|
2015-04-27 21:11:29 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
2015-08-07 22:24:18 +00:00
|
|
|
logrus.Debug("Clean shutdown succeeded")
|
2015-04-27 21:11:29 +00:00
|
|
|
case <-time.After(timeout * time.Second):
|
|
|
|
logrus.Error("Force shutdown daemon")
|
|
|
|
}
|
|
|
|
}
|