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
import (
"errors"
"github.com/mitchellh/mapstructure"
"fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"os/exec"
)
type Config struct {
Registry string
Username string
Password string
Email string
common.PackerConfig `mapstructure:",squash"`
Registry string `mapstructure:"registry"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Email string `mapstructure:"email"`
tpl *packer.ConfigTemplate
}
type PostProcessor struct {
config Config
}
func (p *PostProcessor) Configure(raw ...interface{}) error {
if err := mapstructure.Decode(raw, &p.config); err != nil {
func (p *PostProcessor) Configure(raws ...interface{}) error {
_, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err
}
if p.config.Registry == "" {
p.config.Registry = "registry.docker.io"
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{
"username": &p.config.Username,
"password": &p.config.Password,
}
if p.config.Username == "" {
return errors.New("Username is required to push docker image")
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 p.config.Password == "" {
return errors.New("Password is required to push docker image")
if len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
id := artifact.Id()
ui.Say("Pushing image: " + id)
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
}
}
} else {
if p.config.Email == "" {
cmd := exec.Command("docker", "login",
"-u=\""+p.config.Username+"\"",
"-p=\""+p.config.Password+"\"")
cmd := exec.Command("docker",
"login",
"-u="+p.config.Username,
"-p="+p.config.Password,
p.config.Registry)
if err := cmd.Run(); err != nil {
ui.Say("Login to the registry " + p.config.Registry + " failed")
......@@ -55,9 +110,10 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
} else {
cmd := exec.Command("docker",
"login",
"-u=\""+p.config.Username+"\"",
"-p=\""+p.config.Password+"\"",
"-e=\""+p.config.Email+"\"")
"-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")
......@@ -65,6 +121,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
}
}
}
cmd := exec.Command("docker", "push", id)
if err := cmd.Run(); err != nil {
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