github.com/dnephin/dobi@v0.15.0/tasks/image/pull.go (about) 1 package image 2 3 import ( 4 "io" 5 "os" 6 "time" 7 8 "github.com/dnephin/dobi/tasks/context" 9 docker "github.com/fsouza/go-dockerclient" 10 ) 11 12 // RunPull builds or pulls an image if it is out of date 13 func RunPull(ctx *context.ExecuteContext, t *Task, _ bool) (bool, error) { 14 record, err := getImageRecord(recordPath(ctx, t.config)) 15 switch { 16 case !t.config.Pull.Required(record.LastPull): 17 t.logger().Debugf("Pull not required") 18 return false, nil 19 case err != nil: 20 t.logger().Warnf("Failed to get image record: %s", err) 21 } 22 23 pullTag := func(tag string) error { 24 return pullImage(ctx, t, tag) 25 } 26 if err := t.ForEachTag(ctx, pullTag); err != nil { 27 return false, err 28 } 29 30 image, err := GetImage(ctx, t.config) 31 if err != nil { 32 return false, err 33 } 34 record = imageModifiedRecord{LastPull: now(), ImageID: image.ID} 35 36 if err := updateImageRecord(recordPath(ctx, t.config), record); err != nil { 37 t.logger().Warnf("Failed to update image record: %s", err) 38 } 39 40 t.logger().Info("Pulled") 41 return true, nil 42 } 43 44 func now() *time.Time { 45 now := time.Now() 46 return &now 47 } 48 49 func pullImage(ctx *context.ExecuteContext, t *Task, imageTag string) error { 50 registry := parseAuthRepo(t.config.Image) 51 repo, tag := docker.ParseRepositoryTag(imageTag) 52 return Stream(os.Stdout, func(out io.Writer) error { 53 return ctx.Client.PullImage(docker.PullImageOptions{ 54 Repository: repo, 55 Tag: tag, 56 OutputStream: out, 57 RawJSONStream: true, 58 // TODO: timeout 59 }, ctx.GetAuthConfig(registry)) 60 }) 61 }