2013-07-30 18:30:53 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepCopyFiles copies some files from the host into the chroot environment.
|
2013-07-30 19:41:29 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// copy_files_cleanup CleanupFunc - A function to clean up the copied files
|
|
|
|
// early.
|
2013-07-30 18:30:53 -04:00
|
|
|
type StepCopyFiles struct {
|
2013-07-30 19:41:29 -04:00
|
|
|
files []string
|
2013-07-30 18:30:53 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
mountPath := state.Get("mount_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 18:30:53 -04:00
|
|
|
|
2013-07-30 19:45:49 -04:00
|
|
|
s.files = make([]string, 0, len(config.CopyFiles))
|
2013-07-30 18:30:53 -04:00
|
|
|
if len(config.CopyFiles) > 0 {
|
|
|
|
ui.Say("Copying files from host to chroot...")
|
|
|
|
for _, path := range config.CopyFiles {
|
|
|
|
ui.Message(path)
|
|
|
|
chrootPath := filepath.Join(mountPath, path)
|
|
|
|
log.Printf("Copying '%s' to '%s'", path, chrootPath)
|
|
|
|
|
2013-09-26 21:28:06 -04:00
|
|
|
if err := copySingle(chrootPath, path, config.CopyCommand); err != nil {
|
2013-07-30 18:30:53 -04:00
|
|
|
err := fmt.Errorf("Error copying file: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 18:30:53 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-30 19:41:29 -04:00
|
|
|
|
|
|
|
s.files = append(s.files, chrootPath)
|
2013-07-30 18:30:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("copy_files_cleanup", s)
|
2013-07-30 18:30:53 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCopyFiles) Cleanup(state multistep.StateBag) {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 19:41:29 -04:00
|
|
|
if err := s.CleanupFunc(state); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCopyFiles) CleanupFunc(multistep.StateBag) error {
|
2013-07-30 19:41:29 -04:00
|
|
|
if s.files != nil {
|
|
|
|
for _, file := range s.files {
|
|
|
|
log.Printf("Removing: %s", file)
|
2013-09-26 21:28:06 -04:00
|
|
|
if err := copySingle(file, "", "rm"); err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.files = nil
|
|
|
|
return nil
|
|
|
|
}
|