Skip to content

Commit 6a29636

Browse files
Tom HeyDavidS
Tom Hey
authored andcommitted
(MODULES-661) Remote DB support
Adds connection-settings (for remote DB support) when creating DB resources. Connection-settings allows a hash of options that can be used when connecting the a remote DB (such as PGHOST, PGPORT, PGPASSWORD PGSSLKEY) and a special option DBVERSION indicating the version of the remote database. Including - Puppet updates - Documentation updates - RSpec unit test updates - RSpec acceptance test updates - Some test coverage for connection-settings - Working acceptance test... Basic vagrant setup: * Two boxes, server and client * Runs puppet code to on server to setup a postgres server that allows all connections and md5 connections, creates db puppet to look at * Runs puppet code on client to make a server that a psql command can be run against puppet db on other server * Does some fancy stuff to get the fact of the IP from the first server to connect to - Backwards compatible, with deprecation warnings around old parameters
1 parent 750a8b6 commit 6a29636

22 files changed

+613
-166
lines changed

README.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,43 @@ In this example, you would grant ALL privileges on the test1 database and on the
120120

121121
At this point, you would just need to plunk these database name/username/password values into your PuppetDB config files, and you are good to go.
122122

123+
###Managing remote users, roles and permissions
124+
125+
Remote SQL objects are managed using the same Puppet resources as local SQL objects with the additional of a connect_settings hash. This provides control over how Puppet should connect to the remote Postgres instances and the version that should be used when generating SQL commands.
126+
127+
When provided the connect_settings hash can contain environment variables to control Postgres client connections, such as: PGHOST, PGPORT, PGPASSWORD PGSSLKEY (see http://www.postgresql.org/docs/9.4/static/libpq-envars.html) Additionally the special value of DBVERSION can be provided to specify the target database's version. If the connect_settings hash is omitted or empty then Puppet will connect to the local Postgres instance.
128+
129+
A connect_settings hash can be provided with each of the Puppet resources or a default connect_settings hash can be set in postgresql::globals. Per resource configuration of connect_settings allows for SQL object to be creating on multiple database by multiple users.
130+
131+
$connection_settings_super2 = {
132+
'PGUSER' => "super2",
133+
'PGPASSWORD' => "foobar2",
134+
'PGHOST' => "127.0.0.1",
135+
'PGPORT' => "5432",
136+
'PGDATABASE' => "postgres",
137+
}
138+
139+
include postgresql::server
140+
141+
# Connect with no special settings, i.e domain sockets, user postges
142+
postgresql::server::role{'super2':
143+
password_hash => "foobar2",
144+
superuser => true,
145+
146+
connect_settings => {},
147+
require => [
148+
Class['postgresql::globals'],
149+
Class['postgresql::server::service'],
150+
],
151+
}
152+
153+
# Now using this new user connect via TCP
154+
postgresql::server::database { 'db1':
155+
connect_settings => $connection_settings_super2,
156+
157+
require => Postgresql::Server::Role['super2'],
158+
}
159+
123160
Reference
124161
---------
125162

@@ -241,6 +278,7 @@ This setting is used to specify the name of the default database to connect with
241278
Path to the `initdb` command.
242279

243280
####`createdb_path`
281+
**Deprecated**
244282
Path to the `createdb` command.
245283

246284
####`psql_path`
@@ -370,6 +408,7 @@ List of strings for access control for connection method, users, databases, IPv6
370408
Path to the `initdb` command.
371409

372410
####`createdb_path`
411+
**Deprecated**
373412
Path to the `createdb` command.
374413

375414
####`psql_path`
@@ -539,7 +578,7 @@ Value for the setting.
539578

540579

541580
###Resource: postgresql::server::db
542-
This is a convenience resource that creates a database, user and assigns necessary permissions in one go.
581+
This is a convenience resource that creates a local database, user and assigns necessary permissions in one go.
543582

544583
For example, to create a database called `test1` with a corresponding user of the same name, you can use:
545584

@@ -612,6 +651,8 @@ Override the locale during creation of the database. Defaults to the default def
612651
####`istemplate`
613652
Define database as a template. Defaults to `false`.
614653

654+
####`connect_settings`
655+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
615656

616657
###Resource: postgresql::server::database\_grant
617658
This defined type manages grant based access privileges for users, wrapping the `postgresql::server::database_grant` for database specific permissions. Consult the PostgreSQL documentation for `grant` for more information.
@@ -634,6 +675,8 @@ Database to execute the grant against. This should not ordinarily be changed fro
634675
####`psql_user`
635676
OS user for running `psql`. Defaults to the default user for the module, usually `postgres`.
636677

678+
####`connect_settings`
679+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
637680

638681
###Resource: postgresql::server::extension
639682
Manages a postgresql extension.
@@ -683,6 +726,9 @@ OS user for running `psql`. Defaults to the default user for the module, usually
683726
####`port`
684727
Port to use when connecting. Default to 'undef' which generally defaults to 5432 depending on your PostgreSQL packaging.
685728

729+
####`connect_settings`
730+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
731+
686732
###Resource: postgresql::server::pg\_hba\_rule
687733
This defined type allows you to create an access rule for `pg_hba.conf`. For more details see the [PostgreSQL documentation](http://www.postgresql.org/docs/8.2/static/auth-pg-hba-conf.html).
688734

@@ -886,6 +932,9 @@ Specifies how many concurrent connections the role can make. Defaults to `-1` me
886932
####`username`
887933
The username of the role to create, defaults to `namevar`.
888934

935+
####`connect_settings`
936+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
937+
889938
###Resource: postgresql::server::schema
890939
This defined type can be used to create a schema. For example:
891940

@@ -909,6 +958,9 @@ The default owner of the schema.
909958
####`schema`
910959
Name of the schma. Defaults to `namevar`.
911960

961+
####`connect_settings`
962+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
963+
912964

913965
###Resource: postgresql::server::table\_grant
914966
This defined type manages grant based access privileges for users. Consult the PostgreSQL documentation for `grant` for more information.
@@ -934,6 +986,8 @@ Database to execute the grant against. This should not ordinarily be changed fro
934986
####`psql_user`
935987
OS user for running `psql`. Defaults to the default user for the module, usually `postgres`.
936988

989+
####`connect_settings`
990+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
937991

938992
###Resource: postgresql::server::tablespace
939993
This defined type can be used to create a tablespace. For example:
@@ -957,8 +1011,11 @@ The default owner of the tablespace.
9571011
####`spcname`
9581012
Name of the tablespace. Defaults to `namevar`.
9591013

1014+
####`connect_settings`
1015+
Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance.
9601016

9611017
###Resource: postgresql::validate\_db\_connection
1018+
9621019
This resource can be utilised inside composite manifests to validate that a client has a valid connection with a remote PostgreSQL database. It can be ran from any node where the PostgreSQL client software is installed to validate connectivity before commencing other dependent tasks in your Puppet manifests, so it is often used when chained to other tasks such as: starting an application server, performing a database migration.
9631020

9641021
Example usage:
@@ -991,6 +1048,9 @@ Username to connect with. Defaults to 'undef', which when using a unix socket an
9911048
####`database_password`
9921049
Password to connect with. Can be left blank, but that is not recommended.
9931050

1051+
####`connect_settings`
1052+
Hash of environment variable used when connecting to a remote server, this is an alternative to providing individual parameters (database_host, etc.). If provided the individual parameters take precedence.
1053+
9941054
####`run_as`
9951055
The user to run the `psql` command with for authenticiation. This is important when trying to connect to a database locally using Unix sockets and `ident` authentication. It is not needed for remote testing.
9961056

lib/puppet/provider/postgresql_psql/ruby.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ def run_sql_command(sql)
1616
command.push("-p", resource[:port]) if resource[:port]
1717
command.push("-t", "-c", '"' + sql.gsub('"', '\"') + '"')
1818

19+
environment = get_environment
20+
1921
if resource[:cwd]
2022
Dir.chdir resource[:cwd] do
21-
run_command(command, resource[:psql_user], resource[:psql_group])
23+
run_command(command, resource[:psql_user], resource[:psql_group], environment)
2224
end
2325
else
24-
run_command(command, resource[:psql_user], resource[:psql_group])
26+
run_command(command, resource[:psql_user], resource[:psql_group], environment)
2527
end
2628
end
2729

2830
private
2931

3032
def get_environment
31-
environment = {}
33+
environment = resource[:connect_settings] || {}
3234
if envlist = resource[:environment]
3335
envlist = [envlist] unless envlist.is_a? Array
3436
envlist.each do |setting|
@@ -47,7 +49,7 @@ def get_environment
4749
return environment
4850
end
4951

50-
def run_command(command, user, group)
52+
def run_command(command, user, group, environment)
5153
command = command.join ' '
5254
environment = get_environment
5355
if Puppet::PUPPETVERSION.to_f < 3.0
@@ -66,7 +68,7 @@ def run_command(command, user, group)
6668
:failonfail => false,
6769
:combine => true,
6870
:override_locale => true,
69-
:custom_environment => environment
71+
:custom_environment => environment,
7072
})
7173
[output, $CHILD_STATUS.dup]
7274
end

