Finish up parameter parsing and validation.
Login to a docker index now works, ready for implementation of the actual push logic.
This commit is contained in:
parent
a0e533db41
commit
0ec18a723a
|
@ -1,70 +1,128 @@
|
|||
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.Email == "" {
|
||||
cmd := exec.Command("docker", "login",
|
||||
"-u=\""+p.config.Username+"\"",
|
||||
"-p=\""+p.config.Password+"\"")
|
||||
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
|
||||
}
|
||||
|
||||
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 p.config.Email == "" {
|
||||
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")
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err := cmd.Run(); err != nil {
|
||||
ui.Say("Failed to push image: " + id)
|
||||
|
|
Loading…
Reference in New Issue