This commit is contained in:
Matthew Hooker 2013-09-26 00:58:25 -07:00
parent 73c5aec24d
commit a03807f8e8
5 changed files with 38 additions and 10 deletions

View File

@ -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,
}

View File

@ -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)

View File

@ -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
}

View File

@ -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

View File

@ -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())