github.com/Hashicorp/packer@v1.3.2/builder/parallels/common/step_upload_parallels_tools.go (about) 1 package common 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "os" 8 9 "github.com/hashicorp/packer/helper/multistep" 10 "github.com/hashicorp/packer/packer" 11 "github.com/hashicorp/packer/template/interpolate" 12 ) 13 14 // This step uploads the Parallels Tools ISO to the virtual machine. 15 // 16 // Uses: 17 // communicator packer.Communicator 18 // parallels_tools_path string 19 // ui packer.Ui 20 // 21 // Produces: 22 type toolsPathTemplate struct { 23 Flavor string 24 } 25 26 // StepUploadParallelsTools is a step that uploads the Parallels Tools ISO 27 // to the VM. 28 // 29 // Uses: 30 // communicator packer.Communicator 31 // parallels_tools_path string 32 // ui packer.Ui 33 type StepUploadParallelsTools struct { 34 ParallelsToolsFlavor string 35 ParallelsToolsGuestPath string 36 ParallelsToolsMode string 37 Ctx interpolate.Context 38 } 39 40 // Run uploads the Parallels Tools ISO to the VM. 41 func (s *StepUploadParallelsTools) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 42 comm := state.Get("communicator").(packer.Communicator) 43 ui := state.Get("ui").(packer.Ui) 44 45 // If we're attaching then don't do this, since we attached. 46 if s.ParallelsToolsMode != ParallelsToolsModeUpload { 47 log.Println("Not uploading Parallels Tools since mode is not upload") 48 return multistep.ActionContinue 49 } 50 51 // Get the Parallels Tools path on the host machine 52 parallelsToolsPath := state.Get("parallels_tools_path").(string) 53 54 f, err := os.Open(parallelsToolsPath) 55 if err != nil { 56 state.Put("error", fmt.Errorf("Error opening Parallels Tools ISO: %s", err)) 57 return multistep.ActionHalt 58 } 59 defer f.Close() 60 61 s.Ctx.Data = &toolsPathTemplate{ 62 Flavor: s.ParallelsToolsFlavor, 63 } 64 65 s.ParallelsToolsGuestPath, err = interpolate.Render(s.ParallelsToolsGuestPath, &s.Ctx) 66 if err != nil { 67 err = fmt.Errorf("Error preparing Parallels Tools path: %s", err) 68 state.Put("error", err) 69 ui.Error(err.Error()) 70 return multistep.ActionHalt 71 } 72 73 ui.Say(fmt.Sprintf("Uploading Parallels Tools for '%s' to path: '%s'", 74 s.ParallelsToolsFlavor, s.ParallelsToolsGuestPath)) 75 if err := comm.Upload(s.ParallelsToolsGuestPath, f, nil); err != nil { 76 err = fmt.Errorf("Error uploading Parallels Tools: %s", err) 77 state.Put("error", err) 78 ui.Error(err.Error()) 79 return multistep.ActionHalt 80 } 81 82 return multistep.ActionContinue 83 } 84 85 // Cleanup does nothing. 86 func (s *StepUploadParallelsTools) Cleanup(state multistep.StateBag) {}