2013-12-21 18:20:15 -05:00
|
|
|
package common
|
2013-06-11 18:45:52 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-16 23:45:05 -05:00
|
|
|
"fmt"
|
2013-07-31 18:16:39 -04:00
|
|
|
"log"
|
2013-06-11 18:45:52 -04:00
|
|
|
"os"
|
2013-12-16 23:45:05 -05:00
|
|
|
"path/filepath"
|
2013-07-31 18:16:39 -04:00
|
|
|
"time"
|
2013-12-16 23:45:05 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-11 18:45:52 -04:00
|
|
|
)
|
|
|
|
|
2013-12-21 18:20:15 -05:00
|
|
|
// StepOutputDir sets up the output directory by creating it if it does
|
|
|
|
// not exist, deleting it if it does exist and we're forcing, and cleaning
|
|
|
|
// it up when we're done with it.
|
|
|
|
type StepOutputDir struct {
|
|
|
|
Force bool
|
|
|
|
Path string
|
2015-06-22 12:09:12 -04:00
|
|
|
|
|
|
|
cleanup bool
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
2013-06-11 18:45:52 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepOutputDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-12 22:23:00 -04:00
|
|
|
|
2015-06-13 16:58:37 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-12 22:23:00 -04:00
|
|
|
ui.Say("Deleting previous output directory...")
|
2013-12-21 18:20:15 -05:00
|
|
|
os.RemoveAll(s.Path)
|
2013-07-12 22:23:00 -04:00
|
|
|
}
|
2013-06-11 18:45:52 -04:00
|
|
|
|
2015-06-22 12:09:12 -04:00
|
|
|
// Enable cleanup
|
|
|
|
s.cleanup = true
|
|
|
|
|
2013-12-16 23:45:05 -05:00
|
|
|
// Create the directory
|
2013-12-21 18:20:15 -05:00
|
|
|
if err := os.MkdirAll(s.Path, 0755); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-11 18:45:52 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-12-16 23:45:05 -05:00
|
|
|
// Make sure we can write in the directory
|
2013-12-21 18:20:15 -05:00
|
|
|
f, err := os.Create(filepath.Join(s.Path, "_packer_perm_check"))
|
2013-12-16 23:45:05 -05:00
|
|
|
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())
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:20:15 -05:00
|
|
|
func (s *StepOutputDir) Cleanup(state multistep.StateBag) {
|
2015-06-22 12:09:12 -04:00
|
|
|
if !s.cleanup {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
|
|
|
if cancelled || halted {
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting output directory...")
|
2013-07-31 18:16:39 -04:00
|
|
|
for i := 0; i < 5; i++ {
|
2013-12-21 18:20:15 -05:00
|
|
|
err := os.RemoveAll(s.Path)
|
2013-07-31 18:16:39 -04:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Error removing output dir: %s", err)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
2013-06-27 22:23:40 -04:00
|
|
|
}
|
|
|
|
}
|