Skip to content

Commit

Permalink
Merge pull request #38 from WheresAlice/pull_logic
Browse files Browse the repository at this point in the history
change logic to only call PullImage if necessary, and PullImage always pulls
  • Loading branch information
lucymhdavies authored Apr 7, 2018
2 parents 73432e8 + 1162159 commit 5d97dd3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ 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]

- Mostly refactoring at this point


[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
- Other miscelaneous refactoring

[0.1.1]: https://github.com/skybet/cali/compare/v0.1.0...v0.1.1
## [0.1.1] - 2018-02-11
Expand Down
82 changes: 41 additions & 41 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

0 comments on commit 5d97dd3

Please sign in to comment.