Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,70 @@
name: CI

# Run validation whenever changes are proposed or merged into main.
# Manual execution is also supported through GitHub Actions.
on:
pull_request:
branches: [main]
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

# Prevent redundant CI runs for the same branch or pull request.
# Only superseded pull request runs are cancelled, ensuring that
# every commit pushed to main can complete its validation.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
# Only cancel superseded pull request runs.
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

# CI only needs to read repository contents.
# Infrastructure deployment permissions are handled separately.
permissions:
contents: read

jobs:
validate:
name: Validate OpenTofu
check:
name: Format and validate infrastructure
runs-on: [self-hosted, linux, assignment2]
timeout-minutes: 15
timeout-minutes: 30

steps:
- name: Checkout repository
# Retrieve the repository without persisting Git credentials.
- name: Checkout
uses: actions/checkout@v5
with:
persist-credentials: false

# Install a consistent OpenTofu version across CI runs.
- name: Setup OpenTofu
uses: opentofu/setup-opentofu@v2
with:
tofu_version: "1.12.6"
tofu_wrapper: false

# Enforce consistent formatting across all OpenTofu configurations
- name: Check formatting
run: tofu fmt -check -recursive

- name: Initialize bootstrap configuration
# Initialize providers and modules without connecting to remote state.
# The committed lock file ensures consistent provider versions.
# Validation checks configuration correctness without deploying resources.
- name: Validate bootstrap
working-directory: bootstrap
# Skip backend initialization
run: tofu init -backend=false -input=false
run: |
tofu init -backend=false -input=false -lockfile=readonly
tofu validate -no-color
- name: Validate bootstrap configuration
working-directory: bootstrap
# Validate without accessing Openstack resources
run: tofu validate -no-color
# Validate the TEST network stack independently of other stacks.
- name: Validate TEST network
working-directory: environments/test/network
run: |
tofu init -backend=false -input=false -lockfile=readonly
tofu validate -no-color
# Validate the TEST frontend stack without requiring deployed infrastructure.
- name: Validate TEST frontend
working-directory: environments/test/frontend
run: |
tofu init -backend=false -input=false -lockfile=readonly
tofu validate -no-color
75 changes: 75 additions & 0 deletions environments/test/frontend/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions environments/test/frontend/cloud_init/nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#cloud-config
package_update: true

packages:
- nginx

runcmd:
- systemctl enable --now nginx
8 changes: 8 additions & 0 deletions environments/test/frontend/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "terraform_remote_state" "network" {
backend = "kubernetes"

config = {
namespace = "iac"
secret_suffix = "assignment2-test-network"
}
}
31 changes: 31 additions & 0 deletions environments/test/frontend/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

module "frontend_vm" {
source = "../../../modules/vm"
count = var.frontend_count

name = format("%s-%02d", var.frontend_name, count.index + 1)
distro = var.frontend_image
flavor = var.frontend_flavor
ssh_key_name = var.ssh_key_name

network_id = data.terraform_remote_state.network.outputs.network_id
subnet_id = data.terraform_remote_state.network.outputs.frontend_subnet_id

template = "${path.module}/cloud_init/nginx.yaml"

enable_fip = true
ntnu_internal_network = var.external_network_name

security_group_rules = [
{
port = 22
protocol = "tcp"
remote_ip_prefix = var.admin_cidr
},
{
port = 80
protocol = "tcp"
remote_ip_prefix = var.http_cidr
}
]
}
24 changes: 24 additions & 0 deletions environments/test/frontend/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
output "frontend_vm_ids" {
description = "IDs of the TEST frontend VMs"
value = module.frontend_vm[*].vm_id
}

output "frontend_vm_names" {
description = "Names of the TEST frontend VMs"
value = module.frontend_vm[*].vm_name
}

output "frontend_private_ips" {
description = "Private IP addresses of the TEST frontend VMs"
value = module.frontend_vm[*].private_ip
}

output "frontend_floating_ips" {
description = "Floating IP addresses of the TEST frontend VMs"
value = module.frontend_vm[*].floating_ip
}

output "frontend_security_group_ids" {
description = "Security group IDs of the TEST frontend VMs"
value = module.frontend_vm[*].security_group_id
}
20 changes: 20 additions & 0 deletions environments/test/frontend/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_version = ">= 1.12.0"

required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 3"
}

cloudinit = {
source = "hashicorp/cloudinit"
version = "~> 2"
}
}

backend "kubernetes" {
namespace = "iac"
secret_suffix = "assignment2-test-frontend"
}
}
17 changes: 17 additions & 0 deletions environments/test/frontend/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Frontend VM
frontend_name = "assignment2-test-frontend"
frontend_count = 1

# Image and flavor
frontend_image = "Debian 13 (Trixie) stable amd64"
frontend_flavor = "gx1.1c1r"

# SSH
ssh_key_name = "YOUR_SSH_KEYPAIR_NAME"

# Security group rules
admin_cidr = "YOUR_ADMIN_CIDR"
http_cidr = "YOUR_HTTP_CIDR"

# External network
external_network_name = "ntnu-internal"
58 changes: 58 additions & 0 deletions environments/test/frontend/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
variable "frontend_name" {
description = "Name of the frontend VM"
type = string
}

variable "frontend_image" {
description = "OpenStack image used for the frontend VM"
type = string
default = "Debian 13 (Trixie) stable amd64"
}

variable "frontend_flavor" {
description = "OpenStack flavor used for the frontend VM"
type = string
default = "gx1.1c1r"
}

variable "ssh_key_name" {
description = "SSH keypair available in SkyHiGh"
type = string
}

variable "admin_cidr" {
description = "CIDR range allowed to access the frontend VM via SSH"
type = string

validation {
condition = can(cidrhost(var.admin_cidr, 0))
error_message = "admin_cidr must be a valid CIDR block."
}
}

variable "http_cidr" {
description = "CIDR range allowed to access the frontend VM via HTTP"
type = string

validation {
condition = can(cidrhost(var.http_cidr, 0))
error_message = "http_cidr must be a valid CIDR block."
}
}

variable "external_network_name" {
description = "Name of the external network used for floating IP allocation"
type = string
default = "ntnu-internal"
}

variable "frontend_count" {
description = "Number of frontend VMs to create"
type = number
default = 1

validation {
condition = var.frontend_count >= 1 && floor(var.frontend_count) == var.frontend_count
error_message = "frontend_count must be a positive integer."
}
}
38 changes: 38 additions & 0 deletions environments/test/network/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions environments/test/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module "network" {
source = "../../../modules/network"

network_name = var.network_name

frontend_subnet_name = var.frontend_subnet_name
frontend_subnet_cidr = var.frontend_subnet_cidr

backend_subnet_name = var.backend_subnet_name
backend_subnet_cidr = var.backend_subnet_cidr

database_subnet_name = var.database_subnet_name
database_subnet_cidr = var.database_subnet_cidr

router_name = var.router_name
external_network_name = var.external_network_name
}
Loading