2014-08-01 13:34:06 -04:00
|
|
|
// +build daemon
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-21 19:55:05 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-03-11 10:33:06 -04:00
|
|
|
"strings"
|
2015-01-21 19:55:05 -05:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-04 16:22:38 -05:00
|
|
|
"github.com/docker/docker/autogen/dockerversion"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2014-08-01 13:34:06 -04:00
|
|
|
"github.com/docker/docker/builtins"
|
2014-08-08 05:12:39 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2014-08-08 16:18:18 -04:00
|
|
|
_ "github.com/docker/docker/daemon/execdriver/lxc"
|
|
|
|
_ "github.com/docker/docker/daemon/execdriver/native"
|
2014-08-01 13:34:06 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2015-02-06 13:18:49 -05:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2014-08-01 13:34:06 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-08-06 04:12:22 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-03-29 15:48:52 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-03-27 21:38:00 -04:00
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
2014-08-20 11:31:24 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2014-08-01 13:34:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const CanDaemon = true
|
|
|
|
|
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() {
|
|
|
|
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-02-06 13:18:49 -05:00
|
|
|
oldPath := filepath.Join(homedir.Get(), ".docker", 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-01-21 19:55:05 -05:00
|
|
|
if err := os.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() {
|
|
|
|
if flag.NArg() != 0 {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
2015-03-27 21:38:00 -04:00
|
|
|
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: timeutils.RFC3339NanoFixed})
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
eng := engine.New()
|
2014-08-06 04:12:22 -04:00
|
|
|
signal.Trap(eng.Shutdown)
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2015-01-21 19:55:05 -05:00
|
|
|
if err := migrateKey(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Fatal(err)
|
2015-01-21 19:55:05 -05:00
|
|
|
}
|
2014-11-17 14:23:41 -05:00
|
|
|
daemonCfg.TrustKeyPath = *flTrustKey
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
// Load builtins
|
|
|
|
if err := builtins.Register(eng); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Fatal(err)
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
|
|
|
|
2015-03-31 19:21:37 -04:00
|
|
|
registryService := registry.NewService(registryCfg)
|
2014-08-01 13:34:06 -04:00
|
|
|
// load the daemon in the background so we can immediately start
|
|
|
|
// the http api so that connections don't fail while the daemon
|
|
|
|
// is booting
|
2015-03-11 10:33:06 -04:00
|
|
|
daemonInitWait := make(chan error)
|
2014-08-01 13:34:06 -04:00
|
|
|
go func() {
|
2015-03-31 19:21:37 -04:00
|
|
|
d, err := daemon.NewDaemon(daemonCfg, eng, registryService)
|
2014-08-08 05:12:39 -04:00
|
|
|
if err != nil {
|
2015-03-11 10:33:06 -04:00
|
|
|
daemonInitWait <- err
|
2015-03-06 15:44:31 -05:00
|
|
|
return
|
2014-08-08 05:12:39 -04:00
|
|
|
}
|
2015-03-06 15:44:31 -05:00
|
|
|
|
2015-03-21 22:39:44 -04:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"version": dockerversion.VERSION,
|
|
|
|
"commit": dockerversion.GITCOMMIT,
|
|
|
|
"execdriver": d.ExecutionDriver().Name(),
|
|
|
|
"graphdriver": d.GraphDriver().String(),
|
|
|
|
}).Info("Docker daemon")
|
2014-10-21 03:00:25 -04:00
|
|
|
|
2014-08-08 05:12:39 -04:00
|
|
|
if err := d.Install(eng); err != nil {
|
2015-03-11 10:33:06 -04:00
|
|
|
daemonInitWait <- err
|
2015-03-06 15:44:31 -05:00
|
|
|
return
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
|
|
|
|
b := &builder.BuilderJob{eng, d}
|
|
|
|
b.Install()
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
// after the daemon is done setting up we can tell the api to start
|
|
|
|
// accepting connections
|
|
|
|
if err := eng.Job("acceptconnections").Run(); err != nil {
|
2015-03-11 10:33:06 -04:00
|
|
|
daemonInitWait <- err
|
2015-03-06 15:44:31 -05:00
|
|
|
return
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-11 10:33:06 -04:00
|
|
|
daemonInitWait <- nil
|
2014-08-01 13:34:06 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Serve api
|
2014-08-11 18:30:01 -04:00
|
|
|
job := eng.Job("serveapi", flHosts...)
|
2014-08-01 13:34:06 -04:00
|
|
|
job.SetenvBool("Logging", true)
|
2015-01-29 21:55:22 -05:00
|
|
|
job.SetenvBool("EnableCors", daemonCfg.EnableCors)
|
2015-02-09 02:15:07 -05:00
|
|
|
job.Setenv("CorsHeaders", daemonCfg.CorsHeaders)
|
2014-08-01 13:34:06 -04:00
|
|
|
job.Setenv("Version", dockerversion.VERSION)
|
2015-01-29 21:55:22 -05:00
|
|
|
job.Setenv("SocketGroup", daemonCfg.SocketGroup)
|
2014-08-01 13:34:06 -04:00
|
|
|
|
|
|
|
job.SetenvBool("Tls", *flTls)
|
|
|
|
job.SetenvBool("TlsVerify", *flTlsVerify)
|
|
|
|
job.Setenv("TlsCa", *flCa)
|
|
|
|
job.Setenv("TlsCert", *flCert)
|
|
|
|
job.Setenv("TlsKey", *flKey)
|
2015-03-11 10:33:06 -04:00
|
|
|
|
|
|
|
// The serve API job 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)
|
|
|
|
go func() {
|
|
|
|
if err := job.Run(); 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
|
|
|
|
|
|
|
// Wait for the daemon startup goroutine to finish
|
|
|
|
// This makes sure we can actually cleanly shutdown the daemon
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debug("waiting for daemon to initialize")
|
2015-03-11 10:33:06 -04:00
|
|
|
errDaemon := <-daemonInitWait
|
|
|
|
if errDaemon != nil {
|
|
|
|
eng.Shutdown()
|
|
|
|
outStr := fmt.Sprintf("Shutting down daemon due to errors: %v", errDaemon)
|
|
|
|
if strings.Contains(errDaemon.Error(), "engine is shutdown") {
|
|
|
|
// if the error is "engine is shutdown", we've already reported (or
|
|
|
|
// will report below in API server errors) the error
|
|
|
|
outStr = "Shutting down daemon due to reported errors"
|
|
|
|
}
|
|
|
|
// we must "fatal" exit here as the API server may be happy to
|
|
|
|
// continue listening forever if the error had no impact to API
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Fatal(outStr)
|
2015-03-11 10:33:06 -04:00
|
|
|
} else {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Info("Daemon has completed initialization")
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-06 15:44:31 -05:00
|
|
|
|
2015-03-11 10:33:06 -04:00
|
|
|
// Daemon is fully initialized and handling API traffic
|
|
|
|
// Wait for serve API job to complete
|
|
|
|
errAPI := <-serveAPIWait
|
|
|
|
// If we have an error here it is unique to API (as daemonErr would have
|
|
|
|
// exited the daemon process above)
|
2015-03-18 17:26:14 -04:00
|
|
|
eng.Shutdown()
|
2015-03-11 10:33:06 -04:00
|
|
|
if errAPI != nil {
|
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
|
|
|
}
|
2015-03-18 17:26:14 -04:00
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-29 15:48:52 -04:00
|
|
|
|
|
|
|
// currentUserIsOwner checks whether the current user is the owner of the given
|
|
|
|
// file.
|
|
|
|
func currentUserIsOwner(f string) bool {
|
|
|
|
if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil {
|
|
|
|
if int(fileInfo.Uid()) == os.Getuid() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|