lib/puppet/type/postgresql_psql.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ def matches(value)
6262
end
6363
end
6464

65+
newparam(:connect_settings) do
66+
desc "Connection settings that will be used when connecting to postgres"
67+
end
68+
6569
newparam(:db) do
66-
desc "The name of the database to execute the SQL command against."
70+
desc "The name of the database to execute the SQL command against, this overrides any PGDATABASE value in connect_settings"
6771
end
6872

6973
newparam(:port) do
70-
desc "The port of the database server to execute the SQL command against."
74+
desc "The port of the database server to execute the SQL command against, this overrides any PGPORT value in connect_settings."
7175
end
7276

7377
newparam(:search_path) do

manifests/globals.pp

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
11
# Class for setting cross-class global overrides. See README.md for more
22
# details.
33
class postgresql::globals (
4-
$client_package_name = undef,
5-
$server_package_name = undef,
6-
$contrib_package_name = undef,
7-
$devel_package_name = undef,
8-
$java_package_name = undef,
9-
$docs_package_name = undef,
10-
$perl_package_name = undef,
11-
$plperl_package_name = undef,
12-
$plpython_package_name = undef,
13-
$python_package_name = undef,
14-
$postgis_package_name = undef,
4+
$client_package_name = undef,
5+
$server_package_name = undef,
6+
$contrib_package_name = undef,
7+
$devel_package_name = undef,
8+
$java_package_name = undef,
9+
$docs_package_name = undef,
10+
$perl_package_name = undef,
11+
$plperl_package_name = undef,
12+
$plpython_package_name = undef,
13+
$python_package_name = undef,
14+
$postgis_package_name = undef,
1515

16-
$service_name = undef,
17-
$service_provider = undef,
18-
$service_status = undef,
19-
$default_database = undef,
16+
$service_name = undef,
17+
$service_provider = undef,
18+
$service_status = undef,
19+
$default_database = undef,
2020

21-
$validcon_script_path = undef,
21+
$validcon_script_path = undef,
2222

23-
$initdb_path = undef,
24-
$createdb_path = undef,
25-
$psql_path = undef,
26-
$pg_hba_conf_path = undef,
27-
$pg_ident_conf_path = undef,
28-
$postgresql_conf_path = undef,
29-
$recovery_conf_path = undef,
23+
$initdb_path = undef,
24+
$createdb_path = undef,
25+
$psql_path = undef,
26+
$pg_hba_conf_path = undef,
27+
$pg_ident_conf_path = undef,
28+
$postgresql_conf_path = undef,
29+
$recovery_conf_path = undef,
30+
$default_connect_settings = undef,
3031

31-
$pg_hba_conf_defaults = undef,
32+
$pg_hba_conf_defaults = undef,
3233

33-
$datadir = undef,
34-
$confdir = undef,
35-
$bindir = undef,
36-
$xlogdir = undef,
37-
$logdir = undef,
34+
$datadir = undef,
35+
$confdir = undef,
36+
$bindir = undef,
37+
$xlogdir = undef,
38+
$logdir = undef,
3839

39-
$user = undef,
40-
$group = undef,
40+
$user = undef,
41+
$group = undef,
4142

42-
$version = undef,
43-
$postgis_version = undef,
44-
$repo_proxy = undef,
43+
$version = undef,
44+
$postgis_version = undef,
45+
$repo_proxy = undef,
4546

46-
$needs_initdb = undef,
47+
$needs_initdb = undef,
4748

48-
$encoding = undef,
49-
$locale = undef,
49+
$encoding = undef,
50+
$locale = undef,
5051

51-
$manage_pg_hba_conf = undef,
52-
$manage_pg_ident_conf = undef,
53-
$manage_recovery_conf = undef,
52+
$manage_pg_hba_conf = undef,
53+
$manage_pg_ident_conf = undef,
54+
$manage_recovery_conf = undef,
5455

55-
$manage_package_repo = undef,
56+
$manage_package_repo = undef,
5657
) {
5758
# We are determining this here, because it is needed by the package repo
5859
# class.

manifests/params.pp

-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@
253253

254254
$validcon_script_path = pick($validcon_script_path, '/usr/local/bin/validate_postgresql_connection.sh')
255255
$initdb_path = pick($initdb_path, "${bindir}/initdb")
256-
$createdb_path = pick($createdb_path, "${bindir}/createdb")
257256
$pg_hba_conf_path = pick($pg_hba_conf_path, "${confdir}/pg_hba.conf")
258257
$pg_hba_conf_defaults = pick($pg_hba_conf_defaults, true)
259258
$pg_ident_conf_path = pick($pg_ident_conf_path, "${confdir}/pg_ident.conf")

manifests/server.pp

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
$_version = $postgresql::params::version
6464
}
6565

66+
if $createdb_path != undef{
67+
warning('Passing "createdb_path" to postgresql::server is deprecated, it can be removed safely for the same behaviour')
68+
}
69+
6670
# Reload has its own ordering, specified by other defines
6771
class { "${pg}::reload": require => Class["${pg}::install"] }
6872

0 commit comments

Comments
 (0)