From a03807f8e88339ff213ad0772fa385aef6d141be Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Thu, 26 Sep 2013 00:58:25 -0700 Subject: [PATCH] wip --- builder/amazon/chroot/builder.go | 6 ++++++ builder/amazon/chroot/communicator.go | 12 +++++------ builder/amazon/chroot/copy_files.go | 21 +++++++++++++++++++ .../amazon/chroot/step_chroot_provision.go | 6 ++++-- builder/amazon/chroot/step_copy_files.go | 3 ++- 5 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 builder/amazon/chroot/copy_files.go diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 6647b769e..93685de72 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -31,6 +31,7 @@ type Config struct { DevicePath string `mapstructure:"device_path"` MountCommand string `mapstructure:"mount_command"` ChrootCommand string `mapstructure:"chroot_command"` + CopyCommand string `mapstructure:"copy_command"` MountPath string `mapstructure:"mount_path"` SourceAmi string `mapstructure:"source_ami"` UnmountCommand string `mapstructure:"unmount_command"` @@ -87,6 +88,10 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.ChrootCommand = "chroot" } + if b.config.CopyCommand == "" { + b.config.CopyCommand = "cp" + } + if b.config.MountPath == "" { b.config.MountPath = "packer-amazon-chroot-volumes/{{.Device}}" } @@ -135,6 +140,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { "device_path": &b.config.DevicePath, "mount_command": &b.config.MountCommand, "chroot_command": &b.config.ChrootCommand, + "copy_command": &b.config.CopyCommand, "source_ami": &b.config.SourceAmi, "unmount_command": &b.config.UnmountCommand, } diff --git a/builder/amazon/chroot/communicator.go b/builder/amazon/chroot/communicator.go index b81300563..2af07833c 100644 --- a/builder/amazon/chroot/communicator.go +++ b/builder/amazon/chroot/communicator.go @@ -15,6 +15,7 @@ import ( type Communicator struct { Chroot string ChrootCommand string + CopyCommand string } func (c *Communicator) Start(cmd *packer.RemoteCmd) error { @@ -71,13 +72,10 @@ func (c *Communicator) UploadDir(dst string, src string, exclude []string) error } dstPath := filepath.Join(dst, path) - f, err := os.Open(fullPath) - if err != nil { - return err - } - defer f.Close() - - return c.Upload(dstPath, f) + dst = filepath.Join(c.Chroot, dst) + log.Printf("Uploading to chroot dir: %s", dst) + return copySingle(dst, "", c.CopyCommand) + //return c.Upload(dstPath, f) } log.Printf("Uploading directory '%s' to '%s'", src, dst) diff --git a/builder/amazon/chroot/copy_files.go b/builder/amazon/chroot/copy_files.go new file mode 100644 index 000000000..8d75cb65d --- /dev/null +++ b/builder/amazon/chroot/copy_files.go @@ -0,0 +1,21 @@ +package chroot + +import ( + "log" + "os" + "os/exec" + "path/filepath" + "syscall" +) + +func copySingle(dst string, src string, copyCommand string) error { + cpCommand := fmt.Sprintf("sudo cp -fn %s %s", src, dest) + localcmd := exec.Command("/bin/sh", "-c", cpCommand) + log.Println(localcmd.Args) + out, err := localcmd.CombinedOutput() + if err != nil { + log.Println(err) + } + log.Printf("output: %s", out) + return nil +} diff --git a/builder/amazon/chroot/step_chroot_provision.go b/builder/amazon/chroot/step_chroot_provision.go index b1a156216..bb53e0603 100644 --- a/builder/amazon/chroot/step_chroot_provision.go +++ b/builder/amazon/chroot/step_chroot_provision.go @@ -15,12 +15,14 @@ func (s *StepChrootProvision) Run(state multistep.StateBag) multistep.StepAction hook := state.Get("hook").(packer.Hook) mountPath := state.Get("mount_path").(string) chrootCommand := state.Get("chroot_command").(string) + copyCommand := state.Get("copy_command").(string) ui := state.Get("ui").(packer.Ui) // Create our communicator comm := &Communicator{ - Chroot: mountPath, - ChrootCommand: chrootCommand, + Chroot: mountPath, + ChrootCommand: chrootCommand, + CopyCommand: copyCommand, } // Provision diff --git a/builder/amazon/chroot/step_copy_files.go b/builder/amazon/chroot/step_copy_files.go index d383103a4..efe411354 100644 --- a/builder/amazon/chroot/step_copy_files.go +++ b/builder/amazon/chroot/step_copy_files.go @@ -22,6 +22,7 @@ type StepCopyFiles struct { func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) mountPath := state.Get("mount_path").(string) + copyCmd := state.Get("copy_command").(string) ui := state.Get("ui").(packer.Ui) s.files = make([]string, 0, len(config.CopyFiles)) @@ -32,7 +33,7 @@ func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction { chrootPath := filepath.Join(mountPath, path) log.Printf("Copying '%s' to '%s'", path, chrootPath) - if err := s.copySingle(chrootPath, path); err != nil { + if err := copySingle(chrootPath, path, copyCmd); err != nil { err := fmt.Errorf("Error copying file: %s", err) state.Put("error", err) ui.Error(err.Error())