-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhcloud_server.tf
executable file
·151 lines (133 loc) · 3.82 KB
/
hcloud_server.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
locals {
server_configuration = templatefile("${path.module}/scripts/server_setup.sh", {
enable_nomad_acls = var.enable_nomad_acls
SERVER_COUNT = var.nomad_server_count
IP_RANGE = var.virtual_network_cidr
SERVER_IPs = jsonencode([for server in hcloud_server.server : (server.network[*].ip)[0]])
})
client_configuration = templatefile("${path.module}/scripts/client_setup.sh", {
enable_nomad_acls = var.enable_nomad_acls
IP_RANGE = var.virtual_network_cidr
SERVER_IPs = jsonencode([for server in hcloud_server.server : (server.network[*].ip)[0]])
})
}
resource "hcloud_server" "server" {
depends_on = [
hcloud_network_subnet.network
]
count = var.nomad_server_count
name = "nomad-server-${count.index}"
server_type = var.hetzner_server_sku
image = "ubuntu-20.04"
location = element(var.node_locations, count.index)
ssh_keys = [hcloud_ssh_key.default.id]
labels = {
"nomad-server" = "any"
}
network {
network_id = hcloud_network.network.id
}
public_net {
ipv6_enabled = false
}
user_data = templatefile("${path.module}/scripts/base_configuration.sh", {
CONSUL_VERSION = var.apt_consul_version
NOMAD_VERSION = var.apt_nomad_version
})
provisioner "remote-exec" {
inline = [
"echo 'Waiting for cloud-init to complete...'",
"cloud-init status --wait > /dev/null",
"echo 'Completed cloud-init!'"
]
connection {
type = "ssh"
host = self.ipv4_address
user = "root"
private_key = tls_private_key.machines.private_key_openssh
}
}
}
resource "null_resource" "deployment_server" {
count = var.nomad_server_count
triggers = {
"vm" = "${hcloud_server.server[count.index].id}"
"config" = "${local.server_configuration}"
}
connection {
type = "ssh"
user = "root"
private_key = tls_private_key.machines.private_key_openssh
host = hcloud_server.server[count.index].ipv4_address
}
provisioner "file" {
content = local.server_configuration
destination = "setup.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x setup.sh",
"./setup.sh"
]
}
}
resource "hcloud_server" "client" {
depends_on = [
hcloud_network_subnet.network
]
count = var.nomad_client_count
name = "nomad-client-${count.index}"
server_type = var.hetzner_client_sku
image = "ubuntu-20.04"
location = element(var.node_locations, count.index)
ssh_keys = [hcloud_ssh_key.default.id]
labels = {
"nomad-client" = "any"
}
network {
network_id = hcloud_network.network.id
}
public_net {
ipv6_enabled = false
}
user_data = templatefile("${path.module}/scripts/base_configuration.sh", {
CONSUL_VERSION = var.apt_consul_version
NOMAD_VERSION = var.apt_nomad_version
})
provisioner "remote-exec" {
inline = [
"echo 'Waiting for cloud-init to complete...'",
"cloud-init status --wait > /dev/null",
"echo 'Completed cloud-init!'"
]
connection {
type = "ssh"
host = self.ipv4_address
user = "root"
private_key = tls_private_key.machines.private_key_openssh
}
}
}
resource "null_resource" "deployment_client" {
count = var.nomad_client_count
triggers = {
"vm" = "${hcloud_server.client[count.index].id}"
"config" = "${local.client_configuration}"
}
connection {
type = "ssh"
user = "root"
private_key = tls_private_key.machines.private_key_openssh
host = hcloud_server.client[count.index].ipv4_address
}
provisioner "file" {
content = local.client_configuration
destination = "setup.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x setup.sh",
"./setup.sh"
]
}
}