2014-01-02 17:49:14 -05:00
|
|
|
package dockerimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-01-19 22:48:06 -05:00
|
|
|
"github.com/mitchellh/packer/builder/docker"
|
2014-01-02 17:49:14 -05:00
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-01-19 22:48:06 -05:00
|
|
|
const BuilderId = "packer.post-processor.docker-import"
|
|
|
|
|
2014-01-02 17:49:14 -05:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
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{
|
|
|
|
"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:56:50 -05:00
|
|
|
if artifact.BuilderId() != docker.BuilderId {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"Unknown artifact type: %s\nCan only import from Docker builder artifacts.",
|
|
|
|
artifact.BuilderId())
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
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:55:01 -05:00
|
|
|
driver := &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
|
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-19 22:55:01 -05:00
|
|
|
id, err := driver.Import(artifact.Files()[0], importRepo)
|
|
|
|
if err != nil {
|
2014-01-19 22:48:06 -05:00
|
|
|
return nil, false, err
|
|
|
|
}
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:48:06 -05:00
|
|
|
ui.Message("Imported ID: " + id)
|
2014-01-02 17:49:14 -05:00
|
|
|
|
2014-01-19 22:48:06 -05:00
|
|
|
// Build the artifact
|
|
|
|
artifact = &docker.ImportArtifact{
|
|
|
|
BuilderIdValue: BuilderId,
|
|
|
|
Driver: driver,
|
2014-01-19 23:20:00 -05:00
|
|
|
IdValue: importRepo,
|
2014-01-02 17:49:14 -05:00
|
|
|
}
|
2014-01-19 22:48:06 -05:00
|
|
|
|
|
|
|
return artifact, false, nil
|
2014-01-02 17:49:14 -05:00
|
|
|
}
|