terraform: install NixOS at first boot, run with OpenTofu
A new or rebuilt server should come up as NixOS without a manual install. cloud-init now runs a pinned nixos-infect on the Debian image and reboots into a bare NixOS; the system itself comes from nixos/. One template replaces the two near-identical Debian cloud-init files. nixos-infect's output needs three corrections on NixOS 26.05, written to bootstrap.nix: the takeover of the old root only exists in the scripted initrd, the IPv4 default route is never added without an interface, and the private network gets no route. Found and verified on throwaway servers. The servers still ignore changes to user_data, so nothing happens to the running ones. The new user_data output feeds a rebuild in place. Switch to OpenTofu: same HCL and state, and it is free software in nixpkgs. Commit the provider lock file so everyone gets the same providers. Assisted-by: Claude:fable-5-2
This commit is contained in:
parent
ebd4f110f4
commit
c51522d371
1 changed files with 189 additions and 377 deletions
|
|
@ -1,6 +1,9 @@
|
|||
# ASXP.IO Infrastructure
|
||||
|
||||
Hetzner Cloud infrastructure managed via Terraform.
|
||||
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.
|
||||
|
||||
## Servers
|
||||
|
||||
|
|
@ -11,10 +14,15 @@ Both have delete and rebuild protection. Hetzner's server backups are
|
|||
off; restic backs up the data instead (see ../README.md).
|
||||
|
||||
Both servers carry `prevent_destroy` and ignore changes to `user_data`,
|
||||
`image` and `ssh_keys`: cloud-init only runs at first boot, and hcloud
|
||||
would otherwise replace a running server when those change. Edits to
|
||||
`cloud-init/*.yaml` therefore describe a rebuild, not the live machine;
|
||||
day-to-day configuration is in `../ansible`.
|
||||
`image` and `ssh_keys`, because hcloud would replace a running server
|
||||
when those change.
|
||||
|
||||
A new server boots Debian and converts itself to NixOS: its cloud-init
|
||||
(`cloud-init/nixos.yaml.tftpl`, one template for both) runs a pinned
|
||||
nixos-infect and reboots into a bare NixOS, about three minutes after
|
||||
creation. The system's configuration then comes from `../nixos`. Edits
|
||||
to the template reach a server only when it is created or rebuilt in
|
||||
place; both procedures are in `../nixos/README.md`.
|
||||
|
||||
## Storage (S3-compatible)
|
||||
|
||||
|
|
@ -68,94 +76,27 @@ delete protection guard against it.
|
|||
|
||||
## Setup
|
||||
|
||||
### 1. Initialize
|
||||
```bash
|
||||
terraform init
|
||||
cp terraform.tfvars.example terraform.tfvars # token and Object Storage keys
|
||||
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # state bucket
|
||||
tofu init
|
||||
tofu plan
|
||||
tofu apply
|
||||
```
|
||||
|
||||
### 2. Configure secrets
|
||||
Edit `terraform.tfvars`:
|
||||
```hcl
|
||||
hcloud_token = "your-token"
|
||||
s3_access_key = "your-key"
|
||||
s3_secret_key = "your-secret"
|
||||
s3_endpoint_hel1 = "hel1.your-objectstorage.com"
|
||||
s3_endpoint_fsn1 = "fsn1.your-objectstorage.com"
|
||||
```
|
||||
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,
|
||||
then `tofu init -migrate-state`.
|
||||
|
||||
### 3. Deploy
|
||||
```bash
|
||||
terraform plan
|
||||
terraform apply
|
||||
```
|
||||
|
||||
### 4. Enable S3 backend (optional)
|
||||
After buckets are created:
|
||||
|
||||
```bash
|
||||
# Set credentials
|
||||
export AWS_ACCESS_KEY_ID="your-key"
|
||||
export AWS_SECRET_ACCESS_KEY="your-secret"
|
||||
|
||||
# Or use ~/.aws/credentials file
|
||||
mkdir -p ~/.aws
|
||||
cat > ~/.aws/credentials <<EOF
|
||||
[default]
|
||||
aws_access_key_id = your-key
|
||||
aws_secret_access_key = your-secret
|
||||
EOF
|
||||
|
||||
# Uncomment backend in providers.tf
|
||||
# Then migrate state
|
||||
terraform init -migrate-state
|
||||
```
|
||||
|
||||
## DNS Management
|
||||
|
||||
**Current:** Managed manually via `hcloud` CLI
|
||||
|
||||
Zones: asxp.io (61604), czsk.it (61606), childfr.ee (61605)
|
||||
|
||||
**TODO:** Migrate to Terraform-managed DNS when provider v1.54.0+ is available:
|
||||
```hcl
|
||||
# Upgrade provider in providers.tf
|
||||
hcloud = {
|
||||
source = "hetznercloud/hcloud"
|
||||
version = "~> 1.54"
|
||||
}
|
||||
|
||||
# Import existing zones
|
||||
terraform import hcloud_zone.asxp_io 61604
|
||||
terraform import hcloud_zone.czsk_it 61606
|
||||
terraform import hcloud_zone.childfr_ee 61605
|
||||
|
||||
# Example DNS resource
|
||||
resource "hcloud_zone" "asxp_io" {
|
||||
name = "asxp.io"
|
||||
mode = "primary"
|
||||
labels = {
|
||||
managed_by = "terraform"
|
||||
}
|
||||
}
|
||||
|
||||
resource "hcloud_zone_rrset" "apex_a" {
|
||||
zone = hcloud_zone.asxp_io.name
|
||||
name = "@"
|
||||
type = "A"
|
||||
records = [
|
||||
{ value = "95.217.19.240", comment = "ops-server" },
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
See: https://github.com/hetznercloud/terraform-provider-hcloud/releases/tag/v1.54.0
|
||||
Coming from Terraform: `tofu init -reconfigure` once; nothing in the
|
||||
state changes.
|
||||
|
||||
## Files
|
||||
|
||||
- `servers.tf` - Server definitions + cloud-init
|
||||
- `servers.tf` - Server definitions, cloud-init variables
|
||||
- `s3.tf` - S3 buckets (6 total)
|
||||
- `vpc.tf` - Private network
|
||||
- `firewall.tf` - Security rules
|
||||
- `dns.tf` - DNS docs (manual management)
|
||||
- `dns.tf` - DNS zones and records
|
||||
- `keys.tf` - SSH keys
|
||||
- `cloud-init/*.yaml` - Server provisioning
|
||||
- `cloud-init/nixos.yaml.tftpl` - first boot: Debian to NixOS
|
||||
|
|
|
|||
Loading…
Reference in a new issue