195 lines
5.0 KiB
Terraform
195 lines
5.0 KiB
Terraform
# prod: Traefik, Websites, Bitwarden, RustDesk, status page
|
|
|
|
# Port sources: services/prod/*docker-compose.yml (published ports) and services/todo.md
|
|
# (the documented UFW allow-list).
|
|
resource "hcloud_firewall" "this" {
|
|
name = "prod-firewall"
|
|
|
|
rule { # server ssh - wired to the vpn server's public IP only, see infra/main.tf
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "22"
|
|
source_ips = var.allowed_ssh_source_ips
|
|
}
|
|
|
|
rule { # Traefik http/https (Websites, Bitwarden, status page)
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "80"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule {
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "443"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule { # rustdesk hbbs
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "21115"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule {
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "21116"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule {
|
|
direction = "in"
|
|
protocol = "udp"
|
|
port = "21116"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule { # rustdesk hbbr
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "21117"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule {
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "21119"
|
|
source_ips = ["0.0.0.0/0", "::/0"]
|
|
}
|
|
|
|
rule { # traffic from dev over the private network
|
|
direction = "in"
|
|
protocol = "tcp"
|
|
port = "1-65535"
|
|
source_ips = [var.network_ip_range]
|
|
}
|
|
|
|
rule {
|
|
direction = "in"
|
|
protocol = "udp"
|
|
port = "1-65535"
|
|
source_ips = [var.network_ip_range]
|
|
}
|
|
}
|
|
|
|
resource "hcloud_server" "this" {
|
|
name = "prod"
|
|
server_type = var.server_type
|
|
image = var.image
|
|
location = var.location
|
|
ssh_keys = var.ssh_key_ids
|
|
firewall_ids = [hcloud_firewall.this.id]
|
|
|
|
network {
|
|
network_id = var.network_id
|
|
ip = var.private_ip
|
|
}
|
|
|
|
connection {
|
|
type = "ssh"
|
|
host = self.ipv4_address
|
|
user = "root"
|
|
private_key = file(var.ssh_private_key_path)
|
|
}
|
|
|
|
provisioner "file" {
|
|
content = var.bootstrap_script
|
|
destination = "/root/bootstrap.sh"
|
|
}
|
|
|
|
provisioner "remote-exec" {
|
|
inline = [
|
|
"chmod +x /root/bootstrap.sh",
|
|
"/root/bootstrap.sh",
|
|
]
|
|
}
|
|
}
|
|
|
|
resource "hcloud_volume" "storage" {
|
|
name = "prod-storage"
|
|
size = var.volume_size
|
|
server_id = hcloud_server.this.id
|
|
automount = true
|
|
format = "ext4"
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
locals {
|
|
# Hetzner's automount convention (hc-utils' 99-hc-volume-automount.rules) is
|
|
# always /mnt/HC_Volume_<id> - deterministic once the volume exists, since the
|
|
# id is stable for the volume's lifetime regardless of which server it's
|
|
# attached to.
|
|
data_dir = "/mnt/HC_Volume_${hcloud_volume.storage.id}"
|
|
}
|
|
|
|
# DATA_DIR is picked up automatically by `docker compose` (which auto-loads
|
|
# .env from its working directory) for every prod/*-docker-compose.yml bind
|
|
# mount - see services/prod/*.yml. Not committed (see .gitignore): it's tied to
|
|
# the live volume's ID, so it's regenerated by tofu apply, not handed off via git.
|
|
resource "local_file" "env" {
|
|
filename = "${path.root}/../services/prod/.env"
|
|
content = "DATA_DIR=${local.data_dir}\n"
|
|
file_permission = "0644"
|
|
}
|
|
|
|
locals {
|
|
# Everything under services/prod except .env: that's tracked via its own
|
|
# resource content (reading it back with fileset()/filesha1() before it
|
|
# exists would fail during `tofu plan`).
|
|
prod_static_files = sort([
|
|
for f in fileset("${path.root}/../services/prod", "**") : f if f != ".env"
|
|
])
|
|
|
|
prod_static_files_hash = sha1(join("", [
|
|
for f in local.prod_static_files : filesha1("${path.root}/../services/prod/${f}")
|
|
]))
|
|
}
|
|
|
|
# Copies services/prod to the server on every apply that changes it (a
|
|
# hand-edited compose file, ...) or recreates the server - not just once at
|
|
# first creation. Split from hcloud_server.this because it must run after
|
|
# local_file.env, which in turn depends on the volume, which depends on the
|
|
# server - so it can't be a provisioner on the server resource itself.
|
|
resource "null_resource" "deploy_services" {
|
|
triggers = {
|
|
server_id = hcloud_server.this.id
|
|
static_files = local.prod_static_files_hash
|
|
env = local_file.env.content
|
|
}
|
|
|
|
connection {
|
|
type = "ssh"
|
|
host = hcloud_server.this.ipv4_address
|
|
user = "root"
|
|
private_key = file(var.ssh_private_key_path)
|
|
}
|
|
|
|
provisioner "remote-exec" {
|
|
inline = ["mkdir -p /home/${var.deploy_user}/services"]
|
|
}
|
|
|
|
provisioner "file" {
|
|
source = "${path.root}/../services/prod"
|
|
destination = "/home/${var.deploy_user}/services"
|
|
}
|
|
|
|
provisioner "remote-exec" {
|
|
inline = [
|
|
"chown -R ${var.deploy_user}:${var.deploy_user} /home/${var.deploy_user}/services",
|
|
"chmod +x /home/${var.deploy_user}/services/prod/*.sh",
|
|
]
|
|
}
|
|
|
|
depends_on = [
|
|
hcloud_server.this,
|
|
local_file.env,
|
|
]
|
|
}
|