From 0ba3f222d2335fb0be5ece5d3c815546d2d4de03 Mon Sep 17 00:00:00 2001
From: Kristian Klausen <kristian@klausen.dk>
Date: Fri, 28 Jul 2023 16:49:39 +0200
Subject: [PATCH] packer: Convert from JSON to HCL

As of version 1.7.0, HCL2 is the preferred way to write Packer
templates. The documentation reflect this and it is easier if we use the
preferred format.
---
 README.md                |  2 +-
 packer/archlinux.json    | 26 -----------------------
 packer/archlinux.pkr.hcl | 45 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 27 deletions(-)
 delete mode 100644 packer/archlinux.json
 create mode 100644 packer/archlinux.pkr.hcl

diff --git a/README.md b/README.md
index e8337d16f..a12032df9 100644
--- a/README.md
+++ b/README.md
@@ -45,7 +45,7 @@ tasks.
 We use packer to build snapshots on hcloud to use as server base images.
 In order to use this, you need to install packer and then run
 
-    packer build -var $(misc/get_key.py misc/vaults/vault_hetzner.yml hetzner_cloud_api_key --format env) packer/archlinux.json
+    packer build -var $(misc/get_key.py misc/vaults/vault_hetzner.yml hetzner_cloud_api_key --format env) packer/archlinux.pkr.hcl
 
 This will take some time after which a new snapshot will have been created on the primary hcloud archlinux project.
 
diff --git a/packer/archlinux.json b/packer/archlinux.json
deleted file mode 100644
index 8f8f689e3..000000000
--- a/packer/archlinux.json
+++ /dev/null
@@ -1,26 +0,0 @@
-{
-    "variables": {
-        "hetzner_cloud_api_key": null
-    },
-    "sensitive-variables": ["hetzner_cloud_api_key"],
-    "builders": [{
-        "type": "hcloud",
-        "snapshot_name": "archlinux-{{ isotime \"2006-01-02T15:04\" }}",
-        "snapshot_labels": {
-            "custom_image": "archlinux"
-        },
-        "token": "{{ user `hetzner_cloud_api_key` }}",
-        "image": "ubuntu-22.04",
-        "server_type": "cx11",
-        "ssh_username": "root",
-        "location": "fsn1",
-        "rescue": "linux64"
-    }],
-    "provisioners": [{
-        "type": "ansible",
-        "playbook_file": "playbooks/tasks/install_arch.yml",
-        "host_alias": "packer-base-image",
-        "inventory_directory": ".",
-        "use_proxy": false
-    }]
-}
diff --git a/packer/archlinux.pkr.hcl b/packer/archlinux.pkr.hcl
new file mode 100644
index 000000000..4acbf0f6c
--- /dev/null
+++ b/packer/archlinux.pkr.hcl
@@ -0,0 +1,45 @@
+# https://www.packer.io/docs/templates/hcl_templates/blocks/packer
+packer {
+  required_plugins {
+    ansible = {
+      source  = "github.com/hashicorp/ansible"
+      version = ">= 1.1.0"
+    }
+    hcloud = {
+      source  = "github.com/hashicorp/hcloud"
+      version = ">= 1.0.0"
+    }
+  }
+}
+
+# https://www.packer.io/docs/templates/hcl_templates/variables#type-constraints
+variable "hetzner_cloud_api_key" {
+  type      = string
+  sensitive = true
+}
+
+# https://www.packer.io/docs/templates/hcl_templates/blocks/source
+source "hcloud" "rescue" {
+  image       = "ubuntu-22.04"
+  location    = "fsn1"
+  rescue      = "linux64"
+  server_type = "cx11"
+  snapshot_labels = {
+    custom_image = "archlinux"
+  }
+  snapshot_name = "archlinux-${timestamp()}"
+  ssh_username  = "root"
+  token         = var.hetzner_cloud_api_key
+}
+
+# https://www.packer.io/docs/templates/hcl_templates/blocks/build
+build {
+  sources = ["source.hcloud.rescue"]
+
+  provisioner "ansible" {
+    host_alias          = "packer-base-image"
+    inventory_directory = "."
+    playbook_file       = "playbooks/tasks/install_arch.yml"
+    use_proxy           = false
+  }
+}
-- 
GitLab