hashicorp/packer-plugin-sdk

Implement DownloadDir for chroot

開放

#89 建立於 2021年11月12日

 (1 則留言) (2 個反應) (0 位負責人)Go (60 個分叉)auto 404
enhancementgood first issuehelp wanted

倉庫指標

星標
 (42 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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
}

貢獻者指南