Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .web-docs/components/post-processor/vagrant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ more details about certain options in following sections.
creation of the Vagrantfile at some previous point in the build.
Defaults to `false`.

- `vagrantfile_content` (string) - Content of the custom Vagrantfile that is
packaged with the box. This option is intended to be used with
[`templatefile`](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/functions/file/templatefile)
function in HCL templates, instead of `vagrantfile_template`.
`vagrantfile_content` and `vagrantfile_template` can't be set both at the same time.

## Using together with the Artifice post-processor

Sometimes you may want to run several builds in a pipeline rather than running
Expand Down
6 changes: 6 additions & 0 deletions docs/post-processors/vagrant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ more details about certain options in following sections.
creation of the Vagrantfile at some previous point in the build.
Defaults to `false`.

- `vagrantfile_content` (string) - Content of the custom Vagrantfile that is
packaged with the box. This option is intended to be used with
[`templatefile`](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/functions/file/templatefile)
function in HCL templates, instead of `vagrantfile_template`.
`vagrantfile_content` and `vagrantfile_template` can't be set both at the same time.

## Using together with the Artifice post-processor

Sometimes you may want to run several builds in a pipeline rather than running
Expand Down
10 changes: 9 additions & 1 deletion post-processor/vagrant/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Config struct {
Include []string `mapstructure:"include"`
OutputPath string `mapstructure:"output"`
Override map[string]interface{}
VagrantfileContent string `mapstructure:"vagrantfile_content"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
VagrantfileTemplateGenerated bool `mapstructure:"vagrantfile_template_generated"`
ProviderOverride string `mapstructure:"provider_override"`
Expand Down Expand Up @@ -188,7 +189,9 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p

// Write our Vagrantfile
var customVagrantfile string
if config.VagrantfileTemplate != "" {
if config.VagrantfileContent != "" {
customVagrantfile = config.VagrantfileContent
} else if config.VagrantfileTemplate != "" {
ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
if err != nil {
Expand Down Expand Up @@ -292,6 +295,11 @@ func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
}

var errs *packersdk.MultiError
if c.VagrantfileContent != "" && c.VagrantfileTemplate != "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(
"You may either set vagrantfile_content or vagrantfile_template but not both"))
}

if c.VagrantfileTemplate != "" && c.VagrantfileTemplateGenerated == false {
_, err := os.Stat(c.VagrantfileTemplate)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions post-processor/vagrant/post-processor.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions post-processor/vagrant/post-processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ func TestPostProcessorPrepare_vagrantfileTemplateExists(t *testing.T) {
}
}

func TestPostProcessorPrepare_vagrantfileContent(t *testing.T) {
c := testConfig()
c["vagrantfile_content"] = "Vagrant.configure('2') do |config|\nend\n"

var p PostProcessor

if err := p.Configure(c); err != nil {
t.Fatalf("err: %s", err)
}

c["vagrantfile_template"] = "Vagrantfile"
if err := p.Configure(c); err == nil {
t.Fatal("expected error since vagrantfile_content and vagrantfile_template are both set")
}
}

func TestPostProcessorPrepare_ProviderOverrideExists(t *testing.T) {
c := testConfig()
c["provider_override"] = "foo"
Expand Down