From fc90ba12f0d0ebb562cb7fb7581389b68440053d Mon Sep 17 00:00:00 2001 From: Aleksandr Mezin Date: Mon, 23 Dec 2024 00:00:23 +0200 Subject: [PATCH] post-processor: add vagrantfile_content option --- .../components/post-processor/vagrant/README.md | 6 ++++++ docs/post-processors/vagrant.mdx | 6 ++++++ post-processor/vagrant/post-processor.go | 10 +++++++++- .../vagrant/post-processor.hcl2spec.go | 2 ++ post-processor/vagrant/post-processor_test.go | 16 ++++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/.web-docs/components/post-processor/vagrant/README.md b/.web-docs/components/post-processor/vagrant/README.md index 809821fb..2b3ff9a5 100644 --- a/.web-docs/components/post-processor/vagrant/README.md +++ b/.web-docs/components/post-processor/vagrant/README.md @@ -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 diff --git a/docs/post-processors/vagrant.mdx b/docs/post-processors/vagrant.mdx index 12aa979f..a1f6eadc 100644 --- a/docs/post-processors/vagrant.mdx +++ b/docs/post-processors/vagrant.mdx @@ -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 diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 954cf9d1..b393f519 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -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"` @@ -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 { @@ -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 { diff --git a/post-processor/vagrant/post-processor.hcl2spec.go b/post-processor/vagrant/post-processor.hcl2spec.go index 3f846b13..b76fa1ae 100644 --- a/post-processor/vagrant/post-processor.hcl2spec.go +++ b/post-processor/vagrant/post-processor.hcl2spec.go @@ -22,6 +22,7 @@ type FlatConfig struct { Include []string `mapstructure:"include" cty:"include" hcl:"include"` OutputPath *string `mapstructure:"output" cty:"output" hcl:"output"` Override map[string]interface{} `cty:"override" hcl:"override"` + VagrantfileContent *string `mapstructure:"vagrantfile_content" cty:"vagrantfile_content" hcl:"vagrantfile_content"` VagrantfileTemplate *string `mapstructure:"vagrantfile_template" cty:"vagrantfile_template" hcl:"vagrantfile_template"` VagrantfileTemplateGenerated *bool `mapstructure:"vagrantfile_template_generated" cty:"vagrantfile_template_generated" hcl:"vagrantfile_template_generated"` ProviderOverride *string `mapstructure:"provider_override" cty:"provider_override" hcl:"provider_override"` @@ -52,6 +53,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "include": &hcldec.AttrSpec{Name: "include", Type: cty.List(cty.String), Required: false}, "output": &hcldec.AttrSpec{Name: "output", Type: cty.String, Required: false}, "override": &hcldec.AttrSpec{Name: "override", Type: cty.Map(cty.String), Required: false}, + "vagrantfile_content": &hcldec.AttrSpec{Name: "vagrantfile_content", Type: cty.String, Required: false}, "vagrantfile_template": &hcldec.AttrSpec{Name: "vagrantfile_template", Type: cty.String, Required: false}, "vagrantfile_template_generated": &hcldec.AttrSpec{Name: "vagrantfile_template_generated", Type: cty.Bool, Required: false}, "provider_override": &hcldec.AttrSpec{Name: "provider_override", Type: cty.String, Required: false}, diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 030a04f6..fda48662 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -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"