-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
125 lines (114 loc) · 3.51 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
terraform {
backend "consul" {
address = "sophon:8500"
scheme = "http"
path = "terraform/drift-keeper"
}
required_providers {
linode = {
source = "linode/linode"
version = "2.13.0"
}
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
provider "digitalocean" {
token = var.do_token
}
provider "linode" {
token = var.linode_token
}
locals {
monitoring_config = {
env = base64encode(templatefile("templates/monitoring/env.monitoring.tpl", var.monitoring))
prometheus = base64encode(templatefile("templates/monitoring/prometheus/prometheus.yml.tpl", {
wallet_address = var.bot.wallet_address
}))
prometheus_web = base64encode(templatefile("templates/monitoring/prometheus/web.yml.tpl", {
prometheus_password_bcrypt = bcrypt(var.monitoring.prometheus_password)
}))
}
cloud_config = { for s in concat(var.linode_instances, var.digitalocean_instances) : s.label => templatefile("cloud-init/cloud-config-small.yaml", {
ntp_server = s.ntp_server
env_file = base64encode(templatefile("templates/bot/env.tpl", merge(var.bot, {
jito_block_engine_url = s.jito_block_engine_url
})))
config_file = base64encode(templatefile("templates/bot/config.yaml.tpl", {
use_jito = s.use_jito
}))
env_monitoring_file = local.monitoring_config.env
prometheus_config_file = local.monitoring_config.prometheus
prometheus_web_file = local.monitoring_config.prometheus_web
docker_compose_file = base64encode(templatefile("templates/bot/docker-compose.yaml.tpl", {
docker_image = var.bot.docker_image
docker_image_wallet_tracker = var.bot.docker_image_wallet_tracker
}))
}) }
}
resource "linode_sshkey" "master" {
label = "master-key"
ssh_key = chomp(file("~/.ssh/id_rsa.pub"))
}
resource "digitalocean_ssh_key" "default" {
name = "master-key"
public_key = chomp(file("~/.ssh/id_rsa.pub"))
}
resource "linode_instance" "keeper" {
for_each = { for s in var.linode_instances : s.label => s }
label = each.key
image = each.value.image
group = each.value.group
region = each.value.region
type = each.value.type
authorized_keys = [linode_sshkey.master.ssh_key]
metadata {
user_data = base64encode(local.cloud_config[each.key])
}
lifecycle {
ignore_changes = [
metadata
]
}
}
resource "digitalocean_droplet" "keeper" {
for_each = { for s in var.digitalocean_instances : s.label => s }
image = each.value.image
name = each.key
region = each.value.region
size = each.value.type
ssh_keys = [digitalocean_ssh_key.default.fingerprint]
user_data = local.cloud_config[each.key]
lifecycle {
ignore_changes = [
user_data
]
}
}
output "instances" {
value = merge(
tomap({ for k, v in linode_instance.keeper : k => v.ip_address }),
tomap({ for k, v in digitalocean_droplet.keeper : k => v.ipv4_address })
)
}
output "panopticonf" {
value = templatefile("templates/monitoring/prometheus/panopticon.yml.tpl", {
user = "prom"
password = var.monitoring.prometheus_password
targets = <<-EOT
%{for k, v in merge(
tomap({ for k, v in linode_instance.keeper : k => v.ip_address }),
tomap({ for k, v in digitalocean_droplet.keeper : k => v.ipv4_address })
)}
- targets: ['${v}:9090']
labels:
server: ${k}
%{endfor}
EOT
})
}
output "configurations" {
value = local.cloud_config
}