2024-08-20 19:03:23 +00:00
|
|
|
locals {
|
2024-08-25 15:47:41 +00:00
|
|
|
domain = "ingress.tjo.cloud"
|
|
|
|
|
2024-08-20 19:03:23 +00:00
|
|
|
nodes = {
|
2024-08-24 22:15:57 +00:00
|
|
|
for k, v in var.nodes : k => merge(v, {
|
|
|
|
id = 700 + index(keys(var.nodes), k)
|
|
|
|
hash = sha1(v.name)
|
|
|
|
mac_address = "AA:BB:07:00:${format("%v:%v", substr(sha1(v.name), 0, 2), substr(sha1(v.name), 2, 2))}"
|
2024-08-25 15:47:41 +00:00
|
|
|
domain = local.domain
|
2024-08-20 19:03:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4_addresses = {
|
|
|
|
for key, node in local.nodes : key => {
|
|
|
|
for k, v in proxmox_virtual_environment_vm.nodes[key].ipv4_addresses :
|
|
|
|
proxmox_virtual_environment_vm.nodes[key].network_interface_names[k] => v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ipv6_addresses = {
|
|
|
|
for key, node in local.nodes : key => {
|
|
|
|
for k, v in proxmox_virtual_environment_vm.nodes[key].ipv6_addresses :
|
|
|
|
proxmox_virtual_environment_vm.nodes[key].network_interface_names[k] => v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes_with_address = {
|
|
|
|
for k, v in local.nodes :
|
|
|
|
k => merge(v, {
|
2024-08-24 10:13:05 +00:00
|
|
|
public_ipv4 = local.ipv4_addresses[k]["ens18"][0]
|
|
|
|
public_ipv6 = local.ipv6_addresses[k]["ens18"][0]
|
2024-08-23 20:03:47 +00:00
|
|
|
internal_ipv4 = local.ipv4_addresses[k]["tailscale0"][0]
|
|
|
|
internal_ipv6 = local.ipv6_addresses[k]["tailscale0"][0]
|
2024-08-25 15:47:41 +00:00
|
|
|
#internal_ipv4 = data.tailscale_device.ingress[k].addresses[0]
|
|
|
|
#internal_ipv6 = data.tailscale_device.ingress[k].addresses[1]
|
2024-08-20 19:03:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-23 20:03:47 +00:00
|
|
|
resource "tailscale_tailnet_key" "ingress" {
|
|
|
|
reusable = true
|
|
|
|
ephemeral = true
|
|
|
|
preauthorized = true
|
|
|
|
tags = ["tag:ingress-tjo-cloud"]
|
2024-08-25 15:47:41 +00:00
|
|
|
description = "tailscale key for ingress-tjo-cloud instances"
|
2024-08-23 20:03:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 19:03:23 +00:00
|
|
|
resource "proxmox_virtual_environment_file" "ingress" {
|
|
|
|
content_type = "iso"
|
|
|
|
datastore_id = var.common_storage
|
|
|
|
node_name = values(var.nodes)[0].host
|
|
|
|
|
|
|
|
source_file {
|
|
|
|
path = var.image_path
|
|
|
|
file_name = "ingress-tjo-cloud.img"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:42:11 +00:00
|
|
|
resource "proxmox_virtual_environment_file" "userdata" {
|
2024-08-20 19:03:23 +00:00
|
|
|
for_each = local.nodes
|
|
|
|
|
|
|
|
node_name = each.value.host
|
|
|
|
content_type = "snippets"
|
|
|
|
datastore_id = var.common_storage
|
|
|
|
|
|
|
|
source_raw {
|
|
|
|
data = <<-EOF
|
2024-08-23 20:03:47 +00:00
|
|
|
#cloud-config
|
2024-08-24 22:15:57 +00:00
|
|
|
write_files:
|
2024-08-25 15:47:41 +00:00
|
|
|
- path: /etc/ingress.tjo.cloud.json
|
|
|
|
encoding: base64
|
|
|
|
content: ${base64encode(jsonencode({ name : each.value.name, domain : each.value.domain, ssh_keys : var.ssh_keys }))}
|
2024-08-24 22:15:57 +00:00
|
|
|
- path: /run/secrets/tailscale.com/authkey
|
2024-08-25 15:47:41 +00:00
|
|
|
permissions: '0600'
|
2024-08-24 22:15:57 +00:00
|
|
|
content: ${var.tailscale_apikey}
|
|
|
|
- path: /etc/nixos/configuration.nix
|
2024-08-25 15:47:41 +00:00
|
|
|
encoding: base64
|
|
|
|
content: ${base64encode(file("${path.module}/configuration.nix"))}
|
2024-08-24 10:13:05 +00:00
|
|
|
runcmd:
|
2024-08-25 15:47:41 +00:00
|
|
|
- source /etc/profile && nixos-rebuild switch
|
|
|
|
power_state:
|
|
|
|
mode: reboot
|
2024-08-20 19:03:23 +00:00
|
|
|
EOF
|
2024-08-24 22:15:57 +00:00
|
|
|
file_name = "${each.value.name}.ingress.tjo.cloud.userconfig.yaml"
|
2024-08-20 19:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "proxmox_virtual_environment_vm" "nodes" {
|
|
|
|
for_each = local.nodes
|
|
|
|
|
|
|
|
vm_id = each.value.id
|
2024-08-25 15:47:41 +00:00
|
|
|
name = "${each.value.name}.${each.value.domain}"
|
2024-08-20 19:03:23 +00:00
|
|
|
node_name = each.value.host
|
|
|
|
|
2024-08-25 15:47:41 +00:00
|
|
|
description = "Node ${each.value.name} for ${each.value.domain}."
|
|
|
|
tags = [each.value.domain]
|
2024-08-20 19:03:23 +00:00
|
|
|
|
|
|
|
stop_on_destroy = true
|
|
|
|
timeout_start_vm = 60
|
|
|
|
timeout_stop_vm = 60
|
|
|
|
timeout_shutdown_vm = 60
|
|
|
|
timeout_reboot = 60
|
2024-08-24 22:15:57 +00:00
|
|
|
timeout_create = 600
|
2024-08-20 19:03:23 +00:00
|
|
|
|
|
|
|
cpu {
|
|
|
|
cores = each.value.cores
|
|
|
|
type = "host"
|
|
|
|
}
|
|
|
|
memory {
|
|
|
|
dedicated = each.value.memory
|
|
|
|
}
|
|
|
|
|
2024-08-21 21:00:23 +00:00
|
|
|
bios = "ovmf"
|
2024-08-22 16:16:39 +00:00
|
|
|
efi_disk {
|
|
|
|
datastore_id = each.value.storage
|
|
|
|
}
|
2024-08-21 21:00:23 +00:00
|
|
|
|
2024-08-20 19:03:23 +00:00
|
|
|
operating_system {
|
|
|
|
type = "l26"
|
|
|
|
}
|
|
|
|
|
|
|
|
agent {
|
|
|
|
enabled = true
|
2024-08-24 10:13:05 +00:00
|
|
|
timeout = "5m"
|
2024-08-20 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
network_device {
|
|
|
|
bridge = each.value.bridge
|
|
|
|
mac_address = each.value.mac_address
|
|
|
|
}
|
|
|
|
|
|
|
|
scsi_hardware = "virtio-scsi-single"
|
|
|
|
disk {
|
2024-08-21 21:00:23 +00:00
|
|
|
file_id = proxmox_virtual_environment_file.ingress.id
|
|
|
|
file_format = "qcow2"
|
2024-08-20 19:03:23 +00:00
|
|
|
interface = "virtio0"
|
|
|
|
datastore_id = each.value.storage
|
|
|
|
size = each.value.boot_size
|
|
|
|
backup = true
|
|
|
|
cache = "none"
|
|
|
|
iothread = true
|
|
|
|
}
|
|
|
|
|
|
|
|
initialization {
|
2024-08-24 17:42:11 +00:00
|
|
|
interface = "sata0"
|
2024-08-20 19:03:23 +00:00
|
|
|
datastore_id = each.value.storage
|
2024-08-24 17:42:11 +00:00
|
|
|
user_data_file_id = proxmox_virtual_environment_file.userdata[each.key].id
|
2024-08-20 19:03:23 +00:00
|
|
|
}
|
|
|
|
}
|