Commit ebd24078 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

post-processor/docker-import: refactor

/cc @mmckeen - Did a refactor here, CC'd in case you're curious
or want to learn :)
parent bbca2c32
......@@ -12,9 +12,9 @@ import (
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Dockerfile string `mapstructure:"dockerfile"`
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Dockerfile string `mapstructure:"dockerfile"`
tpl *packer.ConfigTemplate
}
......@@ -39,7 +39,9 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
errs := new(packer.MultiError)
templates := map[string]*string{
"dockerfile": &p.config.Dockerfile,
"repository": &p.config.Repository,
"tag": &p.config.Tag,
}
for key, ptr := range templates {
......@@ -64,155 +66,68 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
}
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])
importRepo := p.config.Repository
if p.config.Tag != "" {
importRepo += ":" + p.config.Tag
}
if err != nil {
return nil, false, err
}
cmd := exec.Command("docker", "import", "-", importRepo)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, false, err
}
defer file.Close()
// 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
}
ui.Message("Importing image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
go func() {
io.Copy(stdin, file)
// close stdin so that program will exit
stdin.Close()
}()
if err := cmd.Start(); err != nil {
return nil, false, err
}
cmd.Wait()
go func() {
defer stdin.Close()
io.Copy(stdin, file)
}()
} else {
cmd.Wait()
cmd := exec.Command("docker",
"import",
"-",
p.config.Repository+":"+p.config.Tag)
// Process Dockerfile if provided
if p.config.Dockerfile != "" {
cmd := exec.Command("docker", "build", "-t="+importRepo, "-")
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])
// open Dockerfile
file, err := os.Open(p.config.Dockerfile)
if err != nil {
err = fmt.Errorf("Couldn't open Dockerfile: %s", err)
return nil, false, err
}
defer file.Close()
ui.Message("Running docker build with Dockerfile: " + p.config.Dockerfile)
if err := cmd.Start(); err != nil {
ui.Say("Image import failed")
err = fmt.Errorf("Failed to start docker build: %s", err)
return nil, false, err
}
go func() {
defer stdin.Close()
io.Copy(stdin, file)
// close stdin so that program will exit
stdin.Close()
}()
cmd.Wait()
}
// Process Dockerfile if provided
if p.config.Dockerfile != "" {
if p.config.Tag != "" {
cmd := exec.Command("docker", "build", "-t="+p.config.Repository+":"+p.config.Tag, "-")
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
}
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()
} else {
cmd := exec.Command("docker", "build", "-t="+p.config.Repository, "-")
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
}
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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment