Skip to content

Commit

Permalink
refactor: use terragrunt, remove links
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Sep 5, 2024
1 parent 1444a88 commit 251d275
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 0 deletions.
14 changes: 14 additions & 0 deletions envs/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
generate "backend" {
path = "tf_backend.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
terraform {
backend "s3" {
bucket = "soerenschneider-terraform"
key = "${replace(basename(get_repo_root()), "/^tf-/", "")}-${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
encrypt = true
}
}
EOF
}
24 changes: 24 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/.terraform.lock.hcl

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

7 changes: 7 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
provider_uri = "qemu+ssh://soeren@testserver.dd.soeren.cloud/system"
datacenter = "dd"
vm_host = "testserver"

iso_urls = {
"debian-12" = "https://cloud.debian.org/images/cloud/bookworm/20240901-1857/debian-12-generic-amd64-20240901-1857.qcow2"
}
Empty file.
29 changes: 29 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/tf_locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
locals {
hosts_file = "~/src/gitlab/ansible/inventory/prod/group_vars/all/hosts.yml"
hosts_yaml = file(local.hosts_file)
hosts_data = try(yamldecode(local.hosts_yaml), [])

ssh_pubkeys = distinct(compact(concat(try([chomp(file(var.ssh_public_key_file))], []), split(",", var.ssh_fallback_public_keys))))

defined_hosts = flatten([for hosts_key, hosts_value in local.hosts_data : [
for datacenter_key, datacenter_values in hosts_value : [
for host in datacenter_values : host if lookup(host, "physical", null) != null
] if datacenter_key == var.datacenter
] if hosts_key == "local_hosts"
])

hosts_macs = {
for host in local.defined_hosts :
host.host => host.physical
}

domains = {
for host in local.hosts_data.local_hosts[var.datacenter] :
host.host => host.vm_config
if try(host.vm_config.host, "") == var.vm_host && try(host.vm_config.disabled, false) == false
}

mac_domains = { for domain, val in local.domains :
domain => merge(val, { "mac" = lookup(val, "mac", null) != null ? val["mac"] : lookup(local.hosts_macs, domain, null) })
}
}
40 changes: 40 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/tf_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
path = "/var/lib/libvirt/images"
}

resource "libvirt_network" "bridge" {
name = "bridge"
mode = "bridge"
bridge = "br0"
autostart = true
}

resource "libvirt_volume" "base" {
for_each = var.iso_urls
name = "base-${each.key}"
source = each.value
format = "qcow2"
pool = libvirt_pool.default.name
}

module "domains" {
for_each = local.mac_domains
source = "../../domain-cloudinit"
domain_name = each.key

memory_m = each.value.memory
vcpus = each.value.vcpus
running = lookup(each.value, "running", true)
base_image_id = try(each.value.create_volume, false) ? libvirt_volume.base[each.value.os].id : null
block_devices = try(each.value.block_devices, [])
domain_mac = each.value.mac
create_volume = try(each.value.create_volume, false)
disk_size_bytes = each.value.disk_size_b

ssh_public_keys = local.ssh_pubkeys

pool_name = libvirt_pool.default.name
network_name = libvirt_network.bridge.name
}
13 changes: 13 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/tf_provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_version = ">= 1.0"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.6"
}
}
}

provider "libvirt" {
uri = var.provider_uri
}
31 changes: 31 additions & 0 deletions envs/vserver-2.dd.soeren.cloud/tf_variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "provider_uri" {
description = "uri to the provider endpoint"
type = string
}

variable "ssh_public_key_file" {
description = "file that contains the ssh public key"
type = string
default = "~/.ssh/id_ed25519.pub"
}

variable "ssh_fallback_public_keys" {
description = "ssh public key"
type = string
default = ""
}

variable "vm_host" {
type = string
description = "the name of this vm host"
}

variable "iso_urls" {
description = "url to fetch debian iso from"
type = map(string)
}

variable "datacenter" {
type = string
default = ""
}
Empty file.
29 changes: 29 additions & 0 deletions envs/vserver.dd.soeren.cloud/tf_locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
locals {
hosts_file = "~/src/gitlab/ansible/inventory/prod/group_vars/all/hosts.yml"
hosts_yaml = file(local.hosts_file)
hosts_data = try(yamldecode(local.hosts_yaml), [])

ssh_pubkeys = distinct(compact(concat(try([chomp(file(var.ssh_public_key_file))], []), split(",", var.ssh_fallback_public_keys))))

defined_hosts = flatten([for hosts_key, hosts_value in local.hosts_data : [
for datacenter_key, datacenter_values in hosts_value : [
for host in datacenter_values : host if lookup(host, "physical", null) != null
] if datacenter_key == var.datacenter
] if hosts_key == "local_hosts"
])

hosts_macs = {
for host in local.defined_hosts :
host.host => host.physical
}

domains = {
for host in local.hosts_data.local_hosts[var.datacenter] :
host.host => host.vm_config
if try(host.vm_config.host, "") == var.vm_host && try(host.vm_config.disabled, false) == false
}

mac_domains = { for domain, val in local.domains :
domain => merge(val, { "mac" = lookup(val, "mac", null) != null ? val["mac"] : lookup(local.hosts_macs, domain, null) })
}
}
40 changes: 40 additions & 0 deletions envs/vserver.dd.soeren.cloud/tf_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
path = "/var/lib/libvirt/images"
}

