From cefa86029c9d4fc4cffaa05d33da9c978dbb1c16 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sat, 7 Apr 2018 15:45:00 +0100 Subject: Improve the support for Libvirt Separate out the backend things, like the virtual network, from the mini environment. This makes it easier to have resources shared between mini environments. Also handle the deployment of this new backend related Terraform configuration. --- terraform/libvirt/backend/main.tf | 20 +++++++ terraform/libvirt/mini_environment.tf | 68 ------------------------ terraform/libvirt/mini_environment/main.tf | 83 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 68 deletions(-) create mode 100644 terraform/libvirt/backend/main.tf delete mode 100644 terraform/libvirt/mini_environment.tf create mode 100644 terraform/libvirt/mini_environment/main.tf (limited to 'terraform') diff --git a/terraform/libvirt/backend/main.tf b/terraform/libvirt/backend/main.tf new file mode 100644 index 0000000..24995e0 --- /dev/null +++ b/terraform/libvirt/backend/main.tf @@ -0,0 +1,20 @@ +variable "uri" { + type = "string" +} + +terraform { + backend "http" {} +} + +provider "libvirt" { + uri = "${var.uri}" +} + +resource "libvirt_network" "vm_network" { + name = "vm_network" + addresses = ["10.0.1.0/24"] +} + +output "network_name" { + value = "${libvirt_network.vm_network.name}" +} diff --git a/terraform/libvirt/mini_environment.tf b/terraform/libvirt/mini_environment.tf deleted file mode 100644 index 4b9965d..0000000 --- a/terraform/libvirt/mini_environment.tf +++ /dev/null @@ -1,68 +0,0 @@ -terraform { - backend "http" {} -} - -variable "machine_name" { - type = "string" -} - -variable "uri" { - type = "string" -} - -variable "vm_image_and_system" { - type = "string" -} - -provider "libvirt" { - uri = "${var.uri}" -} - -resource "libvirt_network" "vm_network" { - name = "vm_network" - addresses = ["10.0.1.0/24"] -} - -# Create the machine -resource "libvirt_domain" "domain-ubuntu" { - name = "${var.machine_name}" - memory = "8196" - vcpu = 4 - - kernel = "${var.vm_image_and_system}/system/kernel/bzImage" - initrd = "${var.vm_image_and_system}/system/initrd" - - cmdline { - "--root" = "/dev/vda1" - "--system" = "${var.vm_image_and_system}/system" - "--load" = "${var.vm_image_and_system}/system/boot" - } - - filesystem { - source = "/gnu/store" - target = "TAG_gnu_store" - readonly = true - accessmode = "passthrough" - } - - disk { - file = "${var.vm_image_and_system}/image" - readonly = true - } - - network_interface { - network_name = "${libvirt_network.vm_network.name}" - } - - console { - type = "pty" - target_port = "0" - target_type = "serial" - } - - console { - type = "pty" - target_type = "virtio" - target_port = "1" - } -} diff --git a/terraform/libvirt/mini_environment/main.tf b/terraform/libvirt/mini_environment/main.tf new file mode 100644 index 0000000..00c3394 --- /dev/null +++ b/terraform/libvirt/mini_environment/main.tf @@ -0,0 +1,83 @@ +variable "backend_remote_state_address" { + type = "string" +} + +variable "machine_name" { + type = "string" +} + +variable "host_name" { + type = "string" +} + +variable "uri" { + type = "string" +} + +variable "vm_image_and_system" { + type = "string" +} + +terraform { + backend "http" {} +} + +provider "libvirt" { + uri = "${var.uri}" +} + +data "terraform_remote_state" "backend" { + backend = "http" + config { + address = "${var.backend_remote_state_address}" + } +} + +resource "local_file" "dnsmasq-config" { + content = "address=/${var.host_name}/${libvirt_domain.main.network_interface.0.addresses.0}" + filename = "/etc/NetworkManager/dnsmasq.d/${var.host_name}.conf" +} + +resource "libvirt_domain" "main" { + name = "${var.machine_name}" + memory = "8196" + vcpu = 4 + + kernel = "${var.vm_image_and_system}/system/kernel/bzImage" + initrd = "${var.vm_image_and_system}/system/initrd" + + cmdline { + "--root" = "/dev/vda1" + "--system" = "${var.vm_image_and_system}/system" + "--load" = "${var.vm_image_and_system}/system/boot" + } + + filesystem { + source = "/gnu/store" + target = "TAG_gnu_store" + readonly = true + accessmode = "passthrough" + } + + disk { + file = "${var.vm_image_and_system}/image" + readonly = true + } + + network_interface { + network_name = "${data.terraform_remote_state.backend.network_name}" + wait_for_lease = true + } + + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + + console { + type = "pty" + target_type = "virtio" + target_port = "1" + } +} -- cgit v1.2.3