In Vagrant post-processor, check whether the host system is able to create a dummy Vagrant box before processing (#8431)

This commit is contained in:
Hanjie Wang 2020-01-08 11:33:39 -08:00 committed by Sylvia Moss
parent 49a33c04cd
commit d9a128a375
2 changed files with 39 additions and 0 deletions

View File

@ -93,6 +93,11 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p
config = specificConfig
}
err := CreateDummyBox(ui, config.CompressionLevel)
if err != nil {
return nil, false, err
}
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))
config.ctx.Data = &outputPathTemplate{

View File

@ -5,6 +5,7 @@ import (
"compress/flate"
"encoding/json"
"fmt"
"github.com/hashicorp/packer/packer/tmp"
"io"
"log"
"os"
@ -158,6 +159,39 @@ func DirToBox(dst, dir string, ui packer.Ui, level int) error {
return filepath.Walk(dir, tarWalk)
}
// CreateDummyBox create a dummy Vagrant-compatible box under temporary dir
// This function is mainly used to check cases such as the host system having
// a GNU tar incompatible uname that will cause the actual Vagrant box creation
// to fail later
func CreateDummyBox(ui packer.Ui, level int) error {
ui.Say("Creating a dummy Vagrant box to ensure the host system can create one correctly")
// Create a temporary dir to create dummy Vagrant box from
tempDir, err := tmp.Dir("packer")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
// Write some dummy metadata for the box
if err := WriteMetadata(tempDir, make(map[string]string)); err != nil {
return err
}
// Create the dummy Vagrant box
tempBox, err := tmp.File("box-*.box")
if err != nil {
return err
}
defer tempBox.Close()
defer os.Remove(tempBox.Name())
if err := DirToBox(tempBox.Name(), tempDir, nil, level); err != nil {
return err
}
return nil
}
// WriteMetadata writes the "metadata.json" file for a Vagrant box.
func WriteMetadata(dir string, contents interface{}) error {
if _, err := os.Stat(filepath.Join(dir, "metadata.json")); os.IsNotExist(err) {