2014-01-02 01:30:28 -05:00
|
|
|
package dockerpush
|
2014-01-01 23:29:53 -05:00
|
|
|
|
|
|
|
import (
|
2014-01-02 02:29:27 -05:00
|
|
|
"fmt"
|
2014-01-19 23:34:20 -05:00
|
|
|
"github.com/mitchellh/packer/builder/docker"
|
2014-01-02 02:29:27 -05:00
|
|
|
"github.com/mitchellh/packer/common"
|
2014-01-01 23:29:53 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-01-19 23:34:20 -05:00
|
|
|
"github.com/mitchellh/packer/post-processor/docker-import"
|
|
|
|
"strings"
|
2014-01-01 23:29:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-01-02 02:29:27 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
tpl *packer.ConfigTemplate
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
2014-03-13 00:30:13 -04:00
|
|
|
Driver docker.Driver
|
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
_, err := common.DecodeConfig(&p.config, raws...)
|
|
|
|
if err != nil {
|
2014-01-01 23:29:53 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
2014-01-02 02:29:27 -05:00
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
|
|
|
// Accumulate any errors
|
|
|
|
errs := new(packer.MultiError)
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-02 01:30:28 -05:00
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2014-01-19 23:34:20 -05:00
|
|
|
if artifact.BuilderId() != dockerimport.BuilderId {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"Unknown artifact type: %s\nCan only import from docker-import artifacts.",
|
|
|
|
artifact.BuilderId())
|
|
|
|
return nil, false, err
|
2014-01-02 01:30:28 -05:00
|
|
|
}
|
2014-01-02 02:29:27 -05:00
|
|
|
|
2014-03-13 00:30:13 -04:00
|
|
|
driver := p.Driver
|
|
|
|
if driver == nil {
|
|
|
|
// If no driver is set, then we use the real driver
|
|
|
|
driver = &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
|
|
|
|
}
|
2014-01-13 16:37:09 -05:00
|
|
|
|
2014-01-19 23:34:20 -05:00
|
|
|
// Get the name. We strip off any tags from the name because the
|
|
|
|
// push doesn't use those.
|
|
|
|
name := artifact.Id()
|
2014-03-13 00:30:13 -04:00
|
|
|
|
|
|
|
if i := strings.Index(name, "/"); i >= 0 {
|
|
|
|
// This should always be true because the / is required. But we have
|
|
|
|
// to get the index to this so we don't accidentally strip off the port
|
|
|
|
if j := strings.Index(name[i:], ":"); j >= 0 {
|
|
|
|
name = name[:i+j]
|
|
|
|
}
|
2014-01-19 23:34:20 -05:00
|
|
|
}
|
2014-01-13 16:37:09 -05:00
|
|
|
|
2014-01-19 23:34:20 -05:00
|
|
|
ui.Message("Pushing: " + name)
|
|
|
|
if err := driver.Push(name); err != nil {
|
|
|
|
return nil, false, err
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:49:14 -05:00
|
|
|
return nil, false, nil
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|