From ac2201f72c4b21b22d8a3662f8d47e227de06975 Mon Sep 17 00:00:00 2001 From: Vojtech Mares Date: Tue, 26 Sep 2023 21:42:33 +0000 Subject: [PATCH] Initial commit --- .editorconfig | 13 +++++++ .gitignore | 3 ++ main.tf | 79 +++++++++++++++++++++++++++++++++++++++++++ templates/config.yaml | 14 ++++++++ versions.tf | 12 +++++++ 5 files changed, 121 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 main.tf create mode 100644 templates/config.yaml create mode 100644 versions.tf diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..87a0020 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +end_of_line = lf +max_line_length = null + +[Makefile] +indent_style = tab diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca58d2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.terraform +.terraform.lock.hcl +variables.auto.tfvars diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..8ead322 --- /dev/null +++ b/main.tf @@ -0,0 +1,79 @@ +variable "hcloud_token" { + type = string +} + +provider "hcloud" { + token = var.hcloud_token +} + +data "hcloud_ssh_key" "vojtechmares" { + name = "iam@vojtechmares.com" +} + +resource "hcloud_ssh_key" "ssh_key" { + name = "ssh-key" + public_key = var.ssh_public_key +} + +data "ct_config" "ignition" { + content = file("templates/config.yaml") + strict = true + pretty_print = true +} + +resource "hcloud_server" "server" { + name = "flatcar-linux--test" + server_type = "cx11" + + labels = { + "os" = "flatcar-linux" + } + + location = "nbg1" + + // Unused because of rescue mode, but required field + image = "debian-11" + rescue = "linux64" + ssh_keys = [data.hcloud_ssh_key.vojtechmares.id] + + connection { + host = hcloud_server.test.ipv4_address + timeout = "5m" + user = "root" + } + + provisioner "file" { + content = data.ct_config.ignition.rendered + destination = "/root/ignition.json" + } + + provisioner "remote-exec" { + inline = [ + "set -ex", + "apt update -y", + "apt install -y gawk", + "curl -fsSLO --retry-delay 1 --retry 60 --retry-connrefused --retry-max-time 60 --connect-timeout 20 https://raw.githubusercontent.com/kinvolk/init/flatcar-master/bin/flatcar-install", + "chmod +x flatcar-install", + "./flatcar-install -s -i /root/ignition.json", + "shutdown -r +1", + ] + } + + # Configure after installation + provisioner "remote-exec" { + connection { + host = self.ipv4_address + timeout = "3m" + user = "core" + } + + inline = [ + "sudo hostnamectl set-hostname ${self.name}", + ] + } + +} + +output "ip" { + value = hcloud_server.server.ipv4_address +} diff --git a/templates/config.yaml b/templates/config.yaml new file mode 100644 index 0000000..2213fc2 --- /dev/null +++ b/templates/config.yaml @@ -0,0 +1,14 @@ +--- +passwd: + users: + - name: core + ssh_authorized_keys: + - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEUz21xFatGKfSwjeg/BOBus2Jn17o8xVthrMFAsDtRD iam@vojtechmares.com + +systemd: + units: + # Using flatcar-linux-update-operator instead. See: https://github.com/flatcar-linux/flatcar-linux-update-operator#requirements for more information + - name: locksmithd.service + mask: true + - name: update-engine.service + enabled: true diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..db704ae --- /dev/null +++ b/versions.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + hcloud = { + source = "hetznercloud/hcloud" + version = "1.32.1" + } + ct = { + source = "poseidon/ct" + version = "0.9.1" + } + } +}