init: copied modules from lawndale-infra

This commit is contained in:
2022-05-26 00:40:29 +02:00
commit 414feb48ee
39 changed files with 1435 additions and 0 deletions

119
lawndale-vm/compute.tf Normal file
View File

@@ -0,0 +1,119 @@
resource "libvirt_pool" "this" {
count = var.create_root_storage_pool ? 1 : 0
name = local.root_storage_pool
type = "dir"
path = "/vmstore/${var.name}"
}
resource "libvirt_volume" "this" {
name = local.root_storage_volume_name
pool = local.root_storage_pool
size = var.root_storage_volume_size_gb * 1024 * 1024 * 1024
base_volume_name = var.base_image_volume
base_volume_pool = var.base_image_pool
}
resource "macaddress" "this" {
}
module "ipam" {
source = "../../modules/lawndale-vm-ipam"
id = var.id
name = var.name
interface = var.interface
}
resource "libvirt_domain" "this" {
name = "${var.id}-${var.name}"
description = var.description
vcpu = var.vcpu
memory = var.memory_mb
autostart = var.autostart
cloudinit = libvirt_cloudinit_disk.this.id
network_interface {
bridge = module.ipam.lawndale_interface
mac = macaddress.this.address
}
disk {
volume_id = libvirt_volume.this.id
scsi = true
}
# video { type = "virtio" }
# graphics {}
console {
type = "pty"
target_port = 0
target_type = "serial"
}
qemu_agent = true
dynamic "filesystem" {
for_each = var.filesystems
content {
source = filesystem.value.source
target = filesystem.value.target
readonly = filesystem.value.readonly
accessmode = filesystem.value.accessmode
}
}
dynamic "xml" {
for_each = var.xslt == null ? [] : [var.xslt]
content {
xslt = xml.value
}
}
}
resource "libvirt_cloudinit_disk" "this" {
name = "${var.id}-${var.name}-cloudinit"
pool = local.root_storage_pool
meta_data = local.meta_data
network_config = local.network_config
user_data = var.user_data
}
locals {
default_network_config = {
version = 2
ethernets = {
eth = {
match = {
macaddress = macaddress.this.address
}
addresses = [
"${module.ipam.ip_address}/${module.ipam.cidr}"
]
gateway4 = module.ipam.gateway
nameservers = {
search = module.ipam.search_domains
addresses = [
module.ipam.nameserver
]
}
}
}
}
default_meta_data = <<EOM
instance-id: ${var.id}-${var.name}
local-hostname: ${var.name}
EOM
meta_data = var.meta_data != null ? var.meta_data : local.default_meta_data
network_config = var.network_config != null ? var.network_config : jsonencode(local.default_network_config)
}

4
lawndale-vm/locals.tf Normal file
View File

@@ -0,0 +1,4 @@
locals {
root_storage_pool = var.root_storage_pool != "" ? var.root_storage_pool : var.name
root_storage_volume_name = var.root_storage_volume_name != "" ? "${var.root_storage_volume_name}.qcow2" : "${var.name}.qcow2"
}

0
lawndale-vm/outputs.tf Normal file
View File

13
lawndale-vm/providers.tf Normal file
View File

@@ -0,0 +1,13 @@
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.6.14"
}
macaddress = {
source = "ivoronin/macaddress"
version = "~> 0.3.0"
}
}
}

112
lawndale-vm/variables.tf Normal file
View File

@@ -0,0 +1,112 @@
variable "id" {
type = number
description = "The lawndale id of the virtual machine"
}
variable "name" {
type = string
description = "The name of the virtual machine (must be a [-_a-z0-9])"
validation {
condition = can(regex("^[-_a-z0-9]+$", var.name))
error_message = "A virtual machine name must be lowercase, and can only contain alphanumeral characters, dashes and underscores."
}
}
variable "vcpu" {
type = number
description = "CPU count"
default = 1
}
variable "memory_mb" {
type = number
description = "VM memory allocation in megabytes"
}
variable "description" {
type = string
description = "(Short) Description for the virtual machine"
}
variable "base_image_pool" {
type = string
description = "Base image storage pool"
}
variable "base_image_volume" {
type = string
description = "Base image storage pool"
}
variable "root_storage_pool" {
type = string
description = "The name of the storage pool. It will default to the VM name"
default = ""
}
variable "create_root_storage_pool" {
type = bool
description = "Create the storage pool as part of the module"
default = true
}
variable "root_storage_volume_size_gb" {
type = number
description = "The size of the storage volume (in gigabytes)"
}
variable "root_storage_volume_name" {
type = string
description = "the name of the storage volume (must be unique in the pool)"
default = ""
}
variable "interface" {
type = string
description = "Network interface to attach the vm on"
}
variable "autostart" {
type = bool
description = "Start the VM at host start?"
default = true
}
variable "user_data" {
type = string
description = "Cloud-init userdata script to run"
}
variable "network_config" {
type = string
description = "Cloud-init network config"
default = null
nullable = true
}
variable "meta_data" {
type = string
description = "Cloud-init meta-data"
default = null
nullable = true
}
variable "filesystems" {
type = list(object({
source = string
target = string
accessmode = string
readonly = bool
}))
description = "9p shared filesystem devices"
default = []
}
variable "xslt" {
type = string
description = "XSLT applied to the domain before sent to libvirt"
default = null
}