mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-30 03:34:02 -04:00
/cc @andytson - Wanted to CC you in here so you could see some changes I made. First, I changed commit to opt-in, so you set "commit": true to get commit behavior. This simplifies the logic a bit. Then, I removed the skipping for StepExport and StepCommit and put that into the Builder itself. This simplifies those steps (limits abstraction leakage). Otherwise, everything looks great!
91 lines
1.6 KiB
Go
91 lines
1.6 KiB
Go
package docker
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/mitchellh/packer/common"
|
|
"github.com/mitchellh/packer/packer"
|
|
)
|
|
|
|
type Config struct {
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Commit bool
|
|
ExportPath string `mapstructure:"export_path"`
|
|
Image string
|
|
Pull bool
|
|
RunCommand []string `mapstructure:"run_command"`
|
|
|
|
tpl *packer.ConfigTemplate
|
|
}
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
c := new(Config)
|
|
md, err := common.DecodeConfig(c, raws...)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
c.tpl, err = packer.NewConfigTemplate()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
c.tpl.UserVars = c.PackerUserVars
|
|
|
|
// Defaults
|
|
if len(c.RunCommand) == 0 {
|
|
c.RunCommand = []string{
|
|
"run",
|
|
"-d", "-i", "-t",
|
|
"-v", "{{.Volumes}}",
|
|
"{{.Image}}",
|
|
"/bin/bash",
|
|
}
|
|
}
|
|
|
|
// Default Pull if it wasn't set
|
|
hasPull := false
|
|
for _, k := range md.Keys {
|
|
if k == "Pull" {
|
|
hasPull = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !hasPull {
|
|
c.Pull = true
|
|
}
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
|
|
|
templates := map[string]*string{
|
|
"export_path": &c.ExportPath,
|
|
"image": &c.Image,
|
|
}
|
|
|
|
for n, ptr := range templates {
|
|
var err error
|
|
*ptr, err = c.tpl.Process(*ptr, nil)
|
|
if err != nil {
|
|
errs = packer.MultiErrorAppend(
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
}
|
|
}
|
|
|
|
if c.Image == "" {
|
|
errs = packer.MultiErrorAppend(errs,
|
|
fmt.Errorf("image must be specified"))
|
|
}
|
|
|
|
if c.ExportPath != "" && c.Commit {
|
|
errs = packer.MultiErrorAppend(errs,
|
|
fmt.Errorf("both commit and export_path cannot be set"))
|
|
}
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
return nil, nil, errs
|
|
}
|
|
|
|
return c, nil, nil
|
|
}
|