2014-01-02 17:49:14 -05:00
|
|
|
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"`
|
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
Dockerfile string `mapstructure:"dockerfile"`
|
2014-01-02 17:49:14 -05:00
|
|
|
Repository string `mapstructure:"repository"`
|
|
|
|
Tag string `mapstructure:"tag"`
|
|
|
|
|
|
|
|
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{
|
2014-01-19 22:15:25 -05:00
|
|
|
"dockerfile": &p.config.Dockerfile,
|
2014-01-02 17:49:14 -05:00
|
|
|
"repository": &p.config.Repository,
|
2014-01-19 22:15:25 -05:00
|
|
|
"tag": &p.config.Tag,
|
2014-01-02 17:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-01-19 22:15:25 -05:00
|
|
|
importRepo := p.config.Repository
|
|
|
|
if p.config.Tag != "" {
|
|
|
|
importRepo += ":" + p.config.Tag
|
|
|
|
}
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
cmd := exec.Command("docker", "import", "-", importRepo)
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
// 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()
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
ui.Message("Importing image: " + artifact.Id())
|
|
|
|
ui.Message("Repository: " + importRepo)
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.Copy(stdin, file)
|
|
|
|
}()
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
cmd.Wait()
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
// Process Dockerfile if provided
|
|
|
|
if p.config.Dockerfile != "" {
|
|
|
|
cmd := exec.Command("docker", "build", "-t="+importRepo, "-")
|
2014-01-02 17:49:14 -05:00
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
// open Dockerfile
|
|
|
|
file, err := os.Open(p.config.Dockerfile)
|
2014-01-02 17:49:14 -05:00
|
|
|
if err != nil {
|
2014-01-19 22:15:25 -05:00
|
|
|
err = fmt.Errorf("Couldn't open Dockerfile: %s", err)
|
2014-01-02 17:49:14 -05:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2014-01-19 22:15:25 -05:00
|
|
|
ui.Message("Running docker build with Dockerfile: " + p.config.Dockerfile)
|
2014-01-02 17:49:14 -05:00
|
|
|
if err := cmd.Start(); err != nil {
|
2014-01-19 22:15:25 -05:00
|
|
|
err = fmt.Errorf("Failed to start docker build: %s", err)
|
2014-01-02 17:49:14 -05:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2014-01-19 22:15:25 -05:00
|
|
|
defer stdin.Close()
|
2014-01-02 17:49:14 -05:00
|
|
|
io.Copy(stdin, file)
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil, false, nil
|
|
|
|
}
|