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
pull/9523/head
Adrien Delorme 6 years ago committed by GitHub
parent 445cf12b65
commit 3d371a2d5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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]

@ -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" ]
}
}

@ -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;

@ -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

@ -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

@ -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;

@ -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

@ -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

@ -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.

@ -0,0 +1,99 @@
build {
name = "ubuntu"
description = <<EOF
This build creates ubuntu images for ubuntu versions :
* 16.04
* 18.04
For the following builers :
* virtualbox-iso
* parallels-iso
* vmware-iso
* qemu
* vsphere-iso
EOF
// the common fields of the source blocks are defined in the
// source.builder-type.pkr.hcl files, here we only set the fields specific to
// the different versions of ubuntu.
source "source.virtualbox-iso.base-ubuntu-amd64" {
name = "16.04"
iso_url = local.iso_url_ubuntu_1604
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1604}"
output_directory = "virtualbox_iso_ubuntu_1604_amd64"
boot_command = local.ubuntu_1604_boot_command
boot_wait = "10s"
}
source "source.virtualbox-iso.base-ubuntu-amd64" {
name = "18.04"
iso_url = local.iso_url_ubuntu_1804
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1804}"
output_directory = "virtualbox_iso_ubuntu_1804_amd64"
boot_command = local.ubuntu_1804_boot_command
boot_wait = "5s"
}
source "source.parallels-iso.base-ubuntu-amd64" {
name = "16.04"
iso_url = local.iso_url_ubuntu_1604
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1604}"
output_directory = "parallels_iso_ubuntu_1604_amd64"
boot_command = local.ubuntu_1604_boot_command
}
source "source.parallels-iso.base-ubuntu-amd64" {
name = "18.04"
iso_url = local.iso_url_ubuntu_1804
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1804}"
output_directory = "parallels_iso_ubuntu_1804_amd64"
boot_command = local.ubuntu_1804_boot_command
}
source "source.vmware-iso.base-ubuntu-amd64" {
name = "16.04"
iso_url = local.iso_url_ubuntu_1604
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1604}"
output_directory = "vmware_iso_ubuntu_1604_amd64"
boot_command = local.ubuntu_1604_boot_command
}
source "source.vmware-iso.base-ubuntu-amd64" {
name = "18.04"
iso_url = local.iso_url_ubuntu_1804
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1804}"
output_directory = "vmware_iso_ubuntu_1804_amd64"
boot_command = local.ubuntu_1804_boot_command
}
source "source.qemu.base-ubuntu-amd64" {
name = "16.04"
iso_url = local.iso_url_ubuntu_1604
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1604}"
output_directory = "qemu_iso_ubuntu_1604_amd64"
boot_command = local.ubuntu_1604_boot_command
}
source "source.qemu.base-ubuntu-amd64" {
name = "18.04"
iso_url = local.iso_url_ubuntu_1804
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1804}"
output_directory = "qemu_iso_ubuntu_1804_amd64"
boot_command = local.ubuntu_1804_boot_command
}
source "source.vsphere-iso.base-ubuntu-amd64" {
name = "16.04"
vm_name = "ubuntu-16.04"
iso_url = local.iso_url_ubuntu_1604
iso_checksum = "file:${local.iso_checksum_url_ubuntu_1604}"
}
provisioner "shell" {
environment_vars = [ "HOME_DIR=/home/vagrant" ]
execute_command = "echo 'vagrant' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'"
expect_disconnect = true
// fileset will list files in etc/scripts sorted in an alphanumerical way.
scripts = fileset(".", "etc/scripts/*.sh")
}
}

@ -0,0 +1,35 @@
choose-mirror-bin mirror/http/proxy string
d-i base-installer/kernel/override-image string linux-server
d-i clock-setup/utc boolean true
d-i clock-setup/utc-auto boolean true
d-i finish-install/reboot_in_progress note
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i mirror/country string manual
d-i mirror/http/directory string /ubuntu/
d-i mirror/http/hostname string archive.ubuntu.com
d-i mirror/http/proxy string
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm_write_new_label boolean true
d-i passwd/user-fullname string vagrant
d-i passwd/user-uid string 1000
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i passwd/username string vagrant
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev linux-source dkms nfs-common linux-headers-$(uname -r) perl cifs-utils software-properties-common rsync ifupdown
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select full-upgrade
d-i time/zone string UTC
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
tasksel tasksel/first multiselect standard, server

