From d9a128a375cc448cc0a5de7930f0cf1fb6d47f1a Mon Sep 17 00:00:00 2001 From: Hanjie Wang <48261337+hwang381@users.noreply.github.com> Date: Wed, 8 Jan 2020 11:33:39 -0800 Subject: [PATCH] In Vagrant post-processor, check whether the host system is able to create a dummy Vagrant box before processing (#8431) --- post-processor/vagrant/post-processor.go | 5 ++++ post-processor/vagrant/util.go | 34 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 0e57829f2..004365d27 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -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{ diff --git a/post-processor/vagrant/util.go b/post-processor/vagrant/util.go index 55c89ae5c..476136122 100644 --- a/post-processor/vagrant/util.go +++ b/post-processor/vagrant/util.go @@ -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) {