Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rickard-von-essen committed Nov 29, 2016
1 parent a26a7f2 commit 4509cce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 76 deletions.
18 changes: 8 additions & 10 deletions builder/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
var driver Driver

if os.Getenv("PACKER_DOCKER_API") != "" {
var err error
driver, err = DockerApiDriverInit(&b.config.ctx, &b.config.DockerHostConfig, ui)
if err != nil {
return nil, err
}
driver = &DockerApiDriver{Ctx: &b.config.ctx, Config: b.config.DockerHostConfig, Ui: ui}
} else {
driver = &DockerDriver{Ctx: &b.config.ctx, Ui: ui}
}
Expand All @@ -48,11 +44,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
return nil, err
}

version, err := driver.Version()
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Docker version: %s", version.String())
/*
version, err := driver.Version()
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Docker version: %s", version.String())
*/

steps := []multistep.Step{
&StepPull{},
Expand Down
112 changes: 51 additions & 61 deletions builder/docker/driver_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"bytes"
"fmt"
"io"
"log"
"os"
Expand All @@ -15,48 +16,21 @@ import (
)

type DockerApiDriver struct {
Ui packer.Ui
Ctx *interpolate.Context
Ui packer.Ui
Ctx *interpolate.Context
Config DockerHostConfig

client *godocker.Client
auth godocker.AuthConfiguration
identityToken string
}

func DockerApiDriverInit(ctx *interpolate.Context, config *DockerHostConfig, ui packer.Ui) (DockerApiDriver, error) {

var client *godocker.Client
var err error

if config.Host == "" {
log.Println("Using Docker Host settings from environment variables.")
client, err = godocker.NewClientFromEnv()
} else {
if *config.TlsVerify {
log.Printf("Using Docker Host: %s with verified TLS.", config.Host)
client, err = godocker.NewTLSClient(config.Host,
filepath.Join(config.CertPath, "cert.pem"),
filepath.Join(config.CertPath, "key.pem"),
filepath.Join(config.CertPath, "ca.pem"))
} else {
log.Printf("Using Docker Host: %s", config.Host)
client, err = godocker.NewClient(config.Host)
}
}

return DockerApiDriver{
Ui: ui,
Ctx: ctx,
client: client,
}, err
}

func (d DockerApiDriver) DeleteImage(id string) error {
func (d *DockerApiDriver) DeleteImage(id string) error {
log.Printf("Deleting image: %s", id)
return d.client.RemoveImage(id)
}

func (d DockerApiDriver) Commit(id, author string, changes []string, message string) (string, error) {
func (d *DockerApiDriver) Commit(id, author string, changes []string, message string) (string, error) {

// TODO changes
config := godocker.Config{}
Expand All @@ -73,7 +47,7 @@ func (d DockerApiDriver) Commit(id, author string, changes []string, message str
return image.ID, nil
}

func (d DockerApiDriver) Export(id string, dst io.Writer) error {
func (d *DockerApiDriver) Export(id string, dst io.Writer) error {
log.Printf("Exporting container: %s", id)

return d.client.ExportContainer(godocker.ExportContainerOptions{
Expand All @@ -82,7 +56,7 @@ func (d DockerApiDriver) Export(id string, dst io.Writer) error {
})
}

func (d DockerApiDriver) Import(path string, repo string) (string, error) {
func (d *DockerApiDriver) Import(path string, repo string) (string, error) {
// There should be only one artifact of the Docker builder
file, err := os.Open(path)
if err != nil {
Expand All @@ -104,13 +78,13 @@ func (d DockerApiDriver) Import(path string, repo string) (string, error) {
return "", nil // TODO
}

func (d DockerApiDriver) IPAddress(id string) (string, error) {
func (d *DockerApiDriver) IPAddress(id string) (string, error) {

resp, err := d.client.InspectContainer(id)
return resp.NetworkSettings.IPAddress, err
}

func (d DockerApiDriver) Login(repo, email, user, pass string) error {
func (d *DockerApiDriver) Login(repo, email, user, pass string) error {
auth := godocker.AuthConfiguration{
ServerAddress: repo,
Email: email,
Expand All @@ -129,13 +103,13 @@ func (d DockerApiDriver) Login(repo, email, user, pass string) error {
return nil
}

func (d DockerApiDriver) Logout(repo string) error {
func (d *DockerApiDriver) Logout(repo string) error {
d.identityToken = ""
return nil
}

// TODO split imageTag -> image, tag
func (d DockerApiDriver) Pull(imageTag string) error {
func (d *DockerApiDriver) Pull(imageTag string) error {

tmp := strings.Split(imageTag, ":")
image := tmp[0]
Expand All @@ -157,7 +131,7 @@ func (d DockerApiDriver) Pull(imageTag string) error {
return readAndStream(&output, d.Ui)
}

func (d DockerApiDriver) Push(name string) error {
func (d *DockerApiDriver) Push(name string) error {

var output bytes.Buffer
opts := godocker.PushImageOptions{
Expand All @@ -173,7 +147,7 @@ func (d DockerApiDriver) Push(name string) error {
return readAndStream(&output, d.Ui)
}

func (d DockerApiDriver) SaveImage(id string, dst io.Writer) error {
func (d *DockerApiDriver) SaveImage(id string, dst io.Writer) error {

opts := godocker.ExportImageOptions{
Name: id,
Expand All @@ -184,7 +158,7 @@ func (d DockerApiDriver) SaveImage(id string, dst io.Writer) error {
return err
}

func (d DockerApiDriver) StartContainer(config *ContainerConfig) (string, error) {
func (d *DockerApiDriver) StartContainer(config *ContainerConfig) (string, error) {

// for host, guest := range config.Volumes {
// args = append(args, "-v", fmt.Sprintf("%s:%s", host, guest))
Expand Down Expand Up @@ -229,7 +203,7 @@ func (d DockerApiDriver) StartContainer(config *ContainerConfig) (string, error)
return container.ID, nil
}

func (d DockerApiDriver) StopContainer(id string) error {
func (d *DockerApiDriver) StopContainer(id string) error {

err := d.client.KillContainer(godocker.KillContainerOptions{
ID: id,
Expand All @@ -242,34 +216,50 @@ func (d DockerApiDriver) StopContainer(id string) error {
return d.client.RemoveContainer(godocker.RemoveContainerOptions{ID: id})
}

func (d DockerApiDriver) TagImage(id string, repo string, force bool) error {
func (d *DockerApiDriver) TagImage(id string, repo string, force bool) error {
return d.client.TagImage(id, godocker.TagImageOptions{
Repo: repo,
Force: force,
// Tag: "",
})
}

func (d DockerApiDriver) Verify() error {
func (d *DockerApiDriver) Verify() error {
d.Ui.Say("Warning! You are running the EXPERMINATAL Docker API driver!")
// var err error
// d.client, err = client.NewEnvClient()
// return err
// TODO
return nil
}

func (d DockerApiDriver) Version() (*version.Version, error) {
// output, err := exec.Command("docker", "-v").Output()
// if err != nil {
// return nil, err
// }
var err error
var client *godocker.Client

// match := regexp.MustCompile(version.VersionRegexpRaw).FindSubmatch(output)
// if match == nil {
// return nil, fmt.Errorf("unknown version: %s", output)
// }
if d.client == nil {
if d.Config.Host == "" {
log.Println("Using Docker Host settings from environment variables.")
client, err = godocker.NewClientFromEnv()
} else {
if *d.Config.TlsVerify {
log.Printf("Using Docker Host: %s with verified TLS.", d.Config.Host)
client, err = godocker.NewTLSClient(d.Config.Host,
filepath.Join(d.Config.CertPath, "cert.pem"),
filepath.Join(d.Config.CertPath, "key.pem"),
filepath.Join(d.Config.CertPath, "ca.pem"))
} else {
log.Printf("Using Docker Host: %s", d.Config.Host)
client, err = godocker.NewClient(d.Config.Host)
}
}
d.client = client
}

// return version.NewVersion(string(match[0]))
return version.NewVersion("1.0")
log.Printf("Docker: %+v", d.client)
return err
}

func (d *DockerApiDriver) Version() (*version.Version, error) {
if d.client == nil {
return nil, fmt.Errorf("No client %+v", d)
}
env, err := d.client.Version()
if err != nil {
return nil, err
}
return version.NewVersion(env.Get("Version"))
}
11 changes: 6 additions & 5 deletions post-processor/docker-push/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
if driver == nil {
// If no driver is set, then we use the real driver
if os.Getenv("PACKER_DOCKER_API") != "" {
var err error
driver, err = docker.DockerApiDriverInit(&p.config.ctx, &p.config.DockerHostConfig, ui)
if err != nil {
return nil, false, err
}
driver = &docker.DockerApiDriver{Ctx: &p.config.ctx, Config: p.config.DockerHostConfig, Ui: ui}
} else {
driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
}

err := driver.Verify()
if err != nil {
return nil, false, err
}
}

if p.config.EcrLogin {
Expand Down

0 comments on commit 4509cce

Please sign in to comment.