2023-02-02 14:43:16 +01:00
|
|
|
variable "vms" {
|
2023-03-03 15:03:25 +01:00
|
|
|
type = map(object(
|
2023-03-03 14:59:50 +01:00
|
|
|
{
|
|
|
|
domain = string
|
|
|
|
cpu = number
|
|
|
|
vcpu = number
|
|
|
|
ram = number
|
|
|
|
system_image_persistent = bool
|
|
|
|
data_image_persistent = bool
|
|
|
|
data_disks = list(object({
|
|
|
|
name = string
|
|
|
|
size = string
|
|
|
|
persistent = bool
|
|
|
|
}))
|
|
|
|
network_interfaces = list(object({
|
|
|
|
network_id = string
|
|
|
|
ip = string
|
|
|
|
mac = string
|
|
|
|
}))
|
|
|
|
sysdatastore = string
|
|
|
|
}
|
|
|
|
))
|
2023-02-02 14:43:16 +01:00
|
|
|
description = "Virtual Machines description"
|
|
|
|
}
|
|
|
|
|
|
|
|
variable "image_format" {
|
|
|
|
default = "qcow2"
|
|
|
|
}
|
|
|
|
|
|
|
|
variable "dev_prefix" {
|
2023-02-27 14:13:58 +01:00
|
|
|
default = "sd"
|
2023-02-02 14:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
variable "permissions" {
|
|
|
|
default = "600"
|
|
|
|
}
|
|
|
|
|
|
|
|
variable "graphics" {
|
|
|
|
default = {
|
|
|
|
keymap = "fr"
|
|
|
|
listen = "0.0.0.0"
|
|
|
|
type = "VNC"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-02 10:26:50 +01:00
|
|
|
variable "one_image_ds" {
|
2023-02-02 14:43:16 +01:00
|
|
|
default = "101"
|
|
|
|
}
|
|
|
|
|
2023-02-24 06:03:44 +01:00
|
|
|
variable "one_sys_datastore" {
|
|
|
|
default = "100"
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:10:47 +01:00
|
|
|
locals {
|
|
|
|
data_disks = flatten([
|
|
|
|
for vm_key, vm in var.vms : [
|
|
|
|
for disk in vm.data_disks : {
|
2023-03-02 16:14:55 +01:00
|
|
|
vm_key = "${vm_key}.${vm.domain}"
|
|
|
|
name = disk.name
|
2023-03-02 16:10:47 +01:00
|
|
|
size = disk.size
|
|
|
|
persist = disk.persistent
|
|
|
|
}
|
|
|
|
]
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-02 14:43:16 +01:00
|
|
|
// system image
|
|
|
|
resource "opennebula_image" "system" {
|
|
|
|
for_each = var.vms
|
|
|
|
|
|
|
|
name = "system-${each.key}.${each.value.domain}"
|
|
|
|
description = "System disk for ${each.key}"
|
2023-03-02 10:26:50 +01:00
|
|
|
datastore_id = var.one_image_ds
|
2023-02-02 14:43:16 +01:00
|
|
|
path = "https://vulcain.cadoles.com/empty_20G"
|
|
|
|
persistent = each.value.system_image_persistent
|
|
|
|
format = var.image_format
|
|
|
|
dev_prefix = var.dev_prefix
|
|
|
|
permissions = var.permissions
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:10:47 +01:00
|
|
|
|
|
|
|
// data image not needed now
|
2023-02-02 14:43:16 +01:00
|
|
|
resource "opennebula_image" "data" {
|
2023-03-02 16:10:47 +01:00
|
|
|
for_each = {
|
|
|
|
for ds in local.data_disks : "${ds.vm_key}.${ds.name}" => ds
|
|
|
|
}
|
2023-02-02 14:43:16 +01:00
|
|
|
|
2023-03-02 16:16:02 +01:00
|
|
|
name = "${each.value.vm_key}-data-${each.value.name}"
|
2023-03-02 16:10:47 +01:00
|
|
|
path = "https://vulcain.cadoles.com/empty_${each.value.size}"
|
|
|
|
datastore_id = var.one_image_ds
|
|
|
|
description = "Data disk of ${each.value.size} for ${each.value.vm_key}"
|
|
|
|
persistent = each.value.persist
|
2023-02-02 14:43:16 +01:00
|
|
|
format = var.image_format
|
|
|
|
dev_prefix = var.dev_prefix
|
|
|
|
permissions = var.permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "opennebula_template" "vm_template" {
|
|
|
|
for_each = var.vms
|
|
|
|
|
2023-02-24 10:40:18 +01:00
|
|
|
name = "${each.key}.${each.value.domain}"
|
2023-02-02 14:43:16 +01:00
|
|
|
cpu = each.value.cpu
|
|
|
|
vcpu = each.value.vcpu
|
|
|
|
memory = each.value.ram
|
|
|
|
graphics {
|
|
|
|
keymap = var.graphics.keymap
|
|
|
|
listen = var.graphics.listen
|
|
|
|
type = var.graphics.type
|
|
|
|
}
|
|
|
|
context = {
|
|
|
|
SET_HOSTNAME = "$NAME"
|
|
|
|
DNS_HOSTNAME = "YES"
|
|
|
|
NETWORK = "YES"
|
|
|
|
SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]"
|
|
|
|
USERNAME = "root"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
image_id = opennebula_image.system[each.key].id
|
|
|
|
}
|
2023-02-27 14:13:58 +01:00
|
|
|
|
2023-03-02 17:13:08 +01:00
|
|
|
dynamic "disk" {
|
|
|
|
for_each = {
|
|
|
|
for ds in local.data_disks : "${ds.vm_key}.${ds.name}" => ds if "${each.key}.${each.value.domain}" == ds.vm_key
|
|
|
|
}
|
|
|
|
iterator = current_disk
|
|
|
|
content {
|
|
|
|
image_id = opennebula_image.data[current_disk.key].id
|
|
|
|
}
|
2023-02-02 14:43:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:20:52 +01:00
|
|
|
//sched_ds_requirements = ( each.value.sysdatastore != "" ? "ID=\"${each.value.sysdatastore}\"" : "ID=\"${var.one_sys_datastore}\"" )
|
|
|
|
sched_ds_requirements = ( each.value.sysdatastore != "" ? "ID=\"${each.value.sysdatastore}\"" : "" )
|
2023-02-24 06:03:44 +01:00
|
|
|
|
2023-02-02 14:43:16 +01:00
|
|
|
os {
|
|
|
|
arch = "x86_64"
|
|
|
|
boot = "disk0,nic0"
|
|
|
|
}
|
|
|
|
//labels = var.tag_labels
|
|
|
|
|
|
|
|
dynamic "nic" {
|
|
|
|
for_each = each.value.network_interfaces
|
|
|
|
|
|
|
|
content {
|
|
|
|
network_id = nic.value.network_id
|
|
|
|
model = "virtio"
|
2023-03-02 10:12:45 +01:00
|
|
|
mac = ( nic.value.mac != "" ? nic.value.mac : "" )
|
|
|
|
ip = ( nic.value.ip != "" ? nic.value.ip : "" )
|
2023-02-02 14:43:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
permissions = var.permissions
|
2023-02-27 14:16:49 +01:00
|
|
|
depends_on = [ opennebula_image.system ]
|
|
|
|
|
|
|
|
/* Not needed now
|
2023-02-02 14:43:16 +01:00
|
|
|
depends_on = [ opennebula_image.system,
|
|
|
|
opennebula_image.data ]
|
2023-02-27 14:16:49 +01:00
|
|
|
*/
|
2023-02-02 14:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "opennebula_virtual_machine" "vm" {
|
|
|
|
for_each = var.vms
|
|
|
|
name = "${each.key}.${each.value.domain}"
|
|
|
|
template_id = opennebula_template.vm_template[each.key].id
|
|
|
|
depends_on = [ opennebula_template.vm_template ]
|
|
|
|
}
|