From 4b042ac4022f195867d27b971bf32754d8aa8324 Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 5 Aug 2019 18:26:59 +0100 Subject: [PATCH 1/4] Tests: Should not error when `vagrantfile_template` exists --- post-processor/vagrant/post-processor_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 7e6be2b29..372e1da68 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -137,11 +137,16 @@ 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") } From 19f3a63ee1cb0ee97ac965d15a3f50292f1e7a4b Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 5 Aug 2019 18:30:39 +0100 Subject: [PATCH 2/4] Tests: Should not error when template is to be created during build --- post-processor/vagrant/post-processor_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 372e1da68..fccf4cf24 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -148,7 +148,14 @@ func TestPostProcessorPrepare_vagrantfileTemplateExists(t *testing.T) { } 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") } } From 1bffdd9bff51d1e7023e4133e69078bb1dc154f5 Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 5 Aug 2019 18:35:01 +0100 Subject: [PATCH 3/4] Add option to allow box Vagrantfile to be dynamically generated during build --- post-processor/vagrant/post-processor.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 02dde3209..90197c316 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -42,11 +42,12 @@ var builtins = map[string]string{ type Config struct { common.PackerConfig `mapstructure:",squash"` - CompressionLevel int `mapstructure:"compression_level"` - Include []string `mapstructure:"include"` - OutputPath string `mapstructure:"output"` - Override map[string]interface{} - VagrantfileTemplate string `mapstructure:"vagrantfile_template"` + CompressionLevel int `mapstructure:"compression_level"` + Include []string `mapstructure:"include"` + 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( From cfcff0760f2f4ebc49461d2b2ef46c5dbfae1c55 Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 5 Aug 2019 18:36:48 +0100 Subject: [PATCH 4/4] Add docs for option to allow dynamic generation of box Vagrantfiles --- website/source/docs/post-processors/vagrant.html.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/website/source/docs/post-processors/vagrant.html.md b/website/source/docs/post-processors/vagrant.html.md index f62ff5afd..d9168cfce 100644 --- a/website/source/docs/post-processors/vagrant.html.md +++ b/website/source/docs/post-processors/vagrant.html.md @@ -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