Commit 0ec18a72 authored by Matthew McKeen's avatar Matthew McKeen

Finish up parameter parsing and validation.

Login to a docker index now works, ready for
implementation of the actual push logic.
parent a0e533db
package dockerpush package dockerpush
import ( import (
"errors" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os/exec" "os/exec"
) )
type Config struct { type Config struct {
Registry string common.PackerConfig `mapstructure:",squash"`
Username string
Password string Registry string `mapstructure:"registry"`
Email string Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Email string `mapstructure:"email"`
tpl *packer.ConfigTemplate
} }
type PostProcessor struct { type PostProcessor struct {
config Config config Config
} }
func (p *PostProcessor) Configure(raw ...interface{}) error { func (p *PostProcessor) Configure(raws ...interface{}) error {
if err := mapstructure.Decode(raw, &p.config); err != nil { _, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err return err
} }
if p.config.Registry == "" { p.config.tpl, err = packer.NewConfigTemplate()
p.config.Registry = "registry.docker.io" if err != nil {
return err
} }
p.config.tpl.UserVars = p.config.PackerUserVars
// Accumulate any errors
errs := new(packer.MultiError)
if p.config.Username == "" { templates := map[string]*string{
return errors.New("Username is required to push docker image") "username": &p.config.Username,
"password": &p.config.Password,
} }
if p.config.Password == "" { for key, ptr := range templates {
return errors.New("Password is required to push docker image") 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 return nil
} }
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
id := artifact.Id() id := artifact.Id()
ui.Say("Pushing image: " + id) ui.Say("Pushing image: " + id)
if p.config.Email == "" { if p.config.Registry == "" {
cmd := exec.Command("docker", "login",
"-u=\""+p.config.Username+"\"", if p.config.Email == "" {
"-p=\""+p.config.Password+"\"") 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
}
if err := cmd.Run(); err != nil {
ui.Say("Login to the registry " + p.config.Registry + " failed")
return nil, false, err
} }
} else { } else {
cmd := exec.Command("docker", if p.config.Email == "" {
"login", cmd := exec.Command("docker",
"-u=\""+p.config.Username+"\"", "login",
"-p=\""+p.config.Password+"\"", "-u="+p.config.Username,
"-e=\""+p.config.Email+"\"") "-p="+p.config.Password,
p.config.Registry)
if err := cmd.Run(); err != nil {
ui.Say("Login to the registry " + p.config.Registry + " failed")
return nil, false, err
}
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
}
}
} }
cmd := exec.Command("docker", "push", id) cmd := exec.Command("docker", "push", id)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
ui.Say("Failed to push image: " + id) ui.Say("Failed to push image: " + id)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment