From 8bdb7232642fcb343c982b44e1eb9f8f0c497105 Mon Sep 17 00:00:00 2001 From: Matthew McKeen Date: Wed, 1 Jan 2014 20:29:53 -0800 Subject: [PATCH] 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 +}