From a6c518a78814404e7b2b14c83cdcdfa20e1ab020 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 23 Apr 2025 11:41:42 -0700 Subject: [PATCH] Allow box architecture to be set in vagrant builder When adding the source box for the vagrant builder, allow a specific architecture to be requested. By default Vagrant will use the host architecture when adding the box, so this option is only required when the guest architecture differs from the host. --- .../components/builder/vagrant/README.md | 2 ++ builder/vagrant/builder.go | 27 +++++++++-------- builder/vagrant/builder.hcl2spec.go | 2 ++ builder/vagrant/step_add_box.go | 29 +++++++++++-------- builder/vagrant/step_add_box_test.go | 8 +++++ .../builder/vagrant/Config-not-required.mdx | 2 ++ 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/.web-docs/components/builder/vagrant/README.md b/.web-docs/components/builder/vagrant/README.md index bd9a2703..a04790d1 100644 --- a/.web-docs/components/builder/vagrant/README.md +++ b/.web-docs/components/builder/vagrant/README.md @@ -95,6 +95,8 @@ the Compress post-processor will not work with this builder. - `box_version` (string) - What box version to use when initializing Vagrant. +- `box_architecture` (string) - Architecture of the box to use when initializing Vagrant. + - `template` (string) - a path to a golang template for a vagrantfile. Our default template can be found [here](https://github.com/hashicorp/packer-plugin-vagrant/blob/main/builder/vagrant/step_create_vagrantfile.go#L39-L54). The template variables available to you are `{{ .BoxName }}`, `{{ .SyncedFolder }}`, and `{{.InsertKey}}`, which diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index f3c3d1a5..8c7384a4 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -99,6 +99,8 @@ type Config struct { TeardownMethod string `mapstructure:"teardown_method" required:"false"` // What box version to use when initializing Vagrant. BoxVersion string `mapstructure:"box_version" required:"false"` + // Architecture of the box to use when initializing Vagrant. + BoxArchitecture string `mapstructure:"box_architecture" required:"false"` // a path to a golang template for a vagrantfile. Our default template can // be found [here](https://github.com/hashicorp/packer-plugin-vagrant/blob/main/builder/vagrant/step_create_vagrantfile.go#L39-L54). The template variables available to you are // `{{ .BoxName }}`, `{{ .SyncedFolder }}`, and `{{.InsertKey}}`, which @@ -324,18 +326,19 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) InsertKey: b.config.InsertKey, }, &StepAddBox{ - BoxVersion: b.config.BoxVersion, - CACert: b.config.AddCACert, - CAPath: b.config.AddCAPath, - DownloadCert: b.config.AddCert, - Clean: b.config.AddClean, - Force: b.config.AddForce, - Insecure: b.config.AddInsecure, - Provider: b.config.Provider, - SourceBox: b.config.SourceBox, - BoxName: b.config.BoxName, - GlobalID: b.config.GlobalID, - SkipAdd: b.config.SkipAdd, + BoxArchitecture: b.config.BoxArchitecture, + BoxVersion: b.config.BoxVersion, + CACert: b.config.AddCACert, + CAPath: b.config.AddCAPath, + DownloadCert: b.config.AddCert, + Clean: b.config.AddClean, + Force: b.config.AddForce, + Insecure: b.config.AddInsecure, + Provider: b.config.Provider, + SourceBox: b.config.SourceBox, + BoxName: b.config.BoxName, + GlobalID: b.config.GlobalID, + SkipAdd: b.config.SkipAdd, }, &StepUp{ TeardownMethod: b.config.TeardownMethod, diff --git a/builder/vagrant/builder.hcl2spec.go b/builder/vagrant/builder.hcl2spec.go index 8bbf2667..240c4457 100644 --- a/builder/vagrant/builder.hcl2spec.go +++ b/builder/vagrant/builder.hcl2spec.go @@ -94,6 +94,7 @@ type FlatConfig struct { Provider *string `mapstructure:"provider" required:"false" cty:"provider" hcl:"provider"` TeardownMethod *string `mapstructure:"teardown_method" required:"false" cty:"teardown_method" hcl:"teardown_method"` BoxVersion *string `mapstructure:"box_version" required:"false" cty:"box_version" hcl:"box_version"` + BoxArchitecture *string `mapstructure:"box_architecture" required:"false" cty:"box_architecture" hcl:"box_architecture"` Template *string `mapstructure:"template" required:"false" cty:"template" hcl:"template"` SyncedFolder *string `mapstructure:"synced_folder" cty:"synced_folder" hcl:"synced_folder"` SkipAdd *bool `mapstructure:"skip_add" required:"false" cty:"skip_add" hcl:"skip_add"` @@ -204,6 +205,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "provider": &hcldec.AttrSpec{Name: "provider", Type: cty.String, Required: false}, "teardown_method": &hcldec.AttrSpec{Name: "teardown_method", Type: cty.String, Required: false}, "box_version": &hcldec.AttrSpec{Name: "box_version", Type: cty.String, Required: false}, + "box_architecture": &hcldec.AttrSpec{Name: "box_architecture", Type: cty.String, Required: false}, "template": &hcldec.AttrSpec{Name: "template", Type: cty.String, Required: false}, "synced_folder": &hcldec.AttrSpec{Name: "synced_folder", Type: cty.String, Required: false}, "skip_add": &hcldec.AttrSpec{Name: "skip_add", Type: cty.Bool, Required: false}, diff --git a/builder/vagrant/step_add_box.go b/builder/vagrant/step_add_box.go index ec56bc08..a5220eae 100644 --- a/builder/vagrant/step_add_box.go +++ b/builder/vagrant/step_add_box.go @@ -14,18 +14,19 @@ import ( ) type StepAddBox struct { - BoxVersion string - CACert string - CAPath string - DownloadCert string - Clean bool - Force bool - Insecure bool - Provider string - SourceBox string - BoxName string - GlobalID string - SkipAdd bool + BoxArchitecture string + BoxVersion string + CACert string + CAPath string + DownloadCert string + Clean bool + Force bool + Insecure bool + Provider string + SourceBox string + BoxName string + GlobalID string + SkipAdd bool } func (s *StepAddBox) generateAddArgs() []string { @@ -37,6 +38,10 @@ func (s *StepAddBox) generateAddArgs() []string { addArgs = append(addArgs, s.SourceBox) + if s.BoxArchitecture != "" { + addArgs = append(addArgs, "--architecture", s.BoxArchitecture) + } + if s.BoxVersion != "" { addArgs = append(addArgs, "--box-version", s.BoxVersion) } diff --git a/builder/vagrant/step_add_box_test.go b/builder/vagrant/step_add_box_test.go index 82420e1d..48da4615 100644 --- a/builder/vagrant/step_add_box_test.go +++ b/builder/vagrant/step_add_box_test.go @@ -38,6 +38,14 @@ func TestPrepAddArgs(t *testing.T) { }, Expected: []string{"my_source_box"}, }, + { + Step: StepAddBox{ + SourceBox: "my_source_box", + Provider: "virtualbox", + BoxArchitecture: "amd64", + }, + Expected: []string{"my_source_box", "--architecture", "amd64", "--provider", "virtualbox"}, + }, { Step: StepAddBox{ BoxVersion: "eleventyone", diff --git a/docs-partials/builder/vagrant/Config-not-required.mdx b/docs-partials/builder/vagrant/Config-not-required.mdx index 89cacadc..9a4c0d24 100644 --- a/docs-partials/builder/vagrant/Config-not-required.mdx +++ b/docs-partials/builder/vagrant/Config-not-required.mdx @@ -42,6 +42,8 @@ - `box_version` (string) - What box version to use when initializing Vagrant. +- `box_architecture` (string) - Architecture of the box to use when initializing Vagrant. + - `template` (string) - a path to a golang template for a vagrantfile. Our default template can be found [here](https://github.com/hashicorp/packer-plugin-vagrant/blob/main/builder/vagrant/step_create_vagrantfile.go#L39-L54). The template variables available to you are `{{ .BoxName }}`, `{{ .SyncedFolder }}`, and `{{.InsertKey}}`, which