packer-cn/common/chroot/step_copy_files.go

104 lines
2.6 KiB
Go
Raw Normal View History

package chroot
import (
2013-09-27 21:10:33 -04:00
"bytes"
"context"
"fmt"
"log"
"path/filepath"
2020-08-02 00:09:43 -04:00
"runtime"
2018-01-22 20:21:10 -05:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
2018-01-22 20:21:10 -05:00
"github.com/hashicorp/packer/packer"
)
// StepCopyFiles copies some files from the host into the chroot environment.
//
// Produces:
// copy_files_cleanup CleanupFunc - A function to clean up the copied files
// early.
type StepCopyFiles struct {
Files []string
files []string
}
func (s *StepCopyFiles) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
mountPath := state.Get("mount_path").(string)
ui := state.Get("ui").(packer.Ui)
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
2013-09-27 21:10:33 -04:00
stderr := new(bytes.Buffer)
s.files = make([]string, 0, len(s.Files))
if len(s.Files) > 0 {
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"
}
for _, path := range s.Files {
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))
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()
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())
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.files = append(s.files, chrootPath)
}
}
state.Put("copy_files_cleanup", s)
return multistep.ActionContinue
}
func (s *StepCopyFiles) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
if err := s.CleanupFunc(state); err != nil {
ui.Error(err.Error())
}
}
func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error {
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
if s.files != nil {
for _, file := range s.files {
log.Printf("Removing: %s", file)
localCmdText, err := wrappedCommand(fmt.Sprintf("rm -f %s", file))
if err != nil {
return err
}
localCmd := common.ShellCommand(localCmdText)
2013-09-26 21:34:01 -04:00
if err := localCmd.Run(); err != nil {
return err
}
}
}
s.files = nil
return nil
}