2022-05-22 09:39:30 -04:00
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
libvirt = {
|
|
|
|
source = "dmacvicar/libvirt"
|
2024-06-27 03:14:10 -04:00
|
|
|
version = "0.7.6"
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
2023-06-28 08:42:34 -04:00
|
|
|
random = {
|
|
|
|
source = "hashicorp/random"
|
2024-06-27 03:14:10 -04:00
|
|
|
version = "3.6.2"
|
2023-06-28 08:42:34 -04:00
|
|
|
}
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resource "libvirt_domain" "instance_group" {
|
2022-10-19 07:10:15 -04:00
|
|
|
count = var.amount
|
2023-06-28 08:42:34 -04:00
|
|
|
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
|
2023-02-21 05:24:04 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 05:24:04 -05:00
|
|
|
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
|
2022-05-22 09:39:30 -04:00
|
|
|
tpm {
|
|
|
|
backend_type = "emulator"
|
|
|
|
backend_version = "2.0"
|
|
|
|
}
|
2022-10-18 05:24:43 -04:00
|
|
|
disk {
|
|
|
|
volume_id = element(libvirt_volume.boot_volume.*.id, count.index)
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
volume_id = element(libvirt_volume.state_volume.*.id, count.index)
|
|
|
|
}
|
2022-05-22 09:39:30 -04:00
|
|
|
network_interface {
|
|
|
|
network_id = var.network_id
|
|
|
|
hostname = "${var.role}-${count.index}"
|
2022-09-27 04:47:45 -04:00
|
|
|
addresses = [cidrhost(var.cidr, local.ip_range_start + count.index)]
|
2022-05-22 09:39:30 -04:00
|
|
|
wait_for_lease = true
|
|
|
|
}
|
|
|
|
console {
|
|
|
|
type = "pty"
|
|
|
|
target_port = "0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "libvirt_volume" "boot_volume" {
|
|
|
|
count = var.amount
|
2023-06-28 08:42:34 -04:00
|
|
|
name = "constellation-${var.role}-${local.group_uid}-${count.index}-boot"
|
2022-05-22 09:39:30 -04:00
|
|
|
pool = var.pool
|
|
|
|
base_volume_id = var.boot_volume_id
|
2023-06-28 08:42:34 -04:00
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [
|
|
|
|
name, # required. Allow legacy scale sets to keep their old names
|
|
|
|
]
|
|
|
|
}
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "libvirt_volume" "state_volume" {
|
|
|
|
count = var.amount
|
2023-06-28 08:42:34 -04:00
|
|
|
name = "constellation-${var.role}-${local.group_uid}-${count.index}-state"
|
2022-05-22 09:39:30 -04:00
|
|
|
pool = var.pool
|
|
|
|
size = local.state_disk_size_byte
|
|
|
|
format = "qcow2"
|
2023-06-28 08:42:34 -04:00
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [
|
|
|
|
name, # required. Allow legacy scale sets to keep their old names
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_id" "uid" {
|
|
|
|
byte_length = 4
|
2022-05-22 09:39:30 -04:00
|
|
|
}
|
2023-05-16 08:13:10 -04:00
|
|
|
|
|
|
|
locals {
|
2023-06-28 08:42:34 -04:00
|
|
|
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
|
2023-02-21 05:24:04 -05:00
|
|
|
xslt_filename = var.boot_mode == "direct-linux-boot" ? "tdx_domain.xsl" : "domain.xsl"
|
2023-05-16 08:13:10 -04:00
|
|
|
}
|