feat(module): adding virtualMachine module

This module gives you the ability to create virual machines on
OpenNebula

Extracted from "Cadoles v2 Production project"
This commit is contained in:
Philippe Caseiro 2023-01-19 14:15:52 +01:00
parent 2185af67c5
commit 65b95c56a1
4 changed files with 238 additions and 0 deletions

View File

@ -0,0 +1,109 @@
// Dependencies management
resource "null_resource" "depends_on" {
triggers = {
depends_on = join("", var.depends)
}
}
// OpenNebula disk image !
resource "opennebula_image" "system" {
name = var.system_image_name
description = "System disk image"
datastore_id = var.one_prod_ds
persistent = false
//lock = "MANAGE"
path = var.system_image_source
dev_prefix = "vd"
driver = "qcow2"
format = "qcow2"
permissions = var.imagemode
group = var.group
}
resource "opennebula_image" "data" {
name = var.data_image_name
description = "Data disk image"
datastore_id = var.one_prod_ds
persistent = true
//lock = "MANAGE"
path = var.data_image_source
dev_prefix = "vd"
driver = "qcow2"
format = "qcow2"
permissions = "660"
group = "production"
}
// OpenNebula (VM) Template definition
resource "opennebula_template" "vm_template" {
name = var.vm_shortname
cpu = var.cpu
vcpu = var.vcpu
memory = var.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.id
}
disk {
image_id = opennebula_image.data.id
}
//labels = var.tag_labels
dynamic "nic" {
for_each = var.network_interfaces
content {
network_id = nic.value.network_id
model = "virtio"
ip = nic.value.ip
}
}
permissions = "600"
group = var.group
depends_on = [ opennebula_image.system,
opennebula_image.data ]
}
// OpenNebula Virtual Machine
resource "opennebula_virtual_machine" "vm" {
name = var.vm_fqdn
group = var.group
timeout = 10
template_id = opennebula_template.vm_template.id
depends_on = [ opennebula_template.vm_template,
null_resource.depends_on ]
}
resource "dns_a_record_set" "vm_dns_a_record" {
count = var.dns_a_record == "yes" ? 1 : 0
zone = var.vm_domain
name = var.vm_shortname
ttl = 300
addresses = [ var.network_interfaces[0].ip ]
depends_on = [ opennebula_virtual_machine.vm ]
}
resource "dns_cname_record" "vm_dns_cname_record" {
for_each = var.dns_cname_record
zone = var.vm_domain
name = each.value
cname = "${var.vm_fqdn}."
ttl = 300
depends_on = [ opennebula_virtual_machine.vm ]
}

View File

@ -0,0 +1,11 @@
output "virtual_machine_shortname" {
value = opennebula_virtual_machine.vm.name
}
output "virtual_machine_nic0_mac" {
value = opennebula_virtual_machine.vm.nic[0].mac
}
output "network_interfaces" {
value = var.network_interfaces
}

View File

@ -0,0 +1,104 @@
variable "one_user" {
description = "OpenNebula user"
type = string
}
variable "one_prod_ds" {
description = "ID du Datastore de production"
type = string
}
variable "vm_fqdn" {
description = "Virtual Machine FQDN Name"
type = string
}
variable "vm_shortname" {
description = "Virtual machine short name"
type = string
}
variable "vm_domain" {
description = "Full qualified dns domain name"
type = string
}
variable "system_image_name" {
description = "System image disk name"
type = string
}
variable "system_image_source" {
description = "Virtual Machine system image source"
type = string
}
variable "data_image_name" {
description = "Data image disk name"
type = string
}
variable "data_image_source" {
description = "Virtual Machine data disk image source"
type = string
}
variable "tag_labels" {
description = "Labels"
type = string
}
variable "network_interfaces" {
description = "Network interfaces"
type = list
}
variable "cpu" {
description = "VM CPU"
type = string
}
variable "vcpu" {
description = "VM vCPU"
type = string
}
variable "ram" {
description = "VM RAM"
type = string
}
variable "depends" {
description = "List of output_variables for dependences management"
type = list
default = []
}
variable "dns_a_record" {
description = "Create a dns A record (yes/no)"
type = string
default = "no"
}
variable "dns_cname_record" {
description = "List of CNAME for A entry"
type = map(string)
default = {}
}
variable "group" {
description = "Group (owner group)"
type = string
default = "production"
}
variable "graphics" {
description = "Graphics declaration"
type = map(string)
default = {
keymap = "fr"
listen = "0.0.0.0"
type = "VNC"
}
}

View File

@ -0,0 +1,14 @@
terraform {
required_providers {
dns = {
source = "hashicorp/dns"
}
null = {
source = "hashicorp/null"
}
opennebula = {
source = "terraform-providers/opennebula"
}
}
required_version = ">= 0.13"
}