diff --git a/cl/machine-mynode.yaml.tmpl b/cl/machine-mynode.yaml.tmpl new file mode 100644 index 0000000..b28d86e --- /dev/null +++ b/cl/machine-mynode.yaml.tmpl @@ -0,0 +1,18 @@ +--- +variant: flatcar +version: 1.1.0 +passwd: + users: + - name: core + ssh_authorized_keys: ${ssh_keys} +storage: + files: + - path: /home/core/works + mode: 0755 + contents: + inline: | + #!/bin/bash + set -euo pipefail + # This script demonstrates how templating and variable substitution works when using Terraform templates for Container Linux Configs. + hostname="$(hostname)" + echo My name is ${name} and the hostname is $${hostname} and this is updated! diff --git a/main.tf b/main.tf index bdd03e9..a5ea3d3 100644 --- a/main.tf +++ b/main.tf @@ -1,28 +1,111 @@ terraform { required_providers { digitalocean = { - source = "digitalocean/digitalocean" + source = "digitalocean/digitalocean" version = "2.62.0" } + ct = { + source = "poseidon/ct" + version = "0.13.0" + } + template = { + source = "hashicorp/template" + version = "2.2.0" + } + null = { + source = "hashicorp/null" + version = "3.2.4" + } } } -variable "do_token"{ +variable "do_token" { description = "DigitalOcean token" - type = string + type = string +} + +variable "machines" { + type = list(string) + description = "Machine names, corresponding to machine-NAME.yaml.tmpl files" +} + +variable "cluster_name" { + type = string + description = "Cluster name used as prefix for the machine names" +} + +variable "ssh_keys" { + type = list(string) + description = "SSH public keys for user 'core' (and to register on Digital Ocean for the first)" +} + +variable "server_type" { + type = string + default = "s-1vcpu-1gb" + description = "The server type to rent" +} + +variable "datacenter" { + type = string + description = "The region to deploy in" +} + +variable "flatcar_stable_version" { + type = string + description = "The Flatcar Stable release you want to use for the initial installation, e.g., 2605.12.0" } provider "digitalocean" { token = var.do_token } -resource "digitalocean_droplet" "web" { - image = "ubuntu-25-04-x64" - name = "web-1" - region = "sfo3" - size = "s-1vcpu-1gb" +resource "digitalocean_ssh_key" "first" { + name = var.cluster_name + public_key = var.ssh_keys.0 +} + +resource "digitalocean_custom_image" "flatcar" { + name = "flatcar-stable-${var.flatcar_stable_version}" + url = "https://stable.release.flatcar-linux.net/amd64-usr/${var.flatcar_stable_version}/flatcar_production_digitalocean_image.bin.bz2" + regions = [var.datacenter] +} + +resource "digitalocean_droplet" "machine" { + for_each = toset(var.machines) + name = "${var.cluster_name}-${each.key}" + image = digitalocean_custom_image.flatcar.id + region = var.datacenter + size = var.server_type + ssh_keys = [digitalocean_ssh_key.first.fingerprint] + user_data = data.ct_config.machine-ignitions[each.key].rendered + lifecycle { + create_before_destroy = true + } +} + +data "ct_config" "machine-ignitions" { + for_each = toset(var.machines) + # content = data.template_file.machine-configs[each.key].rendered + content = templatefile("${path.module}/cl/machine-${each.key}.yaml.tmpl", { + ssh_keys = jsonencode(var.ssh_keys), + name = each.key + }) + strict = true } -output "web_ip" { - value = digitalocean_droplet.web.ipv4_address +#data "template_file" "machine-configs" { +# for_each = toset(var.machines) +# template = file("${path.module}/cl/machine-${each.key}.yaml.tmpl") + +# vars = { +# ssh_keys = jsonencode(var.ssh_keys) +# name = each.key +# } +#} + +output "ip-addresses" { + value = { + for key in var.machines : + "${var.cluster_name}-${key}" => digitalocean_droplet.machine[key].ipv4_address + } } diff --git a/template.tfvars b/template.tfvars new file mode 100644 index 0000000..d30d9c9 --- /dev/null +++ b/template.tfvars @@ -0,0 +1,7 @@ +do_token = "" # token from "API" settings on DigitalOcean + +cluster_name = "mycluster" +machines = ["mynode"] +datacenter = "sfo3" +ssh_keys = [""] # paste contents of id_rsa.pub +flatcar_stable_version = "4230.2.1"