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

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

View File

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

View File

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

View File

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

View File

@ -2,13 +2,11 @@ package vagrant
import ( import (
"fmt" "fmt"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"text/template"
) )
type VMwareBoxConfig struct { type VMwareBoxConfig struct {
@ -16,6 +14,8 @@ type VMwareBoxConfig struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"` VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
tpl *common.Template
} }
type VMwareBoxPostProcessor struct { type VMwareBoxPostProcessor struct {
@ -23,13 +23,48 @@ type VMwareBoxPostProcessor struct {
} }
func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error { func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error {
for _, raw := range raws { md, err := common.DecodeConfig(&p.config, raws...)
err := mapstructure.Decode(raw, &p.config) 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 { 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 return nil
} }
@ -77,8 +112,11 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif
} }
defer vf.Close() defer vf.Close()
t := template.Must(template.New("vagrantfile").Parse(string(contents))) vagrantfileContents, err := p.config.tpl.Process(string(contents), nil)
t.Execute(vf, new(struct{})) if err != nil {
return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err)
}
vf.Write([]byte(vagrantfileContents))
vf.Close() vf.Close()
} }