@ -0,0 +1,68 @@
#
# Based upon: https://help.ubuntu.com/12.04/installation-guide/example-preseed.txt
#
# localisation
d-i debian-installer/locale string en_US.utf8
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layoutcode string us
# networking
# Static network configuration.
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/get_nameservers string 8.8.8.8
d-i netcfg/get_netmask string 255.255.255.248
d-i netcfg/confirm_static boolean true
# d-i netcfg/choose_interface select auto
# d-i netcfg/get_hostname string unassigned-hostname
# d-i netcfg/get_domain string unassigned-domain
# d-i netcfg/wireless_wep string
# apt mirrors
d-i mirror/country string manual
d-i mirror/http/directory string /ubuntu/
d-i mirror/http/hostname string archive.ubuntu.com
d-i mirror/http/proxy string
# clock and time zone
d-i clock-setup/utc boolean true
d-i time/zone string GMT
d-i clock-setup/ntp boolean true
# partitioning
d-i partman-auto/method string lvm
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
# users
d-i passwd/user-fullname string Vagrant
d-i passwd/username string vagrant
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
# packages
tasksel tasksel/first multiselect standard, ubuntu-server
d-i pkgsel/install-language-support boolean false
d-i pkgsel/include string openssh-server nfs-common open-vm-tools
d-i pkgsel/upgrade select full-upgrade
d-i pkgsel/update-policy select none
# postfix postfix/main_mailer_type select No configuration
# boot loader
d-i grub-installer/only_debian boolean true
# hide the shutdown notice
d-i finish-install/reboot_in_progress note

@ -0,0 +1,40 @@
#!/bin/sh -eux
export DEBIAN_FRONTEND=noninteractive
ubuntu_version="`lsb_release -r | awk '{print $2}'`";
major_version="`echo $ubuntu_version | awk -F. '{print $1}'`";
# Disable release-upgrades
sed -i.bak 's/^Prompt=.*$/Prompt=never/' /etc/update-manager/release-upgrades;
# Disable systemd apt timers/services
if [ "$major_version" -ge "16" ]; then
systemctl stop apt-daily.timer;
systemctl stop apt-daily-upgrade.timer;
systemctl disable apt-daily.timer;
systemctl disable apt-daily-upgrade.timer;
systemctl mask apt-daily.service;
systemctl mask apt-daily-upgrade.service;
systemctl daemon-reload;
fi
# Disable periodic activities of apt to be safe
cat <<EOF >/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

@ -0,0 +1 @@
../../../../_common/sshd.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 <<EOF >/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

@ -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;

@ -0,0 +1 @@
../../../../_common/vagrant.sh

@ -0,0 +1 @@
../../../../_common/virtualbox.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

@ -0,0 +1 @@
../../../../_common/parallels.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

@ -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

@ -0,0 +1 @@
../../../../_common/minimize.sh

@ -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"
}

@ -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"
}

@ -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
}

@ -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
}

@ -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 = [
"<enter><wait><f6><wait><esc><wait>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs>",
"/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}",
"<enter>"
]
}

@ -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 = [
"<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"/install/vmlinuz<wait>",
" auto<wait>",
" console-setup/ask_detect=false<wait>",
" console-setup/layoutcode=us<wait>",
" console-setup/modelcode=pc105<wait>",
" debconf/frontend=noninteractive<wait>",
" debian-installer=en_US.UTF-8<wait>",
" fb=false<wait>",
" initrd=/install/initrd.gz<wait>",
" kbd-chooser/method=us<wait>",
" keyboard-configuration/layout=USA<wait>",
" keyboard-configuration/variant=USA<wait>",
" locale=en_US.UTF-8<wait>",
" netcfg/get_domain=vm<wait>",
" netcfg/get_hostname=vagrant<wait>",
" grub-installer/bootdev=/dev/sda<wait>",
" noapic<wait>",
" preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/${var.preseed_path}<wait>",
" -- <wait>",
"<enter><wait>"
]
}

@ -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 = [
"<esc><wait>",
"<esc><wait>",
"<enter><wait>",
"/install/vmlinuz<wait>",
" auto<wait>",
" console-setup/ask_detect=false<wait>",
" console-setup/layoutcode=us<wait>",
" console-setup/modelcode=pc105<wait>",
" debconf/frontend=noninteractive<wait>",
" debian-installer=en_US.UTF-8<wait>",
" fb=false<wait>",
" initrd=/install/initrd.gz<wait>",
" kbd-chooser/method=us<wait>",
" keyboard-configuration/layout=USA<wait>",
" keyboard-configuration/variant=USA<wait>",
" locale=en_US.UTF-8<wait>",
" netcfg/get_domain=vm<wait>",
" netcfg/get_hostname=vagrant<wait>",
" grub-installer/bootdev=/dev/sda<wait>",
" noapic<wait>",
" preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/${var.preseed_path}<wait>",
" -- <wait>",
"<enter><wait>"
]
}

@ -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])
}

@ -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" {

@ -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" {

@ -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{},
},

@ -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.

@ -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

@ -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"},
},
},
},

@ -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" {

@ -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`).
Loading…
Cancel
Save