resource "libvirt_network" "bridge" {
name = "bridge"
mode = "bridge"
bridge = "br0"
autostart = true
}

resource "libvirt_volume" "base" {
for_each = var.iso_urls
name = "base-${each.key}"
source = each.value
format = "qcow2"
pool = libvirt_pool.default.name
}

module "domains" {
for_each = local.mac_domains
source = "../../domain-cloudinit"
domain_name = each.key

memory_m = each.value.memory
vcpus = each.value.vcpus
running = lookup(each.value, "running", true)
base_image_id = try(each.value.create_volume, false) ? libvirt_volume.base[each.value.os].id : null
block_devices = try(each.value.block_devices, [])
domain_mac = each.value.mac
create_volume = try(each.value.create_volume, false)
disk_size_bytes = each.value.disk_size_b

ssh_public_keys = local.ssh_pubkeys

pool_name = libvirt_pool.default.name
network_name = libvirt_network.bridge.name
}
Empty file.
40 changes: 40 additions & 0 deletions envs/vserver.ez.soeren.cloud/tf_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
path = "/var/lib/libvirt/images"
}

resource "libvirt_network" "bridge" {
name = "bridge"
mode = "bridge"
bridge = "br0"
autostart = true
}

resource "libvirt_volume" "base" {
for_each = var.iso_urls
name = "base-${each.key}"
source = each.value
format = "qcow2"
pool = libvirt_pool.default.name
}

module "domains" {
for_each = local.mac_domains
source = "../../domain-cloudinit"
domain_name = each.key

memory_m = each.value.memory
vcpus = each.value.vcpus
running = lookup(each.value, "running", true)
base_image_id = try(each.value.create_volume, false) ? libvirt_volume.base[each.value.os].id : null
block_devices = try(each.value.block_devices, [])
domain_mac = each.value.mac
create_volume = try(each.value.create_volume, false)
disk_size_bytes = each.value.disk_size_b

ssh_public_keys = local.ssh_pubkeys

pool_name = libvirt_pool.default.name
network_name = libvirt_network.bridge.name
}
Empty file.
29 changes: 29 additions & 0 deletions envs/vserver.pt.soeren.cloud/tf_locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
locals {
hosts_file = "~/src/gitlab/ansible/inventory/prod/group_vars/all/hosts.yml"
hosts_yaml = file(local.hosts_file)
hosts_data = try(yamldecode(local.hosts_yaml), [])

ssh_pubkeys = distinct(compact(concat(try([chomp(file(var.ssh_public_key_file))], []), split(",", var.ssh_fallback_public_keys))))

defined_hosts = flatten([for hosts_key, hosts_value in local.hosts_data : [
for datacenter_key, datacenter_values in hosts_value : [
for host in datacenter_values : host if lookup(host, "physical", null) != null
] if datacenter_key == var.datacenter
] if hosts_key == "local_hosts"
])

hosts_macs = {
for host in local.defined_hosts :
host.host => host.physical
}

domains = {
for host in local.hosts_data.local_hosts[var.datacenter] :
host.host => host.vm_config
if try(host.vm_config.host, "") == var.vm_host && try(host.vm_config.disabled, false) == false
}

mac_domains = { for domain, val in local.domains :
domain => merge(val, { "mac" = lookup(val, "mac", null) != null ? val["mac"] : lookup(local.hosts_macs, domain, null) })
}
}
40 changes: 40 additions & 0 deletions envs/vserver.pt.soeren.cloud/tf_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
path = "/var/lib/libvirt/images"
}

resource "libvirt_network" "bridge" {
name = "bridge"
mode = "bridge"
bridge = "br0"
autostart = true
}

resource "libvirt_volume" "base" {
for_each = var.iso_urls
name = "base-${each.key}"
source = each.value
format = "qcow2"
pool = libvirt_pool.default.name
}

module "domains" {
for_each = local.mac_domains
source = "../../domain-cloudinit"
domain_name = each.key

memory_m = each.value.memory
vcpus = each.value.vcpus
running = lookup(each.value, "running", true)
base_image_id = try(each.value.create_volume, false) ? libvirt_volume.base[each.value.os].id : null
block_devices = try(each.value.block_devices, [])
domain_mac = each.value.mac
create_volume = try(each.value.create_volume, false)
disk_size_bytes = each.value.disk_size_b

ssh_public_keys = local.ssh_pubkeys

pool_name = libvirt_pool.default.name
network_name = libvirt_network.bridge.name
}

0 comments on commit 251d275

Please sign in to comment.