diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index d1d3f37e4..ce5fa9d73 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -356,7 +356,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack state.Put("awsSession", session) state.Put("hook", hook) state.Put("ui", ui) - state.Put("wrappedCommand", CommandWrapper(wrappedCommand)) + state.Put("wrappedCommand", common.CommandWrapper(wrappedCommand)) // Build the steps steps := []multistep.Step{ diff --git a/builder/amazon/chroot/communicator.go b/builder/amazon/chroot/communicator.go index 400c12b53..a813baca8 100644 --- a/builder/amazon/chroot/communicator.go +++ b/builder/amazon/chroot/communicator.go @@ -13,6 +13,7 @@ import ( "strings" "syscall" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer/tmp" ) @@ -21,7 +22,7 @@ import ( // commands locally but within a chroot. type Communicator struct { Chroot string - CmdWrapper CommandWrapper + CmdWrapper common.CommandWrapper } func (c *Communicator) Start(ctx context.Context, cmd *packer.RemoteCmd) error { @@ -33,7 +34,7 @@ func (c *Communicator) Start(ctx context.Context, cmd *packer.RemoteCmd) error { return err } - localCmd := ShellCommand(command) + localCmd := common.ShellCommand(command) localCmd.Stdin = cmd.Stdin localCmd.Stdout = cmd.Stdout localCmd.Stderr = cmd.Stderr @@ -83,7 +84,7 @@ func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error { return err } - return ShellCommand(cpCmd).Run() + return common.ShellCommand(cpCmd).Run() } func (c *Communicator) UploadDir(dst string, src string, exclude []string) error { @@ -105,7 +106,7 @@ func (c *Communicator) UploadDir(dst string, src string, exclude []string) error } var stderr bytes.Buffer - cmd := ShellCommand(cpCmd) + cmd := common.ShellCommand(cpCmd) cmd.Env = append(cmd.Env, "LANG=C") cmd.Env = append(cmd.Env, os.Environ()...) cmd.Stderr = &stderr diff --git a/builder/amazon/chroot/copy_files_test.go b/builder/amazon/chroot/copy_files_test.go index d84ee080f..409bbfb9b 100644 --- a/builder/amazon/chroot/copy_files_test.go +++ b/builder/amazon/chroot/copy_files_test.go @@ -6,6 +6,8 @@ import ( "os" "runtime" "testing" + + "github.com/hashicorp/packer/common" ) func TestCopyFile(t *testing.T) { @@ -26,7 +28,7 @@ func TestCopyFile(t *testing.T) { } first.Sync() - cmd := ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName)) + cmd := common.ShellCommand(fmt.Sprintf("cp %s %s", first.Name(), newName)) if err := cmd.Run(); err != nil { t.Fatalf("Couldn't copy file") } diff --git a/builder/amazon/chroot/run_local_commands.go b/builder/amazon/chroot/run_local_commands.go index 9a93f8791..8141d09e5 100644 --- a/builder/amazon/chroot/run_local_commands.go +++ b/builder/amazon/chroot/run_local_commands.go @@ -4,12 +4,13 @@ import ( "context" "fmt" + "github.com/hashicorp/packer/common" sl "github.com/hashicorp/packer/common/shell-local" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" ) -func RunLocalCommands(commands []string, wrappedCommand CommandWrapper, ictx interpolate.Context, ui packer.Ui) error { +func RunLocalCommands(commands []string, wrappedCommand common.CommandWrapper, ictx interpolate.Context, ui packer.Ui) error { ctx := context.TODO() for _, rawCmd := range commands { intCmd, err := interpolate.Render(rawCmd, &ictx) diff --git a/builder/amazon/chroot/step_chroot_provision.go b/builder/amazon/chroot/step_chroot_provision.go index 4545105b0..a90c79767 100644 --- a/builder/amazon/chroot/step_chroot_provision.go +++ b/builder/amazon/chroot/step_chroot_provision.go @@ -4,6 +4,7 @@ import ( "context" "log" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -16,7 +17,7 @@ func (s *StepChrootProvision) Run(ctx context.Context, state multistep.StateBag) hook := state.Get("hook").(packer.Hook) mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) // Create our communicator comm := &Communicator{ diff --git a/builder/amazon/chroot/step_copy_files.go b/builder/amazon/chroot/step_copy_files.go index af5766386..496578bfc 100644 --- a/builder/amazon/chroot/step_copy_files.go +++ b/builder/amazon/chroot/step_copy_files.go @@ -7,6 +7,7 @@ import ( "log" "path/filepath" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -24,7 +25,7 @@ type StepCopyFiles struct { func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) stderr := new(bytes.Buffer) s.files = make([]string, 0, len(s.Files)) @@ -44,7 +45,7 @@ func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multi } stderr.Reset() - cmd := ShellCommand(cmdText) + cmd := common.ShellCommand(cmdText) cmd.Stderr = stderr if err := cmd.Run(); err != nil { err := fmt.Errorf( @@ -70,7 +71,7 @@ func (s *StepCopyFiles) Cleanup(state multistep.StateBag) { } func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error { - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) if s.files != nil { for _, file := range s.files { log.Printf("Removing: %s", file) @@ -79,7 +80,7 @@ func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error { return err } - localCmd := ShellCommand(localCmdText) + localCmd := common.ShellCommand(localCmdText) if err := localCmd.Run(); err != nil { return err } diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go index dd9a66873..052a2d90a 100644 --- a/builder/amazon/chroot/step_mount_device.go +++ b/builder/amazon/chroot/step_mount_device.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" @@ -39,7 +40,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul // customizable device path for mounting NVME block devices on c5 and m5 HVM device = config.NVMEDevicePath } - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) var virtualizationType string if config.FromScratch || config.AMIVirtType != "" { @@ -104,7 +105,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul return multistep.ActionHalt } log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand) - cmd := ShellCommand(mountCommand) + cmd := common.ShellCommand(mountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { err := fmt.Errorf( @@ -135,7 +136,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error { } ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) ui.Say("Unmounting the root device...") unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath)) @@ -143,7 +144,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error { return fmt.Errorf("Error creating unmount command: %s", err) } - cmd := ShellCommand(unmountCommand) + cmd := common.ShellCommand(unmountCommand) if err := cmd.Run(); err != nil { return fmt.Errorf("Error unmounting root device: %s", err) } diff --git a/builder/amazon/chroot/step_mount_extra.go b/builder/amazon/chroot/step_mount_extra.go index e1b03abbb..a8a9f3ffb 100644 --- a/builder/amazon/chroot/step_mount_extra.go +++ b/builder/amazon/chroot/step_mount_extra.go @@ -8,6 +8,7 @@ import ( "os/exec" "syscall" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -24,7 +25,7 @@ type StepMountExtra struct { func (s *StepMountExtra) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) s.mounts = make([]string, 0, len(s.ChrootMounts)) @@ -58,7 +59,7 @@ func (s *StepMountExtra) Run(ctx context.Context, state multistep.StateBag) mult return multistep.ActionHalt } - cmd := ShellCommand(mountCommand) + cmd := common.ShellCommand(mountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { err := fmt.Errorf( @@ -89,7 +90,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error { return nil } - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) for len(s.mounts) > 0 { var path string lastIndex := len(s.mounts) - 1 @@ -103,7 +104,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error { // Before attempting to unmount, // check to see if path is already unmounted stderr := new(bytes.Buffer) - cmd := ShellCommand(grepCommand) + cmd := common.ShellCommand(grepCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { if exitError, ok := err.(*exec.ExitError); ok { @@ -124,7 +125,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error { } stderr = new(bytes.Buffer) - cmd = ShellCommand(unmountCommand) + cmd = common.ShellCommand(unmountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { return fmt.Errorf( diff --git a/builder/amazon/chroot/step_post_mount_commands.go b/builder/amazon/chroot/step_post_mount_commands.go index c152d409f..77f0cfc72 100644 --- a/builder/amazon/chroot/step_post_mount_commands.go +++ b/builder/amazon/chroot/step_post_mount_commands.go @@ -3,6 +3,7 @@ package chroot import ( "context" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -23,7 +24,7 @@ func (s *StepPostMountCommands) Run(ctx context.Context, state multistep.StateBa device := state.Get("device").(string) mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) if len(s.Commands) == 0 { return multistep.ActionContinue diff --git a/builder/amazon/chroot/step_pre_mount_commands.go b/builder/amazon/chroot/step_pre_mount_commands.go index 635ee84d0..8ff829917 100644 --- a/builder/amazon/chroot/step_pre_mount_commands.go +++ b/builder/amazon/chroot/step_pre_mount_commands.go @@ -3,6 +3,7 @@ package chroot import ( "context" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -20,7 +21,7 @@ func (s *StepPreMountCommands) Run(ctx context.Context, state multistep.StateBag config := state.Get("config").(interpolateContextProvider) device := state.Get("device").(string) ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) if len(s.Commands) == 0 { return multistep.ActionContinue diff --git a/builder/azure/chroot/builder.go b/builder/azure/chroot/builder.go index ee2dbe08f..726610afc 100644 --- a/builder/azure/chroot/builder.go +++ b/builder/azure/chroot/builder.go @@ -317,7 +317,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack state.Put("hook", hook) state.Put("ui", ui) state.Put("azureclient", azcli) - state.Put("wrappedCommand", chroot.CommandWrapper(wrappedCommand)) + state.Put("wrappedCommand", common.CommandWrapper(wrappedCommand)) info, err := azcli.MetadataClient().GetComputeInfo() if err != nil { diff --git a/builder/azure/chroot/step_mount_device.go b/builder/azure/chroot/step_mount_device.go index 7c197fbea..e4fc02595 100644 --- a/builder/azure/chroot/step_mount_device.go +++ b/builder/azure/chroot/step_mount_device.go @@ -6,14 +6,15 @@ import ( "bytes" "context" "fmt" - "github.com/hashicorp/packer/builder/amazon/chroot" - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/template/interpolate" "log" "os" "path/filepath" "strings" + + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" ) var _ multistep.Step = &StepMountDevice{} @@ -30,7 +31,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul ui := state.Get("ui").(packer.Ui) device := state.Get("device").(string) config := state.Get("config").(*Config) - wrappedCommand := state.Get("wrappedCommand").(chroot.CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) ictx := config.ctx @@ -83,7 +84,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul return multistep.ActionHalt } log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand) - cmd := chroot.ShellCommand(mountCommand) + cmd := common.ShellCommand(mountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { err := fmt.Errorf( @@ -114,7 +115,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error { } ui := state.Get("ui").(packer.Ui) - wrappedCommand := state.Get("wrappedCommand").(chroot.CommandWrapper) + wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper) ui.Say("Unmounting the root device...") unmountCommand, err := wrappedCommand(fmt.Sprintf("umount %s", s.mountPath)) @@ -122,7 +123,7 @@ func (s *StepMountDevice) CleanupFunc(state multistep.StateBag) error { return fmt.Errorf("error creating unmount command: %s", err) } - cmd := chroot.ShellCommand(unmountCommand) + cmd := common.ShellCommand(unmountCommand) if err := cmd.Run(); err != nil { return fmt.Errorf("error unmounting root device: %s", err) } diff --git a/builder/amazon/chroot/command.go b/common/command.go similarity index 96% rename from builder/amazon/chroot/command.go rename to common/command.go index 0ca55be67..eaceb75a2 100644 --- a/builder/amazon/chroot/command.go +++ b/common/command.go @@ -1,4 +1,4 @@ -package chroot +package common import ( "os/exec"