2023-10-23 10:44:10 -04:00

495 lines
10 KiB
HCL

variable "tenant" {
description = "Tenant informations"
type = any
default = {
prefix = ""
}
}
variable "instance" {
description = "Instances definitions"
type = any
default = []
}
variable "instance_defaults" {
description = "Instances default definitions"
type = any
default = {}
}
variable "catalog" {
description = "Existing catalog"
type = any
default = {}
}
// variable "networks_catalog" {
// description = "Known networks to find domain"
// type = any
// default = {}
// }
// variable "images_catalog" {
// description = "Available OS images"
// type = any
// default = {}
// }
// variable "oses_catalog" {
// description = "Available falvors"
// type = any
// default = {}
// }
// variable "flavors_catalog" {
// description = "Available falvors"
// type = any
// default = {}
// }
resource "local_file" "config_cloud" {
content = yamlencode(var.catalog)
filename = "out/ns-${var.tenant.ns}-${var.instance.name}-catalogs.yml"
}
resource "local_file" "config_deployment" {
content = yamlencode(var.instance)
count = local.number
filename = "out/ns-${var.tenant.ns}-${var.instance.name}-deployment-${count.index}.yml"
}
locals {
number = "${var.instance.number}"
name = "${var.tenant.prefix}${var.instance.name}"
// default_catalog = {
// oses = {
// "cirros_0.5" = {
// format = "img"
// url = "https://download.cirros-cloud.net/0.5.2/cirros-0.5.2-x86_64-disk.img"
// },
// }
// }
default_instance = {
"name" = null
os = "default"
flavor = "default"
vcpu = 1
memory = 2048
disk_gb = null
cloudinit_file = null
cloudinit_vars = {}
metadata = {}
}
default_os = {
image = null
disk_gb = 1
cloudinit_file = null
cloudinit_vars = {}
memory = 512
metadata = {}
}
default_flavor = {
name = "default"
vcpu = 1
memory = 2048
disk_gb = null
metadata = {}
}
disks = []
# Ensure struct
instance = merge(local.default_instance, var.instance_defaults, var.instance)
resolved_flavor = merge(local.default_flavor, var.catalog.flavors[local.instance.flavor])
resolved_os = merge(local.default_os, var.catalog.oses[local.instance.os])
# Intermediate
cloudinit_file = coalesce(local.instance.cloudinit_file, local.resolved_os.cloudinit_file)
cloudinit_vars = merge(local.resolved_os.cloudinit_vars, local.instance.cloudinit_vars)
// cloudinit_file = local.resolved_os.cloudinit_file
vcpu = coalesce(local.instance.vcpu, local.resolved_flavor.vcpu)
memory = coalesce(local.instance.memory, local.resolved_flavor.memory, local.resolved_os.memory, 10)
disk_gb = coalesce(local.instance.disk_gb, local.resolved_flavor.disk_gb, local.resolved_os.disk_gb, 10)
metadata = merge(local.resolved_flavor.metadata, local.resolved_os.metadata, local.instance.metadata)
// cloudinit_file = coalesce(try(local.instance.cloudinit_file), try(local.resolved_os.cloudinit_file))
}
resource "local_file" "config_instance" {
content = yamlencode(local.resolved_os)
filename = "out/instance-${var.tenant.prefix}${local.name}-deployment.yml"
}
module "virt_instance" {
source = "../../modules_libvirt/virt_instance"
// for_each = { for v in var.instances : v.name => merge(local.instances_defaults, v) }
name = local.name
number = local.number
networks = local.instance.networks
// instance_pool = null
# From Oses
// cloudinit_userdata = "My Template"
cloudinit_vars = local.cloudinit_vars
image_base_file = "debian_12-1532.qcow2"
image_base_pool = "default"
cloudinit_file = local.cloudinit_file
metadata = local.metadata
// disks = local.instance.disks
# From flavors
// vcpu
// memory
// disk_gb = disk
// domain = local.domain
// prefix = each.value.prefix
// name_fqdn = each.value.name_fqdn
// metadata = each.value.metadata
// domain = each.value.domain
// number = each.value.number
// instance_pool = each.value.instance_pool
// images = each.value.images
// os = each.value.os
// image_base_pool = each.value.image_base_pool
// image_base_file = each.value.image_base_file
// disk_gb = each.value.disk_gb
// flavor = each.value.flavor
// memory = each.value.memory
// vcpu = each.value.vcpu
// disks = each.value.disks
// volumes = each.value.volumes
// networks = each.value.networks
// wait_for_lease = each.value.wait_for_lease
// user = each.value.user
// authorized_key = each.value.authorized_key
// password_hash = each.value.password_hash
}
// # Variables
// # =====================
// variable "name" {
// description = "Instance name"
// type = string
// }
// variable "domain" {
// description = "Instance domain"
// type = string
// default = ""
// }
// variable "number" {
// description = "Number of instances"
// type = number
// default = 1
// }
// variable "os" {
// description = "Instance os"
// type = string
// default = "cirros_0.5"
// }
// variable "authorized_key" {
// description = "Default user authorized key"
// type = string
// default = ""
// }
// # Note: This should only be used for debugging purpose
// variable "password_hash" {
// description = "Default user password hash (ie: $1$SaltSalt$GhE887kYCerthShgxern00)"
// type = string
// default = ""
// # sensitive = true
// }
// # Prefix to use ???
// variable "prefix" {
// description = "String to prefix each instances"
// type = string
// default = ""
// }
// variable "name_fqdn" {
// description = "Define instance name with fqdn"
// type = bool
// default = false
// }
// variable "name_prefix" {
// description = "Define instance name with project prefix"
// type = bool
// default = true
// }
// variable "instance_pool" {
// description = "Volume pool for instance disks"
// type = string
// default = "default"
// }
// variable "disk_gb" {
// description = "Size in gb of the root disk"
// type = number
// default = 2
// }
// #
// variable "flavor" {
// description = "Instance flavor"
// type = string
// default = "x-small"
// }
// variable "memory" {
// description = "Instance memory"
// type = string
// default = "2048"
// }
// variable "vcpu" {
// description = "Number of vcpu"
// type = number
// default = 2
// }
// variable "images" {
// description = "Available OS images"
// type = any
// default = {}
// }
// variable "image_base_pool" {
// description = "Default image pool"
// type = string
// default = "default"
// }
// variable "image_base_file" {
// description = "Image base file in the image_pool"
// type = string
// default = "TEST__ MISSING"
// }
// variable "disks" {
// description = "Ephemeral disks list"
// type = list
// default = []
// }
// variable "volumes" {
// description = "Persistant volume list"
// type = list
// default = []
// }
// variable "networks" {
// description = "Network list"
// type = list
// default = []
// }
// variable "wait_for_lease" {
// description = "Wait for network ip assignment"
// type = bool
// default = false
// }
// variable "metadata" {
// description = "Metadata do add in state"
// type = any
// default = {}
// }
// # Cloud init
// # -----------
// variable "user" {
// description = "Default user login"
// type = string
// default = "cloud"
// }
// // locals {
// // pre_default = {
// // network = "default"
// // domain = "no_domain22"
// // }
// // default_network = try(var.networks_catalog[var.instances_defaults.default_network], null) != null ? var.instances_defaults.default_network : local.pre_default.network
// // networks = local.default_network != null ? [{ name = "${var.tenant.prefix}${local.default_network}" }] : []
// // domain = try( var.networks_catalog[local.default_network].domain, local.pre_default.domain)
// // # Recommended default flavor
// // default = {
// // number = 1
// // instance_pool = "default"
// // flavor = "default"
// // # From images !
// // images = {}
// // disk_gb = 14
// // os = "cirros_0.5"
// // memory = 1024
// // user = ""
// // image_base_pool = "default"
// // image_base_file = "missing.qcow2"
// // metadata = {}
// // vcpu = 1
// // disks = []
// // volumes = []
// // name_fqdn = true
// // wait_for_lease = false
// // password_hash = ""
// // authorized_key = ""
// // prefix = var.tenant.prefix
// // default_network = local.default_network
// // networks = local.networks
// // domain = local.domain
// // }
// // instances_defaults = merge(local.default, var.instances_defaults)
// // }
// # Resources
// # =====================
// // module "cloud_instance" {
// // source = "../../modules/virt_instance"
// // for_each = { for v in var.instances : v.name => merge(local.instances_defaults, v) }
// // name = each.value.name
// // prefix = each.value.prefix
// // name_fqdn = each.value.name_fqdn
// // metadata = each.value.metadata
// // domain = each.value.domain
// // number = each.value.number
// // // instance_pool = each.value.instance_pool
// // // images = each.value.images
// // // os = each.value.os
// // // image_base_pool = each.value.image_base_pool
// // // image_base_file = each.value.image_base_file
// // // disk_gb = each.value.disk_gb
// // // flavor = each.value.flavor
// // // memory = each.value.memory
// // // vcpu = each.value.vcpu
// // // disks = each.value.disks
// // // volumes = each.value.volumes
// // // networks = each.value.networks
// // // wait_for_lease = each.value.wait_for_lease
// // // user = each.value.user
// // // authorized_key = each.value.authorized_key
// // // password_hash = each.value.password_hash
// // }
// // # Outputs
// // # =====================
// // output "instances" {
// // value = module.cloud_instance
// // }