From d209fcd5ccdf9404ef4cd770c5960a93904704d3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 16 Dec 2013 20:45:05 -0800 Subject: [PATCH] builder/virtualbox: verify output dir is writable --- CHANGELOG.md | 2 ++ builder/virtualbox/step_prepare_output_dir.go | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7afc519f7..cfdbd8dae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ IMPROVEMENTS: * builder/qemu: Next `run_once` option tells Qemu to run only once, which is useful for Windows installs that handle reboots for you. [GH-687] +* builder/virtualbox: Nice errors if Packer can't write to + the output directory. * provisioner/puppet-masterless: Can now specify a `manifest_dir` to upload manifests to the remote machine for imports. [GH-655] diff --git a/builder/virtualbox/step_prepare_output_dir.go b/builder/virtualbox/step_prepare_output_dir.go index 32d6260ff..4a3e6c844 100644 --- a/builder/virtualbox/step_prepare_output_dir.go +++ b/builder/virtualbox/step_prepare_output_dir.go @@ -1,11 +1,14 @@ package virtualbox import ( - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" + "fmt" "log" "os" + "path/filepath" "time" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) type stepPrepareOutputDir struct{} @@ -19,11 +22,22 @@ func (stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction { os.RemoveAll(config.OutputDir) } + // Create the directory if err := os.MkdirAll(config.OutputDir, 0755); err != nil { state.Put("error", err) return multistep.ActionHalt } + // Make sure we can write in the directory + f, err := os.Create(filepath.Join(config.OutputDir, "_packer_perm_check")) + if err != nil { + err = fmt.Errorf("Couldn't write to output directory: %s", err) + state.Put("error", err) + return multistep.ActionHalt + } + f.Close() + os.Remove(f.Name()) + return multistep.ActionContinue }