Skip to content

Commit

Permalink
core: job-run pull image
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Dec 23, 2015
1 parent 5c6653d commit 567e99f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
58 changes: 55 additions & 3 deletions core/runjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ package core

import (
"fmt"
"strings"
"time"

"github.com/fsouza/go-dockerclient"
"github.com/gobs/args"
)

var dockercfg *docker.AuthConfigurations

func init() {
dockercfg, _ = docker.NewAuthConfigurationsFromDockerCfg()
}

type RunJob struct {
BareJob
Client *docker.Client `json:"-"`
User string `default:"root"`
TTY bool `default:"false"`
Delete bool `default:"true"`
Image string
User string `default:"root"`
TTY bool `default:"false"`
Delete bool `default:"true"`
}

func NewRunJob(c *docker.Client) *RunJob {
return &RunJob{Client: c}
}

func (j *RunJob) Run(ctx *Context) error {
if err := j.pullImage(); err != nil {
return err
}

container, err := j.buildContainer()
if err != nil {
return err
Expand All @@ -38,6 +49,15 @@ func (j *RunJob) Run(ctx *Context) error {
return j.deleteContainer(container.ID)
}

func (j *RunJob) pullImage() error {
o, a := buildPullOptions(j.Image)
if err := j.Client.PullImage(o, a); err != nil {
return fmt.Errorf("error pulling image %q: %s", j.Image, err)
}

return nil
}

func (j *RunJob) buildContainer() (*docker.Container, error) {
c, err := j.Client.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Expand Down Expand Up @@ -108,3 +128,35 @@ func (j *RunJob) deleteContainer(containerID string) error {
ID: containerID,
})
}

func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) {
tag := "latest"
registry := ""

parts := strings.Split(image, ":")
if len(parts) == 2 {
tag = parts[1]
}

name := parts[0]
parts = strings.Split(name, "/")
if len(parts) > 2 {
registry = parts[0]
}

return docker.PullImageOptions{
Repository: name,
Registry: registry,
Tag: tag,
}, buildAuthConfiguration(registry)
}

func buildAuthConfiguration(registry string) docker.AuthConfiguration {
var auth docker.AuthConfiguration
if dockercfg == nil {
return auth
}

auth, _ = dockercfg.Configs[registry]
return auth
}
21 changes: 21 additions & 0 deletions core/runjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ func (s *SuiteRunJob) TestRun(c *C) {
c.Assert(containers, HasLen, 0)
}

func (s *SuiteRunJob) TestBuildPullImageOptionsBareImage(c *C) {
o, _ := buildPullOptions("foo")
c.Assert(o.Repository, Equals, "foo")
c.Assert(o.Tag, Equals, "latest")
c.Assert(o.Registry, Equals, "")
}

func (s *SuiteRunJob) TestBuildPullImageOptionsVersion(c *C) {
o, _ := buildPullOptions("foo:qux")
c.Assert(o.Repository, Equals, "foo")
c.Assert(o.Tag, Equals, "qux")
c.Assert(o.Registry, Equals, "")
}

func (s *SuiteRunJob) TestBuildPullImageOptionsRegistry(c *C) {
o, _ := buildPullOptions("quay.io/srcd/rest:qux")
c.Assert(o.Repository, Equals, "quay.io/srcd/rest")
c.Assert(o.Tag, Equals, "qux")
c.Assert(o.Registry, Equals, "quay.io")
}

func (s *SuiteRunJob) buildImage(c *C) {
inputbuf := bytes.NewBuffer(nil)
tr := tar.NewWriter(inputbuf)
Expand Down

0 comments on commit 567e99f

Please sign in to comment.