github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/hyperv/common/step_create_tempdir.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 "github.com/hashicorp/packer/packer" 9 "github.com/mitchellh/multistep" 10 ) 11 12 type StepCreateTempDir struct { 13 // The user-supplied root directores into which we create subdirectories. 14 TempPath string 15 VhdTempPath string 16 // The subdirectories with the randomly generated name. 17 dirPath string 18 vhdDirPath string 19 } 20 21 func (s *StepCreateTempDir) Run(state multistep.StateBag) multistep.StepAction { 22 ui := state.Get("ui").(packer.Ui) 23 24 ui.Say("Creating temporary directory...") 25 26 if s.TempPath == "" { 27 s.TempPath = os.TempDir() 28 } 29 30 packerTempDir, err := ioutil.TempDir(s.TempPath, "packerhv") 31 if err != nil { 32 err := fmt.Errorf("Error creating temporary directory: %s", err) 33 state.Put("error", err) 34 ui.Error(err.Error()) 35 return multistep.ActionHalt 36 } 37 38 s.dirPath = packerTempDir 39 state.Put("packerTempDir", packerTempDir) 40 41 if s.VhdTempPath == "" { 42 // Fall back to regular temp dir if no separate VHD temp dir set. 43 state.Put("packerVhdTempDir", packerTempDir) 44 } else { 45 packerVhdTempDir, err := ioutil.TempDir(s.VhdTempPath, "packerhv-vhd") 46 if err != nil { 47 err := fmt.Errorf("Error creating temporary VHD directory: %s", err) 48 state.Put("error", err) 49 ui.Error(err.Error()) 50 return multistep.ActionHalt 51 } 52 53 s.vhdDirPath = packerVhdTempDir 54 state.Put("packerVhdTempDir", packerVhdTempDir) 55 } 56 57 // ui.Say("packerTempDir = '" + packerTempDir + "'") 58 59 return multistep.ActionContinue 60 } 61 62 func (s *StepCreateTempDir) Cleanup(state multistep.StateBag) { 63 ui := state.Get("ui").(packer.Ui) 64 65 if s.dirPath != "" { 66 ui.Say("Deleting temporary directory...") 67 68 err := os.RemoveAll(s.dirPath) 69 70 if err != nil { 71 ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err)) 72 } 73 } 74 75 if s.vhdDirPath != "" && s.dirPath != s.vhdDirPath { 76 ui.Say("Deleting temporary VHD directory...") 77 78 err := os.RemoveAll(s.vhdDirPath) 79 80 if err != nil { 81 ui.Error(fmt.Sprintf("Error deleting temporary VHD directory: %s", err)) 82 } 83 } 84 }