Merge pull request #4645 from mitchellh/hyperv-validate
builder/hyper-v: validate output dir in step, not in config
This commit is contained in:
commit
003611e571
|
@ -2,9 +2,9 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
"github.com/mitchellh/packer/template/interpolate"
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type OutputConfig struct {
|
type OutputConfig struct {
|
||||||
|
@ -16,13 +16,5 @@ func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
|
||||||
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
|
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
|
||||||
}
|
}
|
||||||
|
|
||||||
var errs []error
|
return nil
|
||||||
if !pc.PackerForce {
|
|
||||||
if _, err := os.Stat(c.OutputDir); err == nil {
|
|
||||||
errs = append(errs, fmt.Errorf(
|
|
||||||
"Output directory '%s' already exists. It must not exist.", c.OutputDir))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mitchellh/packer/common"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mitchellh/packer/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOutputConfigPrepare(t *testing.T) {
|
func TestOutputConfigPrepare(t *testing.T) {
|
||||||
|
@ -39,7 +40,7 @@ func TestOutputConfigPrepare_exists(t *testing.T) {
|
||||||
PackerForce: false,
|
PackerForce: false,
|
||||||
}
|
}
|
||||||
errs := c.Prepare(testConfigTemplate(t), pc)
|
errs := c.Prepare(testConfigTemplate(t), pc)
|
||||||
if len(errs) == 0 {
|
if len(errs) != 0 {
|
||||||
t.Fatal("should have errors")
|
t.Fatal("should not have errors")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,16 +17,30 @@ import (
|
||||||
type StepOutputDir struct {
|
type StepOutputDir struct {
|
||||||
Force bool
|
Force bool
|
||||||
Path string
|
Path string
|
||||||
|
|
||||||
|
cleanup bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
if _, err := os.Stat(s.Path); err == nil && s.Force {
|
if _, err := os.Stat(s.Path); err == nil {
|
||||||
|
if !s.Force {
|
||||||
|
err := fmt.Errorf(
|
||||||
|
"Output directory exists: %s\n\n"+
|
||||||
|
"Use the force flag to delete it prior to building.",
|
||||||
|
s.Path)
|
||||||
|
state.Put("error", err)
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say("Deleting previous output directory...")
|
ui.Say("Deleting previous output directory...")
|
||||||
os.RemoveAll(s.Path)
|
os.RemoveAll(s.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable cleanup
|
||||||
|
s.cleanup = true
|
||||||
|
|
||||||
// Create the directory
|
// Create the directory
|
||||||
if err := os.MkdirAll(s.Path, 0755); err != nil {
|
if err := os.MkdirAll(s.Path, 0755); err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -47,6 +61,10 @@ func (s *StepOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepOutputDir) Cleanup(state multistep.StateBag) {
|
func (s *StepOutputDir) Cleanup(state multistep.StateBag) {
|
||||||
|
if !s.cleanup {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, cancelled := state.GetOk(multistep.StateCancelled)
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
||||||
_, halted := state.GetOk(multistep.StateHalted)
|
_, halted := state.GetOk(multistep.StateHalted)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue