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"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2014-01-01 23:29:53 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-01-02 02:29:27 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Registry string `mapstructure:"registry"`
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
Email string `mapstructure:"email"`
|
|
|
|
|
|
|
|
tpl *packer.ConfigTemplate
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
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)
|
2014-01-01 23:29:53 -05:00
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
templates := map[string]*string{
|
|
|
|
"username": &p.config.Username,
|
|
|
|
"password": &p.config.Password,
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
2014-01-02 02:29:27 -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
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-01-02 02:29:27 -05:00
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
}
|
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) {
|
|
|
|
id := artifact.Id()
|
2014-01-02 01:30:28 -05:00
|
|
|
ui.Say("Pushing image: " + id)
|
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
if p.config.Registry == "" {
|
|
|
|
|
|
|
|
if p.config.Email == "" {
|
|
|
|
cmd := exec.Command("docker",
|
|
|
|
"login",
|
|
|
|
"-u="+p.config.Username,
|
|
|
|
"-p="+p.config.Password)
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
ui.Say("Login to the registry " + p.config.Registry + " failed")
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cmd := exec.Command("docker",
|
|
|
|
"login",
|
|
|
|
"-u="+p.config.Username,
|
|
|
|
"-p="+p.config.Password,
|
|
|
|
"-e="+p.config.Email)
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
ui.Say("Login to the registry " + p.config.Registry + " failed")
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2014-01-02 01:30:28 -05:00
|
|
|
|
|
|
|
}
|
2014-01-01 23:29:53 -05:00
|
|
|
|
2014-01-02 01:30:28 -05:00
|
|
|
} else {
|
2014-01-02 02:29:27 -05:00
|
|
|
if p.config.Email == "" {
|
|
|
|
cmd := exec.Command("docker",
|
|
|
|
"login",
|
|
|
|
"-u="+p.config.Username,
|
|
|
|
"-p="+p.config.Password,
|
|
|
|
p.config.Registry)
|
2014-01-02 01:30:28 -05:00
|
|
|
|
2014-01-02 02:29:27 -05:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
ui.Say("Login to the registry " + p.config.Registry + " failed")
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cmd := exec.Command("docker",
|
|
|
|
"login",
|
|
|
|
"-u="+p.config.Username,
|
|
|
|
"-p="+p.config.Password,
|
|
|
|
"-e="+p.config.Email,
|
|
|
|
p.config.Registry)
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
ui.Say("Login to the registry " + p.config.Registry + " failed")
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-01-02 01:30:28 -05:00
|
|
|
}
|
2014-01-02 02:29:27 -05:00
|
|
|
|
2014-01-01 23:29:53 -05:00
|
|
|
cmd := exec.Command("docker", "push", id)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
ui.Say("Failed to push image: " + id)
|
2014-01-02 01:30:28 -05:00
|
|
|
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
|
|
|
}
|