Fediversity/deployment/run/tf-proxmox/main.tf
2025-10-11 14:23:02 +02:00

337 lines
9.5 KiB
HCL

terraform {
required_providers {
proxmox = {
source = "bpg/proxmox"
version = "= 0.81.0"
}
}
}
locals {
dump_name = "qemu-nixos-fediversity-${var.category}.qcow2"
}
# https://registry.terraform.io/providers/bpg/proxmox/latest/docs
provider "proxmox" {
endpoint = "https://${var.host}:8006/"
insecure = true
# used only for files and creating custom disks
ssh {
agent = true
# uncomment and configure if using api_token instead of password
username = "root"
# node {
# name = "${var.node_name}"
# address = "${var.host}"
# # port = 22
# }
}
# # Choose one authentication method:
# api_token = var.virtual_environment_api_token
# # OR
username = var.proxmox_user
password = var.proxmox_password
# # OR
# auth_ticket = var.virtual_environment_auth_ticket
# csrf_prevention_token = var.virtual_environment_csrf_prevention_token
}
# # FIXME move to host
# # FIXME add proxmox
# data "external" "base-hash" {
# program = ["sh", "-c", "echo \"{\\\"hash\\\":\\\"$(nix-hash ${path.module}/../common/nixos/base.nix)\\\"}\""]
# }
# # 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 ..)\\\"}\""]
# }
# FIXME handle known-hosts in TF state
# FIXME move to host
# FIXME switch to base image shared between jobs as upload seems a bottleneck? e.g. by:
# - recursive TF
# - hash in name over overwrite
# won't notice file changes: https://github.com/bpg/terraform-provider-proxmox/issues/677
resource "proxmox_virtual_environment_file" "upload" {
# # https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts
# timeouts {
# create = "60m"
# }
# content_type - (Optional) The content type. If not specified, the content type will be inferred from the file extension. Valid values are:
# backup (allowed extensions: .vzdump, .tar.gz, .tar.xz, tar.zst)
# iso (allowed extensions: .iso, .img)
# snippets (allowed extensions: any)
# import (allowed extensions: .raw, .qcow2, .vmdk)
# vztmpl (allowed extensions: .tar.gz, .tar.xz, tar.zst)
# content_type = "backup"
content_type = "import"
# https://192.168.51.81:8006/#v1:0:=storage%2Fnode051%2Flocal:4::=contentIso:::::
# PVE -> Datacenter -> Storage -> local -> Edit -> General -> Content -> check Import + Disk Images -> OK
# that UI action also adds it in `/etc/pve/storage.cfg`
datastore_id = "local"
# datastore_id = "local-lvm"
# datastore_id = "backup"
node_name = var.node_name
overwrite = true
timeout_upload = 3600
# timeout_upload = 1
source_file {
# path = "/tmp/proxmox-image/${local.dump_name}"
path = var.image
file_name = local.dump_name
}
}
# resource "proxmox_virtual_environment_download_file" "latest_ubuntu_22_jammy_qcow2_img" {
# content_type = "import"
# datastore_id = "local"
# node_name = var.node_name
# url = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
# # need to rename the file to *.qcow2 to indicate the actual file format for import
# file_name = "jammy-server-cloudimg-amd64.qcow2"
# }
resource "proxmox_virtual_environment_vm" "nix_vm" {
lifecycle {
# wait, would this not disseminate any changes to this property,
# or just defer syncing when only this changed?
ignore_changes = [
disk["import_from"],
]
}
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 = "5m"
timeout = "40s"
trim = true
}
cpu {
type = "x86-64-v2-AES"
cores = var.cores
sockets = var.sockets
numa = true
}
memory {
dedicated = var.memory
}
disk {
# datastore_id = "linstor_storage"
datastore_id = "local"
file_format = "qcow2"
interface = "scsi0"
discard = "on"
iothread = true
size = var.disk_size
ssd = true
backup = false
cache = "none"
# FIXME make the provider allow this as a distinct block to allow making this depend on VM id?
# import_from = "local:import/${proxmox_virtual_environment_vm.nix_vm.vm_id}-${local.dump_name}" # bogus import name to test if it would accept self-referential values here # may not refer to itself
# import_from = "local:import/${local.dump_name}"
import_from = proxmox_virtual_environment_file.upload.id
# import_from = proxmox_virtual_environment_download_file.latest_ubuntu_22_jammy_qcow2_img.id
# import_from = "local:import/jammy-server-cloudimg-amd64.qcow2"
}
efi_disk {
# datastore_id = "linstor_storage"
datastore_id = "local"
file_format = "qcow2"
type = "4m"
}
network_device {
model = "virtio"
bridge = "ovsbr0"
vlan_id = 1305
}
operating_system {
type = "l26"
}
scsi_hardware = "virtio-scsi-single"
bios = "ovmf"
# # used only for cloud-init
# initialization {
# ip_config {
# ipv4 {
# gateway = "eth0"
# address = "95.215.187.${proxmox_virtual_environment_vm.nix_vm.vm_id}" # error: self-referential block
# }
# ipv6 {
# gateway = "eth0"
# address = "2a00:51c0:13:1305::${proxmox_virtual_environment_vm.nix_vm.vm_id}"
# }
# }
# }
}
# FIXME expose (and handle thru) [`exec`](https://pve.proxmox.com/pve-docs/api-viewer/#/nodes/{node}/qemu/{vmid}/agent/exec) endpoint in proxmox TF provider? wait, what command would i use it for?: https://github.com/bpg/terraform-provider-proxmox/issues/1576
module "nixos-rebuild" {
source = "../tf-single-host"
nixos_conf = var.nixos_conf
# username = var.ssh_user # refers to the proxmox ssh user, not the VM one
username = "root"
# host = proxmox_virtual_environment_vm.nix_vm.ipv4_addresses[1][0] # does not exist (in time)
host = "95.215.187.${proxmox_virtual_environment_vm.nix_vm.vm_id}"
# host = "2a00:51c0:13:1305::${proxmox_virtual_environment_vm.nix_vm.vm_id}"
# host = "95.215.187.101"
# host = "2a00:51c0:13:1305::101"
key_file = var.key_file
ssh_opts = var.ssh_opts
}
# vm output: {
# "acpi" = true
# "agent" = tolist([
# {
# "enabled" = true
# "timeout" = "15m"
# "trim" = false
# "type" = "virtio"
# },
# ])
# "amd_sev" = tolist([])
# "audio_device" = tolist([])
# "bios" = "ovmf"
# "boot_order" = tolist(null) /* of string */
# "cdrom" = tolist([])
# "clone" = tolist([])
# "cpu" = tolist([
# {
# "affinity" = ""
# "architecture" = ""
# "cores" = 1
# "flags" = tolist(null) /* of string */
# "hotplugged" = 0
# "limit" = 0
# "numa" = true
# "sockets" = 1
# "type" = "x86-64-v2-AES"
# "units" = 1024
# },
# ])
# "description" = ""
# "disk" = tolist([
# {
# "aio" = "io_uring"
# "backup" = false
# "cache" = "none"
# "datastore_id" = "local"
# "discard" = "on"
# "file_format" = "qcow2"
# "file_id" = ""
# "import_from" = "local:import/qemu-nixos-fediversity-test.qcow2"
# "interface" = "scsi0"
# "iothread" = true
# "path_in_datastore" = "101/vm-101-disk-1.qcow2"
# "replicate" = true
# "serial" = ""
# "size" = 32
# "speed" = tolist([])
# "ssd" = true
# },
# ])
# "efi_disk" = tolist([
# {
# "datastore_id" = "local"
# "file_format" = "qcow2"
# "pre_enrolled_keys" = false
# "type" = "4m"
# },
# ])
# "hook_script_file_id" = tostring(null)
# "hostpci" = tolist([])
# "id" = "101"
# "initialization" = tolist([])
# "ipv4_addresses" = tolist([])
# "ipv6_addresses" = tolist([])
# "keyboard_layout" = "en-us"
# "kvm_arguments" = ""
# "mac_addresses" = tolist([])
# "machine" = ""
# "memory" = tolist([
# {
# "dedicated" = 2048
# "floating" = 0
# "hugepages" = ""
# "keep_hugepages" = false
# "shared" = 0
# },
# ])
# "migrate" = false
# "name" = ""
# "network_device" = tolist([
# {
# "bridge" = "vnet1306"
# "disconnected" = false
# "enabled" = true
# "firewall" = false
# "mac_address" = "BC:24:11:DE:E5:A8"
# "model" = "virtio"
# "mtu" = 0
# "queues" = 0
# "rate_limit" = 0
# "trunks" = ""
# "vlan_id" = 0
# },
# ])
# "network_interface_names" = tolist([])
# "node_name" = "node051"
# "numa" = tolist([])
# "on_boot" = true
# "operating_system" = tolist([
# {
# "type" = "l26"
# },
# ])
# "pool_id" = "Fediversity"
# "protection" = false
# "reboot" = false
# "reboot_after_update" = true
# "rng" = tolist([])
# "scsi_hardware" = "virtio-scsi-single"
# "serial_device" = tolist([])
# "smbios" = tolist([])
# "started" = true
# "startup" = tolist([])
# "stop_on_destroy" = false
# "tablet_device" = true
# "tags" = tolist(null) /* of string */
# "template" = false
# "timeout_clone" = 1800
# "timeout_create" = 1800
# "timeout_migrate" = 1800
# "timeout_move_disk" = 1800
# "timeout_reboot" = 1800
# "timeout_shutdown_vm" = 1800
# "timeout_start_vm" = 1800
# "timeout_stop_vm" = 300
# "tpm_state" = tolist([])
# "usb" = tolist([])
# "vga" = tolist([])
# "virtiofs" = tolist([])
# "vm_id" = 101
# "watchdog" = tolist([])
# }
output "ips" {
value = proxmox_virtual_environment_vm.nix_vm
}