Skip to content

Commit a228401

Browse files
committed
Convert Packer definition to HCL2
This rewrites the packer JSON files to use the HCL2 language support added in 1.5 and stabilized in 1.7. The initial conversion was performed using the `hcl2_upgrade` command but quite a lot of custom work has been done on top of that. A base shared definition for the VM is created that defines all the shared options (CPU, memory, SSH stuff, etc) and then that is specialized in the `build` block for each `source`, this is where the differences between Ubuntu and Mint are specified. Additionally, we now store version information as complex objects that meet our needs a bit more closely. This is required because some of the interpolation that we did in JSON is no longer available. As I was reviewing the VirtualBox builder config I also moved a few things from manual `VBoxManage` commands to actual packer definitions but some of those could have been done in the JSON format as well.
1 parent f381f5b commit a228401

File tree

9 files changed

+252
-348
lines changed

9 files changed

+252
-348
lines changed

.github/workflows/lint.yml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ jobs:
3030
steps:
3131
- name: Checkout
3232
uses: actions/checkout@v3
33-
- name: Set up Python 3.x
34-
uses: actions/setup-python@v4
35-
with:
36-
python-version: 3.x
37-
- name: Install dependencies
38-
run: |
39-
pip3 install demjson3
40-
- name: Run jsonlint
41-
run: |
42-
jsonlint packer/*json
4333
- name: Install Hashicorp repo
4434
run: >
4535
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor |
@@ -53,10 +43,15 @@ jobs:
5343
sudo apt-get install -y -qq packer
5444
- name: Packer validate
5545
run: |
56-
packer validate packer/ubuntu-build.json
57-
packer validate packer/mint-build.json
58-
# Allow beta/test builds to fail validation
59-
packer validate -var-file=packer/beta-vars.json packer/mint-build.json || true
46+
# Allow beta/test builds to fail validation
47+
packer validate -var-file=packer/mint-beta.pkrvars.hcl . || true
48+
# This runs last to ensure it's exit status is captured
49+
packer validate .
50+
working-directory: packer
51+
- name: Check Packer HCL formatting
52+
run: |
53+
packer fmt -diff -check .
54+
working-directory: packer
6055
Python:
6156
name: Run Python lint tests
6257
runs-on: ubuntu-20.04

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77

88
# Packer-related binaries
99
packer/packer_cache/*
10-
packer/artifacts_mint/*
11-
packer/artifacts_ubuntu/*
10+
packer/artifacts*/*

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ Linux and Windows hosts, but feedback on other platforms is always welcome.
143143

144144
Due to difficulties with Packer packaging, this VM is frequently built with the
145145
latest version of Packer available directly from Hashicorp. Check the
146-
`mint-build.json` file for the current minimum version required.
146+
`main.pkr.hcl` file for the current minimum version required.
147147

148148
Once the prerequisites are installed, change into the `cs-vm-build/packer`
149-
directory and execute `packer build mint-build.json`. This will take a
149+
directory and execute `packer build -only "*.mint" .`. This will take a
150150
considerable amount of time, depending on host resources available, but should
151151
output progress indicators along the way.
152152

@@ -164,15 +164,31 @@ specify `dsound` for Windows, `coreaudio` for Mac.
164164
### Building beta images
165165

166166
A large number of variables can be overridden at once by passing a `var-file`
167-
to Packer. An example of this is provided as `beta-vars.json`, and can be used
167+
to Packer. An example of this is provided as `mint-beta.pkrvars.hcl`, and can be used
168168
like this:
169169

170-
`packer build -var-file=beta-vars.json mint-build.json`
170+
`packer build -var-file=mint-beta.pkrvars.hcl -only "*.mint" .`
171171

172172
Packer allows further overrides, with precedence given to the last option in the
173173
command. For example, to build a beta image on Windows, use this command:
174174

175-
`packer build -var-file=beta-vars.json -var 'audio=dsound' mint-build.json`
175+
`packer build -var-file=mint-beta.pkrvars.hcl -var 'audio=dsound' -only "*.mint" .`
176+
177+
### Building Ubuntu images
178+
179+
Native support is available for creating an Ubuntu variant of the image (and in
180+
fact, all previous commands have specifically excluded Ubuntu image builds). By
181+
default, the `packer` configuration will build both a Mint and Ubuntu VM. Try it
182+
with:
183+
184+
`packer build .`
185+
186+
Much as with the previous commands, you can build only an ubuntu-based image by
187+
running:
188+
189+
`packer build -only "*.ubuntu" .`
190+
191+
Support for building beta variants of Ubuntu images is not currently supported.
176192

177193
## Contributing
178194

packer/beta-vars.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

