-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
136 lines (111 loc) · 3.69 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
resource "random_password" "admin_password" {
length = 32
special = false
}
# Create the Consul certificates
resource "tls_private_key" "consul" {
count = var.config.vm.replicas
algorithm = "RSA"
rsa_bits = "4096"
}
# Create the request to sign the cert with our CA
resource "tls_cert_request" "consul-req" {
count = var.config.vm.replicas
key_algorithm = tls_private_key.consul[count.index].algorithm
private_key_pem = tls_private_key.consul[count.index].private_key_pem
dns_names = [
"consul",
"consul.local",
]
subject {
common_name = "consul.local"
organization = var.config.organization.name
}
}
resource "tls_locally_signed_cert" "consul" {
count = var.config.vm.replicas
cert_request_pem = tls_cert_request.consul-req[count.index].cert_request_pem
ca_key_algorithm = var.config.certificate_authority.algorithm
ca_private_key_pem = var.config.certificate_authority.private_key_pem
ca_cert_pem = var.config.certificate_authority.certificate_pem
validity_period_hours = 8760
allowed_uses = [
"cert_signing",
"client_auth",
"digital_signature",
"key_encipherment",
"server_auth",
]
}
data "triton_image" "os" {
name = "postgresql12-patroni-consul"
version = var.config.image_version
}
resource "triton_machine" "postgresql" {
count = var.config.vm.replicas
name = "postgresql-${count.index}"
package = "k1-highcpu-8G"
image = data.triton_image.os.id
cns {
services = ["postgresql"]
}
networks = var.config.machine_networks
tags = {
role = "postgresql"
}
affinity = ["role!=~postgresql"]
connection {
host = self.primaryip
}
provisioner "remote-exec" {
inline = [
"mkdir -p /opt/local/etc/consul.d/certificates",
# "chown consul /opt/local/etc/consul.d/certificates",
# "chgrp consul /opt/local/etc/consul.d/certificates",
]
}
provisioner "file" {
content = var.config.certificate_authority.certificate_pem
destination = "/opt/local/etc/consul.d/certificates/ca.pem"
}
provisioner "file" {
content = tls_locally_signed_cert.consul[count.index].cert_pem
destination = "/opt/local/etc/consul.d/certificates/cert.pem"
}
provisioner "file" {
content = tls_private_key.consul[count.index].private_key_pem
destination = "/opt/local/etc/consul.d/certificates/private_key.pem"
}
provisioner "file" {
content = templatefile("${path.module}/templates/consul.hcl.tpl", {
datacenter_name = var.config.consul_datacenter_name,
node_name = "postgresql-${count.index}"
consul_addr = var.config.consul_addr,
encryption_key = var.config.consul_encryption_key,
})
destination = "/opt/local/etc/consul.d/consul.hcl"
}
provisioner "remote-exec" {
inline = [
"svcadm enable consul",
]
}
provisioner "file" {
content = templatefile("${path.module}/templates/patroni.yml.tpl", {
hostname = "postgresql-${count.index}"
consul_addr = var.config.consul_addr
consul_scope = var.config.consul_scope
consul_namespace = var.config.consul_namespace
admin_password = random_password.admin_password.result
listen_ip = self.primaryip
})
destination = "/var/pgsql/patroni.yml"
}
provisioner "remote-exec" {
inline = [
"chown postgres /var/pgsql/patroni.yml",
"chgrp postgres /var/pgsql/patroni.yml",
"svcadm enable patroni",
]
}
}