2014-08-01 13:34:06 -04:00
|
|
|
// +build daemon
|
|
|
|
|
|
|
|
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"
|
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-04-16 15:48:04 -04:00
|
|
|
apiserver "github.com/docker/docker/api/server"
|
2015-02-04 16:22:38 -05:00
|
|
|
"github.com/docker/docker/autogen/dockerversion"
|
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"
|
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"
|
2015-03-27 21:38:00 -04:00
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2015-05-07 12:49:07 -04:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2014-08-20 11:31:24 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2015-05-21 13:48:36 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-01 13:34:06 -04:00
|
|
|
)
|
|
|
|
|
2014-08-09 21:18:32 -04:00
|
|
|
var (
|
2014-10-06 21:54:52 -04:00
|
|
|
daemonCfg = &daemon.Config{}
|
|
|
|
registryCfg = ®istry.Options{}
|
2014-08-09 21:18:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-05-04 17:39:48 -04:00
|
|
|
if daemonCfg.LogConfig.Config == nil {
|
|
|
|
daemonCfg.LogConfig.Config = make(map[string]string)
|
|
|
|
}
|
2014-08-09 21:18:32 -04:00
|
|
|
daemonCfg.InstallFlags()
|
2014-10-06 21:54:52 -04:00
|
|
|
registryCfg.InstallFlags()
|
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
|
2015-04-28 11:00:18 -04:00
|
|
|
oldPath := filepath.Join(cliconfig.ConfigDir(), defaultTrustKeyFile)
|
2015-01-21 19:55:05 -05:00
|
|
|
newPath := filepath.Join(getDaemonConfDir(), 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-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
|
|
|
|
}
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
func mainDaemon() {
|
2015-05-21 13:48:36 -04:00
|
|
|
if utils.ExperimentalBuild() {
|
|
|
|
logrus.Warn("Running experimental build")
|
|
|
|
}
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
if flag.NArg() != 0 {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
2015-03-27 21:38:00 -04:00
|
|
|
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: timeutils.RFC3339NanoFixed})
|
|
|
|
|
2015-06-15 09:36:19 -04:00
|
|
|
if err := setDefaultUmask(); err != nil {
|
|
|
|
logrus.Fatalf("Failed to set umask: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
var pfile *pidfile.PidFile
|
|
|
|
if daemonCfg.Pidfile != "" {
|
|
|
|
pf, err := pidfile.New(daemonCfg.Pidfile)
|
|
|
|
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 11:31:24 -04:00
|
|
|
|
2015-04-16 15:48:04 -04:00
|
|
|
serverConfig := &apiserver.ServerConfig{
|
|
|
|
Logging: true,
|
|
|
|
EnableCors: daemonCfg.EnableCors,
|
|
|
|
CorsHeaders: daemonCfg.CorsHeaders,
|
|
|
|
Version: dockerversion.VERSION,
|
2015-05-07 12:49:07 -04:00
|
|
|
}
|
2015-05-15 19:34:26 -04:00
|
|
|
serverConfig = setPlatformServerConfig(serverConfig, daemonCfg)
|
2015-05-07 12:49:07 -04:00
|
|
|
|
|
|
|
if *flTls {
|
|
|
|
if *flTlsVerify {
|
|
|
|
tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Server(tlsOptions)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
serverConfig.TLSConfig = tlsConfig
|
2015-04-16 15:48:04 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
api := apiserver.New(serverConfig)
|
2015-04-17 17:32:18 -04:00
|
|
|
|
2015-04-16 15:48:04 -04:00
|
|
|
// The serve API routine never exits unless an error occurs
|
2015-03-11 10:33:06 -04: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-04-17 17:32:18 -04:00
|
|
|
if err := api.ServeApi(flHosts); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("ServeAPI error: %v", err)
|
2015-03-11 10:33:06 -04:00
|
|
|
serveAPIWait <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
serveAPIWait <- nil
|
|
|
|
}()
|
2015-03-06 15:44:31 -05:00
|
|
|
|
2015-05-07 18:35:12 -04:00
|
|
|
if err := migrateKey(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
daemonCfg.TrustKeyPath = *flTrustKey
|
|
|
|
|
|
|
|
registryService := registry.NewService(registryCfg)
|
|
|
|
d, err := daemon.NewDaemon(daemonCfg, registryService)
|
|
|
|
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,
|
|
|
|
"execdriver": d.ExecutionDriver().Name(),
|
|
|
|
"graphdriver": d.GraphDriver().String(),
|
|
|
|
}).Info("Docker daemon")
|
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
signal.Trap(func() {
|
|
|
|
api.Close()
|
|
|
|
<-serveAPIWait
|
|
|
|
shutdownDaemon(d, 15)
|
|
|
|
if pfile != nil {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-04-17 15:03:11 -04:00
|
|
|
|
|
|
|
// after the daemon is done setting up we can tell the api to start
|
2015-04-17 17:32:18 -04:00
|
|
|
// accepting connections with specified daemon
|
|
|
|
api.AcceptConnections(d)
|
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
|
2015-04-27 17:11:29 -04:00
|
|
|
shutdownDaemon(d, 15)
|
2015-03-11 10:33:06 -04:00
|
|
|
if errAPI != nil {
|
2015-04-27 17:11:29 -04:00
|
|
|
if pfile != nil {
|
|
|
|
if err := pfile.Remove(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Fatalf("Shutting down due to ServeAPI error: %v", errAPI)
|
2015-03-11 10:33:06 -04:00
|
|
|
}
|
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
|
|
|
|
func shutdownDaemon(d *daemon.Daemon, timeout time.Duration) {
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
d.Shutdown()
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
logrus.Debug("Clean shutdown succeded")
|
|
|
|
case <-time.After(timeout * time.Second):
|
|
|
|
logrus.Error("Force shutdown daemon")
|
|
|
|
}
|
|
|
|
}
|