263 lines
6.1 KiB
HCL
263 lines
6.1 KiB
HCL
|
|
# Variables
|
|
# =====================
|
|
#
|
|
variable "name" {
|
|
type = string
|
|
description = "Determine the name of the volume"
|
|
}
|
|
|
|
variable "persistant" {
|
|
type = bool
|
|
default = false
|
|
description = "Enable destroy protection on this volume"
|
|
}
|
|
|
|
|
|
#
|
|
variable "format" {
|
|
type = string
|
|
default = "qcow2"
|
|
description = "Determine the format of the image"
|
|
}
|
|
|
|
variable "file" {
|
|
type = string
|
|
description = "Determine the filename of the volume"
|
|
default = ""
|
|
}
|
|
|
|
|
|
#
|
|
variable "pool" {
|
|
type = string
|
|
default = "default"
|
|
description = "Determine pool to create volume within"
|
|
}
|
|
|
|
variable "pool_dir" {
|
|
type = string
|
|
default = "/virt"
|
|
description = "Default path for all pools"
|
|
}
|
|
|
|
variable "size_gb" {
|
|
type = number
|
|
default = 0
|
|
description = "Default size for the volume"
|
|
}
|
|
|
|
|
|
#
|
|
variable "url" {
|
|
type = string
|
|
default = ""
|
|
description = "Download url for volume"
|
|
}
|
|
|
|
variable "base_pool" {
|
|
type = string
|
|
default = "default"
|
|
description = "Pool of the source image"
|
|
}
|
|
variable "base_file" {
|
|
type = string
|
|
description = "Name of the source image"
|
|
default = ""
|
|
}
|
|
|
|
|
|
# Variables
|
|
# =====================
|
|
|
|
|
|
#agent=var.ssh_agent # "true"
|
|
#host=var.ssh_host # "wks1.office.barbu-it.net"
|
|
#user=var.ssh_user # "root"
|
|
#password=var.password
|
|
|
|
variable "ssh_conn" {
|
|
description = "Default settings for ssh connexion"
|
|
type = any
|
|
default = {
|
|
agent = true,
|
|
#host = "localhost",
|
|
host = "wks1.office.barbu-it.net",
|
|
user = null,
|
|
password = null
|
|
}
|
|
}
|
|
|
|
|
|
|
|
# Resources
|
|
# =====================
|
|
|
|
locals {
|
|
|
|
# Guess things from URL
|
|
url_split = split(".", basename( var.url ))
|
|
url_ext = element(local.url_split, length(local.url_split ) -1 )
|
|
url_file = "${var.name}.${local.url_ext}"
|
|
|
|
# Guess things from name
|
|
name_file = "${var.name}.${var.format}"
|
|
|
|
# Determine resource to use
|
|
is_baked = var.base_file != "" ? true : false
|
|
is_sourced = var.url != "" && ! local.is_baked ? true : false
|
|
is_created = ! local.is_baked && ! local.is_sourced ? true: false
|
|
|
|
# Volume settings
|
|
vol_name = coalesce(var.file, local.is_sourced ? local.url_file : local.name_file)
|
|
size_gb = try(var.size_gb * 1024 * 1024 * 1024, null)
|
|
id = "${var.pool_dir}/${var.pool}/${local.vol_name}"
|
|
|
|
}
|
|
|
|
|
|
# Sourced
|
|
resource "libvirt_volume" "voldef_ephemeral_sourced" {
|
|
count = local.is_sourced && ! var.persistant ? 1 : 0
|
|
|
|
name = local.vol_name
|
|
pool = var.pool
|
|
source = local.is_sourced ? var.url : null
|
|
}
|
|
|
|
# Created
|
|
resource "libvirt_volume" "voldef_ephemeral_created" {
|
|
count = local.is_created && ! var.persistant ? 1 : 0
|
|
|
|
name = local.vol_name
|
|
pool = var.pool
|
|
source = local.is_sourced ? var.url : null
|
|
size = local.size_gb
|
|
format = var.format
|
|
}
|
|
|
|
# Backed
|
|
resource "libvirt_volume" "voldef_ephemeral_baked" {
|
|
count = local.is_baked && ! var.persistant ? 1 : 0
|
|
|
|
name = local.vol_name
|
|
pool = var.pool
|
|
|
|
size = local.is_baked ? try(local.size_gb, 1) : local.size_gb
|
|
base_volume_pool = local.is_baked ? var.base_pool : null
|
|
base_volume_name = local.is_baked ? var.base_file : null
|
|
}
|
|
|
|
# Todo: Manage persistant volumes with:
|
|
# lifecycle {
|
|
# prevent_destroy = false
|
|
# }
|
|
|
|
# # Created Persistant
|
|
# resource "libvirt_volume" "voldef_ephemeral_created" {
|
|
#
|
|
# name = local.vol_name
|
|
# pool = var.pool
|
|
# source = local.is_sourced ? var.url : null
|
|
# size = local.size_gb
|
|
# format = var.format
|
|
# }
|
|
|
|
|
|
resource "null_resource" "voldef_persistant_created" {
|
|
# We only support created volumes basically
|
|
count = var.persistant ? 1 : 0
|
|
#count = local.is_created && var.persistant ? 1 : 0
|
|
#count = 0
|
|
|
|
|
|
# local ssh connection, make sure to pass variables 'user' andd 'password' on cli
|
|
# terraform apply -var user=$USER -var password=xxxxxxx
|
|
connection {
|
|
type="ssh"
|
|
# agent=var.ssh_agent # "true"
|
|
# host=var.ssh_host # "wks1.office.barbu-it.net"
|
|
# user=var.ssh_user # "root"
|
|
# password=var.password
|
|
|
|
agent = var.ssh_conn.agent
|
|
host= var.ssh_conn.host
|
|
user = var.ssh_conn.user
|
|
password = var.ssh_conn.password
|
|
|
|
#user=var.user
|
|
#password=var.password
|
|
|
|
|
|
}
|
|
|
|
provisioner "remote-exec" {
|
|
inline = [
|
|
"/opt/virt-scripts/bin/virt-vol.sh create ${local.vol_name} ${var.size_gb} ${var.pool} ${var.format}"
|
|
#"[[ ! -f '${/virt/cloud_safe/inst_storage0_data.qcow2}' ]] || \{ qemu-img create -f '${var.format}' ${local.id} '${local.size_gb}Gb; virsh pool-refresh '${var.pool}'; \}"
|
|
#"virsh vol-create-as --pool '${var.pool}' --name '${local.name_file}' --format '${var.format}' --prealloc-metadata --capacity '${local.size_gb * 100000000}'"
|
|
]
|
|
}
|
|
|
|
#provisioner "file" {
|
|
# destination = "/tmp/script.sh"
|
|
# content = templatefile(
|
|
# "${path.module}/script.sh.tpl",
|
|
# {
|
|
# "params": local.params
|
|
# "params_for_inline_command" : local.params_for_inline_command
|
|
# }
|
|
# )
|
|
#}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Output
|
|
# =====================
|
|
|
|
output "volume" {
|
|
value = coalescelist(
|
|
resource.libvirt_volume.voldef_ephemeral_sourced,
|
|
resource.libvirt_volume.voldef_ephemeral_created,
|
|
resource.libvirt_volume.voldef_ephemeral_baked,
|
|
[ { id = local.id } ],
|
|
)[0]
|
|
}
|
|
|
|
output "info" {
|
|
value = {
|
|
#id = local.id,
|
|
#id = coalescelist(
|
|
id = coalescelist(
|
|
resource.libvirt_volume.voldef_ephemeral_sourced,
|
|
resource.libvirt_volume.voldef_ephemeral_created,
|
|
resource.libvirt_volume.voldef_ephemeral_baked,
|
|
[ { id = local.id } ],
|
|
)[0].id
|
|
vol_name = local.vol_name,
|
|
is_baked = local.is_baked,
|
|
is_sourced = local.is_sourced,
|
|
is_created = local.is_created,
|
|
name_url = local.url_file,
|
|
name_file = local.name_file,
|
|
format = var.format,
|
|
file = var.file,
|
|
#out = [
|
|
# resource.libvirt_volume.voldef_ephemeral_sourced,
|
|
# resource.libvirt_volume.voldef_ephemeral_baked,
|
|
# resource.libvirt_volume.voldef_ephemeral_created,
|
|
# resource.null_resource.voldef_persistant_created
|
|
#]
|
|
#volume_id = coalescelist(
|
|
# resource.libvirt_volume.voldef_ephemeral_sourced,
|
|
# resource.libvirt_volume.voldef_ephemeral_created,
|
|
# resource.libvirt_volume.voldef_ephemeral_baked,
|
|
# resource.null_resource.voldef_persistant_created,
|
|
# )[0].id
|
|
}
|
|
}
|
|
|