github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/chroot/step_copy_files.go (about)

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