From e443c748c5abae9b3d8f6392869a40b692a40d14 Mon Sep 17 00:00:00 2001 From: Emmanuel Quentin Date: Fri, 6 Jun 2014 17:36:22 +0200 Subject: [PATCH] Do not rebuild binaries each time --- container/container.go | 10 ++++++---- docker/docker.go | 36 ++++++++++++++++++++++++++++++++++++ gaudi/gaudi_unit_test.go | 1 + 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/container/container.go b/container/container.go index d7d1279..ecfa367 100644 --- a/container/container.go +++ b/container/container.go @@ -60,7 +60,7 @@ func (c *Container) Init() { if c.Add == nil { c.Add = make(map[string]string) } - + if c.Custom == nil { c.Custom = make(map[string]interface{}) } @@ -213,9 +213,11 @@ func (c *Container) Start(rebuild bool) { } func (c *Container) BuildAndRun(currentPath string, arguments []string) { - buildChans := make(chan bool, 1) - go c.BuildOrPull(buildChans) - <-buildChans + if docker.ShouldRebuild(c.Image) { + buildChans := make(chan bool, 1) + go c.BuildOrPull(buildChans) + <-buildChans + } c.Run(currentPath, arguments) } diff --git a/docker/docker.go b/docker/docker.go index 74dbcbb..f8c067f 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -220,6 +220,42 @@ func SnapshotProcesses() (map[string]string, error) { return images, nil } +func ShouldRebuild(imageName string) bool { + images, err := GetImages() + + if err != nil { + return false + } + + _, ok := images[imageName] + return !ok +} + +func GetImages() (map[string]string, error) { + images := make(map[string]string) + + imagesCommand := exec.Command(getDockerBinaryPath(), "images") + out, err := imagesCommand.CombinedOutput() + if err != nil { + return nil, errors.New(string(out)) + } + + // Retrieve lines & remove first and last one + lines := strings.Split(string(out), "\n") + lines = lines[1 : len(lines)-1] + + for _, line := range lines { + fields := strings.Fields(line) + if fields[0] == "" { + continue + } + + images[fields[0]] = fields[2] + } + + return images, nil +} + func getDockerBinaryPath() string { if len(docker) != 0 { return docker diff --git a/gaudi/gaudi_unit_test.go b/gaudi/gaudi_unit_test.go index 5663b16..ed3bcd3 100644 --- a/gaudi/gaudi_unit_test.go +++ b/gaudi/gaudi_unit_test.go @@ -297,6 +297,7 @@ func (s *GaudiTestSuite) TestStartBinariesShouldCleanAndBuildThem(c *C) { docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1) docker.EXPECT().HasDocker().Return(true).Times(1) + docker.EXPECT().ShouldRebuild(gomock.Any()).Return(true).Times(1) util.EXPECT().PrintGreen("Building", "gaudi/npm", "...") docker.EXPECT().Build(gomock.Any(), gomock.Any()).Times(1)