Skip to content

Commit

Permalink
docker: adds functions to tag and commit image
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 70d170d commit 15743c0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
82 changes: 82 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package docker

import (
"context"
"fmt"
"os"
"strings"

"github.com/fsouza/go-dockerclient"
)
Expand All @@ -16,6 +19,21 @@ type Container struct {
ID string
}

type Image struct {
ID string
registry string
repository string
tag string
}

func (i Image) Name() string {
return i.registry + "/" + i.repository
}

func (i Image) String() string {
return i.registry + "/" + i.repository + ":" + i.tag
}

type Client struct {
api *docker.Client
}
Expand Down Expand Up @@ -48,3 +66,67 @@ func (c *Client) ListContainersByLabels(labels map[string]string) ([]Container,
}
return conts, err
}

func (c *Client) Commit(ctx context.Context, containerID, image string) (Image, error) {
registry, repo, tag := splitImageName(image)
img := Image{
registry: registry,
repository: repo,
tag: tag,
}
commitedImg, err := c.api.CommitContainer(docker.CommitContainerOptions{
Container: containerID,
Repository: img.repository,
Tag: img.tag,
Context: ctx,
})
if err != nil {
return Image{}, err
}
img.ID = commitedImg.ID
return img, err
}

func (c *Client) Tag(ctx context.Context, img Image) error {
return c.api.TagImage(img.ID, docker.TagImageOptions{
Repo: img.Name(),
Tag: img.tag,
Force: true,
Context: ctx,
})
}

func (c *Client) Push(ctx context.Context, img Image) error {
opts := docker.PushImageOptions{
Name: img.Name(),
Tag: img.tag,
RawJSONStream: true,
OutputStream: os.Stdout,
Context: ctx,
}
return c.api.PushImage(opts, docker.AuthConfiguration{})
}

func splitImageName(imageName string) (registry, repo, tag string) {
imgNameSplit := strings.Split(imageName, ":")
switch len(imgNameSplit) {
case 1:
repo = imgNameSplit[0]
tag = "latest"
case 2:
if strings.Contains(imgNameSplit[1], "/") {
repo = imageName
tag = "latest"
} else {
repo = imgNameSplit[0]
tag = imgNameSplit[1]
}
default:
repo = strings.Join(imgNameSplit[:len(imgNameSplit)-1], ":")
tag = imgNameSplit[len(imgNameSplit)-1]
}
repoSplit := strings.SplitN(repo, "/", 2)
registry = repoSplit[0]
repo = repoSplit[1]
return
}
22 changes: 22 additions & 0 deletions internal/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,25 @@ func (s *S) TestGetContainersByLabel(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(containers, check.DeepEquals, []Container{{ID: cont.ID}})
}

func (s *S) TestSplitImageName(c *check.C) {
tt := []struct {
image string
exReg string
exRepo string
exTag string
}{
{
image: "10.200.10.1:5000/admin/app-myapp:v23-builder",
exReg: "10.200.10.1:5000",
exRepo: "admin/app-myapp",
exTag: "v23-builder",
},
}
for _, t := range tt {
reg, repo, tag := splitImageName(t.image)
c.Assert(reg, check.Equals, t.exReg, check.Commentf("image: %s", t.image))
c.Assert(repo, check.Equals, t.exRepo, check.Commentf("image: %s", t.image))
c.Assert(tag, check.Equals, t.exTag, check.Commentf("image: %s", t.image))
}
}
13 changes: 11 additions & 2 deletions internal/docker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package docker

import (
"fmt"
"os"

"github.com/fsouza/go-dockerclient"
"github.com/tsuru/tsuru/exec"
Expand All @@ -18,11 +19,19 @@ type Executor struct {
}

func (d *Executor) Execute(opts exec.ExecuteOptions) error {
if opts.Stdout == nil {
opts.Stdout = os.Stdout
}
if opts.Stderr == nil {
opts.Stderr = os.Stderr
}
cmd := append([]string{opts.Cmd}, opts.Args...)
if opts.Dir != "" {
cmd = append([]string{"cd", opts.Dir, "&&"}, cmd...)
cmd = append([]string{"/bin/sh", "-c", "cd", opts.Dir, "&&"}, cmd...)
}
if len(opts.Envs) > 0 {
cmd = append([]string{"/bin/sh", "-c", "export"}, append(opts.Envs, cmd...)...)
}
// TODO: Envs for API pre v1.25
e, err := d.Client.api.CreateExec(docker.CreateExecOptions{
Container: d.ContainerID,
Cmd: cmd,
Expand Down

0 comments on commit 15743c0

Please sign in to comment.