generated from ethpandaops/template-devnets
-
Notifications
You must be signed in to change notification settings - Fork 6
/
digitalocean.tf.disabled
296 lines (267 loc) · 9.1 KB
/
digitalocean.tf.disabled
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
////////////////////////////////////////////////////////////////////////////////////////
// VARIABLES
////////////////////////////////////////////////////////////////////////////////////////
variable "digitalocean_project_name" {
type = string
default = "ProjectName"
}
variable "digitalocean_ssh_key_name" {
type = string
default = "examplekey"
}
variable "digitalocean_regions" {
default = [
"nyc1",
"sgp1",
"lon1",
"nyc3",
"ams3",
"fra1",
"tor1",
"blr1",
"sfo3",
"syd1"
]
}
////////////////////////////////////////////////////////////////////////////////////////
// LOCALS
////////////////////////////////////////////////////////////////////////////////////////
locals {
base_cidr_block = var.base_cidr_block
digitalocean_vpcs = {
for region in var.digitalocean_regions : region => {
name = "${var.ethereum_network}-${region}"
region = region
ip_range = cidrsubnet(local.base_cidr_block, 8, index(var.digitalocean_regions, region))
}
}
}
locals {
digitalocean_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}" = {
tags = "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)}"
region = element(var.digitalocean_regions, i % length(var.digitalocean_regions))
size = try(vm_group.size, local.digitalocean_default_size)
}
}
}
]
])
}
locals {
digitalocean_default_region = "ams3"
digitalocean_default_size = "c-2"
digitalocean_default_image = "debian-12-x64"
digitalocean_global_tags = [
"Owner:Devops",
"EthNetwork:${var.ethereum_network}"
]
# flatten vm_groups so that we can use it with for_each()
digitalocean_vms = flatten([
for group in local.digitalocean_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.digitalocean_ssh_key.main.fingerprint])
region = try(vm.region, try(group.region, local.digitalocean_default_region))
image = try(vm.image, local.digitalocean_default_image)
size = try(vm.size, local.digitalocean_default_size)
resize_disk = try(vm.resize_disk, true)
monitoring = try(vm.monitoring, true)
backups = try(vm.backups, false)
ipv6 = try(vm.ipv6, false)
ansible_vars = try(vm.ansible_vars, null)
vpc_uuid = try(vm.vpc_uuid, try(
digitalocean_vpc.main[vm.region].id,
digitalocean_vpc.main[try(group.region, local.digitalocean_default_region)].id
))
tags = concat(local.digitalocean_global_tags, try(split(",", group.tags), []), try(split(",", vm.tags), []))
}
]
])
}
////////////////////////////////////////////////////////////////////////////////////////
// DIGITALOCEAN RESOURCES
////////////////////////////////////////////////////////////////////////////////////////
data "digitalocean_project" "main" {
name = var.digitalocean_project_name
}
data "digitalocean_ssh_key" "main" {
name = var.digitalocean_ssh_key_name
}
resource "digitalocean_vpc" "main" {
for_each = local.digitalocean_vpcs
name = each.value["name"]
region = each.value["region"]
ip_range = each.value["ip_range"]
}
resource "digitalocean_droplet" "main" {
for_each = {
for vm in local.digitalocean_vms : "${vm.id}" => vm
}
name = "${var.ethereum_network}-${each.value.name}"
region = each.value.region
ssh_keys = each.value.ssh_keys
image = each.value.image
size = each.value.size
resize_disk = each.value.resize_disk
monitoring = each.value.monitoring
backups = each.value.backups
ipv6 = each.value.ipv6
vpc_uuid = each.value.vpc_uuid
tags = each.value.tags
}
resource "digitalocean_project_resources" "droplets" {
for_each = digitalocean_droplet.main
project = data.digitalocean_project.main.id
resources = [each.value.urn]
}
resource "digitalocean_firewall" "main" {
name = "${var.ethereum_network}-nodes"
// Tags are used to select which droplets should
// be assigned to this firewall.
tags = [
"EthNetwork:${var.ethereum_network}"
]
// SSH
inbound_rule {
protocol = "tcp"
port_range = "22"
source_addresses = ["0.0.0.0/0", "::/0"]
}
// Nginx / Web
inbound_rule {
protocol = "tcp"
port_range = "80"
source_addresses = ["0.0.0.0/0", "::/0"]
}
inbound_rule {
protocol = "tcp"
port_range = "443"
source_addresses = ["0.0.0.0/0", "::/0"]
}
// Consensus layer p2p port
inbound_rule {
protocol = "tcp"
port_range = "9000-9001"
source_addresses = ["0.0.0.0/0", "::/0"]
}
inbound_rule {
protocol = "udp"
port_range = "9000-9001"
source_addresses = ["0.0.0.0/0", "::/0"]
}
// Execution layer p2p Port
inbound_rule {
protocol = "tcp"
port_range = "30303"
source_addresses = ["0.0.0.0/0", "::/0"]
}
inbound_rule {
protocol = "udp"
port_range = "30303"
source_addresses = ["0.0.0.0/0", "::/0"]
}
// Allow all outbound traffic
outbound_rule {
protocol = "tcp"
port_range = "1-65535"
destination_addresses = ["0.0.0.0/0", "::/0"]
}
outbound_rule {
protocol = "udp"
port_range = "1-65535"
destination_addresses = ["0.0.0.0/0", "::/0"]
}
outbound_rule {
protocol = "icmp"
destination_addresses = ["0.0.0.0/0", "::/0"]
}
depends_on = [digitalocean_project_resources.droplets]
}
////////////////////////////////////////////////////////////////////////////////////////
// DNS NAMES
////////////////////////////////////////////////////////////////////////////////////////
data "cloudflare_zone" "default" {
name = "ethpandaops.io"
}
resource "cloudflare_record" "server_record" {
for_each = {
for vm in local.digitalocean_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "${each.value.name}.${var.ethereum_network}"
type = "A"
value = digitalocean_droplet.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
resource "cloudflare_record" "server_record_short" {
for_each = {
for vm in local.digitalocean_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "${each.value.name}.${var.ethereum_network}"
type = "A"
value = digitalocean_droplet.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
resource "cloudflare_record" "server_record_rpc" {
for_each = {
for vm in local.digitalocean_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "rpc.${each.value.name}.${var.ethereum_network}"
type = "A"
value = digitalocean_droplet.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
resource "cloudflare_record" "server_record_beacon" {
for_each = {
for vm in local.digitalocean_vms : "${vm.id}" => vm
}
zone_id = data.cloudflare_zone.default.id
name = "bn.${each.value.name}.${var.ethereum_network}"
type = "A"
value = digitalocean_droplet.main[each.value.id].ipv4_address
proxied = false
ttl = 120
}
////////////////////////////////////////////////////////////////////////////////////////
// GENERATED FILES AND OUTPUTS
////////////////////////////////////////////////////////////////////////////////////////
resource "local_file" "ansible_inventory" {
content = templatefile("ansible_inventory.tmpl",
{
ethereum_network_name = "${var.ethereum_network}"
groups = merge(
{ for group in local.digitalocean_vm_groups : "${group.group_name}" => true... },
)
hosts = merge(
{
for key, server in digitalocean_droplet.main : "do.${key}" => {
ip = "${server.ipv4_address}"
group = try(split(":", tolist(server.tags)[2])[1], "unknown")
validator_start = try(split(":", tolist(server.tags)[4])[1], 0)
validator_end = try(split(":", tolist(server.tags)[3])[1], 0) # if the tag is not a number it will be 0 - e.g no validator keys
tags = "${server.tags}"
hostname = "${split(".", key)[0]}"
cloud = "digitalocean"
region = "${server.region}"
}
}
)
}
)
filename = "../../ansible/inventories/devnet-0/inventory.ini"
}