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

View File

@ -31,6 +31,7 @@ type Config struct {
DevicePath string `mapstructure:"device_path"` DevicePath string `mapstructure:"device_path"`
MountCommand string `mapstructure:"mount_command"` MountCommand string `mapstructure:"mount_command"`
ChrootCommand string `mapstructure:"chroot_command"` ChrootCommand string `mapstructure:"chroot_command"`
CopyCommand string `mapstructure:"copy_command"`
MountPath string `mapstructure:"mount_path"` MountPath string `mapstructure:"mount_path"`
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
UnmountCommand string `mapstructure:"unmount_command"` UnmountCommand string `mapstructure:"unmount_command"`
@ -87,6 +88,10 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.ChrootCommand = "chroot" b.config.ChrootCommand = "chroot"
} }
if b.config.CopyCommand == "" {
b.config.CopyCommand = "cp"
}
if b.config.MountPath == "" { if b.config.MountPath == "" {
b.config.MountPath = "packer-amazon-chroot-volumes/{{.Device}}" b.config.MountPath = "packer-amazon-chroot-volumes/{{.Device}}"
} }
@ -135,6 +140,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
"device_path": &b.config.DevicePath, "device_path": &b.config.DevicePath,
"mount_command": &b.config.MountCommand, "mount_command": &b.config.MountCommand,
"chroot_command": &b.config.ChrootCommand, "chroot_command": &b.config.ChrootCommand,
"copy_command": &b.config.CopyCommand,
"source_ami": &b.config.SourceAmi, "source_ami": &b.config.SourceAmi,
"unmount_command": &b.config.UnmountCommand, "unmount_command": &b.config.UnmountCommand,
} }

View File

@ -15,6 +15,7 @@ import (
type Communicator struct { type Communicator struct {
Chroot string Chroot string
ChrootCommand string ChrootCommand string
CopyCommand string
} }
func (c *Communicator) Start(cmd *packer.RemoteCmd) error { 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) dstPath := filepath.Join(dst, path)
f, err := os.Open(fullPath) dst = filepath.Join(c.Chroot, dst)
if err != nil { log.Printf("Uploading to chroot dir: %s", dst)
return err return copySingle(dst, "", c.CopyCommand)
} //return c.Upload(dstPath, f)
defer f.Close()
return c.Upload(dstPath, f)
} }
log.Printf("Uploading directory '%s' to '%s'", src, dst) 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) hook := state.Get("hook").(packer.Hook)
mountPath := state.Get("mount_path").(string) mountPath := state.Get("mount_path").(string)
chrootCommand := state.Get("chroot_command").(string) chrootCommand := state.Get("chroot_command").(string)
copyCommand := state.Get("copy_command").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Create our communicator // Create our communicator
comm := &Communicator{ comm := &Communicator{
Chroot: mountPath, Chroot: mountPath,
ChrootCommand: chrootCommand, ChrootCommand: chrootCommand,
CopyCommand: copyCommand,
} }
// Provision // Provision

View File

@ -22,6 +22,7 @@ type StepCopyFiles struct {
func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction { func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
mountPath := state.Get("mount_path").(string) mountPath := state.Get("mount_path").(string)
copyCmd := state.Get("copy_command").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
s.files = make([]string, 0, len(config.CopyFiles)) 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) chrootPath := filepath.Join(mountPath, path)
log.Printf("Copying '%s' to '%s'", path, chrootPath) 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) err := fmt.Errorf("Error copying file: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())