From 954f6ac376f8b07ded29f987f08f81e1f9cd818f Mon Sep 17 00:00:00 2001 From: Serge Logvinov Date: Tue, 13 Jul 2021 14:14:18 +0300 Subject: [PATCH] Use packer.io to upload base os-image --- .gitignore | 3 ++ hetzner/common.tf | 6 ++-- hetzner/instances-master.tf | 2 +- system_os/hetzner/hetzner.pkr.hcl | 47 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 system_os/hetzner/hetzner.pkr.hcl diff --git a/.gitignore b/.gitignore index eef6411..524221d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ *.tfstate *.tfstate.* +# packer files +.auto.pkrvars.hcl + # Crash log files crash.log diff --git a/hetzner/common.tf b/hetzner/common.tf index ca55c20..0da00df 100644 --- a/hetzner/common.tf +++ b/hetzner/common.tf @@ -3,8 +3,10 @@ data "hcloud_image" "talos" { with_selector = "type=infra" } -data "hcloud_ssh_key" "infra" { - with_selector = "type=infra" +resource "hcloud_ssh_key" "infra" { + name = "infra" + public_key = file("~/.ssh/terraform.pub") + labels = merge(var.tags, { type = "infra" }) } # resource "talos_cluster_config" "talos_config" { diff --git a/hetzner/instances-master.tf b/hetzner/instances-master.tf index 09cda71..3352601 100644 --- a/hetzner/instances-master.tf +++ b/hetzner/instances-master.tf @@ -5,7 +5,7 @@ resource "hcloud_server" "controlplane" { name = "master-${count.index + 1}" image = data.hcloud_image.talos.id server_type = lookup(var.controlplane, "type", "cpx11") - ssh_keys = [data.hcloud_ssh_key.infra.id] + ssh_keys = [hcloud_ssh_key.infra.id] keep_disk = true labels = merge(var.tags, { type = "infra", label = "master" }) diff --git a/system_os/hetzner/hetzner.pkr.hcl b/system_os/hetzner/hetzner.pkr.hcl new file mode 100644 index 0000000..7671c2d --- /dev/null +++ b/system_os/hetzner/hetzner.pkr.hcl @@ -0,0 +1,47 @@ + +packer { + required_plugins { + docker = { + version = ">= 1.0.0" + source = "github.com/hashicorp/hcloud" + } + } +} + +variable "hcloud_token" { + type = string + default = "${env("HCLOUD_TOKEN")}" + sensitive = true +} + +variable "talos_version" { + type = string + default = "v0.11.0" +} + +source "hcloud" "talos" { + token = var.hcloud_token + rescue = "linux64" + image = "debian-10" + 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/openstack.tar.gz https://github.com/talos-systems/talos/releases/download/${var.talos_version}/openstack-amd64.tar.gz", + "cd /tmp && tar xzf /tmp/openstack.tar.gz && dd if=/tmp/disk.raw of=/dev/sda && sync", + ] + } +}