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
}

贡献者指南