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
21 changes: 17 additions & 4 deletions post-processor/vagrant/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Config struct {
OutputPath string `mapstructure:"output"`
Override map[string]interface{}
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
VagrantfileTemplateContent string `mapstructure:"vagrantfile_template_content"`
VagrantfileTemplateGenerated bool `mapstructure:"vagrantfile_template_generated"`
ProviderOverride string `mapstructure:"provider_override"`
Architecture string `mapstructure:"architecture"`
Expand Down Expand Up @@ -190,12 +191,20 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p
var customVagrantfile string
if config.VagrantfileTemplate != "" {
ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
if err != nil {
return nil, false, err

var templateContent string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this condition might be nested in a conflicting if statement.

// lets default to using VagrantTemplateContent
templateContent := config.VagrantfileTemplateContent
if templateContent == "" && config.VagrantfileTemplate != "" {
	ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
	customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
	if err != nil {
		return nil, false, err
	}
	templateContent = string(customBytes)
}
	
if templateContent != "" {
       customVagrantfile, err = interpolate.Render(templateContent, &config.ctx)
	if err != nil {
		return nil, false, err
	}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nywilken could you please see the below comment #112 (comment) and let me know if this is still needs to be changed !!

if config.VagrantfileTemplateContent != "" {
templateContent = config.VagrantfileTemplateContent
} else {
customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
if err != nil {
return nil, false, err
}

templateContent = string(customBytes)
}

customVagrantfile, err = interpolate.Render(string(customBytes), &config.ctx)
customVagrantfile, err = interpolate.Render(templateContent, &config.ctx)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -292,6 +301,10 @@ func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
}

var errs *packersdk.MultiError
if c.VagrantfileTemplateContent != "" && c.VagrantfileTemplate == "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(
"'vagrantfile_template' should be provided to use 'vagrantfile_template_content'"))
}
Comment on lines +304 to +307

@nywilken nywilken Mar 19, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am of the understanding that you would want to specify just the content with no filepath needed. Is that not the case?

If they are mutually exclusive I would rewrite the logic to be as follows:

Suggested change
if c.VagrantfileTemplateContent != "" && c.VagrantfileTemplate == "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(
"'vagrantfile_template' should be provided to use 'vagrantfile_template_content'"))
}
if c.VagrantfileTemplateContent != "" && c.VagrantfileTemplate != "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(
"'vagrantfile_template' can not be used with 'vagrantfile_template_content'"))
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nywilken if you see the example here these are Not Exclusive we are trying to load the value for the variable my_ssh_key in the vagrant_template

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.

34 changes: 34 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,40 @@ func TestPostProcessorPrepare_vagrantfileTemplateExists(t *testing.T) {
}
}

// write unit test to cover vagrantfile_template_content but vagrantfile_template is not set
func TestPostProcessorPrepare_vagrantfileTemplateContentExists(t *testing.T) {
f, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}

name := f.Name()

c := testConfig()
c["vagrantfile_template"] = name
c["vagrantfile_template_content"] = "content"

if err := f.Close(); err != nil {
t.Fatalf("err: %s", err)
}
var p PostProcessor

if err := p.Configure(c); err != nil {
t.Fatal("Should not have errored since vagrantfile_template and vagrantfile_template_content both exist")
}

if err := os.Remove(name); err != nil {
t.Fatalf("err: %s", err)
}

c = testConfig()
c["vagrantfile_template_content"] = "content"

if err := p.Configure(c); err == nil {
t.Fatal("Should have errored since vagrantfile_template_content exists but vagrantfile_template is not set")
}
}

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