diff --git a/MIGRATION.md b/MIGRATION.md index e7e6961..230bffa 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -5,7 +5,7 @@ The plan and the runbook. Delete this file when the last step is done. **End state:** both servers run NixOS built from `nixos/`, deployed by `.forgejo/workflows/nixos.yaml`; services are native NixOS services where nixpkgs has a module and Podman containers otherwise; no Docker -daemon, no Ansible; infrastructure in `terraform/` is applied with +daemon, no Ansible; infrastructure in `tofu/` is applied with OpenTofu. The servers themselves stay: they are billed below today's list price, so each one is converted **in place** and never recreated. @@ -43,7 +43,7 @@ Phase 2 took 18.5 minutes of downtime (04:52–05:10 UTC) plus a minute for the reboot. WireGuard was down from 04:52 to 05:04 and came back with the first `test` activation; the dead-man reboot was not needed. Found on the day: nixos-infect's bootstrap system only accepts the keys Debian's -root had (the RSA key from `terraform/keys.tf`); on the new system root +root had (the RSA key from `tofu/keys.tofu`); on the new system root takes the `me@asxp.io` key. The disk reached 90% with the old Docker store under `/old-root`; deleting its image layers brought it to 71%. @@ -112,7 +112,7 @@ Everything here can be done while Ansible still runs production. 2. **OpenTofu.** Once, from a workstation: ```sh - cd terraform + cd tofu tofu init -reconfigure tofu plan # expected: only the new user_data output tofu apply # writes the output; changes no resource diff --git a/README.md b/README.md index 1e90769..0cd8cfa 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ Both are reachable for administration only over WireGuard ## Layout -- `terraform/`: servers, network, firewalls, DNS, object storage buckets. - Applied by hand; see `terraform/README.md`. +- `tofu/`: servers, network, firewalls, DNS, object storage buckets (OpenTofu). + Applied by hand; see `tofu/README.md`. - `ansible/site.yaml`: everything the pipeline manages (see below). - `ansible/playbooks/`: host bootstrap, run by hand. - `.forgejo/workflows/deploy.yaml`: the pipeline. @@ -140,7 +140,7 @@ The password is `vault_restic_password` in the vault. Keep a copy outside the servers and this repo's CI: without it nothing can be read. `s3_replicate` copies `asxpio-backups-main` (minus `restic/`), -`asxpio-terraform-main` and `k8s-asxpio-main` to their fsn1 twins daily +`asxpio-tofu-main`, `asxpio-loki-main` and `k8s-asxpio-main` to their fsn1 twins daily around 06:10 UTC. A run that would delete over 1000 objects stops and mails instead. diff --git a/nixos/README.md b/nixos/README.md index 71508e5..22209a8 100644 --- a/nixos/README.md +++ b/nixos/README.md @@ -1,7 +1,7 @@ # NixOS Each server's whole system, as one flake. A server is created by OpenTofu -(`../terraform`), converts itself to NixOS on first boot, and gets its +(`../tofu`, OpenTofu), converts itself to NixOS on first boot, and gets its configuration from here. Production is moving over from `../ansible`; the plan and its state are in `../MIGRATION.md`. @@ -56,7 +56,7 @@ Try anything risky on the rehearsal pair first (below). ## Building the servers from scratch What you need: a Hetzner Cloud project and its API token, Object Storage -keys (hel1 and fsn1), an SSH key listed in `../terraform/keys.tf`, and, +keys (hel1 and fsn1), an SSH key listed in `../tofu/keys.tofu`, and, to restore data, the restic password (`../RESTORE.md`). The existing servers are billed below today's list price, and a new @@ -67,8 +67,8 @@ place" below**; this section is for a new project or a lost server. servers): ```sh - cd terraform - cp terraform.tfvars.example terraform.tfvars # fill in the token and keys + cd tofu + cp secrets.auto.tfvars.example secrets.auto.tfvars # fill in the token and keys export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # for the state bucket tofu init tofu apply @@ -79,7 +79,7 @@ place" below**; this section is for a new project or a lost server. `tofu init -migrate-state`. 2. **Wait about four minutes.** Each server boots Debian, and cloud-init - (`../terraform/cloud-init/nixos.yaml.tftpl`) runs a pinned + (`../tofu/cloud-init/nixos.yaml.tftpl`) runs a pinned nixos-infect, which installs NixOS over the running system and reboots. The result is a bare NixOS: root login with the keys from `keys.tf`, public and private network, nothing else. @@ -126,7 +126,7 @@ place" below**; this section is for a new project or a lost server. Keeps the server, its addresses and its price; **wipes the disk**. ```sh -cd terraform +cd tofu hcloud server disable-protection asxpio-services rebuild tofu output -json user_data | jq -r '."asxpio-services"' | hcloud server rebuild --image debian-13 --user-data-from-file - asxpio-services @@ -174,8 +174,8 @@ Two servers on a private network with the production addresses: ```sh hcloud network create --name rehearsal --ip-range 10.10.0.0/20 hcloud network add-subnet rehearsal --type cloud --network-zone eu-central --ip-range 10.10.0.0/23 -tofu -chdir=../terraform output -json user_data | jq -r '."asxpio-ops"' > /tmp/ops.yaml -tofu -chdir=../terraform output -json user_data | jq -r '."asxpio-services"' > /tmp/services.yaml +tofu -chdir=../tofu output -json user_data | jq -r '."asxpio-ops"' > /tmp/ops.yaml +tofu -chdir=../tofu output -json user_data | jq -r '."asxpio-services"' > /tmp/services.yaml hcloud server create --name rehearsal-ops --type cx23 --image debian-13 --location hel1 \ --ssh-key --network rehearsal --user-data-from-file /tmp/ops.yaml hcloud server create --name rehearsal-services --type cx23 --image debian-13 --location hel1 \ @@ -199,7 +199,7 @@ carries production credentials. ## How the first boot works, and its limits - nixos-infect is pinned by commit and checksum in - `../terraform/servers.tf`; it replaces the operating system, so bump it + `../tofu/servers.tofu`; it replaces the operating system, so bump it deliberately and rehearse. - It keeps the Debian image's partitions (`modules/hetzner.nix` matches them) and moves the old system to `/old-root`. diff --git a/nixos/hosts/asxpio-services/replica.nix b/nixos/hosts/asxpio-services/replica.nix index e23686d..ffd3ccc 100644 --- a/nixos/hosts/asxpio-services/replica.nix +++ b/nixos/hosts/asxpio-services/replica.nix @@ -14,8 +14,12 @@ let exclude = [ "restic/**" ]; } { - src = "asxpio-terraform-main"; - dst = "asxpio-terraform-repl"; + src = "asxpio-tofu-main"; # OpenTofu state + dst = "asxpio-tofu-repl"; + } + { + src = "asxpio-loki-main"; # Loki's chunks + dst = "asxpio-loki-repl"; } { src = "k8s-asxpio-main"; diff --git a/terraform/s3.tf b/terraform/s3.tf deleted file mode 100644 index 399cd7f..0000000 --- a/terraform/s3.tf +++ /dev/null @@ -1,23 +0,0 @@ -resource "minio_s3_bucket" "terraform_main" { - provider = minio.hel1 - bucket = "asxpio-terraform-main" - acl = "private" -} - -resource "minio_s3_bucket" "terraform_repl" { - provider = minio.fsn1 - bucket = "asxpio-terraform-repl" - acl = "private" -} - -resource "minio_s3_bucket" "backups_main" { - provider = minio.hel1 - bucket = "asxpio-backups-main" - acl = "private" -} - -resource "minio_s3_bucket" "backups_repl" { - provider = minio.fsn1 - bucket = "asxpio-backups-repl" - acl = "private" -} diff --git a/terraform/.gitignore b/tofu/.gitignore similarity index 63% rename from terraform/.gitignore rename to tofu/.gitignore index 4ed7134..40ad755 100644 --- a/terraform/.gitignore +++ b/tofu/.gitignore @@ -1,22 +1,22 @@ -# Terraform state files +# OpenTofu state files (the state itself is in Object Storage) *.tfstate *.tfstate.* *.tfstate.backup -# Terraform plan files +# Plan files tfplan *.tfplan plan.txt -# Terraform variable files with secrets -terraform.tfvars +# Variable files with secrets (secrets.auto.tfvars) +*.tfvars *.auto.tfvars -# Terraform CLI configuration +# CLI configuration .terraformrc terraform.rc -# Terraform directories (the lock file is committed: it pins the providers) +# Provider cache (the lock file is committed: it pins the providers) .terraform/ # Crash log files diff --git a/terraform/.terraform.lock.hcl b/tofu/.terraform.lock.hcl similarity index 100% rename from terraform/.terraform.lock.hcl rename to tofu/.terraform.lock.hcl diff --git a/terraform/README.md b/tofu/README.md similarity index 66% rename from terraform/README.md rename to tofu/README.md index 3cb9f6a..57dc5a4 100644 --- a/terraform/README.md +++ b/tofu/README.md @@ -2,8 +2,9 @@ Hetzner Cloud infrastructure, managed with [OpenTofu](https://opentofu.org) (`tofu`; the dev shell of `../nixos` has it). The files are plain HCL and -the state is the one Terraform wrote; `.terraform.lock.hcl` pins the -providers and is committed. +the state lives in Object Storage (`asxpio-tofu-main`, key +`tofu/state/tofu.tfstate`); `.terraform.lock.hcl` (OpenTofu keeps that +name) pins the providers and is committed. Files end in `.tofu`. ## Servers @@ -26,21 +27,25 @@ place; both procedures are in `../nixos/README.md`. ## Storage (S3-compatible) -**Terraform State:** -- asxpio-terraform-main (hel1) -- asxpio-terraform-repl (fsn1) +**OpenTofu state:** +- asxpio-tofu-main (hel1), replicated daily to asxpio-tofu-repl (fsn1) +- asxpio-terraform-main / -repl: where the state lived until 2026-11-17; + kept for now (`legacy_state_*`), to be emptied and removed **Backups:** -- asxpio-backups-main (hel1) -- asxpio-backups-repl (fsn1) +- asxpio-backups-main (hel1), replicated to asxpio-backups-repl (fsn1), + except `restic/` (restic writes both itself) **Logs:** -- asxpio-loki-main (hel1): Loki chunks; created by hand, not in Terraform +- asxpio-loki-main (hel1): Loki's chunks, replicated to asxpio-loki-repl + (fsn1) +The daily replica runs on asxpio-services +(`nixos/hosts/asxpio-services/replica.nix`). ## DNS -`dns.tf` holds the zones asxp.io and czsk.it (Hetzner DNS, this project) +`dns.tofu` holds the zones asxp.io and czsk.it (Hetzner DNS, this project) and every record set except SOA and NS. Our servers' records reference `hcloud_server`, so moving a service is one edit. @@ -48,7 +53,7 @@ All record sets are change-protected except the test XMPP servers' (`prosody`, `ejabberd`, `anotherim`); both zones are delete-protected. To change a protected record set, apply twice: first with the set excluded from protection, then with the new value. Hand edits in the -console show up as drift in `terraform plan`. +console show up as drift in `tofu plan`. ## Network @@ -77,7 +82,7 @@ delete protection guard against it. ## Setup ```bash -cp terraform.tfvars.example terraform.tfvars # token and Object Storage keys +cp secrets.auto.tfvars.example secrets.auto.tfvars # token and Object Storage keys export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # state bucket tofu init tofu plan @@ -85,18 +90,16 @@ tofu apply ``` In a new project the state bucket does not exist yet: comment out the -`backend "s3"` block in `providers.tf` for the first apply, restore it, +`backend "s3"` block in `providers.tofu` for the first apply, restore it, then `tofu init -migrate-state`. -Coming from Terraform: `tofu init -reconfigure` once; nothing in the -state changes. ## Files -- `servers.tf` - Server definitions, cloud-init variables -- `s3.tf` - S3 buckets (6 total) -- `vpc.tf` - Private network -- `firewall.tf` - Security rules -- `dns.tf` - DNS zones and records -- `keys.tf` - SSH keys +- `servers.tofu` - Server definitions, cloud-init variables +- `s3.tofu` - Object Storage buckets +- `vpc.tofu` - Private network +- `firewall.tofu` - Security rules +- `dns.tofu` - DNS zones and records +- `keys.tofu` - SSH keys - `cloud-init/nixos.yaml.tftpl` - first boot: Debian to NixOS diff --git a/terraform/cloud-init/nixos.yaml.tftpl b/tofu/cloud-init/nixos.yaml.tftpl similarity index 100% rename from terraform/cloud-init/nixos.yaml.tftpl rename to tofu/cloud-init/nixos.yaml.tftpl diff --git a/terraform/dns.tf b/tofu/dns.tofu similarity index 100% rename from terraform/dns.tf rename to tofu/dns.tofu diff --git a/terraform/firewall.tf b/tofu/firewall.tofu similarity index 100% rename from terraform/firewall.tf rename to tofu/firewall.tofu diff --git a/terraform/keys.tf b/tofu/keys.tofu similarity index 100% rename from terraform/keys.tf rename to tofu/keys.tofu diff --git a/terraform/outputs.tf b/tofu/outputs.tofu similarity index 100% rename from terraform/outputs.tf rename to tofu/outputs.tofu diff --git a/terraform/providers.tf b/tofu/providers.tofu similarity index 85% rename from terraform/providers.tf rename to tofu/providers.tofu index 7c61e65..30f8695 100644 --- a/terraform/providers.tf +++ b/tofu/providers.tofu @@ -2,10 +2,11 @@ terraform { required_version = ">= 1.8.0" - # S3 backend - comment this section if bucket not yet created + # State in Object Storage (bucket in s3.tofu). In a new project the + # bucket does not exist yet: see README.md, Setup. backend "s3" { - bucket = "asxpio-terraform-main" - key = "terraform/state/terraform.tfstate" + bucket = "asxpio-tofu-main" + key = "tofu/state/tofu.tfstate" region = "hel1" endpoint = "https://hel1.your-objectstorage.com" skip_credentials_validation = true diff --git a/tofu/s3.tofu b/tofu/s3.tofu new file mode 100644 index 0000000..7e349c7 --- /dev/null +++ b/tofu/s3.tofu @@ -0,0 +1,67 @@ +# OpenTofu state (backend in providers.tofu), replicated hel1 -> fsn1 +resource "minio_s3_bucket" "state_main" { + provider = minio.hel1 + bucket = "asxpio-tofu-main" + acl = "private" +} + +resource "minio_s3_bucket" "state_repl" { + provider = minio.fsn1 + bucket = "asxpio-tofu-repl" + acl = "private" +} + +# Where the state lived until 2026-11-17 (named after Terraform). Kept +# until the migrated state has been used for a while, then emptied and +# removed. +moved { + from = minio_s3_bucket.terraform_main + to = minio_s3_bucket.legacy_state_main +} +moved { + from = minio_s3_bucket.terraform_repl + to = minio_s3_bucket.legacy_state_repl +} + +resource "minio_s3_bucket" "legacy_state_main" { + provider = minio.hel1 + bucket = "asxpio-terraform-main" + acl = "private" +} + +resource "minio_s3_bucket" "legacy_state_repl" { + provider = minio.fsn1 + bucket = "asxpio-terraform-repl" + acl = "private" +} + +resource "minio_s3_bucket" "backups_main" { + provider = minio.hel1 + bucket = "asxpio-backups-main" + acl = "private" +} + +resource "minio_s3_bucket" "backups_repl" { + provider = minio.fsn1 + bucket = "asxpio-backups-repl" + acl = "private" +} + +# Loki's chunks (asxpio-ops, services.loki). Created by hand on +# 2026-11-14, adopted here on 2026-11-17. +import { + to = minio_s3_bucket.loki_main + id = "asxpio-loki-main" +} + +resource "minio_s3_bucket" "loki_main" { + provider = minio.hel1 + bucket = "asxpio-loki-main" + acl = "private" +} + +resource "minio_s3_bucket" "loki_repl" { + provider = minio.fsn1 + bucket = "asxpio-loki-repl" + acl = "private" +} diff --git a/terraform/terraform.tfvars.example b/tofu/secrets.auto.tfvars.example similarity index 100% rename from terraform/terraform.tfvars.example rename to tofu/secrets.auto.tfvars.example diff --git a/terraform/servers.tf b/tofu/servers.tofu similarity index 98% rename from terraform/servers.tf rename to tofu/servers.tofu index c0a5087..a997675 100644 --- a/terraform/servers.tf +++ b/tofu/servers.tofu @@ -52,7 +52,7 @@ resource "hcloud_server" "ops" { labels = { role = "ops" env = "production" - managed_by = "terraform" + managed_by = "opentofu" project = "asxpio" } @@ -100,7 +100,7 @@ resource "hcloud_server" "services" { labels = { role = "services" env = "production" - managed_by = "terraform" + managed_by = "opentofu" project = "asxpio" } diff --git a/terraform/variables.tf b/tofu/variables.tofu similarity index 100% rename from terraform/variables.tf rename to tofu/variables.tofu diff --git a/terraform/vpc.tf b/tofu/vpc.tofu similarity index 100% rename from terraform/vpc.tf rename to tofu/vpc.tofu