From 3d371a2d5dcda5144f018da35157090584d737c6 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 2 Jul 2020 11:07:59 +0200 Subject: [PATCH] Add complete HCL2 examples + allow to name a singular build.source blocks (#9490) * in the examples/hcl folder * add possibility to name singular build.source blocks to differentiate their output and to filter on them --- CHANGELOG.md | 2 + .../test-fixtures/build-only/template.pkr.hcl | 4 +- examples/_common/minimize.sh | 36 +++++++ examples/_common/parallels.sh | 35 +++++++ examples/_common/sshd.sh | 20 ++++ examples/_common/vagrant.sh | 19 ++++ examples/_common/virtualbox.sh | 19 ++++ examples/_common/vmware.sh | 34 +++++++ examples/hcl/ubuntu/README.md | 6 ++ examples/hcl/ubuntu/build.pkr.hcl | 99 +++++++++++++++++++ examples/hcl/ubuntu/etc/http/preseed.cfg | 35 +++++++ .../ubuntu/etc/http/preseed_hardcoded_ip.cfg | 68 +++++++++++++ examples/hcl/ubuntu/etc/scripts/010-update.sh | 40 ++++++++ examples/hcl/ubuntu/etc/scripts/020-sshd.sh | 1 + .../hcl/ubuntu/etc/scripts/030-networking.sh | 25 +++++ .../hcl/ubuntu/etc/scripts/040-sudoers.sh | 7 ++ .../hcl/ubuntu/etc/scripts/050-vagrant.sh | 1 + .../hcl/ubuntu/etc/scripts/060-virtualbox.sh | 1 + examples/hcl/ubuntu/etc/scripts/070-vmware.sh | 10 ++ .../hcl/ubuntu/etc/scripts/080-parallels.sh | 1 + examples/hcl/ubuntu/etc/scripts/090-hyperv.sh | 12 +++ .../hcl/ubuntu/etc/scripts/100-cleanup.sh | 81 +++++++++++++++ .../hcl/ubuntu/etc/scripts/110-minimize.sh | 1 + .../hcl/ubuntu/source.parallels-iso.pkr.hcl | 13 +++ examples/hcl/ubuntu/source.qemu.pkr.hcl | 16 +++ .../hcl/ubuntu/source.virtualbox-iso.pkr.hcl | 14 +++ examples/hcl/ubuntu/source.vmware-iso.pkr.hcl | 17 ++++ .../hcl/ubuntu/source.vsphere-iso.pkr.hcl | 76 ++++++++++++++ examples/hcl/ubuntu/variables.16.04.pkr.hcl | 34 +++++++ examples/hcl/ubuntu/variables.18.04.pkr.hcl | 33 +++++++ examples/hcl/ubuntu/variables.common.pkr.hcl | 23 +++++ .../build/post-processor_onlyexcept.pkr.hcl | 10 +- .../build/provisioner_onlyexcept.pkr.hcl | 10 +- hcl2template/types.build_test.go | 70 +++++++++++-- hcl2template/types.packer_config.go | 7 +- hcl2template/types.source.go | 29 +++++- hcl2template/types.variables_test.go | 4 +- .../docs/from-1.5/blocks/build/source.mdx | 8 +- website/pages/docs/from-1.5/onlyexcept.mdx | 13 ++- 39 files changed, 912 insertions(+), 22 deletions(-) create mode 100644 examples/_common/minimize.sh create mode 100644 examples/_common/parallels.sh create mode 100644 examples/_common/sshd.sh create mode 100644 examples/_common/vagrant.sh create mode 100644 examples/_common/virtualbox.sh create mode 100644 examples/_common/vmware.sh create mode 100644 examples/hcl/ubuntu/README.md create mode 100644 examples/hcl/ubuntu/build.pkr.hcl create mode 100644 examples/hcl/ubuntu/etc/http/preseed.cfg create mode 100644 examples/hcl/ubuntu/etc/http/preseed_hardcoded_ip.cfg create mode 100644 examples/hcl/ubuntu/etc/scripts/010-update.sh create mode 120000 examples/hcl/ubuntu/etc/scripts/020-sshd.sh create mode 100644 examples/hcl/ubuntu/etc/scripts/030-networking.sh create mode 100644 examples/hcl/ubuntu/etc/scripts/040-sudoers.sh create mode 120000 examples/hcl/ubuntu/etc/scripts/050-vagrant.sh create mode 120000 examples/hcl/ubuntu/etc/scripts/060-virtualbox.sh create mode 100644 examples/hcl/ubuntu/etc/scripts/070-vmware.sh create mode 120000 examples/hcl/ubuntu/etc/scripts/080-parallels.sh create mode 100644 examples/hcl/ubuntu/etc/scripts/090-hyperv.sh create mode 100644 examples/hcl/ubuntu/etc/scripts/100-cleanup.sh create mode 120000 examples/hcl/ubuntu/etc/scripts/110-minimize.sh create mode 100644 examples/hcl/ubuntu/source.parallels-iso.pkr.hcl create mode 100644 examples/hcl/ubuntu/source.qemu.pkr.hcl create mode 100644 examples/hcl/ubuntu/source.virtualbox-iso.pkr.hcl create mode 100644 examples/hcl/ubuntu/source.vmware-iso.pkr.hcl create mode 100644 examples/hcl/ubuntu/source.vsphere-iso.pkr.hcl create mode 100644 examples/hcl/ubuntu/variables.16.04.pkr.hcl create mode 100644 examples/hcl/ubuntu/variables.18.04.pkr.hcl create mode 100644 examples/hcl/ubuntu/variables.common.pkr.hcl diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dd50e748..ba196963a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ [GH-9383] * communicator/ssh: Allow users to provide a list of ciphers that they want Packer to support. [GH-9453] +* core/hcl2: add possibility to name singular build.source blocks to differentiate + their output and to filter on them [GH-9490] * core/hcl2: Add the "inspect" command for hcl2 configs. [GH-9468] * core/hcl2: HCL configs now respect only/except using build names instead of types. [GH-9454] diff --git a/command/test-fixtures/build-only/template.pkr.hcl b/command/test-fixtures/build-only/template.pkr.hcl index 408579991..3d8423dff 100644 --- a/command/test-fixtures/build-only/template.pkr.hcl +++ b/command/test-fixtures/build-only/template.pkr.hcl @@ -42,13 +42,13 @@ build { } post-processor "shell-local" { - only = ["vanilla"] + only = ["sources.file.vanilla"] name = "tomato" inline = [ "echo apple > tomato.txt" ] } post-processor "shell-local" { - only = ["chocolate"] + only = ["sources.file.chocolate"] inline = [ "echo apple > unnamed.txt" ] } } diff --git a/examples/_common/minimize.sh b/examples/_common/minimize.sh new file mode 100644 index 000000000..0173b4233 --- /dev/null +++ b/examples/_common/minimize.sh @@ -0,0 +1,36 @@ +#!/bin/sh -eux + +case "$PACKER_BUILDER_TYPE" in + qemu) exit 0 ;; +esac + +# Whiteout root +count=$(df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}') +count=$(($count-1)) +dd if=/dev/zero of=/tmp/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed"; +rm /tmp/whitespace + +# Whiteout /boot +count=$(df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}') +count=$(($count-1)) +dd if=/dev/zero of=/boot/whitespace bs=1M count=$count || echo "dd exit code $? is suppressed"; +rm /boot/whitespace + +set +e +swapuuid="`/sbin/blkid -o value -l -s UUID -t TYPE=swap`"; +case "$?" in + 2|0) ;; + *) exit 1 ;; +esac +set -e + +if [ "x${swapuuid}" != "x" ]; then + # Whiteout the swap partition to reduce box size + # Swap is disabled till reboot + swappart="`readlink -f /dev/disk/by-uuid/$swapuuid`"; + /sbin/swapoff "$swappart"; + dd if=/dev/zero of="$swappart" bs=1M || echo "dd exit code $? is suppressed"; + /sbin/mkswap -U "$swapuuid" "$swappart"; +fi + +sync; diff --git a/examples/_common/parallels.sh b/examples/_common/parallels.sh new file mode 100644 index 000000000..1b2a6896a --- /dev/null +++ b/examples/_common/parallels.sh @@ -0,0 +1,35 @@ +#!/bin/sh -eux + +# set a default HOME_DIR environment variable if not set +HOME_DIR="${HOME_DIR:-/home/vagrant}"; + +case "$PACKER_BUILDER_TYPE" in +parallels-iso|parallels-pvm) + mkdir -p /tmp/parallels; + mount -o loop $HOME_DIR/prl-tools-lin.iso /tmp/parallels; + VER="`cat /tmp/parallels/version`"; + + echo "Parallels Tools Version: $VER"; + + /tmp/parallels/install --install-unattended-with-deps \ + || (code="$?"; \ + echo "Parallels tools installation exited $code, attempting" \ + "to output /var/log/parallels-tools-install.log"; \ + cat /var/log/parallels-tools-install.log; \ + exit $code); + umount /tmp/parallels; + rm -rf /tmp/parallels; + rm -f $HOME_DIR/*.iso; + + # Parallels Tools for Linux includes native auto-mount script, + # which causes losing some of Vagrant-relative shared folders. + # So, we should disable this behavior. + # https://github.com/Parallels/vagrant-parallels/issues/325#issuecomment-418727113 + auto_mount_script='/usr/bin/prlfsmountd' + if [ -f "${auto_mount_script}" ]; then + echo -e '#!/bin/sh\n'\ + '# Shared folders auto-mount is disabled by Vagrant ' \ + > "${auto_mount_script}" + fi + ;; +esac diff --git a/examples/_common/sshd.sh b/examples/_common/sshd.sh new file mode 100644 index 000000000..1bb16f56e --- /dev/null +++ b/examples/_common/sshd.sh @@ -0,0 +1,20 @@ +#!/bin/sh -eux + +SSHD_CONFIG="/etc/ssh/sshd_config" + +# ensure that there is a trailing newline before attempting to concatenate +sed -i -e '$a\' "$SSHD_CONFIG" + +USEDNS="UseDNS no" +if grep -q -E "^[[:space:]]*UseDNS" "$SSHD_CONFIG"; then + sed -i "s/^\s*UseDNS.*/${USEDNS}/" "$SSHD_CONFIG" +else + echo "$USEDNS" >>"$SSHD_CONFIG" +fi + +GSSAPI="GSSAPIAuthentication no" +if grep -q -E "^[[:space:]]*GSSAPIAuthentication" "$SSHD_CONFIG"; then + sed -i "s/^\s*GSSAPIAuthentication.*/${GSSAPI}/" "$SSHD_CONFIG" +else + echo "$GSSAPI" >>"$SSHD_CONFIG" +fi diff --git a/examples/_common/vagrant.sh b/examples/_common/vagrant.sh new file mode 100644 index 000000000..c2a14bb1e --- /dev/null +++ b/examples/_common/vagrant.sh @@ -0,0 +1,19 @@ +#!/bin/sh -eux + +# set a default HOME_DIR environment variable if not set +HOME_DIR="${HOME_DIR:-/home/vagrant}"; + +pubkey_url="https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub"; +mkdir -p $HOME_DIR/.ssh; +if command -v wget >/dev/null 2>&1; then + wget --no-check-certificate "$pubkey_url" -O $HOME_DIR/.ssh/authorized_keys; +elif command -v curl >/dev/null 2>&1; then + curl --insecure --location "$pubkey_url" > $HOME_DIR/.ssh/authorized_keys; +elif command -v fetch >/dev/null 2>&1; then + fetch -am -o $HOME_DIR/.ssh/authorized_keys "$pubkey_url"; +else + echo "Cannot download vagrant public key"; + exit 1; +fi +chown -R vagrant $HOME_DIR/.ssh; +chmod -R go-rwsx $HOME_DIR/.ssh; diff --git a/examples/_common/virtualbox.sh b/examples/_common/virtualbox.sh new file mode 100644 index 000000000..c1e34095c --- /dev/null +++ b/examples/_common/virtualbox.sh @@ -0,0 +1,19 @@ +#!/bin/sh -eux + +# set a default HOME_DIR environment variable if not set +HOME_DIR="${HOME_DIR:-/home/vagrant}"; + +case "$PACKER_BUILDER_TYPE" in +virtualbox-iso|virtualbox-ovf) + VER="`cat $HOME_DIR/.vbox_version`"; + ISO="VBoxGuestAdditions_$VER.iso"; + mkdir -p /tmp/vbox; + mount -o loop $HOME_DIR/$ISO /tmp/vbox; + sh /tmp/vbox/VBoxLinuxAdditions.run \ + || echo "VBoxLinuxAdditions.run exited $? and is suppressed." \ + "For more read https://www.virtualbox.org/ticket/12479"; + umount /tmp/vbox; + rm -rf /tmp/vbox; + rm -f $HOME_DIR/*.iso; + ;; +esac diff --git a/examples/_common/vmware.sh b/examples/_common/vmware.sh new file mode 100644 index 000000000..ae1d874b9 --- /dev/null +++ b/examples/_common/vmware.sh @@ -0,0 +1,34 @@ +#!/bin/sh -eux + +# set a default HOME_DIR environment variable if not set +HOME_DIR="${HOME_DIR:-/home/vagrant}"; + +case "$PACKER_BUILDER_TYPE" in +vmware-iso|vmware-vmx) + + # make sure we have /sbin in our path. RHEL systems lack this + PATH=/sbin:$PATH + export PATH + + mkdir -p /tmp/vmware; + mkdir -p /tmp/vmware-archive; + mount -o loop $HOME_DIR/linux.iso /tmp/vmware; + + TOOLS_PATH="`ls /tmp/vmware/VMwareTools-*.tar.gz`"; + VER="`echo "${TOOLS_PATH}" | cut -f2 -d'-'`"; + MAJ_VER="`echo ${VER} | cut -d '.' -f 1`"; + + echo "VMware Tools Version: $VER"; + + tar xzf ${TOOLS_PATH} -C /tmp/vmware-archive; + if [ "${MAJ_VER}" -lt "10" ]; then + /tmp/vmware-archive/vmware-tools-distrib/vmware-install.pl --default; + else + /tmp/vmware-archive/vmware-tools-distrib/vmware-install.pl --force-install; + fi + umount /tmp/vmware; + rm -rf /tmp/vmware; + rm -rf /tmp/vmware-archive; + rm -f $HOME_DIR/*.iso; + ;; +esac diff --git a/examples/hcl/ubuntu/README.md b/examples/hcl/ubuntu/README.md new file mode 100644 index 000000000..ca0b06f26 --- /dev/null +++ b/examples/hcl/ubuntu/README.md @@ -0,0 +1,6 @@ +# Basic HCL2 Ubuntu builders + +Taking the [chef/bento](https://github.com/chef/bento) repo as an example. + +I recommend you start by reading the build.pkr.hcl file and the +comments/description there. diff --git a/examples/hcl/ubuntu/build.pkr.hcl b/examples/hcl/ubuntu/build.pkr.hcl new file mode 100644 index 000000000..04e306c6d --- /dev/null +++ b/examples/hcl/ubuntu/build.pkr.hcl @@ -0,0 +1,99 @@ + +build { + name = "ubuntu" + description = </etc/apt/apt.conf.d/10periodic; +APT::Periodic::Enable "0"; +APT::Periodic::Update-Package-Lists "0"; +APT::Periodic::Download-Upgradeable-Packages "0"; +APT::Periodic::AutocleanInterval "0"; +APT::Periodic::Unattended-Upgrade "0"; +EOF + +# Clean and nuke the package from orbit +rm -rf /var/log/unattended-upgrades; +apt-get -y purge unattended-upgrades; + +# Update the package list +apt-get -y update; + +# Upgrade all installed packages incl. kernel and kernel headers +apt-get -y dist-upgrade -o Dpkg::Options::="--force-confnew"; + +reboot diff --git a/examples/hcl/ubuntu/etc/scripts/020-sshd.sh b/examples/hcl/ubuntu/etc/scripts/020-sshd.sh new file mode 120000 index 000000000..d70a00bb0 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/020-sshd.sh @@ -0,0 +1 @@ +../../../../_common/sshd.sh \ No newline at end of file diff --git a/examples/hcl/ubuntu/etc/scripts/030-networking.sh b/examples/hcl/ubuntu/etc/scripts/030-networking.sh new file mode 100644 index 000000000..2d4b3372d --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/030-networking.sh @@ -0,0 +1,25 @@ +#!/bin/sh -eux + +ubuntu_version="`lsb_release -r | awk '{print $2}'`"; +major_version="`echo $ubuntu_version | awk -F. '{print $1}'`"; + +if [ "$major_version" -ge "18" ]; then +echo "Create netplan config for eth0" +cat </etc/netplan/01-netcfg.yaml; +network: + version: 2 + ethernets: + eth0: + dhcp4: true +EOF +else + # Adding a 2 sec delay to the interface up, to make the dhclient happy + echo "pre-up sleep 2" >> /etc/network/interfaces; +fi + +if [ "$major_version" -ge "16" ]; then + # Disable Predictable Network Interface names and use eth0 + sed -i 's/en[[:alnum:]]*/eth0/g' /etc/network/interfaces; + sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 \1"/g' /etc/default/grub; + update-grub; +fi diff --git a/examples/hcl/ubuntu/etc/scripts/040-sudoers.sh b/examples/hcl/ubuntu/etc/scripts/040-sudoers.sh new file mode 100644 index 000000000..e56537fea --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/040-sudoers.sh @@ -0,0 +1,7 @@ +#!/bin/sh -eux + +sed -i -e '/Defaults\s\+env_reset/a Defaults\texempt_group=sudo' /etc/sudoers; + +# Set up password-less sudo for the vagrant user +echo 'vagrant ALL=(ALL) NOPASSWD:ALL' >/etc/sudoers.d/99_vagrant; +chmod 440 /etc/sudoers.d/99_vagrant; diff --git a/examples/hcl/ubuntu/etc/scripts/050-vagrant.sh b/examples/hcl/ubuntu/etc/scripts/050-vagrant.sh new file mode 120000 index 000000000..48ec2f910 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/050-vagrant.sh @@ -0,0 +1 @@ +../../../../_common/vagrant.sh \ No newline at end of file diff --git a/examples/hcl/ubuntu/etc/scripts/060-virtualbox.sh b/examples/hcl/ubuntu/etc/scripts/060-virtualbox.sh new file mode 120000 index 000000000..db9a1ad01 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/060-virtualbox.sh @@ -0,0 +1 @@ +../../../../_common/virtualbox.sh \ No newline at end of file diff --git a/examples/hcl/ubuntu/etc/scripts/070-vmware.sh b/examples/hcl/ubuntu/etc/scripts/070-vmware.sh new file mode 100644 index 000000000..0f23f5547 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/070-vmware.sh @@ -0,0 +1,10 @@ +#!/bin/sh -eux + +case "$PACKER_BUILDER_TYPE" in +vmware-iso|vmware-vmx) + apt-get install -y open-vm-tools; + mkdir /mnt/hgfs; + systemctl enable open-vm-tools + systemctl start open-vm-tools + echo "platform specific vmware.sh executed"; +esac diff --git a/examples/hcl/ubuntu/etc/scripts/080-parallels.sh b/examples/hcl/ubuntu/etc/scripts/080-parallels.sh new file mode 120000 index 000000000..0d63dac8b --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/080-parallels.sh @@ -0,0 +1 @@ +../../../../_common/parallels.sh \ No newline at end of file diff --git a/examples/hcl/ubuntu/etc/scripts/090-hyperv.sh b/examples/hcl/ubuntu/etc/scripts/090-hyperv.sh new file mode 100644 index 000000000..af379bcc0 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/090-hyperv.sh @@ -0,0 +1,12 @@ +#!/bin/sh -eux +ubuntu_version="`lsb_release -r | awk '{print $2}'`"; +major_version="`echo $ubuntu_version | awk -F. '{print $1}'`"; + +case "$PACKER_BUILDER_TYPE" in +hyperv-iso) + if [ "$major_version" -eq "16" ]; then + apt-get install -y linux-tools-virtual-lts-xenial linux-cloud-tools-virtual-lts-xenial; + else + apt-get -y install linux-image-virtual linux-tools-virtual linux-cloud-tools-virtual; + fi +esac diff --git a/examples/hcl/ubuntu/etc/scripts/100-cleanup.sh b/examples/hcl/ubuntu/etc/scripts/100-cleanup.sh new file mode 100644 index 000000000..aef78221c --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/100-cleanup.sh @@ -0,0 +1,81 @@ +#!/bin/sh -eux + +# Delete all Linux headers +dpkg --list \ + | awk '{ print $2 }' \ + | grep 'linux-headers' \ + | xargs apt-get -y purge; + +# Remove specific Linux kernels, such as linux-image-3.11.0-15-generic but +# keeps the current kernel and does not touch the virtual packages, +# e.g. 'linux-image-generic', etc. +dpkg --list \ + | awk '{ print $2 }' \ + | grep 'linux-image-.*-generic' \ + | grep -v `uname -r` \ + | xargs apt-get -y purge; + +# Delete Linux source +dpkg --list \ + | awk '{ print $2 }' \ + | grep linux-source \ + | xargs apt-get -y purge; + +# Delete development packages +dpkg --list \ + | awk '{ print $2 }' \ + | grep -- '-dev$' \ + | xargs apt-get -y purge; + +# delete docs packages +dpkg --list \ + | awk '{ print $2 }' \ + | grep -- '-doc$' \ + | xargs apt-get -y purge; + +# Delete X11 libraries +apt-get -y purge libx11-data xauth libxmuu1 libxcb1 libx11-6 libxext6; + +# Delete obsolete networking +apt-get -y purge ppp pppconfig pppoeconf; + +# Delete oddities +apt-get -y purge popularity-contest installation-report command-not-found friendly-recovery bash-completion fonts-ubuntu-font-family-console laptop-detect; + +# 19.10+ don't have this package so fail gracefully +apt-get -y purge command-not-found-data || true; + +# Exlude the files we don't need w/o uninstalling linux-firmware +echo "==> Setup dpkg excludes for linux-firmware" +cat <<_EOF_ | cat >> /etc/dpkg/dpkg.cfg.d/excludes +#BENTO-BEGIN +path-exclude=/lib/firmware/* +path-exclude=/usr/share/doc/linux-firmware/* +#BENTO-END +_EOF_ + +# Delete the massive firmware packages +rm -rf /lib/firmware/* +rm -rf /usr/share/doc/linux-firmware/* + +apt-get -y autoremove; +apt-get -y clean; + +# Remove docs +rm -rf /usr/share/doc/* + +# Remove caches +find /var/cache -type f -exec rm -rf {} \; + +# truncate any logs that have built up during the install +find /var/log -type f -exec truncate --size=0 {} \; + +# Blank netplan machine-id (DUID) so machines get unique ID generated on boot. +truncate -s 0 /etc/machine-id + +# remove the contents of /tmp and /var/tmp +rm -rf /tmp/* /var/tmp/* + +# clear the history so our install isn't there +export HISTSIZE=0 +rm -f /root/.wget-hsts diff --git a/examples/hcl/ubuntu/etc/scripts/110-minimize.sh b/examples/hcl/ubuntu/etc/scripts/110-minimize.sh new file mode 120000 index 000000000..fa918ae11 --- /dev/null +++ b/examples/hcl/ubuntu/etc/scripts/110-minimize.sh @@ -0,0 +1 @@ +../../../../_common/minimize.sh \ No newline at end of file diff --git a/examples/hcl/ubuntu/source.parallels-iso.pkr.hcl b/examples/hcl/ubuntu/source.parallels-iso.pkr.hcl new file mode 100644 index 000000000..e95b467d0 --- /dev/null +++ b/examples/hcl/ubuntu/source.parallels-iso.pkr.hcl @@ -0,0 +1,13 @@ + +source "parallels-iso" "base-ubuntu-amd64" { + boot_wait = "10s" + guest_os_type = "ubuntu" + http_directory = local.http_directory + parallels_tools_flavor = "lin" + prlctl_version_file = ".prlctl_version" + shutdown_command = "echo 'vagrant' | sudo -S shutdown -P now" + ssh_password = "vagrant" + ssh_port = 22 + ssh_timeout = "10000s" + ssh_username = "vagrant" +} diff --git a/examples/hcl/ubuntu/source.qemu.pkr.hcl b/examples/hcl/ubuntu/source.qemu.pkr.hcl new file mode 100644 index 000000000..69d8beafc --- /dev/null +++ b/examples/hcl/ubuntu/source.qemu.pkr.hcl @@ -0,0 +1,16 @@ +source "qemu" "base-ubuntu-amd64" { + headless = var.headless + floppy_files = [ + "${local.http_directory}/preseed.cfg", + ] + http_directory = local.http_directory + shutdown_command = "echo 'vagrant'|sudo -S shutdown -P now" + ssh_password = "vagrant" + ssh_username = "vagrant" + ssh_wait_timeout = "50m" + disk_size = 5000 + disk_interface = "virtio-scsi" + memory = 512 * 4 + cpus = 4 + boot_wait = "5s" +} diff --git a/examples/hcl/ubuntu/source.virtualbox-iso.pkr.hcl b/examples/hcl/ubuntu/source.virtualbox-iso.pkr.hcl new file mode 100644 index 000000000..7a6a4e8da --- /dev/null +++ b/examples/hcl/ubuntu/source.virtualbox-iso.pkr.hcl @@ -0,0 +1,14 @@ +source "virtualbox-iso" "base-ubuntu-amd64" { + headless = var.headless + guest_os_type = "Ubuntu_64" + http_directory = local.http_directory + shutdown_command = "echo 'vagrant' | sudo -S shutdown -P now" + ssh_username = "vagrant" + ssh_password = "vagrant" + ssh_port = 22 + ssh_wait_timeout = "15m" + hard_drive_interface = "sata" + virtualbox_version_file = ".vbox_version" + guest_additions_path = "VBoxGuestAdditions_{{.Version}}.iso" + guest_additions_url = var.guest_additions_url +} diff --git a/examples/hcl/ubuntu/source.vmware-iso.pkr.hcl b/examples/hcl/ubuntu/source.vmware-iso.pkr.hcl new file mode 100644 index 000000000..d7d32eeb6 --- /dev/null +++ b/examples/hcl/ubuntu/source.vmware-iso.pkr.hcl @@ -0,0 +1,17 @@ +source "vmware-iso" "base-ubuntu-amd64" { + headless = var.headless + boot_wait = "10s" + guest_os_type = "ubuntu-64" + http_directory = local.http_directory + shutdown_command = "echo 'vagrant' | sudo -S shutdown -P now" + ssh_password = "vagrant" + ssh_port = 22 + ssh_timeout = "10000s" + ssh_username = "vagrant" + tools_upload_flavor = "linux" + vmx_data = { + "cpuid.coresPerSocket" = "1" + "ethernet0.pciSlotNumber" = "32" + } + vmx_remove_ethernet_interfaces = true +} diff --git a/examples/hcl/ubuntu/source.vsphere-iso.pkr.hcl b/examples/hcl/ubuntu/source.vsphere-iso.pkr.hcl new file mode 100644 index 000000000..09bfcf57b --- /dev/null +++ b/examples/hcl/ubuntu/source.vsphere-iso.pkr.hcl @@ -0,0 +1,76 @@ +// I use the following config with direnv and set the following values: +// export PKR_VAR_bastion_host="" +// export PKR_VAR_bastion_user="" +// export PKR_VAR_datacenter_name="" +// export PKR_VAR_esxi_host="" +// export PKR_VAR_esxi_password="" +// export PKR_VAR_esxi_user="" +// export PKR_VAR_vcenter_endpoint="" +// export PKR_VAR_vcenter_password="" +// export PKR_VAR_vcenter_user="" +// export PKR_VAR_vm_ip="" +// export PKR_VAR_gateway_ip="" + +variable "vcenter_endpoint" { type = string } +variable "vcenter_user" { type = string } +variable "vcenter_password" { type = string } +variable "esxi_host" { type = string } +variable "datacenter_name" { type = string } +variable "vm_ip" { type = string } +variable "gateway_ip" { type = string } +variable "datastore" { + default = "datastore1" +} + +source "vsphere-iso" "base-ubuntu-amd64" { + vcenter_server = var.vcenter_endpoint + username = var.vcenter_user + password = var.vcenter_password + host = var.esxi_host + insecure_connection = true + + datacenter = var.datacenter_name + datastore = "datastore1" + + ssh_password = "vagrant" + ssh_username = "vagrant" + + CPUs = 1 + RAM = 512 * 2 + RAM_reserve_all = true + + disk_controller_type = "pvscsi" + floppy_files = [ + "etc/http/preseed_hardcoded_ip.cfg" + ] + guest_os_type = "ubuntu64Guest" + network_adapters { + network = "VM Network" + network_card = "vmxnet3" + } + storage { + disk_size = 32768 + disk_thin_provisioned = true + } + + boot_command = [ + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "/install/vmlinuz", + " initrd=/install/initrd.gz", + " priority=critical", + " locale=en_US", + " file=/media/preseed_hardcoded_ip.cfg", + " netcfg/get_ipaddress=${var.vm_ip}", + " netcfg/get_gateway=${var.gateway_ip}", + "" + ] +} diff --git a/examples/hcl/ubuntu/variables.16.04.pkr.hcl b/examples/hcl/ubuntu/variables.16.04.pkr.hcl new file mode 100644 index 000000000..9feac1b18 --- /dev/null +++ b/examples/hcl/ubuntu/variables.16.04.pkr.hcl @@ -0,0 +1,34 @@ +variable "ubuntu_1604_version" { + default = "16.04.6" +} + +locals { + iso_url_ubuntu_1604 = "http://releases.ubuntu.com/releases/16.04/ubuntu-${var.ubuntu_1604_version}-server-amd64.iso" + iso_checksum_url_ubuntu_1604 = "http://releases.ubuntu.com/releases/16.04/SHA256SUMS" + ubuntu_1604_boot_command = [ + "", + "", + "", + "", + "/install/vmlinuz", + " auto", + " console-setup/ask_detect=false", + " console-setup/layoutcode=us", + " console-setup/modelcode=pc105", + " debconf/frontend=noninteractive", + " debian-installer=en_US.UTF-8", + " fb=false", + " initrd=/install/initrd.gz", + " kbd-chooser/method=us", + " keyboard-configuration/layout=USA", + " keyboard-configuration/variant=USA", + " locale=en_US.UTF-8", + " netcfg/get_domain=vm", + " netcfg/get_hostname=vagrant", + " grub-installer/bootdev=/dev/sda", + " noapic", + " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/${var.preseed_path}", + " -- ", + "" + ] +} diff --git a/examples/hcl/ubuntu/variables.18.04.pkr.hcl b/examples/hcl/ubuntu/variables.18.04.pkr.hcl new file mode 100644 index 000000000..76c9d48c0 --- /dev/null +++ b/examples/hcl/ubuntu/variables.18.04.pkr.hcl @@ -0,0 +1,33 @@ +variable "ubuntu_1804_version" { + default = "18.04.4" +} + +locals { + iso_url_ubuntu_1804 = "http://cdimage.ubuntu.com/ubuntu/releases/18.04/release/ubuntu-18.04.4-server-amd64.iso" + iso_checksum_url_ubuntu_1804 = "http://cdimage.ubuntu.com/ubuntu/releases/18.04/release/SHA256SUMS" + ubuntu_1804_boot_command = [ + "", + "", + "", + "/install/vmlinuz", + " auto", + " console-setup/ask_detect=false", + " console-setup/layoutcode=us", + " console-setup/modelcode=pc105", + " debconf/frontend=noninteractive", + " debian-installer=en_US.UTF-8", + " fb=false", + " initrd=/install/initrd.gz", + " kbd-chooser/method=us", + " keyboard-configuration/layout=USA", + " keyboard-configuration/variant=USA", + " locale=en_US.UTF-8", + " netcfg/get_domain=vm", + " netcfg/get_hostname=vagrant", + " grub-installer/bootdev=/dev/sda", + " noapic", + " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/${var.preseed_path}", + " -- ", + "" + ] +} diff --git a/examples/hcl/ubuntu/variables.common.pkr.hcl b/examples/hcl/ubuntu/variables.common.pkr.hcl new file mode 100644 index 000000000..19231c04d --- /dev/null +++ b/examples/hcl/ubuntu/variables.common.pkr.hcl @@ -0,0 +1,23 @@ + +variable "preseed_path" { + type = string + default = "preseed.cfg" +} + +variable "guest_additions_url" { + type = string + default = "" +} + +variable "headless" { + type = bool + default = true +} + +locals { + // fileset lists all files in the http directory as a set, we convert that + // set to a list of strings and we then take the directory of the first + // value. This validates that the http directory exists even before starting + // any builder/provisioner. + http_directory = dirname(convert(fileset(".", "etc/http/*"), list(string))[0]) +} diff --git a/hcl2template/testdata/build/post-processor_onlyexcept.pkr.hcl b/hcl2template/testdata/build/post-processor_onlyexcept.pkr.hcl index bb02c563c..b9ba01803 100644 --- a/hcl2template/testdata/build/post-processor_onlyexcept.pkr.hcl +++ b/hcl2template/testdata/build/post-processor_onlyexcept.pkr.hcl @@ -3,8 +3,10 @@ build { sources = [ "source.virtualbox-iso.ubuntu-1204", - "source.amazon-ebs.ubuntu-1604", ] + source "source.amazon-ebs.ubuntu-1604" { + name = "aws-ubuntu-16.04" + } post-processor "amazon-import" { only = ["virtualbox-iso.ubuntu-1204"] @@ -12,6 +14,12 @@ build { post-processor "manifest" { except = ["virtualbox-iso.ubuntu-1204"] } + post-processor "amazon-import" { + only = ["amazon-ebs.aws-ubuntu-16.04"] + } + post-processor "manifest" { + except = ["amazon-ebs.aws-ubuntu-16.04"] + } } source "virtualbox-iso" "ubuntu-1204" { diff --git a/hcl2template/testdata/build/provisioner_onlyexcept.pkr.hcl b/hcl2template/testdata/build/provisioner_onlyexcept.pkr.hcl index 5befb1a26..368375e9f 100644 --- a/hcl2template/testdata/build/provisioner_onlyexcept.pkr.hcl +++ b/hcl2template/testdata/build/provisioner_onlyexcept.pkr.hcl @@ -3,8 +3,10 @@ build { sources = [ "source.virtualbox-iso.ubuntu-1204", - "source.amazon-ebs.ubuntu-1604", ] + source "source.amazon-ebs.ubuntu-1604" { + name = "aws-ubuntu-16.04" + } provisioner "shell" { only = ["virtualbox-iso.ubuntu-1204"] @@ -12,6 +14,12 @@ build { provisioner "file" { except = ["virtualbox-iso.ubuntu-1204"] } + provisioner "shell" { + only = ["amazon-ebs.aws-ubuntu-16.04"] + } + provisioner "file" { + except = ["amazon-ebs.aws-ubuntu-16.04"] + } } source "virtualbox-iso" "ubuntu-1204" { diff --git a/hcl2template/types.build_test.go b/hcl2template/types.build_test.go index 38d0c4806..c3d5dc9e5 100644 --- a/hcl2template/types.build_test.go +++ b/hcl2template/types.build_test.go @@ -134,7 +134,10 @@ func TestParse_build(t *testing.T) { }, Builds: Builds{ &BuildBlock{ - Sources: []SourceRef{refVBIsoUbuntu1204, refAWSEBSUbuntu1604}, + Sources: []SourceRef{ + refVBIsoUbuntu1204, + SourceRef{Type: "amazon-ebs", Name: "ubuntu-1604", LocalName: "aws-ubuntu-16.04"}, + }, ProvisionerBlocks: nil, PostProcessors: []*PostProcessorBlock{ { @@ -145,6 +148,14 @@ func TestParse_build(t *testing.T) { PType: "manifest", OnlyExcept: OnlyExcept{Only: nil, Except: []string{"virtualbox-iso.ubuntu-1204"}}, }, + { + PType: "amazon-import", + OnlyExcept: OnlyExcept{Only: []string{"amazon-ebs.aws-ubuntu-16.04"}, Except: nil}, + }, + { + PType: "manifest", + OnlyExcept: OnlyExcept{Only: nil, Except: []string{"amazon-ebs.aws-ubuntu-16.04"}}, + }, }, }, }, @@ -167,11 +178,20 @@ func TestParse_build(t *testing.T) { }, }, }, + { + PType: "manifest", + PostProcessor: &MockPostProcessor{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{Tags: []MockTag{}}, + NestedSlice: []NestedMockConfig{}, + }, + }, + }, }, }, }, &packer.CoreBuild{ - Type: "amazon-ebs.ubuntu-1604", + Type: "amazon-ebs.aws-ubuntu-16.04", Prepared: true, Builder: emptyMockBuilder, Provisioners: []packer.CoreBuildProvisioner{}, @@ -186,6 +206,15 @@ func TestParse_build(t *testing.T) { }, }, }, + { + PType: "amazon-import", + PostProcessor: &MockPostProcessor{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{Tags: []MockTag{}}, + NestedSlice: []NestedMockConfig{}, + }, + }, + }, }, }, }, @@ -203,15 +232,26 @@ func TestParse_build(t *testing.T) { }, Builds: Builds{ &BuildBlock{ - Sources: []SourceRef{refVBIsoUbuntu1204, refAWSEBSUbuntu1604}, + Sources: []SourceRef{ + refVBIsoUbuntu1204, + SourceRef{Type: "amazon-ebs", Name: "ubuntu-1604", LocalName: "aws-ubuntu-16.04"}, + }, ProvisionerBlocks: []*ProvisionerBlock{ { PType: "shell", - OnlyExcept: OnlyExcept{Only: []string{"virtualbox-iso.ubuntu-1204"}, Except: nil}, + OnlyExcept: OnlyExcept{Only: []string{"virtualbox-iso.ubuntu-1204"}}, }, { PType: "file", - OnlyExcept: OnlyExcept{Only: nil, Except: []string{"virtualbox-iso.ubuntu-1204"}}, + OnlyExcept: OnlyExcept{Except: []string{"virtualbox-iso.ubuntu-1204"}}, + }, + { + PType: "shell", + OnlyExcept: OnlyExcept{Only: []string{"amazon-ebs.aws-ubuntu-16.04"}}, + }, + { + PType: "file", + OnlyExcept: OnlyExcept{Except: []string{"amazon-ebs.aws-ubuntu-16.04"}}, }, }, }, @@ -233,11 +273,20 @@ func TestParse_build(t *testing.T) { }, }, }, + { + PType: "file", + Provisioner: &MockProvisioner{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{Tags: []MockTag{}}, + NestedSlice: []NestedMockConfig{}, + }, + }, + }, }, PostProcessors: [][]packer.CoreBuildPostProcessor{}, }, &packer.CoreBuild{ - Type: "amazon-ebs.ubuntu-1604", + Type: "amazon-ebs.aws-ubuntu-16.04", Prepared: true, Builder: emptyMockBuilder, Provisioners: []packer.CoreBuildProvisioner{ @@ -250,6 +299,15 @@ func TestParse_build(t *testing.T) { }, }, }, + { + PType: "shell", + Provisioner: &MockProvisioner{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{Tags: []MockTag{}}, + NestedSlice: []NestedMockConfig{}, + }, + }, + }, }, PostProcessors: [][]packer.CoreBuildPostProcessor{}, }, diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 419c2bccd..b791dabd1 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -203,7 +203,7 @@ func (cfg *PackerConfig) getCoreBuildProvisioners(source SourceBlock, blocks []* var diags hcl.Diagnostics res := []packer.CoreBuildProvisioner{} for _, pb := range blocks { - if pb.OnlyExcept.Skip(source.Type + "." + source.Name) { + if pb.OnlyExcept.Skip(source.String()) { continue } provisioner, moreDiags := cfg.startProvisioner(source, pb, ectx) @@ -250,7 +250,7 @@ func (cfg *PackerConfig) getCoreBuildPostProcessors(source SourceBlock, blocks [ var diags hcl.Diagnostics res := []packer.CoreBuildPostProcessor{} for _, ppb := range blocks { - if ppb.OnlyExcept.Skip(source.Type + "." + source.Name) { + if ppb.OnlyExcept.Skip(source.String()) { continue } @@ -308,10 +308,11 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build continue } src.addition = from.addition + src.LocalName = from.LocalName pcb := &packer.CoreBuild{ BuildName: build.Name, - Type: src.Ref().String(), + Type: src.String(), } // Apply the -only and -except command-line options to exclude matching builds. diff --git a/hcl2template/types.source.go b/hcl2template/types.source.go index 94c7aa538..51f275aa0 100644 --- a/hcl2template/types.source.go +++ b/hcl2template/types.source.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/packer/packer" ) @@ -20,15 +21,36 @@ type SourceBlock struct { // addition will be merged into block to allow user to override builder settings // per build.source block. addition hcl.Body + // LocalName can be set in a singular source block from a build block, it + // allows to give a special name to a build in the logs. + LocalName string +} + +func (b *SourceBlock) String() string { + if b.LocalName != "" { + return fmt.Sprintf("%s.%s", b.Type, b.LocalName) + } + return fmt.Sprintf("%s.%s", b.Type, b.Name) } // decodeBuildSource reads a used source block from a build: // build { -// source "type.example" {} +// source "type.example" { +// name = "local_name" +// } // } func (p *Parser) decodeBuildSource(block *hcl.Block) (SourceRef, hcl.Diagnostics) { ref := sourceRefFromString(block.Labels[0]) - ref.addition = block.Body + var b struct { + Name string `hcl:"name,optional"` + Rest hcl.Body `hcl:",remain"` + } + diags := gohcl.DecodeBody(block.Body, nil, &b) + if diags.HasErrors() { + return ref, diags + } + ref.addition = b.Rest + ref.LocalName = b.Name return ref, nil } @@ -115,6 +137,9 @@ type SourceRef struct { // The content of this body will be merged into a new block to allow to // override builder settings per build section. addition hcl.Body + // LocalName can be set in a singular source block from a build block, it + // allows to give a special name to a build in the logs. + LocalName string } // the 'addition' field makes of ref a different entry in the sources map, so diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index df87594eb..15e014a66 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -163,7 +163,7 @@ func TestParse_variables(t *testing.T) { }, }, Sources: map[SourceRef]SourceBlock{ - SourceRef{"null", "null-builder", nil}: SourceBlock{ + SourceRef{Type: "null", Name: "null-builder"}: SourceBlock{ Name: "null-builder", Type: "null", }, @@ -171,7 +171,7 @@ func TestParse_variables(t *testing.T) { Builds: Builds{ &BuildBlock{ Sources: []SourceRef{ - {"null", "null-builder", nil}, + {Type: "null", Name: "null-builder"}, }, }, }, diff --git a/website/pages/docs/from-1.5/blocks/build/source.mdx b/website/pages/docs/from-1.5/blocks/build/source.mdx index 27000affe..a6c5e2b66 100644 --- a/website/pages/docs/from-1.5/blocks/build/source.mdx +++ b/website/pages/docs/from-1.5/blocks/build/source.mdx @@ -22,7 +22,10 @@ source "lxd" "arch" { build { source "lxd.arch" { - output_image = "nomad" + // setting the name field allows to rename the source only for this build + // section. + name = "nomad" + output_image = "nomad" } provisioner "shell" { @@ -32,7 +35,8 @@ build { build { source "lxd.arch" { - output_image = "consul" + name = "consul" + output_image = "consul" } provisioner "shell" { diff --git a/website/pages/docs/from-1.5/onlyexcept.mdx b/website/pages/docs/from-1.5/onlyexcept.mdx index e651d6d5b..ba10d8112 100644 --- a/website/pages/docs/from-1.5/onlyexcept.mdx +++ b/website/pages/docs/from-1.5/onlyexcept.mdx @@ -25,16 +25,20 @@ build { name = "my_build" sources [ "source.amazon-ebs.first-example", - "source.amazon-ebs.second-example", ] + source "source.amazon-ebs.second-example" { + // setting the name field allows to rename the source only for this build + // section. + name = "second-example-local-name" + } provisioner "shell-local" { - only = ["source.amazon-ebs.second-example"] + only = ["source.amazon-ebs.first-example"] inline = ["echo I will only run for the second example source"] } provisioner "shell-local" { - except = ["source.amazon-ebs.second-example"] + except = ["source.amazon-ebs.second-example-local-name"] inline = ["echo I will never run for the second example source"] } } @@ -60,6 +64,9 @@ configuration: * `packer build -only '*.amazon-ebs.*' dir`: will only run the builds with a source of type `amazon-ebs`. +* `packer build -only '*.second-example-local-name' dir`: will only run that +specifically named build. + -> Note: In the cli `only` and `except` will match agains **build names** (for example:`my_build.amazon-ebs.first-example`) but in a provisioner they will match on the **source type** (for example:`source.amazon-ebs.third-example`). \ No newline at end of file