Fediversity/deployment/run/tf-proxmox-vm/main.tf
Kiara Grouwstra 23bcca8e67
clean out comments
Signed-off-by: Kiara Grouwstra <kiara@procolix.eu>
2025-10-25 19:41:12 +02:00

138 lines
3 KiB
HCL

terraform {
required_providers {
proxmox = {
source = "bpg/proxmox"
version = "= 0.81.0"
}
}
backend "http" {
}
}
# https://registry.terraform.io/providers/bpg/proxmox/latest/docs
provider "proxmox" {
endpoint = "https://${var.host}:8006/"
# used only for files and creating custom disks
# FIXME handle known-hosts in TF state
ssh {
agent = true
username = "root"
}
}
# hash of our code directory, used to trigger re-deploy
# FIXME calculate separately to reduce false positives
data "external" "hash" {
program = ["sh", "-c", "echo \"{\\\"hash\\\":\\\"$(nix-hash ../../..)\\\"}\""]
}
resource "proxmox_virtual_environment_vm" "nix_vm" {
lifecycle {
ignore_changes = [
disk["import_from"],
initialization,
]
}
node_name = var.node_name
pool_id = var.pool_id
description = var.description
started = true
# https://wiki.nixos.org/wiki/Virt-manager#Guest_Agent
agent {
enabled = true
timeout = "2m"
trim = true
}
cpu {
type = "x86-64-v2-AES"
cores = var.cores
sockets = var.sockets
numa = true
}
memory {
dedicated = var.memory
}
disk {
datastore_id = var.vm_datastore_id
file_format = "qcow2"
interface = "scsi0"
discard = "on"
iothread = true
size = var.disk_size
ssd = true
backup = false
cache = "none"
import_from = var.template_id
}
efi_disk {
datastore_id = var.vm_datastore_id
file_format = "qcow2"
type = "4m"
}
network_device {
model = "virtio"
bridge = var.bridge
vlan_id = var.vlan_id
}
operating_system {
type = "l26"
}
scsi_hardware = "virtio-scsi-single"
bios = "ovmf"
initialization {
datastore_id = var.cd_datastore_id
interface = "sata2"
ip_config {
ipv4 {
gateway = var.ipv4_gateway
address = var.ipv4_address
}
ipv6 {
gateway = var.ipv6_gateway
address = var.ipv6_address
}
}
}
}
resource "null_resource" "await_ssh" {
depends_on = [
proxmox_virtual_environment_vm.nix_vm
]
provisioner "local-exec" {
command = "env username='root' host='${proxmox_virtual_environment_vm.nix_vm.ipv4_addresses[1][0]}' key_file='${var.key_file}' ssh_opts='${var.ssh_opts}' bash ./await-ssh.sh"
}
}
module "nixos-rebuild" {
depends_on = [
data.external.hash,
null_resource.await_ssh,
]
source = "../tf-single-host"
nixos_conf = var.nixos_conf
username = "root"
host = proxmox_virtual_environment_vm.nix_vm.ipv4_addresses[1][0]
key_file = var.key_file
ssh_opts = var.ssh_opts
}
output "id" {
value = proxmox_virtual_environment_vm.nix_vm.vm_id
}
output "ipv4" {
value = proxmox_virtual_environment_vm.nix_vm.ipv4_addresses[1]
}
output "ipv6" {
value = [ for elem in proxmox_virtual_environment_vm.nix_vm.ipv6_addresses[1] : "${elem}%${proxmox_virtual_environment_vm.nix_vm.network_interface_names[1]}" ]
}