constellation/terraform/infrastructure/qemu/modules/instance_group/main.tf

100 lines
2.7 KiB
Terraform
Raw Normal View History

terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.6"
}
random = {
source = "hashicorp/random"
version = "3.6.2"
}
}
}
resource "libvirt_domain" "instance_group" {
2022-10-19 07:10:15 -04:00
count = var.amount
name = "${var.base_name}-${var.role}-${local.group_uid}-${count.index}"
2022-10-19 07:10:15 -04:00
memory = var.memory
vcpu = var.vcpus
machine = var.machine
2023-05-16 08:13:10 -04:00
firmware = local.firmware
dynamic "cpu" {
for_each = var.boot_mode == "direct-linux-boot" ? [1] : []
content {
mode = "host-passthrough"
}
}
2023-05-16 08:13:10 -04:00
dynamic "nvram" {
for_each = var.boot_mode == "uefi" ? [1] : []
content {
file = "/var/lib/libvirt/qemu/nvram/${var.role}-${count.index}_VARS.fd"
template = var.nvram
}
}
xml {
xslt = file("${path.module}/${local.xslt_filename}")
2022-10-19 07:10:15 -04:00
}
2023-05-16 08:13:10 -04:00
kernel = local.kernel
initrd = local.initrd
cmdline = local.cmdline
tpm {
backend_type = "emulator"
backend_version = "2.0"
}
disk {
volume_id = element(libvirt_volume.boot_volume.*.id, count.index)
}
disk {
volume_id = element(libvirt_volume.state_volume.*.id, count.index)
}
network_interface {
network_id = var.network_id
hostname = "${var.role}-${count.index}"
addresses = [cidrhost(var.cidr, local.ip_range_start + count.index)]
wait_for_lease = true
}
console {
type = "pty"
target_port = "0"
}
}
resource "libvirt_volume" "boot_volume" {
count = var.amount
name = "constellation-${var.role}-${local.group_uid}-${count.index}-boot"
pool = var.pool
base_volume_id = var.boot_volume_id
lifecycle {
ignore_changes = [
name, # required. Allow legacy scale sets to keep their old names
]
}
}
resource "libvirt_volume" "state_volume" {
count = var.amount
name = "constellation-${var.role}-${local.group_uid}-${count.index}-state"
pool = var.pool
size = local.state_disk_size_byte
format = "qcow2"
lifecycle {
ignore_changes = [
name, # required. Allow legacy scale sets to keep their old names
]
}
}
resource "random_id" "uid" {
byte_length = 4
}
2023-05-16 08:13:10 -04:00
locals {
group_uid = random_id.uid.hex
2023-05-16 08:13:10 -04:00
state_disk_size_byte = 1073741824 * var.state_disk_size
ip_range_start = 100
kernel = var.boot_mode == "direct-linux-boot" ? var.kernel_volume_id : null
initrd = var.boot_mode == "direct-linux-boot" ? var.initrd_volume_id : null
cmdline = var.boot_mode == "direct-linux-boot" ? [{ "_" = var.kernel_cmdline }] : null
firmware = var.boot_mode == "uefi" ? var.firmware : null
xslt_filename = var.boot_mode == "direct-linux-boot" ? "tdx_domain.xsl" : "domain.xsl"
2023-05-16 08:13:10 -04:00
}