Merge pull request #7951 from DanHam/allow-dynamic-vagrantfile-create

Vagrant pp: Add setting to allow box Vagrantfiles to be generated during the build
This commit is contained in:
Megan Marsh 2019-08-13 10:06:41 -07:00 committed by GitHub
commit 3acfb31595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 8 deletions

View File

@ -47,6 +47,7 @@ type Config struct {
OutputPath string `mapstructure:"output"`
Override map[string]interface{}
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
VagrantfileTemplateGenerated bool `mapstructure:"vagrantfile_template_generated"`
ctx interpolate.Context
}
@ -217,7 +218,7 @@ func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
}
var errs *packer.MultiError
if c.VagrantfileTemplate != "" {
if c.VagrantfileTemplate != "" && c.VagrantfileTemplateGenerated == false {
_, err := os.Stat(c.VagrantfileTemplate)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf(

View File

@ -137,13 +137,25 @@ func TestPostProcessorPrepare_vagrantfileTemplateExists(t *testing.T) {
t.Fatalf("err: %s", err)
}
var p PostProcessor
if err := p.Configure(c); err != nil {
t.Fatal("no error expected as vagrantfile_template exists")
}
if err := os.Remove(name); err != nil {
t.Fatalf("err: %s", err)
}
var p PostProcessor
if err := p.Configure(c); err == nil {
t.Fatal("expected an error since vagrantfile_template does not exist")
t.Fatal("expected error since vagrantfile_template does not exist and vagrantfile_template_generated is unset")
}
// The vagrantfile_template will be generated during the build process
c["vagrantfile_template_generated"] = true
if err := p.Configure(c); err != nil {
t.Fatal("no error expected due to missing vagrantfile_template as vagrantfile_template_generated is set")
}
}

View File

@ -83,6 +83,15 @@ more details about certain options in following sections.
- `vagrantfile_template` (string) - Path to a template to use for the
Vagrantfile that is packaged with the box.
- `vagrantfile_template_generated` (boolean) - By default, Packer will
exit with an error if the file specified using the
`vagrantfile_template` variable is not found. However, under certain
circumstances, it may be desirable to dynamically generate the
Vagrantfile during the course of the build. Setting this variable to
`true` skips the start up check and allows the user to script the
creation of the Vagrantfile at some previous point in the build.
Defaults to `false`.
## Provider-Specific Overrides
If you have a Packer template with multiple builder types within it, you may