diff --git a/builder/MAINTAINERS b/builder/MAINTAINERS new file mode 100644 index 0000000000000..4d158aa20e30a --- /dev/null +++ b/builder/MAINTAINERS @@ -0,0 +1,2 @@ +Tibor Vass (@tiborvass) +Erik Hollensbe (@erikh) diff --git a/server/buildfile.go b/builder/builder.go similarity index 97% rename from server/buildfile.go rename to builder/builder.go index e8e8dbec5a5c9..ae402345dd213 100644 --- a/server/buildfile.go +++ b/builder/builder.go @@ -1,4 +1,4 @@ -package server +package builder import ( "crypto/sha256" @@ -21,6 +21,7 @@ import ( "github.com/docker/docker/archive" "github.com/docker/docker/daemon" + "github.com/docker/docker/engine" "github.com/docker/docker/nat" "github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/system" @@ -41,7 +42,7 @@ type BuildFile interface { type buildFile struct { daemon *daemon.Daemon - srv *Server + eng *engine.Engine image string maintainer string @@ -96,7 +97,7 @@ func (b *buildFile) CmdFrom(name string) error { resolvedAuth := b.configFile.ResolveAuthConfig(endpoint) pullRegistryAuth = &resolvedAuth } - job := b.srv.Eng.Job("pull", remote, tag) + job := b.eng.Job("pull", remote, tag) job.SetenvBool("json", b.sf.Json()) job.SetenvBool("parallel", true) job.SetenvJson("authConfig", pullRegistryAuth) @@ -167,12 +168,12 @@ func (b *buildFile) CmdMaintainer(name string) error { // probeCache checks to see if image-caching is enabled (`b.utilizeCache`) // and if so attempts to look up the current `b.image` and `b.config` pair -// in the current server `b.srv`. If an image is found, probeCache returns +// in the current server `b.daemon`. If an image is found, probeCache returns // `(true, nil)`. If no image is found, it returns `(false, nil)`. If there // is any error, it returns `(false, err)`. func (b *buildFile) probeCache() (bool, error) { if b.utilizeCache { - if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil { + if cache, err := b.daemon.ImageGetCached(b.image, b.config); err != nil { return false, err } else if cache != nil { fmt.Fprintf(b.outStream, " ---> Using cache\n") @@ -889,10 +890,10 @@ func fixPermissions(destination string, uid, gid int) error { }) } -func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, forceRm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile { +func NewBuildFile(d *daemon.Daemon, eng *engine.Engine, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, forceRm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile { return &buildFile{ - daemon: srv.daemon, - srv: srv, + daemon: d, + eng: eng, config: &runconfig.Config{}, outStream: outStream, errStream: errStream, diff --git a/daemon/container.go b/daemon/container.go index fad6103c75263..537cfc6a7546d 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -513,7 +513,7 @@ func (container *Container) monitor(callback execdriver.StartCallback) error { if container.daemon != nil && container.daemon.srv != nil && container.daemon.srv.IsRunning() { // FIXME: here is race condition between two RUN instructions in Dockerfile // because they share same runconfig and change image. Must be fixed - // in server/buildfile.go + // in builder/builder.go if err := container.toDisk(); err != nil { utils.Errorf("Error dumping container %s state to disk: %s\n", container.ID, err) } diff --git a/daemon/daemon.go b/daemon/daemon.go index 5703269431813..0fadc51d75aa0 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -1099,3 +1099,35 @@ func (daemon *Daemon) checkLocaldns() error { } return nil } + +func (daemon *Daemon) ImageGetCached(imgID string, config *runconfig.Config) (*image.Image, error) { + // Retrieve all images + images, err := daemon.Graph().Map() + if err != nil { + return nil, err + } + + // Store the tree in a map of map (map[parentId][childId]) + imageMap := make(map[string]map[string]struct{}) + for _, img := range images { + if _, exists := imageMap[img.Parent]; !exists { + imageMap[img.Parent] = make(map[string]struct{}) + } + imageMap[img.Parent][img.ID] = struct{}{} + } + + // Loop on the children of the given image and check the config + var match *image.Image + for elem := range imageMap[imgID] { + img, err := daemon.Graph().Get(elem) + if err != nil { + return nil, err + } + if runconfig.Compare(&img.ContainerConfig, config) { + if match == nil || match.Created.Before(img.Created) { + match = img + } + } + } + return match, nil +} diff --git a/server/server.go b/server/server.go index 87c910b580a13..072b36f81657d 100644 --- a/server/server.go +++ b/server/server.go @@ -46,6 +46,7 @@ import ( "time" "github.com/docker/docker/archive" + "github.com/docker/docker/builder" "github.com/docker/docker/daemon" "github.com/docker/docker/daemonconfig" "github.com/docker/docker/dockerversion" @@ -534,7 +535,7 @@ func (srv *Server) Build(job *engine.Job) engine.Status { defer context.Close() sf := utils.NewStreamFormatter(job.GetenvBool("json")) - b := NewBuildFile(srv, + b := builder.NewBuildFile(srv.daemon, srv.Eng, &utils.StdoutFormater{ Writer: job.Stdout, StreamFormatter: sf, @@ -2058,38 +2059,6 @@ func (srv *Server) canDeleteImage(imgID string, force, untagged bool) error { return nil } -func (srv *Server) ImageGetCached(imgID string, config *runconfig.Config) (*image.Image, error) { - // Retrieve all images - images, err := srv.daemon.Graph().Map() - if err != nil { - return nil, err - } - - // Store the tree in a map of map (map[parentId][childId]) - imageMap := make(map[string]map[string]struct{}) - for _, img := range images { - if _, exists := imageMap[img.Parent]; !exists { - imageMap[img.Parent] = make(map[string]struct{}) - } - imageMap[img.Parent][img.ID] = struct{}{} - } - - // Loop on the children of the given image and check the config - var match *image.Image - for elem := range imageMap[imgID] { - img, err := srv.daemon.Graph().Get(elem) - if err != nil { - return nil, err - } - if runconfig.Compare(&img.ContainerConfig, config) { - if match == nil || match.Created.Before(img.Created) { - match = img - } - } - } - return match, nil -} - func (srv *Server) setHostConfig(container *daemon.Container, hostConfig *runconfig.HostConfig) error { // Validate the HostConfig binds. Make sure that: // the source exists