2013-07-30 18:30:53 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2013-09-27 21:10:33 -04:00
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-30 18:30:53 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
2020-08-02 00:09:43 -04:00
|
|
|
"runtime"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-30 18:30:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2019-05-31 14:55:58 -04:00
|
|
|
Files []string
|
2013-07-30 19:41:29 -04:00
|
|
|
files []string
|
2013-07-30 18:30:53 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
mountPath := state.Get("mount_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-09-27 21:10:33 -04:00
|
|
|
stderr := new(bytes.Buffer)
|
2013-07-30 18:30:53 -04:00
|
|
|
|
2019-05-31 14:55:58 -04:00
|
|
|
s.files = make([]string, 0, len(s.Files))
|
|
|
|
if len(s.Files) > 0 {
|
2013-07-30 18:30:53 -04:00
|
|
|
ui.Say("Copying files from host to chroot...")
|
2020-08-02 00:09:43 -04:00
|
|
|
var removeDestinationOption string
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "freebsd":
|
|
|
|
// The -f option here is closer to GNU --remove-destination than
|
|
|
|
// what POSIX says -f should do.
|
|
|
|
removeDestinationOption = "-f"
|
|
|
|
default:
|
|
|
|
// This is the GNU binutils version.
|
|
|
|
removeDestinationOption = "--remove-destination"
|
|
|
|
}
|
2019-05-31 14:55:58 -04:00
|
|
|
for _, path := range s.Files {
|
2013-07-30 18:30:53 -04:00
|
|
|
ui.Message(path)
|
|
|
|
chrootPath := filepath.Join(mountPath, path)
|
|
|
|
log.Printf("Copying '%s' to '%s'", path, chrootPath)
|
|
|
|
|
2020-08-02 00:09:43 -04:00
|
|
|
cmdText, err := wrappedCommand(fmt.Sprintf("cp %s %s %s", removeDestinationOption, path, chrootPath))
|
2013-09-30 12:33:57 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error building copy command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-09-27 21:10:33 -04:00
|
|
|
stderr.Reset()
|
2019-10-23 23:57:17 -04:00
|
|
|
cmd := common.ShellCommand(cmdText)
|
2013-09-27 21:10:33 -04:00
|
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"Error copying file: %s\nnStderr: %s", err, stderr.String())
|
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-09-27 06:54:53 -04:00
|
|
|
func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error {
|
2019-10-23 23:57:17 -04:00
|
|
|
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
|
2013-07-30 19:41:29 -04:00
|
|
|
if s.files != nil {
|
|
|
|
for _, file := range s.files {
|
|
|
|
log.Printf("Removing: %s", file)
|
2013-09-30 12:33:57 -04:00
|
|
|
localCmdText, err := wrappedCommand(fmt.Sprintf("rm -f %s", file))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:57:17 -04:00
|
|
|
localCmd := common.ShellCommand(localCmdText)
|
2013-09-26 21:34:01 -04:00
|
|
|
if err := localCmd.Run(); err != nil {
|
2013-07-30 19:41:29 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.files = nil
|
|
|
|
return nil
|
|
|
|
}
|