From 8bdb7232642fcb343c982b44e1eb9f8f0c497105 Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Wed, 1 Jan 2014 20:29:53 -0800 Subject: [PATCH 1/9] Do some forward porting of the old work of mitchellh/packer's docker branch. #774 --- config.go | 3 +- plugin/post-processor-docker/main.go | 15 ++++++ plugin/post-processor-docker/main_test.go | 1 + post-processor/docker/post-processor.go | 56 +++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 plugin/post-processor-docker/main.go create mode 100644 plugin/post-processor-docker/main_test.go create mode 100644 post-processor/docker/post-processor.go diff --git a/config.go b/config.go index a2fe49297..0056dae2e 100644 --- a/config.go +++ b/config.go @@ -42,7 +42,8 @@ const defaultConfig = ` "post-processors": { "vagrant": "packer-post-processor-vagrant", - "vsphere": "packer-post-processor-vsphere" + "vsphere": "packer-post-processor-vsphere", + "docker": "packer-post-processor-docker" }, "provisioners": { diff --git a/plugin/post-processor-docker/main.go b/plugin/post-processor-docker/main.go new file mode 100644 index 000000000..1e83f47f4 --- /dev/null +++ b/plugin/post-processor-docker/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/mitchellh/packer/packer/plugin" + "github.com/mitchellh/packer/post-processor/docker" +) + +func main() { + server, err := plugin.Server() + if err != nil { + panic(err) + } + server.RegisterPostProcessor(new(docker.PostProcessor)) + server.Serve() +} diff --git a/plugin/post-processor-docker/main_test.go b/plugin/post-processor-docker/main_test.go new file mode 100644 index 000000000..06ab7d0f9 --- /dev/null +++ b/plugin/post-processor-docker/main_test.go @@ -0,0 +1 @@ +package main diff --git a/post-processor/docker/post-processor.go b/post-processor/docker/post-processor.go new file mode 100644 index 000000000..a1698b238 --- /dev/null +++ b/post-processor/docker/post-processor.go @@ -0,0 +1,56 @@ +package docker + +import ( + "bytes" + "errors" + "github.com/mitchellh/mapstructure" + "github.com/mitchellh/packer/packer" + "os/exec" +) + +type Config struct { + Registry string + Username string + Password string + Email string +} + +type PostProcessor struct { + config Config +} + +func (p *PostProcessor) Configure(raw ...interface{}) error { + if err := mapstructure.Decode(raw, &p.config); err != nil { + return err + } + + if p.config.Registry == "" { + p.config.Registry = "registry.docker.io" + } + + if p.config.Username == "" { + return errors.New("Username is required to push docker image") + } + + if p.config.Password == "" { + return errors.New("Password is required to push docker image") + } + + return nil +} +func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { + id := artifact.Id() + ui.Say("Pushing imgage: " + id) + + // TODO: docker login + + stdout := new(bytes.Buffer) + cmd := exec.Command("docker", "push", id) + cmd.Stdout = stdout + if err := cmd.Run(); err != nil { + ui.Say("Failed to push image: " + id) + return nil, true, err + } + + return nil, true, nil +} From a0e533db41344cda12e2270bdb52502eb6b15edb Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Wed, 1 Jan 2014 22:30:28 -0800 Subject: [PATCH 2/9] Rename docker post processor to docker-push. Implement login to a docker registry, error handling --- config.go | 2 +- .../main.go | 4 +-- .../main_test.go | 0 .../{docker => docker-push}/post-processor.go | 33 +++++++++++++++---- 4 files changed, 29 insertions(+), 10 deletions(-) rename plugin/{post-processor-docker => post-processor-docker-push}/main.go (58%) rename plugin/{post-processor-docker => post-processor-docker-push}/main_test.go (100%) rename post-processor/{docker => docker-push}/post-processor.go (59%) diff --git a/config.go b/config.go index 0056dae2e..7ebdcef66 100644 --- a/config.go +++ b/config.go @@ -43,7 +43,7 @@ const defaultConfig = ` "post-processors": { "vagrant": "packer-post-processor-vagrant", "vsphere": "packer-post-processor-vsphere", - "docker": "packer-post-processor-docker" + "docker-push": "packer-post-processor-docker-push" }, "provisioners": { diff --git a/plugin/post-processor-docker/main.go b/plugin/post-processor-docker-push/main.go similarity index 58% rename from plugin/post-processor-docker/main.go rename to plugin/post-processor-docker-push/main.go index 1e83f47f4..eb45b13bd 100644 --- a/plugin/post-processor-docker/main.go +++ b/plugin/post-processor-docker-push/main.go @@ -2,7 +2,7 @@ package main import ( "github.com/mitchellh/packer/packer/plugin" - "github.com/mitchellh/packer/post-processor/docker" + "github.com/mitchellh/packer/post-processor/docker-push" ) func main() { @@ -10,6 +10,6 @@ func main() { if err != nil { panic(err) } - server.RegisterPostProcessor(new(docker.PostProcessor)) + server.RegisterPostProcessor(new(dockerpush.PostProcessor)) server.Serve() } diff --git a/plugin/post-processor-docker/main_test.go b/plugin/post-processor-docker-push/main_test.go similarity index 100% rename from plugin/post-processor-docker/main_test.go rename to plugin/post-processor-docker-push/main_test.go diff --git a/post-processor/docker/post-processor.go b/post-processor/docker-push/post-processor.go similarity index 59% rename from post-processor/docker/post-processor.go rename to post-processor/docker-push/post-processor.go index a1698b238..64f893e6c 100644 --- a/post-processor/docker/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -1,7 +1,6 @@ -package docker +package dockerpush import ( - "bytes" "errors" "github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/packer" @@ -38,18 +37,38 @@ func (p *PostProcessor) Configure(raw ...interface{}) error { return nil } + func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { id := artifact.Id() - ui.Say("Pushing imgage: " + id) + ui.Say("Pushing image: " + id) + + if p.config.Email == "" { + cmd := exec.Command("docker", "login", + "-u=\""+p.config.Username+"\"", + "-p=\""+p.config.Password+"\"") + + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } - // TODO: docker login + } else { + cmd := exec.Command("docker", + "login", + "-u=\""+p.config.Username+"\"", + "-p=\""+p.config.Password+"\"", + "-e=\""+p.config.Email+"\"") - stdout := new(bytes.Buffer) + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } + + } cmd := exec.Command("docker", "push", id) - cmd.Stdout = stdout if err := cmd.Run(); err != nil { ui.Say("Failed to push image: " + id) - return nil, true, err + return nil, false, err } return nil, true, nil From 0ec18a723a3931995cf410d723c653d701299f4d Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Wed, 1 Jan 2014 23:29:27 -0800 Subject: [PATCH 3/9] Finish up parameter parsing and validation. Login to a docker index now works, ready for implementation of the actual push logic. --- post-processor/docker-push/post-processor.go | 120 ++++++++++++++----- 1 file changed, 89 insertions(+), 31 deletions(-) diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 64f893e6c..80fc3b2f5 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -1,70 +1,128 @@ package dockerpush import ( - "errors" - "github.com/mitchellh/mapstructure" + "fmt" + "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "os/exec" ) type Config struct { - Registry string - Username string - Password string - Email string + common.PackerConfig `mapstructure:",squash"` + + Registry string `mapstructure:"registry"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Email string `mapstructure:"email"` + + tpl *packer.ConfigTemplate } type PostProcessor struct { config Config } -func (p *PostProcessor) Configure(raw ...interface{}) error { - if err := mapstructure.Decode(raw, &p.config); err != nil { +func (p *PostProcessor) Configure(raws ...interface{}) error { + _, err := common.DecodeConfig(&p.config, raws...) + if err != nil { return err } - if p.config.Registry == "" { - p.config.Registry = "registry.docker.io" + p.config.tpl, err = packer.NewConfigTemplate() + if err != nil { + return err } + p.config.tpl.UserVars = p.config.PackerUserVars + + // Accumulate any errors + errs := new(packer.MultiError) - if p.config.Username == "" { - return errors.New("Username is required to push docker image") + templates := map[string]*string{ + "username": &p.config.Username, + "password": &p.config.Password, } - if p.config.Password == "" { - return errors.New("Password is required to push docker image") + for key, ptr := range templates { + if *ptr == "" { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("%s must be set", key)) + } + + *ptr, err = p.config.tpl.Process(*ptr, nil) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing %s: %s", key, err)) + } + } + + if len(errs.Errors) > 0 { + return errs } return nil + } func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { id := artifact.Id() ui.Say("Pushing image: " + id) - if p.config.Email == "" { - cmd := exec.Command("docker", "login", - "-u=\""+p.config.Username+"\"", - "-p=\""+p.config.Password+"\"") + if p.config.Registry == "" { + + if p.config.Email == "" { + cmd := exec.Command("docker", + "login", + "-u="+p.config.Username, + "-p="+p.config.Password) + + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } + + } else { + cmd := exec.Command("docker", + "login", + "-u="+p.config.Username, + "-p="+p.config.Password, + "-e="+p.config.Email) + + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } - if err := cmd.Run(); err != nil { - ui.Say("Login to the registry " + p.config.Registry + " failed") - return nil, false, err } } else { - cmd := exec.Command("docker", - "login", - "-u=\""+p.config.Username+"\"", - "-p=\""+p.config.Password+"\"", - "-e=\""+p.config.Email+"\"") - - if err := cmd.Run(); err != nil { - ui.Say("Login to the registry " + p.config.Registry + " failed") - return nil, false, err - } + if p.config.Email == "" { + cmd := exec.Command("docker", + "login", + "-u="+p.config.Username, + "-p="+p.config.Password, + p.config.Registry) + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } + + } else { + cmd := exec.Command("docker", + "login", + "-u="+p.config.Username, + "-p="+p.config.Password, + "-e="+p.config.Email, + p.config.Registry) + + if err := cmd.Run(); err != nil { + ui.Say("Login to the registry " + p.config.Registry + " failed") + return nil, false, err + } + + } } + cmd := exec.Command("docker", "push", id) if err := cmd.Run(); err != nil { ui.Say("Failed to push image: " + id) From 3d60bfb312d4b638b62a97617bce74ade87ded42 Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Thu, 2 Jan 2014 14:49:14 -0800 Subject: [PATCH 4/9] Add docker-import post-processor. Implemented initial working version of Docker image importing code. #774 --- config.go | 3 +- plugin/post-processor-docker-import/main.go | 15 ++ .../post-processor-docker-import/main_test.go | 1 + .../docker-import/post-processor.go | 155 ++++++++++++++++++ post-processor/docker-push/post-processor.go | 2 +- 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 plugin/post-processor-docker-import/main.go create mode 100644 plugin/post-processor-docker-import/main_test.go create mode 100644 post-processor/docker-import/post-processor.go diff --git a/config.go b/config.go index 7ebdcef66..72ed19618 100644 --- a/config.go +++ b/config.go @@ -43,7 +43,8 @@ const defaultConfig = ` "post-processors": { "vagrant": "packer-post-processor-vagrant", "vsphere": "packer-post-processor-vsphere", - "docker-push": "packer-post-processor-docker-push" + "docker-push": "packer-post-processor-docker-push", + "docker-import": "packer-post-processor-docker-import" }, "provisioners": { diff --git a/plugin/post-processor-docker-import/main.go b/plugin/post-processor-docker-import/main.go new file mode 100644 index 000000000..e9446b113 --- /dev/null +++ b/plugin/post-processor-docker-import/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/mitchellh/packer/packer/plugin" + "github.com/mitchellh/packer/post-processor/docker-import" +) + +func main() { + server, err := plugin.Server() + if err != nil { + panic(err) + } + server.RegisterPostProcessor(new(dockerimport.PostProcessor)) + server.Serve() +} diff --git a/plugin/post-processor-docker-import/main_test.go b/plugin/post-processor-docker-import/main_test.go new file mode 100644 index 000000000..06ab7d0f9 --- /dev/null +++ b/plugin/post-processor-docker-import/main_test.go @@ -0,0 +1 @@ +package main diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go new file mode 100644 index 000000000..ef2ec3e39 --- /dev/null +++ b/post-processor/docker-import/post-processor.go @@ -0,0 +1,155 @@ +package dockerimport + +import ( + "fmt" + "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/packer" + "io" + "os" + "os/exec" +) + +type Config struct { + common.PackerConfig `mapstructure:",squash"` + + Repository string `mapstructure:"repository"` + Tag string `mapstructure:"tag"` + Dockerfile string `mapstructure:"dockerfile"` + + tpl *packer.ConfigTemplate +} + +type PostProcessor struct { + config Config +} + +func (p *PostProcessor) Configure(raws ...interface{}) error { + _, err := common.DecodeConfig(&p.config, raws...) + if err != nil { + return err + } + + p.config.tpl, err = packer.NewConfigTemplate() + if err != nil { + return err + } + p.config.tpl.UserVars = p.config.PackerUserVars + + // Accumulate any errors + errs := new(packer.MultiError) + + templates := map[string]*string{ + "repository": &p.config.Repository, + } + + for key, ptr := range templates { + if *ptr == "" { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("%s must be set", key)) + } + + *ptr, err = p.config.tpl.Process(*ptr, nil) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Error processing %s: %s", key, err)) + } + } + + if len(errs.Errors) > 0 { + return errs + } + + return nil + +} + +func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { + id := artifact.Id() + ui.Say("Importing image: " + id) + + if p.config.Tag == "" { + + cmd := exec.Command("docker", + "import", + "-", + p.config.Repository) + + stdin, err := cmd.StdinPipe() + + if err != nil { + return nil, false, err + } + + // There should be only one artifact of the Docker builder + file, err := os.Open(artifact.Files()[0]) + + if err != nil { + return nil, false, err + } + + defer file.Close() + + if err := cmd.Start(); err != nil { + ui.Say("Image import failed") + return nil, false, err + } + + go func() { + io.Copy(stdin, file) + // close stdin so that program will exit + stdin.Close() + }() + + cmd.Wait() + + } else { + + cmd := exec.Command("docker", + "import", + "-", + p.config.Repository+":"+p.config.Tag) + + stdin, err := cmd.StdinPipe() + + if err != nil { + return nil, false, err + } + + // There should be only one artifact of the Docker builder + file, err := os.Open(artifact.Files()[0]) + + if err != nil { + return nil, false, err + } + + defer file.Close() + + if err := cmd.Start(); err != nil { + ui.Say("Image import failed") + return nil, false, err + } + + go func() { + io.Copy(stdin, file) + // close stdin so that program will exit + stdin.Close() + }() + + cmd.Wait() + + } + + // Process Dockerfile if provided + if p.config.Dockerfile != "" { + + cmd := exec.Command("docker", "build", id) + if err := cmd.Run(); err != nil { + ui.Say("Failed to build image: " + id) + return nil, false, err + } + + // TODO implement dockerfile provisioning + + } + return nil, false, nil +} diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 80fc3b2f5..0ec2ab492 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -129,5 +129,5 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac return nil, false, err } - return nil, true, nil + return nil, false, nil } From 8147ad66ec71793263f03b39a26c7940ea7ce1aa Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Mon, 6 Jan 2014 13:42:32 -0800 Subject: [PATCH 5/9] Start adding dockerfile provisioning to docker-import post-processor #774 --- .../docker-import/post-processor.go | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go index ef2ec3e39..c2f628af1 100644 --- a/post-processor/docker-import/post-processor.go +++ b/post-processor/docker-import/post-processor.go @@ -143,12 +143,35 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac if p.config.Dockerfile != "" { cmd := exec.Command("docker", "build", id) - if err := cmd.Run(); err != nil { + + stdin, err := cmd.StdinPipe() + + if err != nil { + return nil, false, err + } + + // open Dockerfile + file, err := os.Open(p.config.Dockerfile) + + if err != nil { + ui.Say("Could not open Dockerfile: " + p.config.Dockerfile) + return nil, false, err + } + + defer file.Close() + + if err := cmd.Start(); err != nil { ui.Say("Failed to build image: " + id) return nil, false, err } - // TODO implement dockerfile provisioning + go func() { + io.Copy(stdin, file) + // close stdin so that program will exit + stdin.Close() + }() + + cmd.Wait() } return nil, false, nil From 208b330b843a19c9fb8119733a462199f8555eba Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Mon, 6 Jan 2014 13:43:30 -0800 Subject: [PATCH 6/9] docker-import post-processor, add TODO #774 --- post-processor/docker-import/post-processor.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go index c2f628af1..bc23f8f8a 100644 --- a/post-processor/docker-import/post-processor.go +++ b/post-processor/docker-import/post-processor.go @@ -173,6 +173,9 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac cmd.Wait() + // TODO make sure we re-tag instead of create new image + // automatically use previous image as base of new image + } return nil, false, nil } From 00d3ee42e58dade88c63111d5c6173bd2692082b Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Mon, 6 Jan 2014 15:12:08 -0800 Subject: [PATCH 7/9] docker-import: finish up Dockerfile provisioning, Add TODO for next section #774 --- .../docker-import/post-processor.go | 87 ++++++++++++++----- 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go index bc23f8f8a..a51e6ba6a 100644 --- a/post-processor/docker-import/post-processor.go +++ b/post-processor/docker-import/post-processor.go @@ -67,6 +67,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac id := artifact.Id() ui.Say("Importing image: " + id) + // TODO Set artifact ID so that docker-push can use it + if p.config.Tag == "" { cmd := exec.Command("docker", @@ -142,39 +144,76 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Process Dockerfile if provided if p.config.Dockerfile != "" { - cmd := exec.Command("docker", "build", id) + if p.config.Tag != "" { - stdin, err := cmd.StdinPipe() + cmd := exec.Command("docker", "build", "-t="+p.config.Repository+":"+p.config.Tag, "-") - if err != nil { - return nil, false, err - } + stdin, err := cmd.StdinPipe() - // open Dockerfile - file, err := os.Open(p.config.Dockerfile) + if err != nil { + return nil, false, err + } - if err != nil { - ui.Say("Could not open Dockerfile: " + p.config.Dockerfile) - return nil, false, err - } + // open Dockerfile + file, err := os.Open(p.config.Dockerfile) - defer file.Close() + if err != nil { + ui.Say("Could not open Dockerfile: " + p.config.Dockerfile) + return nil, false, err + } - if err := cmd.Start(); err != nil { - ui.Say("Failed to build image: " + id) - return nil, false, err - } + ui.Say(id) - go func() { - io.Copy(stdin, file) - // close stdin so that program will exit - stdin.Close() - }() + defer file.Close() - cmd.Wait() + if err := cmd.Start(); err != nil { + ui.Say("Failed to build image: " + id) + return nil, false, err + } + + go func() { + io.Copy(stdin, file) + // close stdin so that program will exit + stdin.Close() + }() + + cmd.Wait() + + } else { + + cmd := exec.Command("docker", "build", "-t="+p.config.Repository, "-") + + stdin, err := cmd.StdinPipe() + + if err != nil { + return nil, false, err + } - // TODO make sure we re-tag instead of create new image - // automatically use previous image as base of new image + // open Dockerfile + file, err := os.Open(p.config.Dockerfile) + + if err != nil { + ui.Say("Could not open Dockerfile: " + p.config.Dockerfile) + return nil, false, err + } + + ui.Say(id) + + defer file.Close() + + if err := cmd.Start(); err != nil { + ui.Say("Failed to build image: " + id) + return nil, false, err + } + + go func() { + io.Copy(stdin, file) + // close stdin so that program will exit + stdin.Close() + }() + + cmd.Wait() + } } return nil, false, nil From 358b0078c94de3c40c620bd534aba43a96c8b82b Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Mon, 13 Jan 2014 13:21:34 -0800 Subject: [PATCH 8/9] docker-import + docker-push: Add some beginning tests. #774 --- .../docker-import/post-processor.go | 2 -- .../docker-import/post-processor_test.go | 31 +++++++++++++++++++ .../docker-push/post-processor_test.go | 31 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 post-processor/docker-import/post-processor_test.go create mode 100644 post-processor/docker-push/post-processor_test.go diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go index a51e6ba6a..e0162a9c4 100644 --- a/post-processor/docker-import/post-processor.go +++ b/post-processor/docker-import/post-processor.go @@ -67,8 +67,6 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac id := artifact.Id() ui.Say("Importing image: " + id) - // TODO Set artifact ID so that docker-push can use it - if p.config.Tag == "" { cmd := exec.Command("docker", diff --git a/post-processor/docker-import/post-processor_test.go b/post-processor/docker-import/post-processor_test.go new file mode 100644 index 000000000..43ac0b4ef --- /dev/null +++ b/post-processor/docker-import/post-processor_test.go @@ -0,0 +1,31 @@ +package dockerimport + +import ( + "bytes" + "github.com/mitchellh/packer/packer" + "testing" +) + +func testConfig() map[string]interface{} { + return map[string]interface{}{} +} + +func testPP(t *testing.T) *PostProcessor { + var p PostProcessor + if err := p.Configure(testConfig()); err != nil { + t.Fatalf("err: %s", err) + } + + return &p +} + +func testUi() *packer.BasicUi { + return &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + } +} + +func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { + var _ packer.PostProcessor = new(PostProcessor) +} diff --git a/post-processor/docker-push/post-processor_test.go b/post-processor/docker-push/post-processor_test.go new file mode 100644 index 000000000..7631da79d --- /dev/null +++ b/post-processor/docker-push/post-processor_test.go @@ -0,0 +1,31 @@ +package dockerpush + +import ( + "bytes" + "github.com/mitchellh/packer/packer" + "testing" +) + +func testConfig() map[string]interface{} { + return map[string]interface{}{} +} + +func testPP(t *testing.T) *PostProcessor { + var p PostProcessor + if err := p.Configure(testConfig()); err != nil { + t.Fatalf("err: %s", err) + } + + return &p +} + +func testUi() *packer.BasicUi { + return &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + } +} + +func TestPostProcessor_ImplementsPostProcessor(t *testing.T) { + var _ packer.PostProcessor = new(PostProcessor) +} From c0174309c113e2465f5626815f6671cf539b270d Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Mon, 13 Jan 2014 13:37:09 -0800 Subject: [PATCH 9/9] docker-push: add code to handle seperate registry, push a specific repository/tag #774 --- post-processor/docker-push/post-processor.go | 35 ++++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 0ec2ab492..53b8a17a4 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -10,10 +10,12 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` - Registry string `mapstructure:"registry"` - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - Email string `mapstructure:"email"` + Repository string `mapstructure:"repository"` + Tag string `mapstructure:"tag"` + Registry string `mapstructure:"registry"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Email string `mapstructure:"email"` tpl *packer.ConfigTemplate } @@ -38,8 +40,9 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { errs := new(packer.MultiError) templates := map[string]*string{ - "username": &p.config.Username, - "password": &p.config.Password, + "username": &p.config.Username, + "password": &p.config.Password, + "repository": &p.config.Repository, } for key, ptr := range templates { @@ -123,10 +126,22 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac } } - cmd := exec.Command("docker", "push", id) - if err := cmd.Run(); err != nil { - ui.Say("Failed to push image: " + id) - return nil, false, err + if p.config.Tag != "" { + + cmd := exec.Command("docker", "push", p.config.Repository+":"+p.config.Tag) + if err := cmd.Run(); err != nil { + ui.Say("Failed to push image: " + id) + return nil, false, err + } + + } else { + + cmd := exec.Command("docker", "push", p.config.Repository) + if err := cmd.Run(); err != nil { + ui.Say("Failed to push image: " + id) + return nil, false, err + } + } return nil, false, nil