hashicorp/packer-plugin-sdk

Implement DownloadDir for chroot

Open

#89 opened on Nov 12, 2021

View on GitHub
 (1 comment) (2 reactions) (0 assignees)Go (60 forks)auto 404
enhancementgood first issuehelp wanted

Repository metrics

Stars
 (42 stars)
PR merge metrics
 (PR metrics pending)

Description

Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request. Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request. If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Description

At the moment, chroot simply fails with DownloadDir is not implemented for amazon-chroot, which is an artifact from the past. https://github.com/hashicorp/packer-plugin-sdk/blob/main/chroot/communicator.go#L126

The implementation can be easily done with reversing UploadDir defined above in the same file:

func (c *Communicator) DownloadDir(src string, dst string, exclude []string) error {
	// If src ends with a trailing "/", copy from "src/." so that
	// directory contents (including hidden files) are copied, but the
	// directory "src" is omitted.  BSD does this automatically when
	// the source contains a trailing slash, but linux does not.
	if src[len(src)-1] == '/' {
		src = src + "."
	}

	// TODO: remove any file copied if it appears in `exclude`
	chrootSrc := filepath.Join(c.Chroot, src)
	log.Printf("Downloading directory '%s' to '%s'", chrootSrc, dst)

	cmd, err := c.CmdWrapper(fmt.Sprintf("cp -R '%s' %s", chrootSrc, dst))
	if err != nil {
					return err
	}

	stderr := new(bytes.Buffer)
	shell := common.ShellCommand(cmd)
	shell.Env = append(shell.Env, "LANG=C")
	shell.Env = append(shell.Env, os.Environ()...)
	shell.Stderr = stderr
	if err := shell.Run(); err == nil {
					return err
	}

	if strings.Contains(stderr.String(), "No such file") {
					// This just means that the directory was empty. Just ignore it.
					return nil
	}

	return err
}

Contributor guide