From 88573588309d33e3f7bb54e60ab97c1c5bc3bba8 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 27 Sep 2013 20:47:44 +0000 Subject: [PATCH] building but there's an exec error. --- builder/amazon/chroot/builder.go | 12 +++++++++++ builder/amazon/chroot/communicator.go | 2 +- builder/amazon/chroot/copy_files.go | 2 +- builder/amazon/chroot/copy_files_test.go | 1 + .../amazon/chroot/step_chroot_provision.go | 21 +++---------------- builder/amazon/chroot/step_mount_device.go | 8 ++++++- builder/amazon/chroot/step_mount_extra.go | 2 +- 7 files changed, 26 insertions(+), 22 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 165e29078..a7f46a5f4 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -13,6 +13,7 @@ import ( "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" "log" + "os/exec" "runtime" ) @@ -160,12 +161,23 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ec2conn := ec2.New(auth, region) + wrappedCommand := func(command string) *exec.Cmd { + wrapped, err := b.config.tpl.Process(b.config.CommandWrapper, &WrappedCommandTemplate{ + Command: command, + }) + if err != nil { + ui.Error(err.Error()) + } + return ShellCommand(wrapped) + } + // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", &b.config) state.Put("ec2", ec2conn) state.Put("hook", hook) state.Put("ui", ui) + state.Put("wrappedCommand", Command(wrappedCommand)) // Build the steps steps := []multistep.Step{ diff --git a/builder/amazon/chroot/communicator.go b/builder/amazon/chroot/communicator.go index 2902e5708..7f52d9fdb 100644 --- a/builder/amazon/chroot/communicator.go +++ b/builder/amazon/chroot/communicator.go @@ -21,7 +21,7 @@ type Command func(string) *exec.Cmd type Communicator struct { Chroot string ChrootCmd Command - wrappedCommand Command + WrappedCommand Command } func (c *Communicator) Start(cmd *packer.RemoteCmd) error { diff --git a/builder/amazon/chroot/copy_files.go b/builder/amazon/chroot/copy_files.go index 055a7d403..5af58baa5 100644 --- a/builder/amazon/chroot/copy_files.go +++ b/builder/amazon/chroot/copy_files.go @@ -13,6 +13,6 @@ func ChrootCommand(chroot string, command string) *exec.Cmd { func ShellCommand(command string) *exec.Cmd { cmd := exec.Command("/bin/sh", "-c", command) - log.Printf("WrappedCommand(%s) -> #%v", command, cmd.Args) + log.Printf("ShellCommand(%s) -> #%v", command, cmd.Args) return cmd } diff --git a/builder/amazon/chroot/copy_files_test.go b/builder/amazon/chroot/copy_files_test.go index 2dccbf0bc..c7e00018e 100644 --- a/builder/amazon/chroot/copy_files_test.go +++ b/builder/amazon/chroot/copy_files_test.go @@ -1,6 +1,7 @@ package chroot import ( + "fmt" "io/ioutil" "os" "testing" diff --git a/builder/amazon/chroot/step_chroot_provision.go b/builder/amazon/chroot/step_chroot_provision.go index 59a95f45c..481f58671 100644 --- a/builder/amazon/chroot/step_chroot_provision.go +++ b/builder/amazon/chroot/step_chroot_provision.go @@ -4,7 +4,6 @@ import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" "log" - "os/exec" ) // StepChrootProvision provisions the instance within a chroot. @@ -19,29 +18,15 @@ type WrappedCommandTemplate struct { func (s *StepChrootProvision) Run(state multistep.StateBag) multistep.StepAction { hook := state.Get("hook").(packer.Hook) mountPath := state.Get("mount_path").(string) - config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) - chrootCmd := func(command string) *exec.Cmd { - return ChrootCommand(mountPath, command) - } - wrappedCommand := func(command string) *exec.Cmd { - wrapped, err := config.tpl.Process(config.CommandWrapper, &WrappedCommandTemplate{ - Command: command, - }) - if err != nil { - ui.Error(err.Error()) - } - return ShellCommand(wrapped) - } - - state.Put("chrootCmd", chrootCmd) - state.Put("wrappedCommand", wrappedCommand) + wrappedCommand := state.Get("wrappedCommand").(Command) + chrootCmd := state.Get("chrootCmd").(Command) // Create our communicator comm := &Communicator{ Chroot: mountPath, ChrootCmd: chrootCmd, - wrappedCommand: wrappedCommand, + WrappedCommand: wrappedCommand, } // Provision diff --git a/builder/amazon/chroot/step_mount_device.go b/builder/amazon/chroot/step_mount_device.go index 4cce60f10..e8a987d1d 100644 --- a/builder/amazon/chroot/step_mount_device.go +++ b/builder/amazon/chroot/step_mount_device.go @@ -7,6 +7,7 @@ import ( "github.com/mitchellh/packer/packer" "log" "os" + "os/exec" "path/filepath" ) @@ -56,10 +57,15 @@ func (s *StepMountDevice) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } + chrootCmd := func(command string) *exec.Cmd { + return ChrootCommand(mountPath, command) + } + state.Put("chrootCmd", Command(chrootCmd)) + ui.Say("Mounting the root device...") stderr := new(bytes.Buffer) mountCommand := fmt.Sprintf("mount %s %s", device, mountPath) - wrappedCommand := state.Get("wrappedCommand").(*Command) + wrappedCommand := state.Get("wrappedCommand").(Command) cmd := wrappedCommand(mountCommand) cmd.Stderr = stderr if err := cmd.Run(); err != nil { diff --git a/builder/amazon/chroot/step_mount_extra.go b/builder/amazon/chroot/step_mount_extra.go index bbe958ec8..a5216e055 100644 --- a/builder/amazon/chroot/step_mount_extra.go +++ b/builder/amazon/chroot/step_mount_extra.go @@ -83,7 +83,7 @@ func (s *StepMountExtra) CleanupFunc(state multistep.StateBag) error { var path string lastIndex := len(s.mounts) - 1 path, s.mounts = s.mounts[lastIndex], s.mounts[:lastIndex] - unmountCommand := fmt.Sprintf("unmount %s", path) + unmountCommand := fmt.Sprintf("umount %s", path) stderr := new(bytes.Buffer) cmd := wrappedCommand(unmountCommand)