Skip to content

Commit df9bd2a

Browse files
committed
Updated
1 parent afb67b1 commit df9bd2a

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

postgres/nomad/postgres.hcl

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ variable "replication_password" {
105105
// LOCALS
106106

107107
locals {
108-
data_path = var.data == "" ? "${NOMAD_ALLOC_DIR}/data" : "/var/lib/postgresql/data/pgdata"
109-
replication_slots = [ for host in var.replicas : format("replica_%s", host) ]
108+
data_path = var.data == "" ? "${NOMAD_ALLOC_DIR}/data" : "/var/lib/postgresql/data/pgdata"
109+
replication_slots = [for host in var.replicas : format("replica_%s", host)]
110110
}
111111

112112
///////////////////////////////////////////////////////////////////////////////
@@ -147,7 +147,7 @@ job "postgres" {
147147
}
148148

149149
ephemeral_disk {
150-
migrate = true
150+
migrate = false
151151
}
152152

153153
task "server" {
@@ -160,21 +160,21 @@ job "postgres" {
160160
config {
161161
image = var.docker_image
162162
force_pull = var.docker_always_pull
163-
ports = [ "postgres" ]
163+
ports = ["postgres"]
164164
dns_servers = var.service_dns
165165
volumes = compact([
166166
var.data == "" ? "" : format("%s:/var/lib/postgresql/data", var.data)
167167
])
168168
}
169169

170170
env {
171-
POSTGRES_USER = var.root_user
172-
POSTGRES_PASSWORD = var.root_password
173-
POSTGRES_DB = var.database
174-
PGDATA = local.data_path
175-
POSTGRES_REPLICATION_USER = var.replication_user
171+
POSTGRES_USER = var.root_user
172+
POSTGRES_PASSWORD = var.root_password
173+
POSTGRES_DB = var.database
174+
PGDATA = local.data_path
175+
POSTGRES_REPLICATION_USER = var.replication_user
176176
POSTGRES_REPLICATION_PASSWORD = var.replication_password
177-
POSTGRES_REPLICATION_SLOT = join(",",local.replication_slots)
177+
POSTGRES_REPLICATION_SLOT = join(",", local.replication_slots)
178178
}
179179
}
180180
}
@@ -186,14 +186,14 @@ job "postgres" {
186186
count = length(var.replicas)
187187

188188
constraint {
189-
attribute = node.unique.name
190-
operator = "set_contains_any"
191-
value = join(",",var.replicas)
189+
attribute = node.unique.name
190+
operator = "set_contains_any"
191+
value = join(",", var.replicas)
192192
}
193193

194194
constraint {
195-
operator = "distinct_hosts"
196-
value = "true"
195+
operator = "distinct_hosts"
196+
value = "true"
197197
}
198198

199199
network {
@@ -204,7 +204,7 @@ job "postgres" {
204204
}
205205

206206
service {
207-
tags = [ "postgres", "replica" ]
207+
tags = ["postgres", "replica"]
208208
name = format("%s-replica", var.service_name)
209209
port = "postgres"
210210
provider = var.service_provider
@@ -214,11 +214,41 @@ job "postgres" {
214214
migrate = false
215215
}
216216

217+
task "wait-for-primary" {
218+
lifecycle {
219+
hook = "prestart"
220+
sidecar = false
221+
}
222+
223+
meta {
224+
primary_service_name = format("%s-primary", var.service_name)
225+
}
226+
227+
template {
228+
data = <<-EOH
229+
{{ $primary_service_name := env "NOMAD_META_primary_service_name" }}
230+
{{ range nomadService $primary_service_name -}}
231+
POSTGRES_REPLICATION_PRIMARY="host={{ .Address }} port={{ .Port }}"
232+
{{ end }}
233+
EOH
234+
destination = "tmp/config.env"
235+
env = true
236+
}
237+
238+
driver = "docker"
239+
config {
240+
image = var.docker_image
241+
force_pull = var.docker_always_pull
242+
command = "pg_isready"
243+
args = ["-d", "${POSTGRES_REPLICATION_PRIMARY}", "-t", "60"]
244+
}
245+
}
246+
217247
task "server" {
218248
driver = "docker"
219249

220250
resources {
221-
memory = 1024
251+
memory = 512
222252
}
223253

224254
config {
@@ -236,13 +266,13 @@ job "postgres" {
236266
}
237267

238268
template {
239-
data = <<-EOH
269+
data = <<-EOH
240270
{{ $primary_service_name := env "NOMAD_META_primary_service_name" }}
241271
{{ range nomadService $primary_service_name -}}
242272
POSTGRES_REPLICATION_PRIMARY="host={{ .Address }} port={{ .Port }}"
243273
{{ end }}
244274
EOH
245-
destination = "secrets/config.env"
275+
destination = "tmp/config.env"
246276
env = true
247277
}
248278

@@ -256,5 +286,5 @@ job "postgres" {
256286
POSTGRES_REPLICATION_SLOT = format("replica_%s", node.unique.name)
257287
}
258288
} // task
259-
} // group
260-
} // job
289+
} // group
290+
} // job

0 commit comments

Comments
 (0)