From af5a0846b607e95b988bdbbf66a4edfa0f01ed86 Mon Sep 17 00:00:00 2001 From: "S. Mannem" Date: Tue, 14 Feb 2017 17:40:42 +0000 Subject: [PATCH] - Added fact for detection of replication role. - 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 --- files/pure_cloud_cluster.py | 13 ++++++++ manifests/config.pp | 60 ++++++++++++++++++++--------------- manifests/register.pp | 29 +++++++++++++++++ manifests/register_primary.pp | 14 -------- manifests/register_standby.pp | 16 ---------- 5 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 manifests/register.pp delete mode 100644 manifests/register_primary.pp delete mode 100644 manifests/register_standby.pp diff --git a/files/pure_cloud_cluster.py b/files/pure_cloud_cluster.py index a2e9e27..b05c1cc 100755 --- a/files/pure_cloud_cluster.py +++ b/files/pure_cloud_cluster.py @@ -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 @@ -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)) diff --git a/manifests/config.pp b/manifests/config.pp index 33a49b2..54f56f8 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -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', @@ -56,6 +58,7 @@ source => "${source}/32", connection_type => 'host', user => 'repmgr', + before => Class['pure_postgres::start'], } } @@ -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": @@ -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': + } } } @@ -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'], + } + } } + } diff --git a/manifests/register.pp b/manifests/register.pp new file mode 100644 index 0000000..73b5257 --- /dev/null +++ b/manifests/register.pp @@ -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', + } + +} diff --git a/manifests/register_primary.pp b/manifests/register_primary.pp deleted file mode 100644 index 1e4e90d..0000000 --- a/manifests/register_primary.pp +++ /dev/null @@ -1,14 +0,0 @@ -# == Class: pure_repmgr::register_primary -# Private class -class pure_repmgr::register_primary( -) inherits pure_repmgr -{ - $cmd = shellquote( "$pg_bin_dir/repmgr", '-f', "${pure_repmgr::repmgr_conf}", 'master', 'register') - $unless = shellquote( "select * from repmgr_testdb.repl_nodes where type='master' or name='$fqdn'" ) - - exec { "exec $cmd": - user => $pure_postgres::postgres_user, - command => $cmd, - unless => "/bin/echo ${unless} | $pg_bin_dir/psql -d repmgr --quiet --tuples-only | /bin/grep -q '|'" - } -} diff --git a/manifests/register_standby.pp b/manifests/register_standby.pp deleted file mode 100644 index 43f980a..0000000 --- a/manifests/register_standby.pp +++ /dev/null @@ -1,16 +0,0 @@ -# == Class: pure_repmgr::register_standby -# Private class -class pure_repmgr::register_standby( -) inherits pure_repmgr -{ - - $cmd = shellquote( "$pg_bin_dir/repmgr", '-f', "${pure_repmgr::repmgr_conf}", 'standby', 'register') - $unless = shellquote( "select * from repmgr_testdb.repl_nodes where name='$fqdn'" ) - - exec { "exec $cmd": - user => $pure_postgres::postgres_user, - command => $cmd, - unless => "/usr/bin/echo ${unless} | $pg_bin_dir/psql -d repmgr --quiet --tuples-only | /usr/bin/grep -q '|'", - onlyif => "/bin/test -f $pg_data_dir/PG_VERSION", - } -}