Skip to content

Commit

Permalink
- Added fact for detection of replication role.
Browse files Browse the repository at this point in the history
- Better handling of relationships.
- Doing some stuff on master node (instead of only on initial master)
- Merged pure_repmgr::register_standby and pure_repmgr::register_master to pure_repmgr::register
  • Loading branch information
S. Mannem committed Feb 14, 2017
1 parent f4e9b69 commit af5a084
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 55 deletions.
13 changes: 13 additions & 0 deletions files/pure_cloud_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def process_config_files(list_of_config_files):
my_ip = socket.gethostbyname(socket.gethostname())
my_id = all_sites.index(my_ip) + 1

try:
cn=psycopg2.connect(database='repmgr', host=socket.gethostname(), user='repmgr')
cur=cn.cursor()
cur.execute('select pg_is_in_recovery()')
if cur.next()[0]:
replication_role = 'standby'
else:
replication_role = 'master'
except Exception as e:
replication_role = None

facts = dict()
facts['pure_cloud_cluster'] = repmgr_cluster_name
facts['pure_cloud_clusterdns'] = dns
Expand All @@ -197,5 +208,7 @@ def process_config_files(list_of_config_files):
facts['pure_cloud_primarysite'] = primary_site
facts['pure_cloud_secondarysite'] = secondary_site
facts['pure_postgres_ssh_public_key'] = ssh_public_key('/home/postgres/.ssh/id_rsa.pub')
if replication_role:
facts['pure_replication_role'] = replication_role

print(json.dumps(facts))
60 changes: 35 additions & 25 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

if $nodeid == '1' and size($facts['pure_cloud_available_hosts']) == 0 {

$replication_role = pick( $facts['pure_replication_role'], 'master')

$facts['pure_cloud_nodes'].each | String $source | {
pure_postgres::pg_hba {"pg_hba entry for $source":
database => 'repmgr,replication',
Expand All @@ -56,6 +58,7 @@
source => "${source}/32",
connection_type => 'host',
user => 'repmgr',
before => Class['pure_postgres::start'],
}
}

Expand All @@ -73,27 +76,16 @@
} ->

file_line { 'wal_log_hints on':
path => "$pure_postgres::pg_etc_dir/conf.d/wal.conf",
line => 'wal_log_hints = on',
} ->

class { 'pure_postgres::service':
service_ensure => 'running',
} ->

pure_postgres::role {'repmgr':
with_db => true,
password_hash => 'repmgr',
superuser => true,
#$user will be expanded by postgres and should not be expanded by puppet.
searchpath => [ "\"repmgr_${pure_cloud_cluster}\"", '"$user"', 'public' ],
replication => true,
} ->

class {'pure_repmgr::register_primary':
path => "$pure_postgres::pg_etc_dir/conf.d/wal.conf",
line => 'wal_log_hints = on',
before => Class['pure_postgres::start'],
}

}
elsif size($facts['pure_cloud_available_hosts']) > 0 {

$replication_role = pick( $facts['pure_replication_role'], 'standby')

#There already are running postgres instances in this cluster
# create a directory
file { "${pg_etc_dir}/conf.d":
Expand All @@ -110,13 +102,7 @@
datadir => $pg_data_dir,
require => File["${pg_etc_dir}/conf.d"],
before => Class['pure_postgres::start'],
}
}

class { 'pure_postgres::start':
} ->

class { 'pure_repmgr::register_standby':
}
}

}
Expand All @@ -126,6 +112,30 @@
withpath => true,
}
}

class { 'pure_postgres::start':
} ->

class { 'pure_postgres::reload':
} ->

class {'pure_repmgr::register':
replication_role => $replication_role,
}

if $replication_role == 'master' {
pure_postgres::role {'repmgr':
with_db => true,
password_hash => 'repmgr',
superuser => true,
#$user will be expanded by postgres and should not be expanded by puppet.
searchpath => [ "\"repmgr_${pure_cloud_cluster}\"", '"$user"', 'public' ],
replication => true,
before => Class['pure_repmgr::register'],
require => Class['pure_postgres::reload'],
}
}
}

}

29 changes: 29 additions & 0 deletions manifests/register.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# == Class: pure_repmgr::register
# Private class
class pure_repmgr::register(
$replication_role = $facts['pure_replication_role'],
) inherits pure_repmgr
{
$check = $replication_role ? {
'master' => 'False',
'standby' => 'True',
default => '',
}

if $check == ''{
fail("Invalid value for \$replication_role: $replication_role")
}

$cmd = shellquote( "$pg_bin_dir/repmgr", '-f', "${pure_repmgr::repmgr_conf}", $replication_role, 'register')
$unless = shellquote("${pure_postgres::pg_bin_dir}/psql", "-d", "repmgr", "-c", "select * from repmgr_${pure_cloud_cluster}.repl_nodes where pg_is_in_recovery()=$check or name='$fqdn'" )

exec { "exec $cmd":
user => $pure_postgres::postgres_user,
command => $cmd,
unless => "/bin/test $($unless | wc -l) -gt 1",
onlyif => "/bin/test -f '${pure_postgres::pg_pid_file}'",
cwd => $pure_postgres::pg_bin_dir,
path => '$pg_bin_dir:/usr/local/bin:/bin',
}

}
14 changes: 0 additions & 14 deletions manifests/register_primary.pp

This file was deleted.

16 changes: 0 additions & 16 deletions manifests/register_standby.pp

This file was deleted.

0 comments on commit af5a084

Please sign in to comment.