-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_output_dir.go
59 lines (49 loc) · 1.45 KB
/
step_output_dir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package vmm
import (
"context"
"fmt"
"os"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type stepOutputDir struct {
outputPath string
force bool
cleanup bool
}
func (step *stepOutputDir) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
// Check if the output directory exists.
if _, err := os.Stat(step.outputPath); err == nil {
// If the build isn't forced, error out here.
if !step.force {
state.Put("error", fmt.Errorf("output directory already exists: %s", step.outputPath))
return multistep.ActionHalt
}
// Forced build, so remove the directory.
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting previous output directory...")
os.RemoveAll(step.outputPath)
}
// Mark that a cleanup is definately needed.
step.cleanup = true
// Create the output directory.
if err := os.MkdirAll(step.outputPath, 0755); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (step *stepOutputDir) Cleanup(state multistep.StateBag) {
// Skip if no output directory was made in the first place.
if !step.cleanup {
return
}
// Only clean up if the build didn't finish.
if isCancelled(state) || isHalted(state) {
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting output directory...")
if err := os.RemoveAll(step.outputPath); err != nil {
ui.Error("Unable to delete output directory: " + err.Error())
}
}
}