Add template to createdb and cleanup

This commit is contained in:
Daniel Berteaud 2023-12-15 22:45:16 +01:00
parent 2c84220d3d
commit 67863c3a97
4 changed files with 21 additions and 13 deletions

View File

@ -7,6 +7,7 @@ for IDX in $(printenv | grep -E '^PG_DB_([0-9]+)=' | sed -E 's/^PG_DB_([0-9]+)=.
echo "Found DB ${DB_NAME} to create"
DB_OWNER=$(printenv PG_DB_${IDX}_OWNER || echo "${DB_NAME}")
DB_ENCODING=$(printenv PG_DB_${IDX}_ENCODING || echo "UTF8")
DB_TEMPLATE=$(printenv PG_DB_${IDX}_TEMPLATE || echo "")
DB_LOCALE=$(printenv PG_DB_${IDX}_LOCALE || echo "${LANG}")
DB_EXTENSIONS=$(printenv PG_DB_${IDX}_EXTENSIONS || echo "")
if [ "${LDAP2PG_MODE}" = "real" ]; then
@ -15,9 +16,13 @@ for IDX in $(printenv | grep -E '^PG_DB_([0-9]+)=' | sed -E 's/^PG_DB_([0-9]+)=.
SELECT 'CREATE ROLE "${DB_OWNER}"'
WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${DB_OWNER}')\gexec
_EOSQL
echo "Create postgres database ${DB_NAME} (OWNER \"${DB_OWNER}\" ENCODING \"${DB_ENCODING}\" LOCALE \"${DB_LOCALE}\") if needed"
TEMPLATE=""
if [ "${DB_TEMPLATE}" != "" ]; then
TEMPLATE="TEMPLATE ${DB_TEMPLATE}"
fi
echo "Create postgres database ${DB_NAME} (OWNER \"${DB_OWNER}\" ENCODING \"${DB_ENCODING}\" LOCALE \"${DB_LOCALE}\" ${TEMPLATE}) if needed"
psql <<_EOSQL
SELECT 'CREATE DATABASE "${DB_NAME}" OWNER "${DB_OWNER}" ENCODING "${DB_ENCODING}" LOCALE "${DB_LOCALE}"'
SELECT 'CREATE DATABASE "${DB_NAME}" OWNER "${DB_OWNER}" ENCODING "${DB_ENCODING}" LOCALE "${DB_LOCALE}" ${TEMPLATE}'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_NAME}')\gexec
_EOSQL
psql -d ${DB_NAME} <<_EOSQL

View File

@ -58,6 +58,9 @@ job "[[ .pg.instance ]]-manage[[ $c.consul.suffix ]]" {
[[- if has $db "locale" ]]
PG_DB_[[ $idx ]]_LOCALE = [[ $db.locale | toJSON ]]
[[- end ]]
[[- if has $db "template" ]]
PG_DB_[[ $idx ]]_TEMPLATE = [[ $db.template | toJSON ]]
[[- end ]]
[[- if has $db "extensions" ]]
PG_DB_[[ $idx ]]_EXTENSIONS = "[[ join $db.extensions "," ]]"
[[- end ]]

View File

@ -2,7 +2,7 @@
job [[ .pg.instance | toJSON ]] {
[[ template "common/job_start.tpl" $c ]]
[[ template "common/job_start" $c ]]
group "server" {
@ -44,8 +44,8 @@ job [[ .pg.instance | toJSON ]] {
service {
name = "[[ .pg.instance ]][[ $c.consul.suffix ]]"
port = 5432
[[- template "common/prometheus_meta.tpl" $c ]]
[[- template "common/connect.tpl" $c ]]
[[- template "common/prometheus_meta" $c ]]
[[- template "common/connect" $c ]]
tags = [
"postgres-${NOMAD_ALLOC_INDEX}",
[[- if .pg.server.traefik.enabled ]]
@ -163,7 +163,7 @@ job [[ .pg.instance | toJSON ]] {
disable_file = true
}
[[- template "common/file_env.tpl" $c.env ]]
[[- template "common/file_env" $c.env ]]
template {
data = <<_EOT
@ -290,7 +290,7 @@ _EOT
destination = "/backup"
}
[[ template "common/resources.tpl" .pg.server.resources ]]
[[ template "common/resources" .pg.server.resources ]]
}
[[- if and (not .pg.server.recovery) (or .pg.backup.pgbackrest.enabled .pg.backup.dumps.enabled) ]]
@ -321,7 +321,7 @@ _EOT
PGHOST = "/alloc/data/postgres"
}
[[- template "common/file_env.tpl" $c.env ]]
[[- template "common/file_env" $c.env ]]
template {
data =<<_EOT
@ -350,14 +350,14 @@ _EOT
destination = "/data"
}
[[ template "common/resources.tpl" .pg.backup.resources ]]
[[ template "common/resources" .pg.backup.resources ]]
}
[[- end ]]
[[- if and $c.prometheus.enabled (not .pg.server.recovery) ]]
[[ template "common/task.metrics_proxy.tpl" merge (dict "prometheus" (dict "additional_proxy_conf" (tmpl.Exec "postgres/nginx_patroni.conf.tpl" $c))) $c ]]
[[ template "common/task.metrics_proxy" merge (dict "prometheus" (dict "additional_proxy_conf" (tmpl.Exec "postgres/nginx_patroni.conf.tpl" $c))) $c ]]
task "exporter" {
driver = [[ $c.nomad.driver | toJSON ]]
@ -384,7 +384,7 @@ _EOT
}
env {
[[ template "common/env.tpl" .pg.exporter.env ]]
[[ template "common/env" .pg.exporter.env ]]
}
template {

View File

@ -8,7 +8,7 @@ pg:
# Postgres server settings
server:
# The image to use
image: danielberteaud/patroni:15.23.12-3
image: '[[ .docker.repo ]]patroni:15.23.12-3'
# Number of postgres instance. Patroni will handle leader election and replication
count: 1
@ -170,7 +170,7 @@ pg:
# manage can create database, users and sync permissions from LDAP (using ldap2pg)
manage:
# Image to use
image: danielberteaud/ldap2pg:6.0-5
image: '[[ .docker.repo ]]ldap2pg:6.0-6'
# Resource allocation
resources: