github.com/rothwerx/packer@v0.9.0/builder/amazon/chroot/step_copy_files.go (about)

     1  package chroot
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     8  	"log"
     9  	"path/filepath"
    10  )
    11  
    12  // StepCopyFiles copies some files from the host into the chroot environment.
    13  //
    14  // Produces:
    15  //   copy_files_cleanup CleanupFunc - A function to clean up the copied files
    16  //   early.
    17  type StepCopyFiles struct {
    18  	files []string
    19  }
    20  
    21  func (s *StepCopyFiles) Run(state multistep.StateBag) multistep.StepAction {
    22  	config := state.Get("config").(*Config)
    23  	mountPath := state.Get("mount_path").(string)
    24  	ui := state.Get("ui").(packer.Ui)
    25  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
    26  	stderr := new(bytes.Buffer)
    27  
    28  	s.files = make([]string, 0, len(config.CopyFiles))
    29  	if len(config.CopyFiles) > 0 {
    30  		ui.Say("Copying files from host to chroot...")
    31  		for _, path := range config.CopyFiles {
    32  			ui.Message(path)
    33  			chrootPath := filepath.Join(mountPath, path)
    34  			log.Printf("Copying '%s' to '%s'", path, chrootPath)
    35  
    36  			cmdText, err := wrappedCommand(fmt.Sprintf("cp --remove-destination %s %s", path, chrootPath))
    37  			if err != nil {
    38  				err := fmt.Errorf("Error building copy command: %s", err)
    39  				state.Put("error", err)
    40  				ui.Error(err.Error())
    41  				return multistep.ActionHalt
    42  			}
    43  
    44  			stderr.Reset()
    45  			cmd := ShellCommand(cmdText)
    46  			cmd.Stderr = stderr
    47  			if err := cmd.Run(); err != nil {
    48  				err := fmt.Errorf(
    49  					"Error copying file: %s\nnStderr: %s", err, stderr.String())
    50  				state.Put("error", err)
    51  				ui.Error(err.Error())
    52  				return multistep.ActionHalt
    53  			}
    54  
    55  			s.files = append(s.files, chrootPath)
    56  		}
    57  	}
    58  
    59  	state.Put("copy_files_cleanup", s)
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *StepCopyFiles) Cleanup(state multistep.StateBag) {
    64  	ui := state.Get("ui").(packer.Ui)
    65  	if err := s.CleanupFunc(state); err != nil {
    66  		ui.Error(err.Error())
    67  	}
    68  }
    69  
    70  func (s *StepCopyFiles) CleanupFunc(state multistep.StateBag) error {
    71  	wrappedCommand := state.Get("wrappedCommand").(CommandWrapper)
    72  	if s.files != nil {
    73  		for _, file := range s.files {
    74  			log.Printf("Removing: %s", file)
    75  			localCmdText, err := wrappedCommand(fmt.Sprintf("rm -f %s", file))
    76  			if err != nil {
    77  				return err
    78  			}
    79  
    80  			localCmd := ShellCommand(localCmdText)
    81  			if err := localCmd.Run(); err != nil {
    82  				return err
    83  			}
    84  		}
    85  	}
    86  
    87  	s.files = nil
    88  	return nil
    89  }