From 498bf2231366d921377eaa935223722ab2b1a3da Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:42:05 +0100 Subject: [PATCH 1/9] feat(bootstrap): add external Openstack network lookup --- bootstrap/data.tf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bootstrap/data.tf diff --git a/bootstrap/data.tf b/bootstrap/data.tf new file mode 100644 index 0000000..77b4e7c --- /dev/null +++ b/bootstrap/data.tf @@ -0,0 +1,3 @@ +data "openstack_networking_network_v2" "external" { + name = var.external_network_name +} \ No newline at end of file From 48e537e3c3006bfcfdfbbe42d7fd5d650272e851 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:46:59 +0100 Subject: [PATCH 2/9] feat(bootstrap): configure runner network and virtual machine --- bootstrap/main.tf | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 bootstrap/main.tf diff --git a/bootstrap/main.tf b/bootstrap/main.tf new file mode 100644 index 0000000..2974885 --- /dev/null +++ b/bootstrap/main.tf @@ -0,0 +1,58 @@ +# Dedicated network for the GitHub Actions runner +resource "openstack_networking_network_v2" "runner" { + name = var.network_name + admin_state_up = true +} + +# One subnet for the runner +resource "openstack_networking_subnet_v2" "runner" { + name = var.subnet_name + network_id = openstack_networking_network_v2.runner.id + cidr = var.subnet_cidr + ip_version = 4 + enable_dhcp = true +} + +# Router with an external gateway +resource "openstack_networking_router_v2" "runner" { + name = var.router_name + admin_state_up = true + external_network_id = data.openstack_networking_network_v2.external.id +} + +# Connect the runner subnet to the router +resource "openstack_networking_router_interface_v2" "runner" { + router_id = openstack_networking_router_v2.runner.id + subnet_id = openstack_networking_subnet_v2.runner.id +} + +# GitHub Actions runner VM +module "runner_vm" { + source = "../modules/vm" + + name = var.runner_name + ssh_key_name = var.ssh_key_name + + network_id = openstack_networking_network_v2.runner.id + subnet_id = openstack_networking_subnet_v2.runner.id + + distro = var.runner_image + flavor = var.runner_flavor + + template = "${path.module}/cloud_init/runner.yaml" + + enable_fip = true + ntnu_internal_network = var.external_network_name + + security_group_rules = [ + { + port = 22 + protocol = "tcp" + remote_ip_prefix = var.admin_cidr + } + ] + + depends_on = [ + openstack_networking_router_interface_v2.runner + ] +} \ No newline at end of file From 3b8b19cc186d9a0e68cafa287adcabeb0862a435 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:49:45 +0100 Subject: [PATCH 3/9] feat(bootstrap): configure OpenStack and cloud-init providers --- bootstrap/providers.tf | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 bootstrap/providers.tf diff --git a/bootstrap/providers.tf b/bootstrap/providers.tf new file mode 100644 index 0000000..7285463 --- /dev/null +++ b/bootstrap/providers.tf @@ -0,0 +1,18 @@ +terraform { + required_version = ">= 1.12.0" + + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + + cloudinit = { + source = "hashicorp/cloudinit" + version = "~> 2" + } + } +} + +provider "openstack" { +} \ No newline at end of file From ac4ceb531ee33fe15d536eee0569d0afb68c1878 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:52:52 +0100 Subject: [PATCH 4/9] feat(bootstrap): configure runner dependencies with cloud-init --- bootstrap/cloud_init/runner.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 bootstrap/cloud_init/runner.yaml diff --git a/bootstrap/cloud_init/runner.yaml b/bootstrap/cloud_init/runner.yaml new file mode 100644 index 0000000..fe47620 --- /dev/null +++ b/bootstrap/cloud_init/runner.yaml @@ -0,0 +1,12 @@ +#cloud-config + +package_update: true + +packages: + - ca-certificates + - curl + - git + - jq + - tar + - unzip + - libicu76 \ No newline at end of file From 6fb67f798fdaaa5590ddcc76f1bec2aeb6464acc Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:53:55 +0100 Subject: [PATCH 5/9] feat(bootstrap): define network and runner variables --- bootstrap/variables.tf | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 bootstrap/variables.tf diff --git a/bootstrap/variables.tf b/bootstrap/variables.tf new file mode 100644 index 0000000..0b00e0e --- /dev/null +++ b/bootstrap/variables.tf @@ -0,0 +1,62 @@ +variable "ssh_key_name" { + description = "SSH keypair available in SkyHiGh" + type = string +} + +variable "network_name" { + description = "Name of the network" + type = string +} + +variable "external_network_name" { + description = "Name of the external network" + type = string + default = "ntnu-internal" +} + +variable "subnet_name" { + description = "Name of the subnet" + type = string +} + +variable "subnet_cidr" { + description = "CIDR block for the subnet" + type = string + + validation { + condition = can(cidrhost(var.subnet_cidr, 0)) + error_message = "subnet_cidr must be a valid CIDR block." + } +} + +variable "router_name" { + description = "Name of the router" + type = string +} + +variable "runner_name" { + description = "Name of the GitHub Actions runner VM" + type = string +} + +variable "runner_image" { + description = "Image to use for the GitHub Actions runner VM" + type = string + default = "Debian 13 (Trixie) stable amd64" +} + +variable "runner_flavor" { + description = "Flavor to use for the GitHub Actions runner VM" + type = string + default = "gx1.2c4r" +} + +variable "admin_cidr" { + description = "CIDR block for admin access to the runner VM" + type = string + + validation { + condition = can(cidrhost(var.admin_cidr, 0)) + error_message = "admin_cidr must be a valid CIDR block." + } +} \ No newline at end of file From 6e431f074761bfdb2b8c6d301aadb2e2a20ab3a1 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 23:02:50 +0100 Subject: [PATCH 6/9] feat(bootstrap): expose runner VM and network outputs --- bootstrap/outputs.tf | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 bootstrap/outputs.tf diff --git a/bootstrap/outputs.tf b/bootstrap/outputs.tf new file mode 100644 index 0000000..00ed286 --- /dev/null +++ b/bootstrap/outputs.tf @@ -0,0 +1,24 @@ +output "runner_vm_id" { + description = "ID of the GitHub Actions runner VM" + value = module.runner_vm.vm_id +} + +output "runner_private_ip" { + description = "Private IP address of the GitHub Actions runner VM" + value = module.runner_vm.private_ip +} + +output "runner_floating_ip" { + description = "Floating IP address of the GitHub Actions runner VM" + value = module.runner_vm.floating_ip +} + +output "runner_network_id" { + description = "ID of the runner network" + value = openstack_networking_network_v2.runner.id +} + +output "runner_subnet_id" { + description = "ID of the runner subnet" + value = openstack_networking_subnet_v2.runner.id +} \ No newline at end of file From 00feec8c880d3be592d934ce765eff1a2eb7e23e Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 23:03:34 +0100 Subject: [PATCH 7/9] docs(bootstrap): add example runner configuration variables --- bootstrap/terraform.tfvars.example | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 bootstrap/terraform.tfvars.example diff --git a/bootstrap/terraform.tfvars.example b/bootstrap/terraform.tfvars.example new file mode 100644 index 0000000..c82c0bd --- /dev/null +++ b/bootstrap/terraform.tfvars.example @@ -0,0 +1,17 @@ +# Runner network configuration +network_name = "your-network-name" +subnet_name = "your-subnet-name" +subnet_cidr = "your-subnet-cidr" +router_name = "your-router-name" + +external_network_name = "your-external-network-name" + +# GitHub Actions runner VM +runner_name = "your-runner-name" +runner_image = "Debian 13 (Trixie) stable amd64" +runner_flavor = "gx1.2c4r" + +ssh_key_name = "your-ssh-key-name" + +# Example only - replace with your trusted IP +admin_cidr = "203.0.113.10/32" \ No newline at end of file From 2cbd11f0c2e304f25558092491fc1cad9d65682b Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 23:16:31 +0100 Subject: [PATCH 8/9] chore(bootstrap): lock OpenTofu provider versions --- bootstrap/.terraform.lock.hcl | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 bootstrap/.terraform.lock.hcl diff --git a/bootstrap/.terraform.lock.hcl b/bootstrap/.terraform.lock.hcl new file mode 100644 index 0000000..e5c5f89 --- /dev/null +++ b/bootstrap/.terraform.lock.hcl @@ -0,0 +1,75 @@ +# This file is maintained automatically by "tofu init". +# Manual edits may be lost in future updates. + +provider "registry.opentofu.org/hashicorp/cloudinit" { + version = "2.4.1" + constraints = "~> 2.0" + hashes = [ + "h1:+W0yP9aNwhxG6GkkvAvXAzR3c6oHFrsn0qKzCcd+Pt4=", + "h1:5//sVDkThzeCAUR3avaWF5QkFGoGNHQVohk/03XlwDY=", + "h1:ALxQFbm3LDD9sM9hnJpml3vjEgwwHlfWkynA3QULKzs=", + "h1:IglLKQ1PLLykMGHI5kJgs1KRkQGBHFbQGgwB8d2Evmc=", + "h1:Jp8AyBJHKvSuYUmzImcUJIPasR/dWpPFKHbYYrBkZIk=", + "h1:Q1wbRg2gy6rfc+02mx3am1X4XiQ3t3ohWrD0HhGps3U=", + "h1:QCJhqLabUHdUfcFCxT5RyKrIVyMH20vhvmcwlA3LKnM=", + "h1:SaRx0UjptcMTQe7RAscgvL4yWlniE9fozAY1n8rkWl0=", + "h1:U/s/xR8Qye5WW1Cktz8+J++rHRJCXy29EFXPLSGnxSQ=", + "h1:a3EYZ6Sk+eVf/AuEm8P/vXzHPLr8TfZoVJl2QLaULBQ=", + "h1:m+a2xKJJoawR7H3FF+gFOlBMt80VGnUIDBBCU5Fcp/E=", + "h1:tdO7350ZjYClrjddqhEzXEsX3duMOeKbxWGpRroqDNk=", + "h1:vBTV7h6otZu+5CUa5Mom6qRbGBol1KTgUSW48Z8LnY4=", + "h1:yELihyb80B8udu3NsTq9OYOirr+8+eToSe1ubKecDf8=", + "h1:zKTS0SR/2LWKBAIcUl/yTZulOzKsaYf7qajYARh+yns=", + "zh:1d4e8e1a6c9e87d2f481950d44d907353d8407c74fad3effd1e340a96b2a8ebf", + "zh:1e6070731a6b27d336d67a0a89f954e91efa410d6e309f871e1eb119068852ed", + "zh:2dc72816f28d66de9c7d36f9245dae11810c94321ec9ab54958e94cb077b3129", + "zh:375b260afb24a068e8ea3f9711c995d61eb90eafd8c2f1622a2792c098c67c0c", + "zh:3d7aa8f836560393e3e45243fbcf0a6eb1de429794897439cc9e174c89d2c515", + "zh:3eabd95f6cec88e4190dc1b4c5c1b437343da85633b5803722a08d870d2a203f", + "zh:44672aa5eb610f846af5dfa705b6a8339aea82648b64a4247c0e55a8be590695", + "zh:491020b29c7efdb2288b61babdbd8467315cf3b4e9cf903335cf6f6095d80898", + "zh:520cd99f2c25d0aa22846e650c96f7800837a7e4e666a47e1e42c8e99d3423bd", + "zh:8342d7961404864b0fa4bb36dc686b5e04cbf8771e407cba961c44b8ca3158f0", + "zh:9330950100da6c4a04a6d4806407ab9ab686e079bd7475cff08f7f646cfbd440", + "zh:e12f3bfbee3534b9b0f3310fa80697ef0cca0ff473b6a5c99e457758a366c786", + "zh:eebe47e46ccbff9acf987c653032460e4e572248b1aa7eafce8e340f2120217a", + "zh:ef75fe7352b12b5e5fc968cb9993a94f81101b12766ada290cf1509fb202823f", + "zh:f3b5a1d9fae6e42d8a40cdbb31d6dbbf59a837a7e351b049dfe38474fc26a0ac", + ] +} + +provider "registry.opentofu.org/terraform-provider-openstack/openstack" { + version = "3.4.0" + constraints = "~> 3.0" + hashes = [ + "h1:BB0YFgWM3KZTFY16pCYIzBqM4IpmDpT03m29VZEV2OU=", + "h1:CalEmc/Tn9xv1Xx2bX92EUdsF8z6Lldmb4/gwT5lSRw=", + "h1:DwSYyG1VNcEMUVY1GE9gbo2C3UPS8W2bFGk4WGVJnDY=", + "h1:FpAT7j83xnW4SkiUL6A0eXo9V1bYbABt1Eh467INBgk=", + "h1:FqJxdGUazKAfnZa11qT7h5PstlSTeE4xTKPUiWEgPmg=", + "h1:GgJWG1fXiDuRBcdDuzOWRW0TMW1Lx5N8O6+80v17LZc=", + "h1:HsPiqtbovSqh8jHAuMDR3tNeRNCOBEeHCvRbkgAHiaY=", + "h1:IwC35geJo3X96yLwIqcjSzoLL1Nt+6qJA6BoBMlGgMw=", + "h1:MVSoVvhjbu7s1pfYfsiYED8A++XfAoyOlSX1x9PW68E=", + "h1:YMwKmPy+svf02jWyflqaPaQVX3eIXciwGeA8uwYgRBw=", + "h1:n1yIddr5lsLoYBhlfUmCwIYiCsynDkLvzAE98Tceo/A=", + "h1:nON9j1AYtfbOAmaml0lcPCfEQcb0OLB0K693HjdFggQ=", + "h1:rqRC/cidHw8t2vQb85cjMoWThGSCWnkEmTmmNSaGBVs=", + "h1:tzU+JfFe/RN+7swp0k5V5wUmuZtRNOjKX+spIGcRuKM=", + "zh:11b3c88e24197a29b13cf5ab41771944bd16707b561645323e8cbb4f1da00b7b", + "zh:1c3e89cf19118fc07d7b04257251fc9897e722c16e0a0df7b07fcd261f8c12e7", + "zh:2dc60dbbbb822fbb1e7b17e3b89e3e6a7a8fe54a716d4477d2fa40f836c4de0f", + "zh:68f851e333f0ab2367e9b2680739d47e4151ca9f803934ef265f6fa70a28e6e0", + "zh:6995b0bcae5a291d7b0a4d3a5df109eb97f8d1d34be6eb27768fe29b5e0d8def", + "zh:7b68bf81891a0445c543629d4d160226a6f2d5e8e5ee4baf21e0221412437d17", + "zh:8f117b8b17280598a92c6202b183a77168fe573397cfb597b9c2028da281846d", + "zh:b0a23b990eb9260e9616fd8471f11b37485f92df0e5eef46cd286f87396b0e0a", + "zh:baa5052ab3207f3a996c1fd715935d79a4e1c57d3d24e3c549b6c20ea9307e38", + "zh:c012cbda058aa7b76b718074fd73829ecd4009ad98e3dafe496e5f26099625c0", + "zh:c1c95e5dbe546a935a62c63814aef04718b58dd4258e5da40d681b43322924e9", + "zh:c720bdbdf432041f99572bac40d24659239b989cbf1540f92ebb441900ac8bae", + "zh:c92ac802a3dd1abbf9c9ccf0aba1f261fe9f8c2e7fafc196c7119b73c25a0772", + "zh:fd4faf25ee72acef4ab12c93ea9c7a5ccb41315e8851609e08b37d3c91ea6584", + "zh:ff347392412fc7950635e2c1c5bb878d88b661c1c5ba052edd290d971a14028f", + ] +} From a51fcfcb82d3daecdb70dbdca29b172e8bcfec26 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sat, 31 Oct 2026 23:20:46 +0100 Subject: [PATCH 9/9] feat(modules): add reusable OpenStack network and VM modules --- modules/network/data.tf | 3 ++ modules/network/network.tf | 49 +++++++++++++++++++ modules/network/outputs.tf | 19 ++++++++ modules/network/providers.tf | 8 ++++ modules/network/variables.tf | 45 ++++++++++++++++++ modules/vm/outputs.tf | 24 ++++++++++ modules/vm/providers.tf | 13 ++++++ modules/vm/security_group.tf | 23 +++++++++ modules/vm/variables.tf | 91 ++++++++++++++++++++++++++++++++++++ modules/vm/vm.tf | 77 ++++++++++++++++++++++++++++++ 10 files changed, 352 insertions(+) create mode 100644 modules/network/data.tf create mode 100644 modules/network/network.tf create mode 100644 modules/network/outputs.tf create mode 100644 modules/network/providers.tf create mode 100644 modules/network/variables.tf create mode 100644 modules/vm/outputs.tf create mode 100644 modules/vm/providers.tf create mode 100644 modules/vm/security_group.tf create mode 100644 modules/vm/variables.tf create mode 100644 modules/vm/vm.tf diff --git a/modules/network/data.tf b/modules/network/data.tf new file mode 100644 index 0000000..77b4e7c --- /dev/null +++ b/modules/network/data.tf @@ -0,0 +1,3 @@ +data "openstack_networking_network_v2" "external" { + name = var.external_network_name +} \ No newline at end of file diff --git a/modules/network/network.tf b/modules/network/network.tf new file mode 100644 index 0000000..5f30a33 --- /dev/null +++ b/modules/network/network.tf @@ -0,0 +1,49 @@ +resource "openstack_networking_network_v2" "network" { + name = var.network_name + admin_state_up = true +} + +resource "openstack_networking_subnet_v2" "frontend" { + name = var.frontend_subnet_name + network_id = openstack_networking_network_v2.network.id + cidr = var.frontend_subnet_cidr + ip_version = 4 + enable_dhcp = true +} + +resource "openstack_networking_subnet_v2" "backend" { + name = var.backend_subnet_name + network_id = openstack_networking_network_v2.network.id + cidr = var.backend_subnet_cidr + ip_version = 4 + enable_dhcp = true +} + +resource "openstack_networking_subnet_v2" "database" { + name = var.database_subnet_name + network_id = openstack_networking_network_v2.network.id + cidr = var.database_subnet_cidr + ip_version = 4 + enable_dhcp = true +} + +resource "openstack_networking_router_v2" "router" { + name = var.router_name + admin_state_up = true + external_network_id = data.openstack_networking_network_v2.external.id +} + +resource "openstack_networking_router_interface_v2" "frontend" { + router_id = openstack_networking_router_v2.router.id + subnet_id = openstack_networking_subnet_v2.frontend.id +} + +resource "openstack_networking_router_interface_v2" "backend" { + router_id = openstack_networking_router_v2.router.id + subnet_id = openstack_networking_subnet_v2.backend.id +} + +resource "openstack_networking_router_interface_v2" "database" { + router_id = openstack_networking_router_v2.router.id + subnet_id = openstack_networking_subnet_v2.database.id +} \ No newline at end of file diff --git a/modules/network/outputs.tf b/modules/network/outputs.tf new file mode 100644 index 0000000..dd40e32 --- /dev/null +++ b/modules/network/outputs.tf @@ -0,0 +1,19 @@ +output "network_id" { + value = openstack_networking_network_v2.network.id +} + +output "frontend_subnet_id" { + value = openstack_networking_subnet_v2.frontend.id +} + +output "backend_subnet_id" { + value = openstack_networking_subnet_v2.backend.id +} + +output "database_subnet_id" { + value = openstack_networking_subnet_v2.database.id +} + +output "router_id" { + value = openstack_networking_router_v2.router.id +} \ No newline at end of file diff --git a/modules/network/providers.tf b/modules/network/providers.tf new file mode 100644 index 0000000..3a3b91e --- /dev/null +++ b/modules/network/providers.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + } +} \ No newline at end of file diff --git a/modules/network/variables.tf b/modules/network/variables.tf new file mode 100644 index 0000000..e54850e --- /dev/null +++ b/modules/network/variables.tf @@ -0,0 +1,45 @@ +variable "network_name" { + description = "Name of the network" + type = string +} + +variable "frontend_subnet_name" { + description = "Name of the frontend subnet" + type = string +} + +variable "frontend_subnet_cidr" { + description = "CIDR block for the frontend subnet" + type = string +} + +variable "backend_subnet_name" { + description = "Name of the backend subnet" + type = string +} + +variable "backend_subnet_cidr" { + description = "CIDR block for the backend subnet" + type = string +} + +variable "database_subnet_name" { + description = "Name of the database subnet" + type = string +} + +variable "database_subnet_cidr" { + description = "CIDR block for the database subnet" + type = string +} + +variable "router_name" { + description = "Name of the router" + type = string +} + +variable "external_network_name" { + description = "Name of the external network" + type = string + default = "ntnu-internal" +} \ No newline at end of file diff --git a/modules/vm/outputs.tf b/modules/vm/outputs.tf new file mode 100644 index 0000000..d0dd7d6 --- /dev/null +++ b/modules/vm/outputs.tf @@ -0,0 +1,24 @@ +output "vm_id" { + description = "ID of the virtual machine" + value = openstack_compute_instance_v2.vm.id +} + +output "vm_name" { + description = "Name of the virtual machine" + value = openstack_compute_instance_v2.vm.name +} + +output "private_ip" { + description = "Private IP address of the virtual machine" + value = openstack_networking_port_v2.vm_port.all_fixed_ips[0] +} + +output "floating_ip" { + description = "Floating IP address of the virtual machine" + value = var.enable_fip ? openstack_networking_floatingip_v2.floating_ip[0].address : null +} + +output "security_group_id" { + description = "ID of the VM security group" + value = openstack_networking_secgroup_v2.secgroup.id +} diff --git a/modules/vm/providers.tf b/modules/vm/providers.tf new file mode 100644 index 0000000..0b0afd6 --- /dev/null +++ b/modules/vm/providers.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + openstack = { + source = "terraform-provider-openstack/openstack" + version = "~> 3" + } + + cloudinit = { + source = "hashicorp/cloudinit" + version = "~> 2" + } + } +} \ No newline at end of file diff --git a/modules/vm/security_group.tf b/modules/vm/security_group.tf new file mode 100644 index 0000000..e2610cf --- /dev/null +++ b/modules/vm/security_group.tf @@ -0,0 +1,23 @@ +locals { + sg_name = "sg_${var.name}" +} + +resource "openstack_networking_secgroup_v2" "secgroup" { + name = local.sg_name + description = "Security group for ${var.name}" +} + +resource "openstack_networking_secgroup_rule_v2" "ingress" { + for_each = { + for index, rule in var.security_group_rules : + tostring(index) => rule + } + + direction = "ingress" + ethertype = "IPv4" + port_range_min = each.value.port + port_range_max = each.value.port + protocol = lower(each.value.protocol) + remote_ip_prefix = each.value.remote_ip_prefix + security_group_id = openstack_networking_secgroup_v2.secgroup.id +} \ No newline at end of file diff --git a/modules/vm/variables.tf b/modules/vm/variables.tf new file mode 100644 index 0000000..5783e2e --- /dev/null +++ b/modules/vm/variables.tf @@ -0,0 +1,91 @@ +variable "template" { + description = "Input template for cloud-init" + type = string + default = "" +} + +variable "name" { + description = "Name of the instance" + type = string +} + +variable "ssh_key_name" { + description = "Public key accessible in SkyHiGh to bootstrap VM with" + type = string +} + +variable "ntnu_internal_network" { + description = "Name of the external network in SkyHiGh" + type = string + default = "ntnu-internal" +} + +variable "network_id" { + description = "ID of the network to attach the VM to" + type = string +} + +variable "subnet_id" { + description = "ID of the subnet to attach the VM to" + type = string +} + +variable "distro" { + description = "Image name to use for the VM" + type = string + default = "Debian 13 (Trixie) stable amd64" +} + +variable "flavor" { + description = "Size and feature set of the VM" + type = string + default = "gx1.1c1r" +} + +variable "enable_fip" { + description = "Attach floating IP to VM" + type = bool + default = false +} + +variable "security_group_rules" { + description = "Ingress rules for the VM security group" + + type = list(object({ + port = number + protocol = string + remote_ip_prefix = optional(string, "0.0.0.0/0") + })) + + default = [] + + validation { + condition = alltrue([ + for rule in var.security_group_rules : + contains(["tcp", "udp"], lower(rule.protocol)) + ]) + + error_message = "Protocol must be either tcp or udp." + } + + validation { + condition = alltrue([ + for rule in var.security_group_rules : + rule.port >= 1 && rule.port <= 65535 + ]) + + error_message = "Port must be between 1 and 65535." + } +} + +variable "enable_persistent_storage" { + description = "Attach persistent storage to the VM" + type = bool + default = false +} + +variable "persistent_volume_id" { + description = "ID of the persistent volume to attach to the VM" + type = string + default = null +} diff --git a/modules/vm/vm.tf b/modules/vm/vm.tf new file mode 100644 index 0000000..94eb37f --- /dev/null +++ b/modules/vm/vm.tf @@ -0,0 +1,77 @@ +data "cloudinit_config" "user_data" { + count = var.template != "" ? 1 : 0 + gzip = false + base64_encode = false + + part { + filename = "userdata.yaml" + content = templatefile(var.template, {}) + content_type = "text/cloud-config" + } +} + +data "openstack_compute_keypair_v2" "my_keypair" { + name = var.ssh_key_name +} + +data "openstack_networking_network_v2" "ntnu_internal" { + name = var.ntnu_internal_network +} + +data "openstack_images_image_v2" "image" { + name = var.distro + most_recent = true +} + +data "openstack_compute_flavor_v2" "flavor" { + name = var.flavor +} + +resource "openstack_networking_port_v2" "vm_port" { + name = "${var.name}-port" + network_id = var.network_id + + security_group_ids = [ + openstack_networking_secgroup_v2.secgroup.id + ] + + fixed_ip { + subnet_id = var.subnet_id + } +} + +resource "openstack_compute_instance_v2" "vm" { + name = var.name + image_id = data.openstack_images_image_v2.image.id + flavor_id = data.openstack_compute_flavor_v2.flavor.id + key_pair = data.openstack_compute_keypair_v2.my_keypair.name + + user_data = var.template == "" ? "" : data.cloudinit_config.user_data[0].rendered + + network { + port = openstack_networking_port_v2.vm_port.id + } +} + + + +resource "openstack_networking_floatingip_v2" "floating_ip" { + count = var.enable_fip ? 1 : 0 + pool = data.openstack_networking_network_v2.ntnu_internal.name +} + +resource "openstack_networking_floatingip_associate_v2" "fip" { + count = var.enable_fip ? 1 : 0 + + floating_ip = openstack_networking_floatingip_v2.floating_ip[0].address + port_id = openstack_networking_port_v2.vm_port.id +} + +resource "openstack_compute_volume_attach_v2" "persistent_storage" { + count = var.enable_persistent_storage ? 1 : 0 + + instance_id = openstack_compute_instance_v2.vm.id + volume_id = var.persistent_volume_id +} + +