Skip to content

Instantly share code, notes, and snippets.

@hegerdes
Last active February 27, 2025 13:16

Revisions

  1. hegerdes revised this gist Feb 27, 2025. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion os-setup.sh
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,3 @@ apt-get install --yes --no-install-recommends wget fail2ban

    echo "cleanup..."
    cloud-init clean --machine-id --seed --logs
    rm -rvf /var/lib/cloud/instances /etc/machine-id /var/lib/dbus/machine-id /var/log/cloud-init*
  2. hegerdes revised this gist Dec 1, 2024. 3 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion custom-img-v1.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    source "hcloud" "base-amd64" {
    image = "debian-12"
    location = "nbg1"
    server_type = "cx11"
    server_type = "cx22"
    ssh_keys = []
    user_data = ""
    ssh_username = "root"
    2 changes: 1 addition & 1 deletion custom-img-v2.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ variable "user_data_path" {
    source "hcloud" "base-amd64" {
    image = var.base_image
    location = "nbg1"
    server_type = "cx11"
    server_type = "cx22"
    ssh_keys = []
    user_data = file(var.user_data_path)
    ssh_username = "root"
    2 changes: 1 addition & 1 deletion talos.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ source "hcloud" "talos" {
    rescue = "linux64"
    image = "debian-11"
    location = "hel1"
    server_type = "cx11"
    server_type = "cx22"
    ssh_username = "root"
    snapshot_name = "talos system disk"
    snapshot_labels = {
  3. hegerdes revised this gist Dec 3, 2023. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions talos.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,6 @@
    # talos.pkr.hcl
    # NOTE: Based on https://www.talos.dev/v1.5/talos-guides/install/cloud-platforms/hetzner/

    packer {
    required_plugins {
    hcloud = {
    version = ">= 1.0.0"
    source = "github.com/hashicorp/hcloud"
    }
    }
    }

    variable "talos_version" {
    type = string
    default = "v1.5.5"
  4. hegerdes created this gist Dec 3, 2023.
    22 changes: 22 additions & 0 deletions cloud-init-default.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #cloud-config

    ssh_pwauth: false
    disable_root_opts: no-port-forwarding,no-agent-forwarding,no-X11-forwarding

    # Install base packages
    package_update: true
    package_upgrade: true
    packages:
    - gnupg
    - curl
    - jq
    - unzip
    - apparmor
    - aptitude
    - lsb-release
    - ca-certificates
    - apt-transport-https
    - unattended-upgrades
    - apparmor-profiles-extra
    - logrotate
    - wget
    29 changes: 29 additions & 0 deletions custom-img-v1.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # custom-img-v1.pkr.hcl
    source "hcloud" "base-amd64" {
    image = "debian-12"
    location = "nbg1"
    server_type = "cx11"
    ssh_keys = []
    user_data = ""
    ssh_username = "root"
    snapshot_name = "custom-img"
    snapshot_labels = {
    base = "debian-12",
    version = "v1.0.0",
    name = "custom-img"
    }
    }

    build {
    sources = ["source.hcloud.base-amd64"]
    provisioner "shell" {
    inline = [
    "apt-get update",
    "apt-get install -y wget fail2ban cowsay",
    "/usr/games/cowsay 'Hi Hetzner Cloud' > /etc/motd",
    ]
    env = {
    BUILDER = "packer"
    }
    }
    }
    58 changes: 58 additions & 0 deletions custom-img-v2.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    # custom-img-v2.pkr.hcl
    variable "base_image" {
    type = string
    default = "debian-12"
    }
    variable "output_name" {
    type = string
    default = "snapshot"
    }
    variable "version" {
    type = string
    default = "v1.0.0"
    }
    variable "user_data_path" {
    type = string
    default = "cloud-init-default.yml"
    }

    source "hcloud" "base-amd64" {
    image = var.base_image
    location = "nbg1"
    server_type = "cx11"
    ssh_keys = []
    user_data = file(var.user_data_path)
    ssh_username = "root"
    snapshot_name = "${var.output_name}-${var.version}"
    snapshot_labels = {
    base = var.base_image,
    version = var.version,
    name = "${var.output_name}-${var.version}"
    }
    }
    source "hcloud" "base-arm64" {
    image = var.base_image
    location = "nbg1"
    server_type = "cax11"
    ssh_keys = []
    user_data = file(var.user_data_path)
    ssh_username = "root"
    snapshot_name = "${var.output_name}-${var.version}"
    snapshot_labels = {
    base = var.base_image,
    version = var.version,
    name = "${var.output_name}-${var.version}"
    }
    }

    build {
    sources = ["source.hcloud.base-amd64", "source.hcloud.base-arm64"]
    provisioner "shell" {
    scripts = [
    "os-setup.sh",
    ]
    env = {
    BUILDER = "packer"
    }
    }
    }
    15 changes: 15 additions & 0 deletions os-setup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #!/bin/bash
    set -e -o pipefail

    echo "waiting for cloud-init to finish..."
    cloud-init status --wait

    echo "installing packages..."
    apt-get update
    apt-get install --yes --no-install-recommends wget fail2ban

    # My setup...

    echo "cleanup..."
    cloud-init clean --machine-id --seed --logs
    rm -rvf /var/lib/cloud/instances /etc/machine-id /var/lib/dbus/machine-id /var/log/cloud-init*
    9 changes: 9 additions & 0 deletions packer.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # packer.pkr.hcl
    packer {
    required_plugins {
    hcloud = {
    source = "github.com/hetznercloud/hcloud"
    version = ">= 1.2.0"
    }
    }
    }
    45 changes: 45 additions & 0 deletions talos.pkr.hcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # talos.pkr.hcl
    # NOTE: Based on https://www.talos.dev/v1.5/talos-guides/install/cloud-platforms/hetzner/

    packer {
    required_plugins {
    hcloud = {
    version = ">= 1.0.0"
    source = "github.com/hashicorp/hcloud"
    }
    }
    }

    variable "talos_version" {
    type = string
    default = "v1.5.5"
    }

    locals {
    image = "https://github.com/siderolabs/talos/releases/download/${var.talos_version}/hcloud-amd64.raw.xz"
    }

    source "hcloud" "talos" {
    rescue = "linux64"
    image = "debian-11"
    location = "hel1"
    server_type = "cx11"
    ssh_username = "root"
    snapshot_name = "talos system disk"
    snapshot_labels = {
    type = "infra",
    os = "talos",
    version = "${var.talos_version}",
    }
    }

    build {
    sources = ["source.hcloud.talos"]
    provisioner "shell" {
    inline = [
    "apt-get install -y wget",
    "wget -O /tmp/talos.raw.xz ${local.image}",
    "xz -d -c /tmp/talos.raw.xz | dd of=/dev/sda && sync",
    ]
    }
    }