packer-cn/post-processor/docker-push/post-processor.go

93 lines
2.2 KiB
Go
Raw Normal View History

package dockerpush
import (
"fmt"
2014-01-19 23:34:20 -05:00
"github.com/mitchellh/packer/builder/docker"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer"
2014-01-19 23:34:20 -05:00
"github.com/mitchellh/packer/post-processor/docker-import"
2014-10-14 19:04:37 -04:00
"github.com/mitchellh/packer/post-processor/docker-tag"
"github.com/mitchellh/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Login bool
LoginEmail string `mapstructure:"login_email"`
LoginUsername string `mapstructure:"login_username"`
LoginPassword string `mapstructure:"login_password"`
LoginServer string `mapstructure:"login_server"`
ctx interpolate.Context
}
type PostProcessor struct {
Driver docker.Driver
config Config
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
2014-10-14 19:04:37 -04:00
if artifact.BuilderId() != dockerimport.BuilderId &&
artifact.BuilderId() != dockertag.BuilderId {
2014-01-19 23:34:20 -05:00
err := fmt.Errorf(
"Unknown artifact type: %s\nCan only import from docker-import and docker-tag artifacts.",
2014-01-19 23:34:20 -05:00
artifact.BuilderId())
return nil, false, err
}
driver := p.Driver
if driver == nil {
// If no driver is set, then we use the real driver
driver = &docker.DockerDriver{Ctx: &p.config.ctx, Ui: ui}
}
if p.config.Login {
ui.Message("Logging in...")
err := driver.Login(
p.config.LoginServer,
p.config.LoginEmail,
p.config.LoginUsername,
p.config.LoginPassword)
if err != nil {
return nil, false, fmt.Errorf(
"Error logging in to Docker: %s", err)
}
defer func() {
ui.Message("Logging out...")
if err := driver.Logout(p.config.LoginServer); err != nil {
ui.Error(fmt.Sprintf("Error logging out: %s", err))
}
}()
}
// Get the name.
2014-01-19 23:34:20 -05:00
name := artifact.Id()
2014-01-19 23:34:20 -05:00
ui.Message("Pushing: " + name)
if err := driver.Push(name); err != nil {
return nil, false, err
}
return nil, false, nil
}