packer/main.pkr.hcl

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
packer {
2+
required_version = ">= 1.7.0"
3+
}
4+
5+
source "virtualbox-iso" "base-build" {
6+
cpus = 2
7+
memory = 4096
8+
disk_size = 20480
9+
guest_os_type = "Ubuntu_64"
10+
gfx_vram_size = 64
11+
12+
format = "ova"
13+
gfx_controller = "vmsvga"
14+
gfx_accelerate_3d = true
15+
hard_drive_discard = true
16+
hard_drive_interface = "sata"
17+
hard_drive_nonrotational = true
18+
headless = "${var.headless}"
19+
http_directory = "http"
20+
iso_interface = "sata"
21+
rtc_time_base = "UTC"
22+
sata_port_count = 2
23+
sound = "${var.audio}"
24+
ssh_username = "${var.ssh_user}"
25+
ssh_password = "${var.ssh_pass}"
26+
ssh_timeout = "100m"
27+
28+
boot_wait = "5s"
29+
boot_command = [
30+
"<esc><wait><esc><wait><esc><wait>",
31+
"c<wait>",
32+
"linux /casper/vmlinuz fsck.mode=skip<wait> noprompt",
33+
" auto url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/oem-preseed.cfg",
34+
" automatic-ubiquity debug-ubiquity keymap=us<enter>",
35+
# Different distributions name (and compress) the initrd differently. Fortunately,
36+
# GRUB is mostly smart and if the file doesn't exist, it just won't apply that directive.
37+
# So to prevent duplication, we specify both and let GRUB ignore the wrong one.
38+
"initrd /casper/initrd<enter><wait>",
39+
"initrd /casper/initrd.lz<enter><wait>",
40+
"boot<enter>"
41+
]
42+
shutdown_command = "echo -e \"${var.ssh_pass}\\n\" | sudo -S poweroff"
43+
44+
vboxmanage = [
45+
["modifyvm", "{{ .Name }}", "--audioin", "off"],
46+
["modifyvm", "{{ .Name }}", "--clipboard-mode", "bidirectional"],
47+
["modifyvm", "{{ .Name }}", "--mouse", "usbtablet"],
48+
["modifyvm", "{{ .Name }}", "--pae", "on"],
49+
["modifyvm", "{{ .Name }}", "--usb", "on", "--usbehci", "off", "--usbxhci", "off"],
50+
["modifyvm", "{{ .Name }}", "--vrde", "off"],
51+
["storagectl", "{{ .Name }}", "--name", "IDE Controller", "--remove"],
52+
["storagectl", "{{ .Name }}", "--name", "SATA Controller", "--hostiocache", "on"]
53+
]
54+
vboxmanage_post = [
55+
["storageattach", "{{ .Name }}", "--storagectl", "SATA Controller", "--port", "1", "--type", "dvddrive", "--medium", "emptydrive"]
56+
]
57+
}
58+
59+
build {
60+
source "source.virtualbox-iso.base-build" {
61+
name = "mint"
62+
vm_name = "${var.vm_name} Linux Mint ${var.semester}"
63+
iso_url = "${local.mint_info.mirror_url}/${local.mint_info.iso_file}"
64+
iso_checksum = "file:${local.mint_info.mirror_url}/sha256sum.txt"
65+
output_filename = "image-${lower(var.semester)}-mint"
66+
output_directory = "${local.artifact_dir_prefix}mint"
67+
export_opts = [
68+
"--manifest",
69+
"--vsys", "0",
70+
"--description", "Build date: ${local.build_id}\nPacker version: ${packer.version}",
71+
"--product", "${var.vm_name} Linux Mint ${var.semester}",
72+
"--producturl", "https://linuxmint.com/",
73+
"--vendor", "JMU Unix Users Group",
74+
"--vendorurl", "${var.git_repo}",
75+
"--version", "${var.mint_version.version}"
76+
]
77+
}
78+
79+
source "source.virtualbox-iso.base-build" {
80+
name = "ubuntu"
81+
vm_name = "${var.vm_name} Ubuntu ${var.semester}"
82+
iso_url = "${local.ubuntu_info.mirror_url}/${local.ubuntu_info.iso_file}"
83+
iso_checksum = "file:${local.ubuntu_info.mirror_url}/SHA256SUMS"
84+
output_filename = "image-${lower(var.semester)}-ubuntu"
85+
output_directory = "${local.artifact_dir_prefix}ubuntu"
86+
export_opts = [
87+
"--manifest",
88+
"--vsys", "0",
89+
"--description", "Build date: ${local.build_id}\nPacker version: ${packer.version}",
90+
"--product", "${var.vm_name} Ubuntu ${var.semester}",
91+
"--producturl", "https://ubuntu.com/",
92+
"--vendor", "JMU Unix Users Group",
93+
"--vendorurl", "${var.git_repo}",
94+
"--version", "${var.ubuntu_version.version}"
95+
]
96+
}
97+
98+
provisioner "shell" {
99+
execute_command = "echo 'oem' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'"
100+
environment_vars = [
101+
"DEBIAN_FRONTEND=noninteractive"
102+
]
103+
inline = [
104+
"echo STOPPING APT",
105+
"systemctl stop unattended-upgrades.service || true",
106+
"while(pgrep -a apt-get); do sleep 1; done",
107+
"echo UPDATE",
108+
"apt-get update",
109+
"echo INSTALL",
110+
"apt-get install -V -y git ansible aptitude",
111+
"git clone -b ${var.git_branch} ${var.git_repo}",
112+
"cd /home/oem/cs-vm-build/scripts",
113+
"./oem-build",
114+
"/usr/sbin/oem-config-prepare"
115+
]
116+
}
117+
118+
post-processor "checksum" {
119+
checksum_types = ["sha256"]
120+
keep_input_artifact = true
121+
output = "${local.artifact_dir_prefix}${source.name}/image-${lower(var.semester)}-${source.name}.{{ .ChecksumType }}sum"
122+
}
123+
}

packer/mint-beta.pkrvars.hcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
semester = "Fa22"
2+
3+
mint_version = {
4+
version = "21"
5+
name = "vanessa"
6+
beta = true
7+
}

packer/mint-build.json

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)