-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
165 lines (141 loc) · 4.11 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
terraform {
required_version = ">= 1.5"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.7.1"
}
local = {
source = "hashicorp/local"
version = "~> 2.4.0"
}
}
}
variable "topology_file" {
type = string
default = "topology.dot.json"
}
variable "topology_id" {
type = number
default = 1
}
variable "libvirt_local" {
type = bool
default = false
}
variable "libvirt_host" {
type = string
default = null
}
variable "image_url" {
type = string
default = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
}
variable "user" {
type = string
default = "debian"
}
variable "topology_network_prefix" {
type = string
default = "172.31.0.0/16"
}
locals {
network_cidr = cidrsubnet(var.topology_network_prefix, 8, var.topology_id)
tunnel_cidr = cidrsubnet("127.1.0.0/16", 8, var.topology_id)
}
variable "loopback_cidr" {
type = string
default = "10.0.0.0/24"
}
variable "bgp_asn" {
type = number
default = 64512
}
provider "libvirt" {
uri = var.libvirt_local ? "qemu:///system" : "qemu+ssh://${var.libvirt_host}/system"
}
resource "libvirt_network" "mgmt_network" {
name = format("%02d-mgmt-network", var.topology_id)
bridge = "virbr${100 + var.topology_id}"
mode = "nat"
addresses = [local.network_cidr]
domain = "kvm.local"
autostart = true
dhcp {
enabled = true
}
dns {
enabled = true
local_only = false
}
}
resource "libvirt_volume" "base" {
name = "debian12-latest.qcow2"
source = var.image_url
}
resource "libvirt_volume" "vol" {
for_each = { for host, i in local.hosts : host => i + 1 }
name = format("%02d-%s.qcow2", var.topology_id, each.key)
base_volume_id = libvirt_volume.base.id
}
resource "libvirt_cloudinit_disk" "cloud_init" {
for_each = { for host, i in local.hosts : host => i + 1 }
name = format("%02d-%s-cloudinit.iso", var.topology_id, each.key)
meta_data = templatefile("${path.module}/cloud-init/meta-data.yml", {})
user_data = templatefile("${path.module}/cloud-init/user-data.yml", {
user = var.user
ssh_authorized_key = trimspace(file(pathexpand("~/.ssh/id_rsa.pub")))
routing = strcontains(each.key, "oob-") ? false : true
# spines should have same BGP ASN
bgp_as = strcontains(each.key, "spine") ? var.bgp_asn : var.bgp_asn + each.value
router_id = cidrhost(var.loopback_cidr, each.value)
})
network_config = templatefile("${path.module}/cloud-init/network-config.yml", {
mgmt_mac = format("52:54:00:00:%02X:%02X", var.topology_id, each.value)
routing = strcontains(each.key, "oob-") ? false : true
})
}
resource "libvirt_domain" "domain" {
for_each = { for host, i in local.hosts : host => i + 1 }
name = format("%02d-%s", var.topology_id, each.key)
vcpu = local.host_params[each.key].cpu
memory = local.host_params[each.key].memory
autostart = true
cloudinit = libvirt_cloudinit_disk.cloud_init[each.key].id
cpu {
mode = "host-passthrough"
}
disk {
volume_id = libvirt_volume.vol[each.key].id
}
network_interface {
network_id = libvirt_network.mgmt_network.id
hostname = each.key
# NOTE: first subnet IP is reserved for libvirt network bridge
addresses = [cidrhost(local.network_cidr, each.value + 1)]
# NOTE: MAC address must start from 01
mac = format("52:54:00:00:%02X:%02X", var.topology_id, each.value)
wait_for_lease = true
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
video {
type = "none"
}
xml {
xslt = (lookup(local.links, each.key, null) == null ? null : templatefile("${path.module}/link.xslt.tftpl", {
tunnel_cidr = local.tunnel_cidr
links = local.links[each.key]
}))
}
}
locals {
ssh_cmd = format("ssh%s", !var.libvirt_local ? " -J ${var.libvirt_host}" : "")
}
output "ssh_cmd" {
value = { for name, domain in libvirt_domain.domain :
name => format("%s %s@%s", local.ssh_cmd, var.user, domain.network_interface[0].addresses[0]) }
}