Skip to content

Commit ffa1d59

Browse files
Add multi instance support, refactoring service.pp (4/x)
This commit adds changes a class to add multi instance support to this module. The general idea is to first copy all classes which are used and create defines from them. These classes will use the defines as is. Necessary changes for the instances itself will be added to the classes and defined types at a later point. This ensures, the module will work as it does right now and there are no breaking changes.
1 parent 0b96216 commit ffa1d59

File tree

3 files changed

+97
-45
lines changed

3 files changed

+97
-45
lines changed

manifests/server/instance_service.pp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# lint:ignore:140chars
2+
# @param service_ensure Ensure service is installed
3+
# @param service_enable Enable the PostgreSQL service
4+
# @param service_manage Defines whether or not Puppet should manage the service.
5+
# @param service_name Overrides the default PostgreSQL service name.
6+
# @param service_provider Overrides the default PostgreSQL service provider.
7+
# @param service_status Overrides the default status check command for your PostgreSQL service.
8+
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
9+
# @param port Specifies the port for the PostgreSQL server to listen on. Note: The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change.
10+
# Default value: 5432. Meaning the Postgres server listens on TCP port 5432.
11+
# @param default_database Specifies the name of the default database to connect with. On most systems this is 'postgres'.
12+
# @param psql_path Specifies the path to the psql command.
13+
# @param default_connect_settings Specifies a hash of environment variables used when connecting to a remote server. Becomes the default for other defined types, such as postgresql::server::role.
14+
# lint:endignore:140chars
15+
define postgresql::server::instance_service (
16+
$service_ensure = $postgresql::server::service_ensure,
17+
$service_enable = $postgresql::server::service_enable,
18+
$service_manage = $postgresql::server::service_manage,
19+
$service_name = $postgresql::server::service_name,
20+
$service_provider = $postgresql::server::service_provider,
21+
$service_status = $postgresql::server::service_status,
22+
$user = $postgresql::server::user,
23+
$port = $postgresql::server::port,
24+
$default_database = $postgresql::server::default_database,
25+
$psql_path = $postgresql::server::psql_path,
26+
$connect_settings = $postgresql::server::default_connect_settings,
27+
) {
28+
anchor { 'postgresql::server::service::begin': }
29+
30+
if $service_manage {
31+
service { 'postgresqld':
32+
ensure => $service_ensure,
33+
enable => $service_enable,
34+
name => $service_name,
35+
provider => $service_provider,
36+
hasstatus => true,
37+
status => $service_status,
38+
}
39+
40+
if $service_ensure in ['running', true] {
41+
# This blocks the class before continuing if chained correctly, making
42+
# sure the service really is 'up' before continuing.
43+
#
44+
# Without it, we may continue doing more work before the database is
45+
# prepared leading to a nasty race condition.
46+
postgresql_conn_validator { 'validate_service_is_running':
47+
run_as => $user,
48+
db_name => $default_database,
49+
port => $port,
50+
connect_settings => $connect_settings,
51+
sleep => 1,
52+
tries => 60,
53+
psql_path => $psql_path,
54+
require => Service['postgresqld'],
55+
before => Anchor['postgresql::server::service::end'],
56+
}
57+
Postgresql::Server::Database <| title == $default_database |> -> Postgresql_conn_validator['validate_service_is_running']
58+
}
59+
}
60+
61+
anchor { 'postgresql::server::service::end': }
62+
}

manifests/server/service.pp

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
11
# @api private
22
class postgresql::server::service {
3-
$service_ensure = $postgresql::server::service_ensure
4-
$service_enable = $postgresql::server::service_enable
5-
$service_manage = $postgresql::server::service_manage
6-
$service_name = $postgresql::server::service_name
7-
$service_provider = $postgresql::server::service_provider
8-
$service_status = $postgresql::server::service_status
9-
$user = $postgresql::server::user
10-
$port = $postgresql::server::port
11-
$default_database = $postgresql::server::default_database
12-
$psql_path = $postgresql::server::psql_path
13-
$connect_settings = $postgresql::server::default_connect_settings
14-
15-
anchor { 'postgresql::server::service::begin': }
16-
17-
if $service_manage {
18-
service { 'postgresqld':
19-
ensure => $service_ensure,
20-
enable => $service_enable,
21-
name => $service_name,
22-
provider => $service_provider,
23-
hasstatus => true,
24-
status => $service_status,
25-
}
26-
27-
if $service_ensure in ['running', true] {
28-
# This blocks the class before continuing if chained correctly, making
29-
# sure the service really is 'up' before continuing.
30-
#
31-
# Without it, we may continue doing more work before the database is
32-
# prepared leading to a nasty race condition.
33-
postgresql_conn_validator { 'validate_service_is_running':
34-
run_as => $user,
35-
db_name => $default_database,
36-
port => $port,
37-
connect_settings => $connect_settings,
38-
sleep => 1,
39-
tries => 60,
40-
psql_path => $psql_path,
41-
require => Service['postgresqld'],
42-
before => Anchor['postgresql::server::service::end'],
43-
}
44-
Postgresql::Server::Database <| title == $default_database |> -> Postgresql_conn_validator['validate_service_is_running']
45-
}
3+
postgresql::server::instance_service { 'main':
4+
service_ensure => $postgresql::server::service_ensure,
5+
service_enable => $postgresql::server::service_enable,
6+
service_manage => $postgresql::server::service_manage,
7+
service_name => $postgresql::server::service_name,
8+
service_provider => $postgresql::server::service_provider,
9+
service_status => $postgresql::server::service_status,
10+
user => $postgresql::server::user,
11+
port => $postgresql::server::port,
12+
default_database => $postgresql::server::default_database,
13+
psql_path => $postgresql::server::psql_path,
14+
connect_settings => $postgresql::server::default_connect_settings,
4615
}
47-
48-
anchor { 'postgresql::server::service::end': }
4916
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'postgresql::server::instance_service' do
6+
let(:title) { 'main' }
7+
8+
on_supported_os.each do |os, os_facts|
9+
context "on #{os}" do
10+
let :facts do
11+
os_facts
12+
end
13+
14+
let :pre_condition do
15+
"class {'postgresql::server':}"
16+
end
17+
18+
context 'with defaults from service class' do
19+
it { is_expected.to compile.with_all_deps }
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)