feat: cleanup code and introduce custom labels

This commit is contained in:
Julian Tölle 2024-05-30 16:29:30 +02:00
parent c7730392cc
commit a5c8a49c6a
2 changed files with 51 additions and 70 deletions

View file

@ -1,41 +0,0 @@
variant: flatcar
version: 1.1.0
systemd:
units:
- name: "coreos-metadata.service"
enabled: true
contents: |
[Unit]
Description=Flatcar Metadata Agent
After=nss-lookup.target
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
Restart=on-failure
RemainAfterExit=yes
ExecStart=/usr/bin/coreos-metadata --cmdline --attributes=/run/metadata/flatcar
ExecStartPost=/usr/bin/sed --in-place "s/AFTERBURN/COREOS/g ; s/AWS/EC2/g ; s/GCP/GCE/g" /run/metadata/flatcar
ExecStartPost=/usr/bin/ln -fs /run/metadata/flatcar /run/metadata/coreos
[Install]
RequiredBy=system-config.target
Alias=afterburn.service
- name: "coreos-metadata-sshkeys@root.service"
enabled: true
- name: "coreos-metadata-hostname.service"
# The unit in initramfs has restrictive conditions on the OEM platform that do not include Hetzner
enabled: true
contents: |
[Unit]
Description=Flatcar Metadata Agent Hostname
After=coreos-metadata.service
Requires=coreos-metadata.service
[Service]
EnvironmentFile=/run/metadata/flatcar
Type=oneshot
Restart=on-failure
RemainAfterExit=yes
ExecStart=/usr/bin/hostnamectl hostname "${COREOS_COREOS_HETZNER_HOSTNAME}"
[Install]
RequiredBy=system-config.target

View file

@ -2,7 +2,7 @@ packer {
required_plugins {
hcloud = {
source = "github.com/hetznercloud/hcloud"
version = "~> 1"
version = "~> 1.4.0"
}
}
}
@ -26,65 +26,87 @@ variable "channel" {
default = "beta"
}
variable "image_version_id" {
variable "version" {
type = string
default = "current"
}
variable "flatcar_install_script" {
type = string
default = "https://raw.githubusercontent.com/flatcar/init/flatcar-master/bin/flatcar-install"
variable "labels" {
// Available replacements:
// $architecture
// $board
// $channel
// $version - if "current" was specified, this is resolved to the actual version
type = map(string)
default = {
os = "flatcar"
flatcar-channel = "$channel"
flatcar-board = "$board"
version = "$version"
architecture = "$architecture"
}
}
locals {
hcloud_location = "fsn1"
hcloud_rescue = "linux64"
hcloud_initial_os = "ubuntu-22.04"
flatcar_oem_id = "hetzner"
board = {
boards = {
x86 = "amd64-usr"
arm = "arm64-usr"
}
architectures = ["x86", "arm"]
// If the user wants the "current" version, we still want to make the
// actual version id available through labels + snapshot description
//
// regex matches: FLATCAR_VERSION=1234.0.0
version = regex("FLATCAR_VERSION=(\\d+\\.\\d+\\.\\d+)", data.http.version_info.body)[0]
}
data "http" "version_info" {
// We assume that both boards have the same version
url = "https://${var.channel}.release.flatcar-linux.net/amd64-usr/${var.version}/version.txt"
}
source "hcloud" "flatcar" {
token = var.hcloud_token
image = local.hcloud_initial_os
location = local.hcloud_location
rescue = local.hcloud_rescue
snapshot_labels = {
os = "flatcar"
}
image = "ubuntu-22.04"
location = "fsn1"
rescue = "linux64"
ssh_username = "root"
}
build {
source "hcloud.flatcar" {
name = "x86"
server_type = var.hcloud_server_type["x86"]
snapshot_name = "flatcar-x86"
}
dynamic "source" {
for_each = local.architectures
labels = ["hcloud.flatcar"]
source "hcloud.flatcar" {
name = "arm"
server_type = var.hcloud_server_type["arm"]
snapshot_name = "flatcar-arm"
content {
name = source.value
server_type = var.hcloud_server_type[source.value]
snapshot_name = "flatcar-${var.channel}-${local.version}-${source.value}"
snapshot_labels = {
for k, v in var.labels : k => replace(replace(replace(replace(v,
"$channel", var.channel),
"$version", local.version),
"$architecture", source.value),
"$board", local.boards[source.name])
}
}
}
provisioner "shell" {
inline = [
# Download script and dependencies
"apt-get -y install gawk",
"curl -fsSLO --retry-delay 1 --retry 60 --retry-connrefused --retry-max-time 60 --connect-timeout 20 ${var.flatcar_install_script}",
"curl -fsSLO --retry-delay 1 --retry 60 --retry-connrefused --retry-max-time 60 --connect-timeout 20 https://raw.githubusercontent.com/flatcar/init/flatcar-master/bin/flatcar-install",
"chmod +x flatcar-install",
# Install flatcar
"./flatcar-install -v -s -o hetzner -b https://${var.channel}.release.flatcar-linux.net/${local.board[source.name]} -V ${var.image_version_id}",
"./flatcar-install -s -o hetzner -C ${var.channel} -B ${local.boards[source.name]} -V ${var.version} ",
]
}
}