From 508b96202f2b9b3dd91adb06770568aaa1f5f9b1 Mon Sep 17 00:00:00 2001 From: wheresalice Date: Sat, 7 Apr 2018 15:12:42 +0100 Subject: [PATCH 1/2] change logic to only call PullImage if necessary, and PullImage always pulls --- docker.go | 82 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/docker.go b/docker.go index 995f111..4529899 100644 --- a/docker.go +++ b/docker.go @@ -190,8 +190,10 @@ func (c *DockerClient) StartContainer(rm bool, name string) (string, error) { "cmd": fmt.Sprintf("%v", c.Conf.Cmd), }).Debug("Creating new container") - if err := c.PullImage(c.Conf.Image); err != nil { - return "", fmt.Errorf("Failed to fetch image: %s", err) + if !c.ImageExists(c.Conf.Image){ + if err := c.PullImage(c.Conf.Image); err != nil { + return "", fmt.Errorf("Failed to fetch image: %s", err) + } } resp, err := c.Cli.ContainerCreate(context.Background(), c.Conf, c.HostConf, c.NetConf, name) @@ -358,54 +360,52 @@ func (c *DockerClient) ImageExists(image string) bool { // PullImage - Pull an image locally func (c *DockerClient) PullImage(image string) error { - if !c.ImageExists(image) { - log.WithFields(log.Fields{ - "image": image, - }).Info("Pulling image layers... please wait") + log.WithFields(log.Fields{ + "image": image, + }).Info("Pulling image layers... please wait") - resp, err := c.Cli.ImagePull(context.Background(), image, types.ImagePullOptions{}) + resp, err := c.Cli.ImagePull(context.Background(), image, types.ImagePullOptions{}) - if err != nil { - return fmt.Errorf("API could not fetch \"%s\": %s", image, err) + if err != nil { + return fmt.Errorf("API could not fetch \"%s\": %s", image, err) + } + scanner := bufio.NewScanner(resp) + var cr CreateResponse + bar := pb.New(1) + // Send progress bar to stderr to keep stdout clean when piping + bar.Output = os.Stderr + bar.ShowCounters = true + bar.ShowTimeLeft = false + bar.ShowSpeed = false + bar.Prefix(" ") + bar.Postfix(" ") + started := false + + for scanner.Scan() { + txt := scanner.Text() + byt := []byte(txt) + + if err := json.Unmarshal(byt, &cr); err != nil { + return fmt.Errorf("Error decoding json from create image API: %s", err) } - scanner := bufio.NewScanner(resp) - var cr CreateResponse - bar := pb.New(1) - // Send progress bar to stderr to keep stdout clean when piping - bar.Output = os.Stderr - bar.ShowCounters = true - bar.ShowTimeLeft = false - bar.ShowSpeed = false - bar.Prefix(" ") - bar.Postfix(" ") - started := false - - for scanner.Scan() { - txt := scanner.Text() - byt := []byte(txt) - - if err := json.Unmarshal(byt, &cr); err != nil { - return fmt.Errorf("Error decoding json from create image API: %s", err) - } - if cr.Status == "Downloading" { + if cr.Status == "Downloading" { - if !started { - fmt.Print("\n") - bar.Total = int64(cr.ProgressDetail.Total) - bar.Start() - started = true - } + if !started { + fmt.Print("\n") bar.Total = int64(cr.ProgressDetail.Total) - bar.Set(cr.ProgressDetail.Current) + bar.Start() + started = true } + bar.Total = int64(cr.ProgressDetail.Total) + bar.Set(cr.ProgressDetail.Current) } + } - if err := scanner.Err(); err != nil { - return fmt.Errorf("Failed to get logs: %s", err) - } - bar.Finish() - fmt.Print("\n") + if err := scanner.Err(); err != nil { + return fmt.Errorf("Failed to get logs: %s", err) } + bar.Finish() + fmt.Print("\n") return nil } From 2daf34523b252718561e9558602e665c621694e6 Mon Sep 17 00:00:00 2001 From: wheresalice Date: Sat, 7 Apr 2018 19:22:38 +0100 Subject: [PATCH 2/2] reflect changes to PullImage and StartContainer in CHANGELOG --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7991da..c3422b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -[Unreleased]: https://github.com/skybet/cali/compare/v0.1.1...master +[Unreleased]: https://github.com/skybet/cali/compare/v0.1.2...master ## [Unreleased] - +[0.1.2]: https://github.com/skybet/cali/compare/v0.1.1...v0.1.2 +## [0.1.2] - 2018-04-07 +### Fixed +- PullImage now always pulls as should be expected +- StartContainer now only calls PullImage when the image does not exist locally [0.1.1]: https://github.com/skybet/cali/compare/v0.1.0...v0.1.1 ## [0.1.1] - 2018-02-11