~angdraug/packer-provisioner-apt

00962e7c0cf232f2d47038b526c4ae6cf13c9786 — Dmitry Borodaenko 4 years ago 586b45a
Wait until target has domain name resolution

If apt-get -y runs before the target has finished initializing its
network it's going to fail to fetch some packages, but will report
success anyway. Run resolvectl query in a loop until it can resolve
deb.debian.org to reduce the likelihood of generating an image with
missing packages.
1 files changed, 12 insertions(+), 2 deletions(-)

M provisioner/provisioner.go
M provisioner/provisioner.go => provisioner/provisioner.go +12 -2
@@ 33,6 33,16 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
		return err
	}

	cmd := &packer.RemoteCmd{
		Command: "/bin/sh -c 'for i in $(seq 100); do " +
			"resolvectl query deb.debian.org >/dev/null && break; sleep 0.1; done; " +
			"resolvectl query deb.debian.org'",
	}
	if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
		ui.Error("Failed waiting for domain name resolution")
		return err
	}

	for _, key := range p.config.Keys {
		f, err := os.Open(key)
		if err != nil {


@@ 45,7 55,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
			return err
		}

		err = comm.Upload("/etc/apt/trusted.gpg.d/" + filepath.Base(key), f, &fi)
		err = comm.Upload("/etc/apt/trusted.gpg.d/"+filepath.Base(key), f, &fi)
		if err != nil {
			ui.Error(fmt.Sprintf("Failed to upload APT key %s", key))
			return err


@@ 66,7 76,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
		}
	}

	cmd := &packer.RemoteCmd{
	cmd = &packer.RemoteCmd{
		Command: fmt.Sprintf(
			"DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get install -y --no-install-recommends %s",
			strings.Join(p.config.Packages, " "),