post-processor/vagrant: process user variabels [GH-295]

This commit is contained in:
Mitchell Hashimoto 2013-08-15 15:19:41 -04:00
parent 4f568a0afe
commit 063adf4bbb
4 changed files with 136 additions and 21 deletions

View File

@ -32,6 +32,7 @@ BUG FIXES:
actually starts. [GH-288]
* builder/vmware: dowload progress won't be shown until download
actually starts. [GH-288]
* post-processor/vagrant: Process user variables. [GH-295]
## 0.3.1 (August 12, 2013)

View File

@ -2,7 +2,6 @@ package vagrant
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
@ -10,7 +9,6 @@ import (
"os"
"path/filepath"
"strings"
"text/template"
)
type AWSBoxConfig struct {
@ -18,6 +16,8 @@ type AWSBoxConfig struct {
OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
tpl *common.Template
}
type AWSVagrantfileTemplate struct {
@ -29,13 +29,48 @@ type AWSBoxPostProcessor struct {
}
func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
md, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err
}
p.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
// Accumulate any errors
errs := common.CheckUnusedConfig(md)
templates := map[string]*string{
"output": &p.config.OutputPath,
}
for n, ptr := range templates {
var err error
*ptr, err = p.config.tpl.Process(*ptr, nil)
if err != nil {
return err
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
validates := map[string]*string{
"vagrantfile_template": &p.config.VagrantfileTemplate,
}
for n, ptr := range validates {
if err := p.config.tpl.Validate(*ptr); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error parsing %s: %s", n, err))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
@ -94,8 +129,11 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
vagrantfileContents = string(contents)
}
t := template.Must(template.New("vagrantfile").Parse(vagrantfileContents))
t.Execute(vf, tplData)
vagrantfileContents, err = p.config.tpl.Process(vagrantfileContents, tplData)
if err != nil {
return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err)
}
vf.Write([]byte(vagrantfileContents))
vf.Close()
// Create the metadata

View File

@ -3,7 +3,6 @@ package vagrant
import (
"errors"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
@ -12,7 +11,6 @@ import (
"path/filepath"
"regexp"
"strings"
"text/template"
)
type VBoxBoxConfig struct {
@ -20,6 +18,8 @@ type VBoxBoxConfig struct {
OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
tpl *common.Template
}
type VBoxVagrantfileTemplate struct {
@ -31,13 +31,48 @@ type VBoxBoxPostProcessor struct {
}
func (p *VBoxBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
md, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err
}
p.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
// Accumulate any errors
errs := common.CheckUnusedConfig(md)
templates := map[string]*string{
"output": &p.config.OutputPath,
}
for n, ptr := range templates {
var err error
*ptr, err = p.config.tpl.Process(*ptr, nil)
if err != nil {
return err
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
validates := map[string]*string{
"vagrantfile_template": &p.config.VagrantfileTemplate,
}
for n, ptr := range validates {
if err := p.config.tpl.Validate(*ptr); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error parsing %s: %s", n, err))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
@ -96,8 +131,11 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
vagrantfileContents = string(contents)
}
t := template.Must(template.New("vagrantfile").Parse(vagrantfileContents))
t.Execute(vf, tplData)
vagrantfileContents, err = p.config.tpl.Process(vagrantfileContents, tplData)
if err != nil {
return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err)
}
vf.Write([]byte(vagrantfileContents))
vf.Close()
// Create the metadata

View File

@ -2,13 +2,11 @@ package vagrant
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"path/filepath"
"text/template"
)
type VMwareBoxConfig struct {
@ -16,6 +14,8 @@ type VMwareBoxConfig struct {
OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
tpl *common.Template
}
type VMwareBoxPostProcessor struct {
@ -23,13 +23,48 @@ type VMwareBoxPostProcessor struct {
}
func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws {
err := mapstructure.Decode(raw, &p.config)
md, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err
}
p.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
p.config.tpl.UserVars = p.config.PackerUserVars
// Accumulate any errors
errs := common.CheckUnusedConfig(md)
templates := map[string]*string{
"output": &p.config.OutputPath,
}
for n, ptr := range templates {
var err error
*ptr, err = p.config.tpl.Process(*ptr, nil)
if err != nil {
return err
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
validates := map[string]*string{
"vagrantfile_template": &p.config.VagrantfileTemplate,
}
for n, ptr := range validates {
if err := p.config.tpl.Validate(*ptr); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error parsing %s: %s", n, err))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
@ -77,8 +112,11 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif
}
defer vf.Close()
t := template.Must(template.New("vagrantfile").Parse(string(contents)))
t.Execute(vf, new(struct{}))
vagrantfileContents, err := p.config.tpl.Process(string(contents), nil)
if err != nil {
return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err)
}
vf.Write([]byte(vagrantfileContents))
vf.Close()
}