generated from ethpandaops/template-devnets
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hetzner.tf
216 lines (196 loc) · 7.22 KB
/
hetzner.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
////////////////////////////////////////////////////////////////////////////////////////
// TERRAFORM PROVIDERS & BACKEND
////////////////////////////////////////////////////////////////////////////////////////
provider "hcloud" {
token = var.verkle_hcloud_token
}
////////////////////////////////////////////////////////////////////////////////////////
// VARIABLES
////////////////////////////////////////////////////////////////////////////////////////
variable "verkle_hcloud_token" {
type = string
description = "Hetzner Cloud API Token"
sensitive = true
}
variable "hcloud_ssh_key_fingerprint" {
type = string
default = "d6:76:2d:9c:5b:33:80:ff:0f:09:a2:10:9b:58:7e:dc"
}
variable "hetzner_regions" {
default = [
"nbg1",
"hel1",
"fsn1"
]
}
////////////////////////////////////////////////////////////////////////////////////////
// LOCALS
////////////////////////////////////////////////////////////////////////////////////////
locals {
hetzner_network = {
for region in var.hetzner_regions : region => {
name = "${var.ethereum_network}-${region}"
ip_range = cidrsubnet(var.base_cidr_block, 8, index(var.hetzner_regions, region))
}
}
hetzner_network_subnets = {
for region in var.hetzner_regions : region => {
zone = "eu-central"
ip_range = cidrsubnet(var.base_cidr_block, 8, index(var.hetzner_regions, region))
}
}
}
locals {
hetzner_vm_groups = flatten([
for vm_group in local.vm_groups :
[
for i in range(0, vm_group.count) : {
group_name = "${vm_group.name}"
id = "${vm_group.name}-${i + 1}"
vms = {
"${i + 1}" = {
labels = "group_name:${vm_group.name},val_start:${vm_group.validator_start + (i * (vm_group.validator_end -
vm_group.validator_start) / vm_group.count)},val_end:${min(vm_group.validator_start + ((i + 1) * (vm_group.validator_end -
vm_group.validator_start) / vm_group.count), vm_group.validator_end)}"
location = element(var.hetzner_regions, i % length(var.hetzner_regions))
size = try(vm_group.size, local.hcloud_default_server_type)
ansible_vars = try(vm_group.ansible_vars, null)
}
}
}
]
])
}
locals {
hcloud_default_location = "nbg1"
hcloud_default_image = "debian-12"
hcloud_default_server_type = "ccx13"
hcloud_global_labels = [
"Owner:Devops",
"EthNetwork:${var.ethereum_network}"
]
# hcloud_global_labels_list = [for k, v in local.hcloud_global_labels : "${k}=${v}"]
# flatten vm_groups so that we can use it with for_each()
hcloud_vms = flatten([
for group in local.hetzner_vm_groups : [
for vm_key, vm in group.vms : {
id = "${group.id}"
group_key = "${group.group_name}"
vm_key = vm_key
name = try(vm.name, "${group.id}")
ssh_keys = try(vm.ssh_keys, [data.hcloud_ssh_key.main.id])
location = try(vm.location, try(group.location, local.hcloud_default_location))
image = try(vm.image, local.hcloud_default_image)
server_type = try(vm.size, local.hcloud_default_server_type)
backups = try(vm.backups, false)
ansible_vars = try(vm.ansible_vars, null)
labels = concat(local.hcloud_global_labels, try(split(",", group.labels), []), try(split(",", vm.labels), []))
}
]
])
}
////////////////////////////////////////////////////////////////////////////////////////
// HETZNER RESOURCES
////////////////////////////////////////////////////////////////////////////////////////
resource "hcloud_network" "main" {
for_each = local.hetzner_network
name = try(each.value.name, "${var.ethereum_network}-${each.key}")
ip_range = each.value.ip_range
}
resource "hcloud_network_subnet" "main" {
for_each = local.hetzner_network_subnets
network_id = hcloud_network.main[each.key].id
type = "cloud"
network_zone = each.value.zone
ip_range = each.value.ip_range
}
data "hcloud_ssh_key" "main" {
fingerprint = var.hcloud_ssh_key_fingerprint
}
resource "hcloud_server" "main" {
for_each = {
for vm in local.hcloud_vms : "${vm.id}" => vm
}
name = "${var.ethereum_network}-${each.value.name}"
image = each.value.image
server_type = each.value.server_type
location = each.value.location
ssh_keys = each.value.ssh_keys
backups = each.value.backups
labels = { for label in each.value.labels : split(":", label)[0] => split(":", label)[1] }
}
resource "hcloud_server_network" "main" {
for_each = {
for vm in local.hcloud_vms : "${vm.id}" => vm
}
server_id = hcloud_server.main[each.key].id
network_id = hcloud_network.main[each.value.location].id
}
////////////////////////////////////////////////////////////////////////////////////////
// DNS NAMES
////////////////////////////////////////////////////////////////////////////////////////
data "cloudflare_zone" "default" {
name = "ethpandaops.io"
}
resource "cloudflare_record" "server_record" {
for_each = {
for vm in local.hcloud_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "${each.value.name}.${var.ethereum_network}"
type = "A"
value = hcloud_server.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
resource "cloudflare_record" "server_record_rpc" {
for_each = {
for vm in local.hcloud_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "rpc.${each.value.name}.${var.ethereum_network}"
type = "A"
value = hcloud_server.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
resource "cloudflare_record" "server_record_beacon" {
for_each = {
for vm in local.hcloud_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "bn.${each.value.name}.${var.ethereum_network}"
type = "A"
value = hcloud_server.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
////////////////////////////////////////////////////////////////////////////////////////
// GENERATED FILES AND OUTPUTS
////////////////////////////////////////////////////////////////////////////////////////
resource "local_file" "ansible_inventory" {
depends_on = [hcloud_server.main]
content = templatefile("ansible_inventory.tmpl",
{
ethereum_network_name = "${var.ethereum_network}"
groups = merge(
{ for group in local.hetzner_vm_groups : "${group.group_name}" => true... },
)
hosts = merge(
{
for key, server in hcloud_server.main : "${key}" => {
ip = server.ipv4_address
group = server.labels.group_name
validator_start = server.labels.val_start
validator_end = server.labels.val_end
tags = server.labels
hostname = split(".", key)[0]
cloud = "hetzner"
region = server.datacenter
}
}
)
}
)
filename = "../../ansible/inventories/gen-devnet-3/inventory.ini"
}