add db management - https://gitlab.confdroid.com/internal/confdroid_management/-/issues/239
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -9,6 +9,7 @@
|
|||||||
"csvlog",
|
"csvlog",
|
||||||
"csvlogs",
|
"csvlogs",
|
||||||
"datestyle",
|
"datestyle",
|
||||||
|
"datname",
|
||||||
"ecdh",
|
"ecdh",
|
||||||
"fdatasync",
|
"fdatasync",
|
||||||
"geqo",
|
"geqo",
|
||||||
@@ -34,6 +35,7 @@
|
|||||||
"multixact",
|
"multixact",
|
||||||
"naptime",
|
"naptime",
|
||||||
"nestloop",
|
"nestloop",
|
||||||
|
"onlyif",
|
||||||
"partitionwise",
|
"partitionwise",
|
||||||
"pghba",
|
"pghba",
|
||||||
"pgsql",
|
"pgsql",
|
||||||
@@ -47,6 +49,7 @@
|
|||||||
"tablespaces",
|
"tablespaces",
|
||||||
"tidscan",
|
"tidscan",
|
||||||
"timezonesets",
|
"timezonesets",
|
||||||
|
"trgm",
|
||||||
"usename",
|
"usename",
|
||||||
"walsender",
|
"walsender",
|
||||||
"writethrough",
|
"writethrough",
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
# @param [String] pl_server_key the name of the server key
|
# @param [String] pl_server_key the name of the server key
|
||||||
# @param [String] pl_ca_crt the name of the CA crt
|
# @param [String] pl_ca_crt the name of the CA crt
|
||||||
# @param [Boolean] pl_manage_roles Whether to manage roles
|
# @param [Boolean] pl_manage_roles Whether to manage roles
|
||||||
|
# @param [Boolean] pl_manage_databases Whether to manage databases
|
||||||
# @summary Class contains all parameters for the postgresql_cd module.
|
# @summary Class contains all parameters for the postgresql_cd module.
|
||||||
##############################################################################
|
##############################################################################
|
||||||
class postgresql_cd::params (
|
class postgresql_cd::params (
|
||||||
@@ -45,6 +46,7 @@ class postgresql_cd::params (
|
|||||||
String $pl_server_key = 'server.key',
|
String $pl_server_key = 'server.key',
|
||||||
String $pl_ca_crt = 'root.crt',
|
String $pl_ca_crt = 'root.crt',
|
||||||
Boolean $pl_manage_roles = true,
|
Boolean $pl_manage_roles = true,
|
||||||
|
Boolean $pl_manage_databases = true,
|
||||||
|
|
||||||
) {
|
) {
|
||||||
$fqdn = $facts['networking']['fqdn']
|
$fqdn = $facts['networking']['fqdn']
|
||||||
|
|||||||
47
manifests/server/databases/db_df.pp
Normal file
47
manifests/server/databases/db_df.pp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
## postgresql_cd::server::databases::db_df
|
||||||
|
# Module name: postgresql_cd
|
||||||
|
# Author: Arne Teuke (arne_teuke@confdroid.com.com)
|
||||||
|
# @summary define manages databases
|
||||||
|
# @see https://www.postgresql.org/docs/9.6/static/managing-databases.html
|
||||||
|
# @param [String] pl_db_name the name of the database to be created.
|
||||||
|
# @param [String] pl_owner_name the name of the owner for the database
|
||||||
|
# (optional), if none specified, the postgresql defaults will apply.
|
||||||
|
# @param [String] pl_db_action whether to create or drop the database.
|
||||||
|
# 'CREATE DATABASE' creates it, 'DROP DATABASE' drops it.
|
||||||
|
# @param [String] pl_db_extension
|
||||||
|
##############################################################################
|
||||||
|
define postgresql_cd::server::databases::db_df (
|
||||||
|
|
||||||
|
Optional[String] $pl_db_name = undef,
|
||||||
|
Optional[String] $pl_owner_name = undef,
|
||||||
|
Optional[String] $pl_db_action = undef,
|
||||||
|
String $pl_db_extension = 'pg_trgm',
|
||||||
|
) {
|
||||||
|
$pl_manage_databases = $postgresql_cd::params::pl_manage_databases
|
||||||
|
|
||||||
|
if $pl_manage_databases == true {
|
||||||
|
# create databases
|
||||||
|
|
||||||
|
if $pl_db_action == 'CREATE DATABASE' {
|
||||||
|
exec { "create_database_${name}":
|
||||||
|
command => template('postgresql_cd/server/databases/db_create_sql.erb'),
|
||||||
|
user => 'postgres',
|
||||||
|
path => ['/usr/bin','/bin'],
|
||||||
|
cwd => '/tmp',
|
||||||
|
unless => template('postgresql_cd/server/databases/unless_db_sql.erb'),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Drop databases
|
||||||
|
|
||||||
|
if $pl_db_action == 'DROP DATABASE' {
|
||||||
|
exec { "drop_database_${name}":
|
||||||
|
command => template('postgresql_cd/server/databases/db_drop_sql.erb'),
|
||||||
|
user => 'postgres',
|
||||||
|
path => ['/usr/bin','/bin'],
|
||||||
|
cwd => '/tmp',
|
||||||
|
onlyif => template('postgresql_cd/server/databases/unless_drop_sql.erb'),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
templates/server/databases/db_create_sql.erb
Normal file
2
templates/server/databases/db_create_sql.erb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = '<%= @pl_db_name %>'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE <%= @pl_db_name %> OWNER '<%= @pl_owner_name %>' "
|
||||||
|
psql -U postgres <%= @pl_db_name %> -c 'create extension if not exists <%= @pl_db_extension %>'
|
||||||
1
templates/server/databases/db_drop_sql.erb
Normal file
1
templates/server/databases/db_drop_sql.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
dropdb -U postgres <%= @pl_db_name %> --if-exists
|
||||||
1
templates/server/databases/unless_db_sql.erb
Normal file
1
templates/server/databases/unless_db_sql.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
psql -U postgres -c "SELECT datname FROM pg_database WHERE datname='<%= @pl_db_name %>' " | grep -q 1
|
||||||
1
templates/server/databases/unless_drop_sql.erb
Normal file
1
templates/server/databases/unless_drop_sql.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
psql -U postgres -c "SELECT datname FROM pg_database WHERE datname='<%= @pl_db_name %>' " | grep -q 1
|
||||||
Reference in New Issue
Block a user